Skip to content
Merged
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
19 changes: 12 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import process from 'node:process';
import ansiStyles from 'ansi-styles';
import chalk from 'chalk';
import dotProp from 'dot-prop';
import chalkTemplate from 'chalk-template';
import {getProperty} from 'dot-prop';
import getStdin from 'get-stdin';
import meow from 'meow';

Expand Down Expand Up @@ -46,6 +47,7 @@ const cli = meow(`
${chalk.yellow('--stdin')} Read input from stdin rather than from arguments.
${chalk.yellow('--no-newline, -n')} Don't emit a newline (\`\\n\`) after the input.
${chalk.yellow('--demo')} Demo of all Chalk styles.
${chalk.yellow('--color, -c')} Force color support.

${chalk.redBright.inverse(' Examples ')}

Expand All @@ -56,7 +58,7 @@ const cli = meow(`
${chalk.red.bold('Unicorns & Rainbows')}

$ chalk -t '{red.bold Dungeons and Dragons {~bold.blue (with added fairies)}}'
${chalk`{red.bold Dungeons and Dragons {~bold.blue (with added fairies)}}`}
${chalkTemplate`{red.bold Dungeons and Dragons {~bold.blue (with added fairies)}}`}

$ echo 'Unicorns from stdin' | chalk --stdin red bold
${chalk.red.bold('Unicorns from stdin')}
Expand All @@ -74,14 +76,17 @@ const cli = meow(`

template: {
type: 'string',
alias: 't',
shortFlag: 't',
},
stdin: {
type: 'boolean',
},
color: {
type: 'boolean',
},
noNewline: {
type: 'boolean',
alias: 'n',
shortFlag: 'n',
},
demo: {
type: 'boolean',
Expand All @@ -101,7 +106,7 @@ function handleTemplateFlag(template) {
try {
const tagArray = [template];
tagArray.raw = tagArray;
console.log(chalk(tagArray));
console.log(chalkTemplate(tagArray));
} catch (error) {
console.error('Something went wrong! Maybe review your syntax?\n');
console.error(error.stack);
Expand All @@ -112,13 +117,13 @@ function handleTemplateFlag(template) {
function init(data) {
for (const style of styles) {
if (!Object.keys(ansiStyles).includes(style)) {
console.error(chalk`{red Invalid style: {bold ${style}}}\n`);
console.error(chalkTemplate`{red Invalid style: {bold ${style}}}\n`);
printAllStyles();
process.exit(1);
}
}

const function_ = dotProp.get(chalk, styles.join('.'));
const function_ = getProperty(chalk, styles.join('.'));
process.stdout.write(function_(data.replace(/\n$/, '')));

// The following is unfortunately a bit complex, because we're trying to
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@
"template"
],
"dependencies": {
"ansi-styles": "^6.1.0",
"chalk": "^4.1.2",
"dot-prop": "^6.0.1",
"ansi-styles": "^6.2.1",
"chalk": "^5.4.1",
"chalk-template": "^1.1.0",
"dot-prop": "^9.0.0",
"get-stdin": "^9.0.0",
"meow": "^10.1.1"
"meow": "^13.2.0"
},
"devDependencies": {
"ava": "^3.15.0",
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ $ chalk --help
--stdin Read input from stdin rather than from arguments.
--no-newline, -n Don't emit a newline (`\n`) after the input.
--demo Demo of all Chalk styles.
--color, -c Force color support.

Examples
$ chalk red bold 'Unicorns & Rainbows'
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ test('with --no-newline, output has NO trailing newline', macro,

test('demo', snapshotMacro, {arguments: ['--demo']});

test('color', macro, {arguments: ['yellow', 'bold', 'unicorn', '--color']},
chalk.yellow.bold('unicorn'));

test('color=always', macro, {arguments: ['red', 'bold', 'unicorn', '--color=always']},
chalk.red.bold('unicorn'));

test('color=true', macro, {arguments: ['blue', 'bold', 'unicorn', '--color=true']},
chalk.blue.bold('unicorn'));

/* This case is not testable as FORCE_COLOR is set, and takes precedence over flag
test('no color', macro, {arguments: ['--no-color', 'red', 'bold', 'unicorn']},
'unicorn');
*/

test('unknown flag',
async (t, {arguments: arguments_, options}, expectedRegex) => {
try {
Expand Down