-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
cli.js
executable file
·176 lines (148 loc) · 4.85 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env node
import process from 'node:process';
import ansiStyles from 'ansi-styles';
import chalk from 'chalk';
import dotProp from 'dot-prop';
import getStdin from 'get-stdin';
import meow from 'meow';
const printAllStyles = () => {
const textStyles = ['bold', 'dim', 'italic', 'underline', 'strikethrough'];
const colorStyles = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray'];
const brightColorStyles = ['blackBright', 'redBright', 'greenBright', 'yellowBright', 'blueBright', 'magentaBright', 'cyanBright', 'whiteBright'];
const bgColorStyles = ['bgBlack', 'bgRed', 'bgGreen', 'bgYellow', 'bgBlue', 'bgMagenta', 'bgCyan', 'bgWhite', 'bgGray'];
const bgBrightColorStyles = ['bgBlackBright', 'bgRedBright', 'bgGreenBright', 'bgYellowBright', 'bgBlueBright', 'bgMagentaBright', 'bgCyanBright', 'bgWhiteBright'];
function styled(style, text) {
if (/^bg[^B]/.test(style)) {
text = chalk.black(text);
}
return chalk[style](text);
}
function showStyles(stylesArray) {
const output = stylesArray.map(style => styled(style, style)).join(' ');
console.log(output);
}
console.log('Available styles:\n');
showStyles(textStyles);
showStyles(colorStyles);
showStyles(brightColorStyles);
showStyles(bgColorStyles);
showStyles(bgBrightColorStyles);
};
const cli = meow(`
${chalk.greenBright.inverse(' Usage ')}
$ ${chalk.green('chalk')} ${chalk.yellow('[options…]')} ${chalk.cyan('<style> … <string>')}
$ ${chalk.green('echo')} ${chalk.cyan('<string>')} | ${chalk.green('chalk')} ${chalk.yellow('--stdin [options…]')} ${chalk.cyan('<style> …')}
${chalk.yellowBright.inverse(' Options ')}
${chalk.yellow('--template, -t')} Style template. The \`~\` character negates the style.
${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.redBright.inverse(' Examples ')}
$ chalk red bold 'Unicorns & Rainbows'
${chalk.red.bold('Unicorns & Rainbows')}
$ chalk -t '{red.bold Unicorns & Rainbows}'
${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)}}`}
$ echo 'Unicorns from stdin' | chalk --stdin red bold
${chalk.red.bold('Unicorns from stdin')}
`, {
importMeta: import.meta,
allowUnknownFlags: false,
flags: {
// TODO: Can be removed when https://github.com/sindresorhus/meow/issues/197 is fixed.
help: {
type: 'boolean',
},
version: {
type: 'boolean',
},
template: {
type: 'string',
alias: 't',
},
stdin: {
type: 'boolean',
},
noNewline: {
type: 'boolean',
alias: 'n',
},
demo: {
type: 'boolean',
},
},
});
const styles = cli.input;
function handleTemplateFlag(template) {
if (cli.input.length > 0) {
console.error('The --template option only accepts 1 argument');
process.exitCode = 1;
return;
}
try {
const tagArray = [template];
tagArray.raw = tagArray;
console.log(chalk(tagArray));
} catch (error) {
console.error('Something went wrong! Maybe review your syntax?\n');
console.error(error.stack);
process.exitCode = 1;
}
}
function init(data) {
for (const style of styles) {
if (!Object.keys(ansiStyles).includes(style)) {
console.error(chalk`{red Invalid style: {bold ${style}}}\n`);
printAllStyles();
process.exit(1);
}
}
const fn = dotProp.get(chalk, styles.join('.'));
process.stdout.write(fn(data.replace(/\n$/, '')));
// 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');
}
}
function processDataFromArgs() {
if (cli.flags.demo) {
printAllStyles();
return;
}
if (cli.flags.template) {
handleTemplateFlag(cli.flags.template);
return;
}
if (styles.length < 2) {
console.error('Input required');
process.exitCode = 1;
return;
}
init(styles.pop());
}
async function processDataFromStdin() {
if (styles.length === 0) {
console.error('Input required');
process.exitCode = 1;
return;
}
init(await getStdin());
}
if (process.stdin.isTTY || !cli.flags.stdin) {
processDataFromArgs();
} else {
processDataFromStdin();
}