Skip to content

Commit

Permalink
Workaround to make --no-newline work (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
msabramo committed Sep 14, 2021
1 parent 17f70c3 commit ea08e03
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 15 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,21 @@ function init(data) {

const fn = dotProp.get(chalk, styles.join('.'));
process.stdout.write(fn(data.replace(/\n$/, '')));
if (!cli.flags.noNewline) {

// The following is unfortunately a bit complex, because we're trying to
// support both `-n` and `--no-newline` flags and this is a little tricky
// with the current state of [meow](https://www.npmjs.com/package/meow) and
// [yargs-parser](https://github.com/yargs/yargs-parser), which meow uses.
//
// There are two conditions in the following `if` statement:
//
// - `cli.flags.noNewline` is set when `-n` is passed.
// - `cli.flags.newline` is set to `false` when `--no-newline` is passed.
//
// We're hoping to simplify this in the future. See:
// https://github.com/chalk/chalk-cli/issues/30
//
if (!cli.flags.noNewline && cli.flags.newline !== false) {
process.stdout.write('\n');
}
}
Expand Down
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ test('without -n, output has trailing newline', macro,
test('with -n, output has NO trailing newline', macro,
{args: ['-n', 'red', 'bold', 'unicorn'], opts: {stripEof: false}},
chalk.red.bold('unicorn') /* No trailing newline */);
test('with --no-newline, output has NO trailing newline', macro,
{args: ['--no-newline', 'red', 'bold', 'unicorn'], opts: {stripEof: false}},
chalk.red.bold('unicorn') /* No trailing newline */);

0 comments on commit ea08e03

Please sign in to comment.