1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | const fs = __importStar(require("fs"));
|
---|
30 | const path = __importStar(require("path"));
|
---|
31 | function _loadConfiguration(Configuration, options, root, file) {
|
---|
32 | if (options.tslintConfig) {
|
---|
33 | return Configuration.parseConfigFile(options.tslintConfig, root);
|
---|
34 | }
|
---|
35 | else if (options.tslintPath) {
|
---|
36 | return Configuration.findConfiguration(path.join(root, options.tslintPath)).results;
|
---|
37 | }
|
---|
38 | else if (file) {
|
---|
39 | return Configuration.findConfiguration(null, file).results;
|
---|
40 | }
|
---|
41 | else {
|
---|
42 | throw new Error('Executor must specify a tslint configuration.');
|
---|
43 | }
|
---|
44 | }
|
---|
45 | function _getFileContent(file, options, program) {
|
---|
46 | // The linter retrieves the SourceFile TS node directly if a program is used
|
---|
47 | if (program) {
|
---|
48 | const source = program.getSourceFile(file);
|
---|
49 | if (!source) {
|
---|
50 | const message = `File '${file}' is not part of the TypeScript project '${options.tsConfigPath}'.`;
|
---|
51 | throw new Error(message);
|
---|
52 | }
|
---|
53 | return source.getFullText(source);
|
---|
54 | }
|
---|
55 | // NOTE: The tslint CLI checks for and excludes MPEG transport streams; this does not.
|
---|
56 | try {
|
---|
57 | // Strip BOM from file data.
|
---|
58 | // https://stackoverflow.com/questions/24356713
|
---|
59 | return fs.readFileSync(file, 'utf-8').replace(/^\uFEFF/, '');
|
---|
60 | }
|
---|
61 | catch {
|
---|
62 | throw new Error(`Could not read file '${file}'.`);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | function _listAllFiles(root) {
|
---|
66 | const result = [];
|
---|
67 | function _recurse(location) {
|
---|
68 | const dir = fs.readdirSync(path.join(root, location));
|
---|
69 | dir.forEach((name) => {
|
---|
70 | const loc = path.join(location, name);
|
---|
71 | if (fs.statSync(path.join(root, loc)).isDirectory()) {
|
---|
72 | _recurse(loc);
|
---|
73 | }
|
---|
74 | else {
|
---|
75 | result.push(loc);
|
---|
76 | }
|
---|
77 | });
|
---|
78 | }
|
---|
79 | _recurse('');
|
---|
80 | return result;
|
---|
81 | }
|
---|
82 | /** @deprecated since version 11. Use `ng lint --fix` directly instead. */
|
---|
83 | function default_1() {
|
---|
84 | return async (options = {}, context) => {
|
---|
85 | const root = process.cwd();
|
---|
86 | const tslint = await Promise.resolve().then(() => __importStar(require('tslint'))); // eslint-disable-line import/no-extraneous-dependencies
|
---|
87 | const includes = Array.isArray(options.includes)
|
---|
88 | ? options.includes
|
---|
89 | : options.includes
|
---|
90 | ? [options.includes]
|
---|
91 | : [];
|
---|
92 | const files = Array.isArray(options.files)
|
---|
93 | ? options.files
|
---|
94 | : options.files
|
---|
95 | ? [options.files]
|
---|
96 | : [];
|
---|
97 | const Linter = tslint.Linter;
|
---|
98 | const Configuration = tslint.Configuration;
|
---|
99 | let program = undefined;
|
---|
100 | let filesToLint = files;
|
---|
101 | if (options.tsConfigPath && files.length == 0) {
|
---|
102 | const tsConfigPath = path.join(process.cwd(), options.tsConfigPath);
|
---|
103 | if (!fs.existsSync(tsConfigPath)) {
|
---|
104 | throw new Error('Could not find tsconfig.');
|
---|
105 | }
|
---|
106 | program = Linter.createProgram(tsConfigPath);
|
---|
107 | filesToLint = Linter.getFileNames(program);
|
---|
108 | }
|
---|
109 | if (includes.length > 0) {
|
---|
110 | const allFilesRel = _listAllFiles(root);
|
---|
111 | const pattern = '^(' +
|
---|
112 | includes
|
---|
113 | .map((ex) => '(' +
|
---|
114 | ex
|
---|
115 | .split(/[\/\\]/g)
|
---|
116 | .map((f) => f
|
---|
117 | .replace(/[\-\[\]{}()+?.^$|]/g, '\\$&')
|
---|
118 | .replace(/^\*\*/g, '(.+?)?')
|
---|
119 | .replace(/\*/g, '[^/\\\\]*'))
|
---|
120 | .join('[/\\\\]') +
|
---|
121 | ')')
|
---|
122 | .join('|') +
|
---|
123 | ')($|/|\\\\)';
|
---|
124 | const re = new RegExp(pattern);
|
---|
125 | filesToLint.push(...allFilesRel.filter((x) => re.test(x)).map((x) => path.join(root, x)));
|
---|
126 | }
|
---|
127 | const lintOptions = {
|
---|
128 | fix: true,
|
---|
129 | formatter: options.format || 'prose',
|
---|
130 | };
|
---|
131 | const linter = new Linter(lintOptions, program);
|
---|
132 | // If directory doesn't change, we
|
---|
133 | let lastDirectory = null;
|
---|
134 | let config;
|
---|
135 | for (const file of filesToLint) {
|
---|
136 | const dir = path.dirname(file);
|
---|
137 | if (lastDirectory !== dir) {
|
---|
138 | lastDirectory = dir;
|
---|
139 | config = _loadConfiguration(Configuration, options, root, file);
|
---|
140 | }
|
---|
141 | const content = _getFileContent(file, options, program);
|
---|
142 | if (!content) {
|
---|
143 | continue;
|
---|
144 | }
|
---|
145 | linter.lint(file, content, config);
|
---|
146 | }
|
---|
147 | const result = linter.getResult();
|
---|
148 | // Format and show the results.
|
---|
149 | if (!options.silent) {
|
---|
150 | const Formatter = tslint.findFormatter(options.format || 'prose');
|
---|
151 | if (!Formatter) {
|
---|
152 | throw new Error(`Invalid lint format "${options.format}".`);
|
---|
153 | }
|
---|
154 | const formatter = new Formatter();
|
---|
155 | // Certain tslint formatters outputs '\n' when there are no failures.
|
---|
156 | // This will bloat the console when having schematics running refactor tasks.
|
---|
157 | // see https://github.com/palantir/tslint/issues/4244
|
---|
158 | const output = (formatter.format(result.failures, result.fixes) || '').trim();
|
---|
159 | if (output) {
|
---|
160 | context.logger.info(output);
|
---|
161 | }
|
---|
162 | }
|
---|
163 | if (!options.ignoreErrors && result.errorCount > 0) {
|
---|
164 | throw new Error('Lint errors were found.');
|
---|
165 | }
|
---|
166 | };
|
---|
167 | }
|
---|
168 | exports.default = default_1;
|
---|