UNPKG

16 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 mkdirp = require("mkdirp");
24var path = require("path");
25var linter_1 = require("./linter");
26var runner_1 = require("./runner");
27var utils_1 = require("./utils");
28var options = [
29 {
30 short: "c",
31 name: "config",
32 type: "string",
33 describe: "configuration file",
34 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)."]))),
35 },
36 {
37 short: "e",
38 name: "exclude",
39 type: "array",
40 describe: "exclude globs from path expansion",
41 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."]))),
42 },
43 {
44 name: "fix",
45 type: "boolean",
46 describe: "fixes linting errors for select rules (this may overwrite linted files)",
47 description: "Fixes linting errors for select rules. This may overwrite linted files.",
48 },
49 {
50 name: "force",
51 type: "boolean",
52 describe: "return status code 0 even if there are lint errors",
53 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."]))),
54 },
55 {
56 short: "i",
57 name: "init",
58 type: "boolean",
59 describe: "generate a tslint.json config file in the current working directory",
60 description: "Generates a tslint.json config file in the current working directory.",
61 },
62 {
63 short: "o",
64 name: "out",
65 type: "string",
66 describe: "output file",
67 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."]))),
68 },
69 {
70 name: "outputAbsolutePaths",
71 type: "boolean",
72 describe: "whether or not outputted file paths are absolute",
73 description: "If true, all paths in the output will be absolute.",
74 },
75 {
76 name: "print-config",
77 type: "boolean",
78 describe: "print resolved configuration for a file",
79 description: utils_1.dedent(templateObject_5 || (templateObject_5 = tslib_1.__makeTemplateObject(["\n When passed a single file name, prints the configuration that would\n be used to lint that file.\n No linting is performed and only config-related options are valid."], ["\n When passed a single file name, prints the configuration that would\n be used to lint that file.\n No linting is performed and only config-related options are valid."]))),
80 },
81 {
82 short: "r",
83 name: "rules-dir",
84 type: "array",
85 describe: "rules directory",
86 description: utils_1.dedent(templateObject_6 || (templateObject_6 = 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."]))),
87 },
88 {
89 short: "s",
90 name: "formatters-dir",
91 type: "string",
92 describe: "formatters directory",
93 description: utils_1.dedent(templateObject_7 || (templateObject_7 = 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."]))),
94 },
95 {
96 short: "t",
97 name: "format",
98 type: "string",
99 describe: "output format (json, stylish, verbose, pmd, msbuild, checkstyle, vso, fileslist, codeFrame)",
100 description: utils_1.dedent(templateObject_8 || (templateObject_8 = 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 stylish (colored and human readable), json (machine\n readable), and verbose (human readable).\n stylish is the default if this option is not used.\n Other built-in options include pmd, prose, 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 stylish (colored and human readable), json (machine\n readable), and verbose (human readable).\n stylish is the default if this option is not used.\n Other built-in options include pmd, prose, msbuild, checkstyle, and vso.\n Additional formatters can be added and used if the --formatters-dir\n option is set."]))),
101 },
102 {
103 name: "test",
104 type: "boolean",
105 describe: "test that tslint produces the correct output for the specified directory",
106 description: utils_1.dedent(templateObject_9 || (templateObject_9 = 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."]))),
107 },
108 {
109 short: "p",
110 name: "project",
111 type: "string",
112 describe: "tsconfig.json file",
113 description: utils_1.dedent(templateObject_10 || (templateObject_10 = 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."]))),
114 },
115 {
116 short: "q",
117 name: "quiet",
118 type: "boolean",
119 describe: "hide warnings on lint",
120 description: "If true, hides warnings from linting output.",
121 },
122 {
123 name: "type-check",
124 type: "boolean",
125 describe: "(deprecated) check for type errors before linting the project",
126 description: utils_1.dedent(templateObject_11 || (templateObject_11 = 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."]))),
127 },
128];
129var builtinOptions = [
130 {
131 short: "v",
132 name: "version",
133 type: "boolean",
134 describe: "current version",
135 description: "The current version of tslint.",
136 },
137 {
138 short: "h",
139 name: "help",
140 type: "boolean",
141 describe: "display detailed help",
142 description: "Prints this help message.",
143 },
144];
145commander.version(linter_1.Linter.VERSION, "-v, --version");
146for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
147 var option = options_1[_i];
148 var commanderStr = optionUsageTag(option) + optionParam(option);
149 if (option.type === "array") {
150 commander.option(commanderStr, option.describe, collect, []);
151 }
152 else {
153 commander.option(commanderStr, option.describe);
154 }
155}
156commander.on("--help", function () {
157 var indent = "\n ";
158 var optionDetails = options
159 .concat(builtinOptions)
160 .map(function (o) {
161 return optionUsageTag(o) + ":" + (o.description.startsWith("\n")
162 ? o.description.replace(/\n/g, indent)
163 : indent + o.description);
164 });
165 console.log("tslint accepts the following commandline options:\n\n " + optionDetails.join("\n\n ") + "\n\n");
166});
167// Hack to get unknown option errors to work. https://github.com/visionmedia/commander.js/pull/121
168var parsed = commander.parseOptions(process.argv.slice(2));
169commander.args = parsed.args;
170if (parsed.unknown.length !== 0) {
171 commander.parseArgs([], parsed.unknown);
172}
173var argv = commander.opts();
174if (!(argv.init ||
175 argv.test !== undefined ||
176 argv.project !== undefined ||
177 commander.args.length > 0)) {
178 console.error("No files specified. Use --project to lint a project folder.");
179 process.exit(1);
180}
181if (argv.typeCheck) {
182 console.warn("--type-check is deprecated. You only need --project to enable rules which need type information.");
183 if (argv.project === undefined) {
184 console.error("--project must be specified in order to enable type checking.");
185 process.exit(1);
186 }
187}
188var outputStream = process.stdout;
189if (argv.out !== undefined) {
190 mkdirp.sync(path.dirname(argv.out));
191 outputStream = fs.createWriteStream(argv.out, { flags: "w+", mode: 420 });
192}
193runner_1.run({
194 config: argv.config,
195 exclude: argv.exclude,
196 files: utils_1.arrayify(commander.args),
197 fix: argv.fix,
198 force: argv.force,
199 format: argv.format,
200 formattersDirectory: argv.formattersDir,
201 init: argv.init,
202 out: argv.out,
203 outputAbsolutePaths: argv.outputAbsolutePaths,
204 printConfig: argv.printConfig,
205 project: argv.project,
206 quiet: argv.quiet,
207 rulesDirectory: argv.rulesDir,
208 test: argv.test,
209 typeCheck: argv.typeCheck,
210}, {
211 log: function (m) {
212 outputStream.write(m);
213 },
214 error: function (m) {
215 process.stderr.write(m);
216 },
217})
218 .then(function (rc) {
219 process.exitCode = rc;
220})
221 .catch(function (e) {
222 console.error(e);
223 process.exitCode = 1;
224});
225function optionUsageTag(_a) {
226 var short = _a.short, name = _a.name;
227 return short !== undefined ? "-" + short + ", --" + name : "--" + name;
228}
229function optionParam(option) {
230 switch (option.type) {
231 case "string":
232 return " [" + option.name + "]";
233 case "array":
234 return " <" + option.name + ">";
235 case "boolean":
236 return "";
237 }
238}
239function collect(val, memo) {
240 memo.push(val);
241 return memo;
242}
243var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11;