UNPKG

14.8 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: "string",
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 name: "type-check",
109 type: "boolean",
110 describe: "(deprecated) check for type errors before linting the project",
111 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."]))),
112 },
113];
114var builtinOptions = [
115 {
116 short: "v",
117 name: "version",
118 type: "boolean",
119 describe: "current version",
120 description: "The current version of tslint.",
121 },
122 {
123 short: "h",
124 name: "help",
125 type: "boolean",
126 describe: "display detailed help",
127 description: "Prints this help message.",
128 },
129];
130commander.version(linter_1.Linter.VERSION, "-v, --version");
131for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
132 var option = options_1[_i];
133 var commanderStr = optionUsageTag(option) + optionParam(option);
134 if (option.type === "array") {
135 commander.option(commanderStr, option.describe, collect, []);
136 }
137 else {
138 commander.option(commanderStr, option.describe);
139 }
140}
141commander.on("--help", function () {
142 var indent = "\n ";
143 var optionDetails = options.concat(builtinOptions).map(function (o) {
144 return optionUsageTag(o) + ":" + (o.description.startsWith("\n") ? o.description.replace(/\n/g, indent) : indent + o.description);
145 });
146 console.log("tslint accepts the following commandline options:\n\n " + optionDetails.join("\n\n ") + "\n\n");
147});
148// Hack to get unknown option errors to work. https://github.com/visionmedia/commander.js/pull/121
149var parsed = commander.parseOptions(process.argv.slice(2));
150commander.args = parsed.args;
151if (parsed.unknown.length !== 0) {
152 commander.parseArgs([], parsed.unknown);
153}
154var argv = commander.opts();
155if (!(argv.init || argv.test !== undefined || argv.project !== undefined || commander.args.length > 0)) {
156 console.error("No files specified. Use --project to lint a project folder.");
157 process.exit(1);
158}
159if (argv.typeCheck) {
160 console.warn("--type-check is deprecated. You only need --project to enable rules which need type information.");
161 if (argv.project === undefined) {
162 console.error("--project must be specified in order to enable type checking.");
163 process.exit(1);
164 }
165}
166var outputStream = argv.out === undefined
167 ? process.stdout
168 : fs.createWriteStream(argv.out, { flags: "w+", mode: 420 });
169runner_1.run({
170 config: argv.config,
171 exclude: argv.exclude,
172 files: utils_1.arrayify(commander.args),
173 fix: argv.fix,
174 force: argv.force,
175 format: argv.format === undefined ? "prose" : argv.format,
176 formattersDirectory: argv.formattersDir,
177 init: argv.init,
178 out: argv.out,
179 outputAbsolutePaths: argv.outputAbsolutePaths,
180 project: argv.project,
181 rulesDirectory: argv.rulesDir,
182 test: argv.test,
183 typeCheck: argv.typeCheck,
184}, {
185 log: function (m) {
186 outputStream.write(m);
187 },
188 error: function (m) {
189 process.stdout.write(m);
190 },
191})
192 .then(function (rc) {
193 process.exitCode = rc;
194}).catch(function (e) {
195 console.error(e);
196 process.exitCode = 1;
197});
198function optionUsageTag(_a) {
199 var short = _a.short, name = _a.name;
200 return short !== undefined ? "-" + short + ", --" + name : "--" + name;
201}
202function optionParam(option) {
203 switch (option.type) {
204 case "string":
205 return " [" + option.name + "]";
206 case "array":
207 return " <" + option.name + ">";
208 case "boolean":
209 return "";
210 }
211}
212function collect(val, memo) {
213 memo.push(val);
214 return memo;
215}
216var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10;