UNPKG

15.1 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright 2013 Palantir Technologies, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19var tslib_1 = require("tslib");
20// tslint:disable no-console object-literal-sort-keys
21var commander = require("commander");
22var fs = require("fs");
23var linter_1 = require("./linter");
24var runner_1 = require("./runner");
25var utils_1 = require("./utils");
26var options = [
27 {
28 short: "c",
29 name: "config",
30 type: "string",
31 describe: "configuration file",
32 description: utils_1.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n The location of the configuration file that tslint will use to\n determine which rules are activated and what options to provide\n to the rules. If no option is specified, the config file named\n tslint.json is used, so long as it exists in the path.\n The format of the file is { rules: { /* rules list */ } },\n where /* rules list */ is a key: value comma-separated list of\n rulename: rule-options pairs. Rule-options can be either a\n boolean true/false value denoting whether the rule is used or not,\n or a list [boolean, ...] where the boolean provides the same role\n as in the non-list case, and the rest of the list are options passed\n to the rule that will determine what it checks for (such as number\n of characters for the max-line-length rule, or what functions to ban\n for the ban rule)."], ["\n The location of the configuration file that tslint will use to\n determine which rules are activated and what options to provide\n to the rules. If no option is specified, the config file named\n tslint.json is used, so long as it exists in the path.\n The format of the file is { rules: { /* rules list */ } },\n where /* rules list */ is a key: value comma-separated list of\n rulename: rule-options pairs. Rule-options can be either a\n boolean true/false value denoting whether the rule is used or not,\n or a list [boolean, ...] where the boolean provides the same role\n as in the non-list case, and the rest of the list are options passed\n to the rule that will determine what it checks for (such as number\n of characters for the max-line-length rule, or what functions to ban\n for the ban rule)."]))),
33 },
34 {
35 short: "e",
36 name: "exclude",
37 type: "array",
38 describe: "exclude globs from path expansion",
39 description: utils_1.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n A filename or glob which indicates files to exclude from linting.\n This option can be supplied multiple times if you need multiple\n globs to indicate which files to exclude."], ["\n A filename or glob which indicates files to exclude from linting.\n This option can be supplied multiple times if you need multiple\n globs to indicate which files to exclude."]))),
40 },
41 {
42 name: "fix",
43 type: "boolean",
44 describe: "fixes linting errors for select rules (this may overwrite linted files)",
45 description: "Fixes linting errors for select rules. This may overwrite linted files.",
46 },
47 {
48 name: "force",
49 type: "boolean",
50 describe: "return status code 0 even if there are lint errors",
51 description: utils_1.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n Return status code 0 even if there are any lint errors.\n Useful while running as npm script."], ["\n Return status code 0 even if there are any lint errors.\n Useful while running as npm script."]))),
52 },
53 {
54 short: "i",
55 name: "init",
56 type: "boolean",
57 describe: "generate a tslint.json config file in the current working directory",
58 description: "Generates a tslint.json config file in the current working directory.",
59 },
60 {
61 short: "o",
62 name: "out",
63 type: "string",
64 describe: "output file",
65 description: utils_1.dedent(templateObject_4 || (templateObject_4 = tslib_1.__makeTemplateObject(["\n A filename to output the results to. By default, tslint outputs to\n stdout, which is usually the console where you're running it from."], ["\n A filename to output the results to. By default, tslint outputs to\n stdout, which is usually the console where you're running it from."]))),
66 },
67 {
68 name: "outputAbsolutePaths",
69 type: "boolean",
70 describe: "whether or not outputted file paths are absolute",
71 description: "If true, all paths in the output will be absolute.",
72 },
73 {
74 short: "r",
75 name: "rules-dir",
76 type: "array",
77 describe: "rules directory",
78 description: utils_1.dedent(templateObject_5 || (templateObject_5 = tslib_1.__makeTemplateObject(["\n An additional rules directory, for user-created rules.\n tslint will always check its default rules directory, in\n node_modules/tslint/lib/rules, before checking the user-provided\n rules directory, so rules in the user-provided rules directory\n with the same name as the base rules will not be loaded."], ["\n An additional rules directory, for user-created rules.\n tslint will always check its default rules directory, in\n node_modules/tslint/lib/rules, before checking the user-provided\n rules directory, so rules in the user-provided rules directory\n with the same name as the base rules will not be loaded."]))),
79 },
80 {
81 short: "s",
82 name: "formatters-dir",
83 type: "string",
84 describe: "formatters directory",
85 description: utils_1.dedent(templateObject_6 || (templateObject_6 = tslib_1.__makeTemplateObject(["\n An additional formatters directory, for user-created formatters.\n Formatters are files that will format the tslint output, before\n writing it to stdout or the file passed in --out. The default\n directory, node_modules/tslint/build/formatters, will always be\n checked first, so user-created formatters with the same names\n as the base formatters will not be loaded."], ["\n An additional formatters directory, for user-created formatters.\n Formatters are files that will format the tslint output, before\n writing it to stdout or the file passed in --out. The default\n directory, node_modules/tslint/build/formatters, will always be\n checked first, so user-created formatters with the same names\n as the base formatters will not be loaded."]))),
86 },
87 {
88 short: "t",
89 name: "format",
90 type: "string",
91 describe: "output format (prose, json, stylish, verbose, pmd, msbuild, checkstyle, vso, fileslist, codeFrame)",
92 description: utils_1.dedent(templateObject_7 || (templateObject_7 = tslib_1.__makeTemplateObject(["\n The formatter to use to format the results of the linter before\n outputting it to stdout or the file passed in --out. The core\n formatters are prose (human readable), json (machine readable)\n and verbose. prose is the default if this option is not used.\n Other built-in options include pmd, msbuild, checkstyle, and vso.\n Additional formatters can be added and used if the --formatters-dir\n option is set."], ["\n The formatter to use to format the results of the linter before\n outputting it to stdout or the file passed in --out. The core\n formatters are prose (human readable), json (machine readable)\n and verbose. prose is the default if this option is not used.\n Other built-in options include pmd, msbuild, checkstyle, and vso.\n Additional formatters can be added and used if the --formatters-dir\n option is set."]))),
93 },
94 {
95 name: "test",
96 type: "boolean",
97 describe: "test that tslint produces the correct output for the specified directory",
98 description: utils_1.dedent(templateObject_8 || (templateObject_8 = tslib_1.__makeTemplateObject(["\n Runs tslint on matched directories and checks if tslint outputs\n match the expected output in .lint files. Automatically loads the\n tslint.json files in the directories as the configuration file for\n the tests. See the full tslint documentation for more details on how\n this can be used to test custom rules."], ["\n Runs tslint on matched directories and checks if tslint outputs\n match the expected output in .lint files. Automatically loads the\n tslint.json files in the directories as the configuration file for\n the tests. See the full tslint documentation for more details on how\n this can be used to test custom rules."]))),
99 },
100 {
101 short: "p",
102 name: "project",
103 type: "string",
104 describe: "tsconfig.json file",
105 description: utils_1.dedent(templateObject_9 || (templateObject_9 = tslib_1.__makeTemplateObject(["\n The path to the tsconfig.json file or to the directory containing\n the tsconfig.json file. The file will be used to determine which\n files will be linted. This flag also enables rules that require the\n type checker."], ["\n The path to the tsconfig.json file or to the directory containing\n the tsconfig.json file. The file will be used to determine which\n files will be linted. This flag also enables rules that require the\n type checker."]))),
106 },
107 {
108 short: "q",
109 name: "quiet",
110 type: "boolean",
111 describe: "hide errors on lint",
112 description: "If true, hides warnings from linting output.",
113 },
114 {
115 name: "type-check",
116 type: "boolean",
117 describe: "(deprecated) check for type errors before linting the project",
118 description: utils_1.dedent(templateObject_10 || (templateObject_10 = tslib_1.__makeTemplateObject(["\n (deprecated) Checks for type errors before linting a project.\n --project must be specified in order to enable type checking."], ["\n (deprecated) Checks for type errors before linting a project.\n --project must be specified in order to enable type checking."]))),
119 },
120];
121var builtinOptions = [
122 {
123 short: "v",
124 name: "version",
125 type: "boolean",
126 describe: "current version",
127 description: "The current version of tslint.",
128 },
129 {
130 short: "h",
131 name: "help",
132 type: "boolean",
133 describe: "display detailed help",
134 description: "Prints this help message.",
135 },
136];
137commander.version(linter_1.Linter.VERSION, "-v, --version");
138for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
139 var option = options_1[_i];
140 var commanderStr = optionUsageTag(option) + optionParam(option);
141 if (option.type === "array") {
142 commander.option(commanderStr, option.describe, collect, []);
143 }
144 else {
145 commander.option(commanderStr, option.describe);
146 }
147}
148commander.on("--help", function () {
149 var indent = "\n ";
150 var optionDetails = options
151 .concat(builtinOptions)
152 .map(function (o) {
153 return optionUsageTag(o) + ":" + (o.description.startsWith("\n")
154 ? o.description.replace(/\n/g, indent)
155 : indent + o.description);
156 });
157 console.log("tslint accepts the following commandline options:\n\n " + optionDetails.join("\n\n ") + "\n\n");
158});
159// Hack to get unknown option errors to work. https://github.com/visionmedia/commander.js/pull/121
160var parsed = commander.parseOptions(process.argv.slice(2));
161commander.args = parsed.args;
162if (parsed.unknown.length !== 0) {
163 commander.parseArgs([], parsed.unknown);
164}
165var argv = commander.opts();
166if (!(argv.init ||
167 argv.test !== undefined ||
168 argv.project !== undefined ||
169 commander.args.length > 0)) {
170 console.error("No files specified. Use --project to lint a project folder.");
171 process.exit(1);
172}
173if (argv.typeCheck) {
174 console.warn("--type-check is deprecated. You only need --project to enable rules which need type information.");
175 if (argv.project === undefined) {
176 console.error("--project must be specified in order to enable type checking.");
177 process.exit(1);
178 }
179}
180var outputStream = argv.out === undefined
181 ? process.stdout
182 : fs.createWriteStream(argv.out, { flags: "w+", mode: 420 });
183runner_1.run({
184 config: argv.config,
185 exclude: argv.exclude,
186 files: utils_1.arrayify(commander.args),
187 fix: argv.fix,
188 force: argv.force,
189 format: argv.format,
190 formattersDirectory: argv.formattersDir,
191 init: argv.init,
192 out: argv.out,
193 outputAbsolutePaths: argv.outputAbsolutePaths,
194 project: argv.project,
195 quiet: argv.quiet,
196 rulesDirectory: argv.rulesDir,
197 test: argv.test,
198 typeCheck: argv.typeCheck,
199}, {
200 log: function (m) {
201 outputStream.write(m);
202 },
203 error: function (m) {
204 process.stderr.write(m);
205 },
206})
207 .then(function (rc) {
208 process.exitCode = rc;
209})
210 .catch(function (e) {
211 console.error(e);
212 process.exitCode = 1;
213});
214function optionUsageTag(_a) {
215 var short = _a.short, name = _a.name;
216 return short !== undefined ? "-" + short + ", --" + name : "--" + name;
217}
218function optionParam(option) {
219 switch (option.type) {
220 case "string":
221 return " [" + option.name + "]";
222 case "array":
223 return " <" + option.name + ">";
224 case "boolean":
225 return "";
226 }
227}
228function collect(val, memo) {
229 memo.push(val);
230 return memo;
231}
232var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10;