-
-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathcli.ts
247 lines (217 loc) · 6.65 KB
/
cli.ts
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env node
import path from 'node:path';
import fs from 'node:fs/promises';
import process from 'node:process';
import {type Rule, type ESLint} from 'eslint';
import formatterPretty from 'eslint-formatter-pretty';
import getStdin from 'get-stdin';
import meow from 'meow';
import {pathExists} from 'path-exists';
import {tsExtensions} from './lib/constants.js';
import type {LinterOptions, XoConfigOptions} from './lib/types.js';
import {Xo} from './lib/xo.js';
import openReport from './lib/open-report.js';
const cli = meow(
`
Usage
$ xo [<file|glob> ...]
Options
--fix Automagically fix issues
--reporter Reporter to use
--space Use space indent instead of tabs [Default: 2]
--semicolon Use semicolons [Default: true]
--prettier Conform to Prettier code style [Default: false]
--react Include React specific parsing and xo-react linting rules [Default: false]
--prettier Format with prettier or turn off prettier conflicted rules when set to 'compat' [Default: false]
--print-config Print the effective ESLint config for the given file
--open Open files with issues in your editor
--stdin Validate/fix code from stdin
--stdin-filename Specify a filename for the --stdin option
--ignore Ignore pattern globs, can be set multiple times
--cwd=<dir> Working directory for files [Default: process.cwd()]
Examples
$ xo
$ xo index.js
$ xo *.js !foo.js
$ xo --space
$ xo --print-config=index.js
`,
{
importMeta: import.meta,
autoVersion: false,
booleanDefault: undefined,
flags: {
fix: {
type: 'boolean',
default: false,
},
reporter: {
type: 'string',
},
space: {
type: 'string',
},
config: {
type: 'string',
},
quiet: {
type: 'boolean',
},
semicolon: {
type: 'boolean',
},
prettier: {
type: 'boolean',
},
react: {
type: 'boolean',
default: false,
},
cwd: {
type: 'string',
default: process.cwd(),
},
printConfig: {
type: 'string',
},
version: {
type: 'boolean',
},
stdin: {
type: 'boolean',
},
stdinFilename: {
type: 'string',
default: 'stdin.js',
},
open: {
type: 'boolean',
},
ignore: {
type: 'string',
isMultiple: true,
aliases: ['ignores'],
},
},
},
);
export type CliOptions = typeof cli;
const {input, flags: cliOptions, showVersion} = cli;
const baseXoConfigOptions: XoConfigOptions = {
space: cliOptions.space,
semicolon: cliOptions.semicolon,
prettier: cliOptions.prettier,
ignores: cliOptions.ignore,
react: cliOptions.react,
};
const linterOptions: LinterOptions = {
fix: cliOptions.fix,
cwd: (cliOptions.cwd && path.resolve(cliOptions.cwd)) ?? process.cwd(),
quiet: cliOptions.quiet,
ts: true,
};
// Make data types for `options.space` match those of the API
if (typeof cliOptions.space === 'string') {
cliOptions.space = cliOptions.space.trim();
if (/^\d+$/u.test(cliOptions.space)) {
baseXoConfigOptions.space = Number.parseInt(cliOptions.space, 10);
} else if (cliOptions.space === 'true') {
baseXoConfigOptions.space = true;
} else if (cliOptions.space === 'false') {
baseXoConfigOptions.space = false;
} else {
if (cliOptions.space !== '') {
// Assume `options.space` was set to a filename when run as `xo --space file.js`
input.push(cliOptions.space);
}
baseXoConfigOptions.space = true;
}
}
if (
process.env['GITHUB_ACTIONS']
&& !linterOptions.fix
&& !cliOptions.reporter
) {
linterOptions.quiet = true;
}
const log = async (report: {
errorCount: number;
warningCount: number;
fixableErrorCount: number;
fixableWarningCount: number;
results: ESLint.LintResult[];
rulesMeta: Record<string, Rule.RuleMetaData>;
}) => {
const reporter = cliOptions.reporter
? await new Xo(linterOptions, baseXoConfigOptions).getFormatter(cliOptions.reporter)
: {format: formatterPretty};
// @ts-expect-error the types don't quite match up here
console.log(reporter.format(report.results, {cwd: linterOptions.cwd, ...report}));
process.exitCode = report.errorCount === 0 ? 0 : 1;
};
if (cliOptions.version) {
showVersion();
}
if (cliOptions.stdin) {
const stdin = await getStdin();
let shouldRemoveStdInFile = false;
// For TypeScript, we need a file on the filesystem to lint it or else @typescript-eslint will blow up.
// We create a temporary file in the node_modules/.cache/xo-linter directory to avoid conflicts with the user's files and lint that file as if it were the stdin input as a work around.
// We clean up the file after linting.
if (cliOptions.stdinFilename && tsExtensions.includes(path.extname(cliOptions.stdinFilename).slice(1))) {
const absoluteFilePath = path.resolve(cliOptions.cwd, cliOptions.stdinFilename);
if (!await pathExists(absoluteFilePath)) {
cliOptions.stdinFilename = path.join(cliOptions.cwd, 'node_modules', '.cache', 'xo-linter', path.basename(absoluteFilePath));
shouldRemoveStdInFile = true;
baseXoConfigOptions.ignores = [
'!**/node_modules/**',
'!node_modules/**',
'!node_modules/',
`!${path.relative(cliOptions.cwd, cliOptions.stdinFilename)}`,
];
if (!await pathExists(path.dirname(cliOptions.stdinFilename))) {
await fs.mkdir(path.dirname(cliOptions.stdinFilename), {recursive: true});
}
await fs.writeFile(cliOptions.stdinFilename, stdin);
}
}
if (cliOptions.fix) {
const xo = new Xo(linterOptions, baseXoConfigOptions);
const {results: [result]} = await xo.lintText(stdin, {
filePath: cliOptions.stdinFilename,
});
process.stdout.write((result?.output) ?? stdin);
process.exit(0);
}
if (cliOptions.open) {
console.error('The `--open` flag is not supported on stdin');
if (shouldRemoveStdInFile) {
await fs.rm(cliOptions.stdinFilename);
}
process.exit(1);
}
const xo = new Xo(linterOptions, baseXoConfigOptions);
await log(await xo.lintText(stdin, {filePath: cliOptions.stdinFilename, warnIgnored: true}));
if (shouldRemoveStdInFile) {
await fs.rm(cliOptions.stdinFilename);
}
process.exit(0);
}
if (typeof cliOptions.printConfig === 'string') {
if (input.length > 0 || cliOptions.printConfig === '') {
console.error('The `--print-config` flag must be used with exactly one filename');
process.exit(1);
}
const config = await new Xo(linterOptions, baseXoConfigOptions).calculateConfigForFile(cliOptions.printConfig);
console.log(JSON.stringify(config, undefined, '\t'));
} else {
const xo = new Xo(linterOptions, baseXoConfigOptions);
const report = await xo.lintFiles(input);
if (cliOptions.fix) {
await Xo.outputFixes(report);
}
if (cliOptions.open) {
await openReport(report);
}
await log(report);
}