1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const Task = require('../ember-cli/lib/models/task');
|
4 | const chalk = require("chalk");
|
5 | const glob = require("glob");
|
6 | const require_project_module_1 = require("../utilities/require-project-module");
|
7 | const config_1 = require("../models/config");
|
8 | const common_tags_1 = require("common-tags");
|
9 | exports.default = Task.extend({
|
10 | run: function (commandOptions) {
|
11 | const ui = this.ui;
|
12 | const projectRoot = this.project.root;
|
13 | const lintConfigs = config_1.CliConfig.fromProject().config.lint || [];
|
14 | if (lintConfigs.length === 0) {
|
15 | ui.writeLine(chalk.yellow(common_tags_1.oneLine `
|
16 | No lint config(s) found.
|
17 | If this is not intended, run "ng update".
|
18 | `));
|
19 | return Promise.resolve(0);
|
20 | }
|
21 | const tslint = require_project_module_1.requireProjectModule(projectRoot, 'tslint');
|
22 | const Linter = tslint.Linter;
|
23 | const Configuration = tslint.Configuration;
|
24 | let errors = 0;
|
25 | let results = '';
|
26 | lintConfigs
|
27 | .forEach((config) => {
|
28 | const program = Linter.createProgram(config.project);
|
29 | const files = getFilesToLint(program, config, Linter);
|
30 | const linter = new Linter({
|
31 | fix: commandOptions.fix,
|
32 | formatter: commandOptions.format
|
33 | }, program);
|
34 | files.forEach((file) => {
|
35 | const sourceFile = program.getSourceFile(file);
|
36 | if (!sourceFile) {
|
37 | return;
|
38 | }
|
39 | const fileContents = sourceFile.getFullText();
|
40 | const configLoad = Configuration.findConfiguration(config.tslintConfig, file);
|
41 | linter.lint(file, fileContents, configLoad.results);
|
42 | });
|
43 | const result = linter.getResult();
|
44 | errors += result.failureCount;
|
45 | results = results.concat(result.output.trim().concat('\n'));
|
46 | });
|
47 |
|
48 | if (['prose', 'verbose', 'stylish'].indexOf(commandOptions.format) == -1) {
|
49 | ui.writeLine(results.trim());
|
50 | return (errors == 0 || commandOptions.force) ? Promise.resolve(0) : Promise.resolve(2);
|
51 | }
|
52 | if (errors > 0) {
|
53 | ui.writeLine(results.trim());
|
54 | ui.writeLine(chalk.red('Lint errors found in the listed files.'));
|
55 | return commandOptions.force ? Promise.resolve(0) : Promise.resolve(2);
|
56 | }
|
57 | ui.writeLine(chalk.green('All files pass linting.'));
|
58 | return Promise.resolve(0);
|
59 | }
|
60 | });
|
61 | function getFilesToLint(program, lintConfig, Linter) {
|
62 | let files = [];
|
63 | if (lintConfig.files !== null) {
|
64 | files = Array.isArray(lintConfig.files) ? lintConfig.files : [lintConfig.files];
|
65 | }
|
66 | else {
|
67 | files = Linter.getFileNames(program);
|
68 | }
|
69 | let globOptions = {};
|
70 | if (lintConfig.exclude !== null) {
|
71 | const excludePatterns = Array.isArray(lintConfig.exclude)
|
72 | ? lintConfig.exclude
|
73 | : [lintConfig.exclude];
|
74 | globOptions = { ignore: excludePatterns, nodir: true };
|
75 | }
|
76 | files = files
|
77 | .map((file) => glob.sync(file, globOptions))
|
78 | .reduce((a, b) => a.concat(b), []);
|
79 | return files;
|
80 | }
|
81 |
|
\ | No newline at end of file |