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 | .parse(process.argv);
|
12 |
|
13 | require('eslint/lib/config/plugins').define('es-beautifier', esBeautifier);
|
14 | const CLIEngine = require('eslint').CLIEngine;
|
15 |
|
16 | const config = esBeautifier.configs[program.config];
|
17 | if (!config) {
|
18 | console.error('no such config');
|
19 | process.exit(1);
|
20 | }
|
21 |
|
22 | config.fix = true;
|
23 | config.useEslintrc = false;
|
24 | const cli = new CLIEngine(config);
|
25 |
|
26 | if (program.args.length) {
|
27 | const report = cli.executeOnFiles(program.args);
|
28 | CLIEngine.outputFixes(report);
|
29 | } else {
|
30 | process.stdin.setEncoding('utf8');
|
31 | const chunks = [];
|
32 | process.stdin.on('readable', () => {
|
33 | const chunk = process.stdin.read();
|
34 | if (chunk) chunks.push(chunk);
|
35 | });
|
36 | process.stdin.on('end', () => {
|
37 | const text = chunks.join('');
|
38 | const report = cli.executeOnText(text);
|
39 | const output = report.results[0].output || text;
|
40 | process.stdout.write(output, 'utf8');
|
41 | });
|
42 | }
|