Skip to content

feat(kleur-to-util-styletext): add codemod#509

Open
GiHoon1123 wants to merge 1 commit into
nodejs:mainfrom
GiHoon1123:feat-kleur-to-styletext
Open

feat(kleur-to-util-styletext): add codemod#509
GiHoon1123 wants to merge 1 commit into
nodejs:mainfrom
GiHoon1123:feat-kleur-to-styletext

Conversation

@GiHoon1123

Copy link
Copy Markdown

Fixes #479.

Summary

Adds a codemod that migrates kleur to node:util's styleText(), following the same shape as the existing chalk-to-util-styletext recipe. kleur's API is basically a subset of chalk's (chainable style methods, direct calls), so this is adapted from that recipe rather than written from scratch.

kleur's real API is smaller than chalk's - no hex()/rgb()/ansi256(), no "Bright" color variants, no chalkStderr. Every method kleur actually ships has a direct styleText equivalent, so there's nothing left unsupported by design (the unsupported-method guard is kept anyway, in case a future kleur version adds something new).

Examples

- import kleur from 'kleur';
+ import { styleText } from 'node:util';
- console.log(kleur.red('Error message'));
+ console.log(styleText('red', 'Error message'));
- import kleur from 'kleur';
+ import { styleText } from 'node:util';
- console.log(kleur.red.bold('Important error'));
+ console.log(styleText(['red', 'bold'], 'Important error'));
- const kleur = require('kleur');
+ const { styleText } = require('node:util');
- const red = kleur.red;
+ const red = (text) => styleText('red', text);

Changes

  • recipes/kleur-to-util-styletext/src/workflow.ts: main transform. Handles default import/require, named/destructured imports, dynamic import, chained calls (up to 4 styles), method assignment to a variable, and ternary-based style selection.
  • recipes/kleur-to-util-styletext/src/remove-dependencies.ts: removes kleur from package.json (no @types/kleur exists, kleur ships its own types).
  • 18 workflow tests + 1 remove-dependencies test, covering the different import styles kleur can be used with.
  • README.md documents one real limitation: direct calls chaining more than 4 styles (kleur.a.b.c.d.e(...)) aren't transformed and are left as-is (no crash, just a no-op). Chaining through a variable assignment first isn't affected by this - only direct calls are.

Test plan

  • node --run test:workflow - 18/18 passing
  • node --run test:remove-dependencies - 1/1 passing
  • node --run pre-commit (lint + type-check + full workspace test suite) passes

Adapted from recipes/chalk-to-util-styletext, since kleur's API
(chainable style methods, direct calls) closely mirrors chalk's.

kleur's real API is a subset of chalk's - no hex/rgb/ansi256, no
Bright color variants, no chalkStderr - so those branches are
dropped rather than adapted.

Fixes nodejs#479
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

spec(kleur): migrate kleur to node:util styleText

1 participant