Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions recipes/colors-to-util-styletext/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Colors to util.styleText

This recipe migrates compatible `colors` package usage to Node.js built-in `util.styleText`.

## Examples

```diff
- const colors = require('colors');
+ const { styleText } = require('node:util');
- console.log('Error message'.red);
+ console.log(styleText('red', 'Error message'));
```

```diff
- import colors from 'colors';
+ import { styleText } from 'node:util';
- console.log('Success'.green.bold);
+ console.log(styleText(['green', 'bold'], 'Success'));
```

```diff
- const colors = require('colors/safe');
+ const { styleText } = require('node:util');
- console.log(colors.green('Success message'));
+ console.log(styleText('green', 'Success message'));
```

## Compatibility

- Removes the `colors` dependency from package.json automatically.
- Supports string prototype colors and modifiers, including chained styles.
- Supports `colors/safe` namespace calls.
- Unsupported extras such as `rainbow`, `zebra`, `america`, `trap`, and `random` are left unchanged and reported for manual review.
26 changes: 26 additions & 0 deletions recipes/colors-to-util-styletext/codemod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
schema_version: "1.0"
name: "@nodejs/colors-to-util-styletext"
version: 1.0.0
capabilities:
- fs
- child_process
description: Migrate from the colors package to Node.js's built-in util.styleText API
author: Herrtian
license: MIT
workflow: workflow.yaml
category: migration
repository: https://github.com/nodejs/userland-migrations

targets:
languages:
- javascript
- typescript

keywords:
- nodejs
Comment thread
AugustinMauroy marked this conversation as resolved.
- colors
- styleText

registry:
access: public
visibility: public
26 changes: 26 additions & 0 deletions recipes/colors-to-util-styletext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@nodejs/colors-to-util-styletext",
"version": "1.0.0",
"description": "Migrate from the colors package to Node.js's built-in util.styleText API",
"type": "module",
"scripts": {
"test": "node --run test:workflow && node --run test:remove-dependencies",
"test:workflow": "npx codemod jssg test -l typescript ./src/workflow.ts ./tests",
"test:remove-dependencies": "npx codemod jssg test -l json ./src/remove-dependencies.ts ./tests/remove-dependencies --allow-child-process --allow-fs --strictness cst"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nodejs/userland-migrations.git",
"directory": "recipes/colors-to-util-styletext",
"bugs": "https://github.com/nodejs/userland-migrations/issues"
},
"author": "Herrtian",
"license": "MIT",
"homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/colors-to-util-styletext/README.md",
"devDependencies": {
"@codemod.com/jssg-types": "^1.6.0"
},
"dependencies": {
"@nodejs/codemod-utils": "*"
}
}
13 changes: 13 additions & 0 deletions recipes/colors-to-util-styletext/src/remove-dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Transform } from '@codemod.com/jssg-types/main';
import type Json from '@codemod.com/jssg-types/langs/json';
import removeDependencies from '@nodejs/codemod-utils/remove-dependencies';

const transform: Transform<Json> = async (root) => {
return removeDependencies(['colors', '@types/colors'], {
packageJsonPath: root.filename(),
runInstall: false,
persistFileWrite: false,
});
};

export default transform;
Loading
Loading