1 | #!/usr/bin/env node
|
2 |
|
3 | const program = require('commander');
|
4 | const version = require('../package.json').version;
|
5 | const esBeautifier = require('./main.js');
|
6 |
|
7 | program
|
8 | .version(version)
|
9 | .usage('[options] [<file ...>]')
|
10 | .option('-c, --config [name]', 'specify config name [standard]', 'standard')
|
11 | .option('-u, --use-eslintrc', 'use eslintrc files')
|
12 | .parse(process.argv);
|
13 |
|
14 | require('eslint/lib/config/plugins').define('es-beautifier', esBeautifier);
|
15 | const CLIEngine = require('eslint').CLIEngine;
|
16 |
|
17 | const config = esBeautifier.configs[program.config];
|
18 | if (!config) {
|
19 | console.error('no such config');
|
20 | process.exit(1);
|
21 | }
|
22 |
|
23 | config.fix = true;
|
24 | config.useEslintrc = !!program.useEslintrc;
|
25 | const cli = new CLIEngine(config);
|
26 |
|
27 | if (program.args.length) {
|
28 | const report = cli.executeOnFiles(program.args);
|
29 | CLIEngine.outputFixes(report);
|
30 | } else {
|
31 | process.stdin.setEncoding('utf8');
|
32 | const chunks = [];
|
33 | process.stdin.on('readable', () => {
|
34 | const chunk = process.stdin.read();
|
35 | if (chunk) chunks.push(chunk);
|
36 | });
|
37 | process.stdin.on('end', () => {
|
38 | const text = chunks.join('');
|
39 | const report = cli.executeOnText(text);
|
40 | const output = report.results[0].output || text;
|
41 | process.stdout.write(output, 'utf8');
|
42 | });
|
43 | }
|