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.

60 changes: 60 additions & 0 deletions recipes/ansi-colors-to-styletext/README.md
Comment thread
AugustinMauroy marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# ansi-colors to util.styleText

This recipe migrates from the external `ansi-colors` package to Node.js's built-in `util.styleText` API. It transforms ansi-colors method calls to use the native Node.js styling functionality.

## Usage

Run this codemod with:

```sh
npx codemod @nodejs/ansi-colors-to-styletext
```

## Examples

```diff
- import ac from 'ansi-colors';
+ import { styleText, stripVTControlCharacters } from 'node:util';

- console.log(ac.red('Error message'));
+ console.log(styleText('red', 'Error message'));

- console.log(ac.green('Success message'));
+ console.log(styleText('green', 'Success message'));

- console.log(ac.unstyle(ac.bold.blue('Info message')));
+ console.log(stripVTControlCharacters(styleText(['bold', 'blue'], 'Info message')));
```

```diff
- const ac = require('ansi-colors');
+ const { styleText } = require('node:util');

- console.log(ac.bold.red('Critical error'));
+ console.log(styleText(['bold', 'red'], 'Critical error'));
```

```diff
- const { red, blue } = require('ansi-colors');
+ const { styleText } = require('node:util');

- console.log(red('Error'));
+ console.log(styleText('red', 'Error'));

- console.log(blue('Info'));
+ console.log(styleText('blue', 'Info'));
```

## Compatibility

- **Removes ansi-colors dependency** from package.json automatically
- **Supports all ansi-colors methods**: colors, background colors, text modifiers, and chained styles
- **Unsupported methods**: `enabled`, `visible`, `unstyle`, `alias`, `theme`, `create` (warnings will be shown)

## Limitations

- **Runtime toggles** like `ac.enabled = false` require manual intervention
- **Custom themes and aliases** need to be rewritten as plain objects
- **Dynamic imports with `.then()`** are not transformed and require manual migration

<!-- sync_to_learn: true -->
28 changes: 28 additions & 0 deletions recipes/ansi-colors-to-styletext/codemod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
schema_version: "1.0"
name: "@nodejs/ansi-colors-to-styletext"
version: 1.0.0
capabilities:
- fs
- child_process
description: "Migrate from ansi-colors package to Node.js util.styleText API"
author: Shamya Haria
license: MIT
workflow: workflow.yaml
category: migration
repository: https://github.com/nodejs/userland-migrations

targets:
languages:
- javascript
- typescript

keywords:
- transformation
- migration
- nodejs
- ansi-colors
- styletext

registry:
access: public
visibility: public
26 changes: 26 additions & 0 deletions recipes/ansi-colors-to-styletext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@nodejs/ansi-colors-to-styletext",
"version": "1.0.0",
"description": "Migrate from ansi-colors package to Node.js util.styleText API",
"type": "module",
"scripts": {
"test": "npx codemod jssg test -l typescript ./src/workflow.ts ./tests && npx codemod jssg test -l json ./src/remove-dependencies.ts ./tests/remove-dependencies --allow-child-process --allow-fs --strictness cst",
"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/ansi-colors-to-styletext",
"bugs": "https://github.com/nodejs/userland-migrations/issues"
},
"author": "Shamya Haria",
"license": "MIT",
"homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/ansi-colors-to-styletext/README.md",
"devDependencies": {
"@codemod.com/jssg-types": "^1.6.3"
},
"dependencies": {
"@nodejs/codemod-utils": "*"
}
}
13 changes: 13 additions & 0 deletions recipes/ansi-colors-to-styletext/src/remove-dependencies.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really doing anything—it seems just bloat.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I what we do for all ecosystem codemod

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(['ansi-colors', '@types/ansi-colors'], {
packageJsonPath: root.filename(),
runInstall: false,
persistFileWrite: false,
});
};

export default transform;
Loading