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
- Zero Dependencies: Eliminates need for external
colors package
- Better Performance: No additional bundle size or installation overhead
- Native Support: Uses Node.js built-in APIs
- 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
Description
This issue tracks the migration from the
colorspackage to Node.js's built-inutil.styleTextAPI. The goal is to reduce external dependencies by leveraging Node.js's native terminal styling capabilities.Migration Scope
Compatible Features:
Non-Compatible Features:
rainbow,zebra,america,trap,random)console.log(colors.green('Hello %s'), name))colors.setTheme({error: 'red'}))Benefits
colorspackageExamples
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:
After:
Case 2: Basic safe colors usage
Before:
After:
Case 3: Chained Styles
Before:
After:
Case 4: Chained Safe Styles
Before:
After:
Case 5: Inline String Concatenation
Before:
After:
Case 6: Template Literals with Colors
Before:
After:
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