forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tslint-cli.ts
300 lines (283 loc) · 10.5 KB
/
tslint-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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// tslint:disable no-console object-literal-sort-keys
import commander = require("commander");
import * as fs from "fs";
import { Linter } from "./linter";
import { run } from "./runner";
import { arrayify, dedent } from "./utils";
interface Argv {
config?: string;
exclude: string[];
fix?: boolean;
force?: boolean;
help?: boolean;
init?: boolean;
out?: string;
outputAbsolutePaths: boolean;
project?: string;
rulesDir?: string;
formattersDir: string;
format?: string;
typeCheck?: boolean;
test?: boolean;
version?: boolean;
}
interface Option {
short?: string;
// Commander will camelCase option names.
name: keyof Argv | "rules-dir" | "formatters-dir" | "type-check";
type: "string" | "boolean" | "array";
describe: string; // Short, used for usage message
description: string; // Long, used for `--help`
}
const options: Option[] = [
{
short: "c",
name: "config",
type: "string",
describe: "configuration file",
description: dedent`
The location of the configuration file that tslint will use to
determine which rules are activated and what options to provide
to the rules. If no option is specified, the config file named
tslint.json is used, so long as it exists in the path.
The format of the file is { rules: { /* rules list */ } },
where /* rules list */ is a key: value comma-separated list of
rulename: rule-options pairs. Rule-options can be either a
boolean true/false value denoting whether the rule is used or not,
or a list [boolean, ...] where the boolean provides the same role
as in the non-list case, and the rest of the list are options passed
to the rule that will determine what it checks for (such as number
of characters for the max-line-length rule, or what functions to ban
for the ban rule).`,
},
{
short: "e",
name: "exclude",
type: "array",
describe: "exclude globs from path expansion",
description: dedent`
A filename or glob which indicates files to exclude from linting.
This option can be supplied multiple times if you need multiple
globs to indicate which files to exclude.`,
},
{
name: "fix",
type: "boolean",
describe: "fixes linting errors for select rules (this may overwrite linted files)",
description: "Fixes linting errors for select rules. This may overwrite linted files.",
},
{
name: "force",
type: "boolean",
describe: "return status code 0 even if there are lint errors",
description: dedent`
Return status code 0 even if there are any lint errors.
Useful while running as npm script.`,
},
{
short: "i",
name: "init",
type: "boolean",
describe: "generate a tslint.json config file in the current working directory",
description: "Generates a tslint.json config file in the current working directory.",
},
{
short: "o",
name: "out",
type: "string",
describe: "output file",
description: dedent`
A filename to output the results to. By default, tslint outputs to
stdout, which is usually the console where you're running it from.`,
},
{
name: "outputAbsolutePaths",
type: "boolean",
describe: "whether or not outputted file paths are absolute",
description: "If true, all paths in the output will be absolute.",
},
{
short: "r",
name: "rules-dir",
type: "array",
describe: "rules directory",
description: dedent`
An additional rules directory, for user-created rules.
tslint will always check its default rules directory, in
node_modules/tslint/lib/rules, before checking the user-provided
rules directory, so rules in the user-provided rules directory
with the same name as the base rules will not be loaded.`,
},
{
short: "s",
name: "formatters-dir",
type: "string",
describe: "formatters directory",
description: dedent`
An additional formatters directory, for user-created formatters.
Formatters are files that will format the tslint output, before
writing it to stdout or the file passed in --out. The default
directory, node_modules/tslint/build/formatters, will always be
checked first, so user-created formatters with the same names
as the base formatters will not be loaded.`,
},
{
short: "t",
name: "format",
type: "string",
describe: "output format (prose, json, stylish, verbose, pmd, msbuild, checkstyle, vso, fileslist, codeFrame)",
description: dedent`
The formatter to use to format the results of the linter before
outputting it to stdout or the file passed in --out. The core
formatters are prose (human readable), json (machine readable)
and verbose. prose is the default if this option is not used.
Other built-in options include pmd, msbuild, checkstyle, and vso.
Additional formatters can be added and used if the --formatters-dir
option is set.`,
},
{
name: "test",
type: "boolean",
describe: "test that tslint produces the correct output for the specified directory",
description: dedent`
Runs tslint on matched directories and checks if tslint outputs
match the expected output in .lint files. Automatically loads the
tslint.json files in the directories as the configuration file for
the tests. See the full tslint documentation for more details on how
this can be used to test custom rules.`,
},
{
short: "p",
name: "project",
type: "string",
describe: "tsconfig.json file",
description: dedent`
The path or directory containing a tsconfig.json file that will be
used to determine which files will be linted. This flag also enables
rules that require the type checker.`,
},
{
name: "type-check",
type: "boolean",
describe: "(deprecated) check for type errors before linting the project",
description: dedent`
(deprecated) Checks for type errors before linting a project.
--project must be specified in order to enable type checking.`,
},
];
const builtinOptions: Option[] = [
{
short: "v",
name: "version",
type: "boolean",
describe: "current version",
description: "The current version of tslint.",
},
{
short: "h",
name: "help",
type: "boolean",
describe: "display detailed help",
description: "Prints this help message.",
},
];
commander.version(Linter.VERSION, "-v, --version");
for (const option of options) {
const commanderStr = optionUsageTag(option) + optionParam(option);
if (option.type === "array") {
commander.option(commanderStr, option.describe, collect, []);
} else {
commander.option(commanderStr, option.describe);
}
}
commander.on("--help", () => {
const indent = "\n ";
const optionDetails = options.concat(builtinOptions).map((o) =>
`${optionUsageTag(o)}:${o.description.startsWith("\n") ? o.description.replace(/\n/g, indent) : indent + o.description}`);
console.log(`tslint accepts the following commandline options:\n\n ${optionDetails.join("\n\n ")}\n\n`);
});
// Hack to get unknown option errors to work. https://github.com/visionmedia/commander.js/pull/121
const parsed = commander.parseOptions(process.argv.slice(2));
commander.args = parsed.args;
if (parsed.unknown.length !== 0) {
(commander.parseArgs as (args: string[], unknown: string[]) => void)([], parsed.unknown);
}
const argv = commander.opts() as any as Argv;
if (!(argv.init || argv.test !== undefined || argv.project !== undefined || commander.args.length > 0)) {
console.error("No files specified. Use --project to lint a project folder.");
process.exit(1);
}
if (argv.typeCheck) {
console.warn("--type-check is deprecated. You only need --project to enable rules which need type information.");
if (argv.project === undefined) {
console.error("--project must be specified in order to enable type checking.");
process.exit(1);
}
}
const outputStream: NodeJS.WritableStream = argv.out === undefined
? process.stdout
: fs.createWriteStream(argv.out, {flags: "w+", mode: 420});
run(
{
config: argv.config,
exclude: argv.exclude,
files: arrayify(commander.args),
fix: argv.fix,
force: argv.force,
format: argv.format === undefined ? "prose" : argv.format,
formattersDirectory: argv.formattersDir,
init: argv.init,
out: argv.out,
outputAbsolutePaths: argv.outputAbsolutePaths,
project: argv.project,
rulesDirectory: argv.rulesDir,
test: argv.test,
typeCheck: argv.typeCheck,
},
{
log(m) {
outputStream.write(m);
},
error(m) {
process.stdout.write(m);
},
})
.then((rc) => {
process.exitCode = rc;
}).catch((e) => {
console.error(e);
process.exitCode = 1;
});
function optionUsageTag({short, name}: Option) {
return short !== undefined ? `-${short}, --${name}` : `--${name}`;
}
function optionParam(option: Option) {
switch (option.type) {
case "string":
return ` [${option.name}]`;
case "array":
return ` <${option.name}>`;
case "boolean":
return "";
}
}
function collect(val: string, memo: string[]) {
memo.push(val);
return memo;
}