Skip to content
Closed
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
57 changes: 57 additions & 0 deletions recipes/kleur-to-util-styletext/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# kleur to util.styleText

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

## Examples

```diff
- import kleur from 'kleur';
+ import { styleText } from 'node:util';
- console.log(kleur.red('Error message'));
+ console.log(styleText('red', 'Error message'));
- console.log(kleur.green('Success message'));
+ console.log(styleText('green', 'Success message'));
- console.log(kleur.blue('Info message'));
+ console.log(styleText('blue', 'Info message'));
```

```diff
- import kleur from 'kleur';
+ import { styleText } from 'node:util';
- console.log(kleur.red.bold('Important error'));
+ console.log(styleText(['red', 'bold'], 'Important error'));
- console.log(kleur.green.underline('Success with emphasis'));
+ console.log(styleText(['green', 'underline'], 'Success with emphasis'));
```

```diff
- const kleur = require('kleur');
+ const { styleText } = require('node:util');
- const red = kleur.red;
+ const red = (text) => styleText('red', text);
- const boldBlue = kleur.blue.bold;
+ const boldBlue = (text) => styleText(['blue', 'bold'], text);
- console.log(red('Error'));
+ console.log(red('Error'));
- console.log(boldBlue('Info'));
+ console.log(boldBlue('Info'));
```

## Usage

Run this codemod with:

```sh
npx codemod nodejs/kleur-to-util-styletext
```

## Compatibility

- **Removes kleur dependency** from package.json automatically
- **Supports kleur's full API**: every modifier, color, and background color kleur ships has a direct `util.styleText` equivalent, so no kleur method is left unsupported.

## Limitations

- **Complex conditional expressions** in some contexts may need manual review
- kleur exposes a secondary `kleur/colors` entry point (non-chainable individual color functions). This codemod targets the default `kleur` import/require only.
- **Direct calls chaining more than 4 styles** (e.g. `kleur.a.b.c.d.e("text")`) are left unchanged rather than transformed. Assigning the chain to a variable first (`const x = kleur.a.b.c.d.e; x("text")`) is transformed regardless of depth.
26 changes: 26 additions & 0 deletions recipes/kleur-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/kleur-to-util-styletext"
version: 1.0.0
capabilities:
- fs
- child_process
description: Migrate from the kleur package to Node.js's built-in util.styleText API
author: Node.js contributors
license: MIT
workflow: workflow.yaml
category: migration
repository: https://github.com/nodejs/userland-migrations

targets:
languages:
- javascript
- typescript

keywords:
- transformation
- migration
- nodejs

registry:
access: public
visibility: public
26 changes: 26 additions & 0 deletions recipes/kleur-to-util-styletext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@nodejs/kleur-to-util-styletext",
"version": "1.0.0",
"description": "Migrate from the kleur 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 ./",
"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/kleur-to-util-styletext",
"bugs": "https://github.com/nodejs/userland-migrations/issues"
},
"author": "Node.js contributors",
"license": "MIT",
"homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/kleur-to-util-styletext/README.md",
"devDependencies": {
"@codemod.com/jssg-types": "^1.6.2"
},
"dependencies": {
"@nodejs/codemod-utils": "*"
}
}
13 changes: 13 additions & 0 deletions recipes/kleur-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(['kleur'], {
packageJsonPath: root.filename(),
runInstall: false,
persistFileWrite: false,
});
};

export default transform;
Loading