Skip to content

feat(colors-to-util.styleText): handle migration #474

Description

@brunocroh

Description

This issue tracks the migration from the colors package to Node.js's built-in util.styleText API. The goal is to reduce external dependencies by leveraging Node.js's native terminal styling capabilities.

Migration Scope

Compatible Features:

  • Basic colors (red, green, blue, yellow, etc.)
  • Bright colors (redBright, greenBright, etc.)
  • Background colors (bgRed, bgGreen, etc.)
  • Text modifiers (bold, dim, italic, underline, strikethrough, etc.)
  • Style chaining via array syntax
  • Environment variable support (NO_COLOR, NODE_DISABLE_COLORS, FORCE_COLOR)

Non-Compatible Features:

  • extras (rainbow, zebra, america, trap, random)
  • string substitution (console.log(colors.green('Hello %s'), name))
  • Custom themes (colors.setTheme({error: 'red'}))

Benefits

  1. Zero Dependencies: Eliminates need for external colors package
  2. Better Performance: No additional bundle size or installation overhead
  3. Native Support: Uses Node.js built-in APIs
  4. Environment Awareness: Automatic respect for color-related environment variables

Examples

Suite of examples that demonstrate how the codemod will work. These can also serve as the base test suite.

Case 1: Basic Color Usage

Before:

const colors = require('colors')

console.log('Error message'.red);

After:

const { styleText } = require('node:util');

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

Case 2: Basic safe colors usage

Before:

const colors = require('colors/safe')

console.log(colors.green('Success message'));

After:

const { styleText } = require('node:util');

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

Case 3: Chained Styles

Before:

import colors from 'colors'

console.log('Success message'.green.bold);

After:

import { styleText } from 'node:util';

console.log(styleText(['green', 'bold'], 'Success message'));

Case 4: Chained Safe Styles

Before:

import colors from 'colors/safe'

console.log(colors.green.bold('Success message'));

After:

import { styleText } from 'node:util';

console.log(styleText(['green', 'bold'], 'Success message'));

Case 5: Inline String Concatenation

Before:

const colors = require('colors')

const name = 'World';
console.log('Hello, ' + name.green + '!');
console.log('Status: ' + 'OK'.green.bold + ' - All systems operational');

After:

const { styleText } = require('node:util');

const name = 'World';
console.log('Hello, ' + styleText('green', name) + '!');
console.log('Status: ' + styleText(['green', 'bold'], 'OK') + ' - All systems operational');

Case 6: Template Literals with Colors

Before:

const colors = require('colors')

const user = 'Alice';
const action = 'logged in';
console.log(`${'[INFO]'.blue} User ${user.green} ${action.yellow}`);

After:

const { styleText } = require('node:util');

const user = 'Alice';
const action = 'logged in';
console.log(`${styleText('blue', '[INFO]')} User ${styleText('green', user)} ${styleText('yellow', action)}`);

Additional note

If the codemod detect un-migratable apis we should warn it and advice the user to take a manual look at the code.

https://github.com/nodejs/userland-migrations/blob/main/utils/src/ast-grep/package-json.ts may need to be extended to support dependency removal

REFS

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issue humanGood for newcomers AI agent aren't welcomme !help wantedExtra attention is needed
    No fields configured for Feature.

    Projects

    Status
    🏗 In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions