|
1 | 1 | const Task = require('../ember-cli/lib/models/task');
|
2 | 2 | import * as chalk from 'chalk';
|
3 |
| -import {exec} from 'child_process'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { requireDependency } from '../utilities/require-project-module'; |
| 5 | +import { CliConfig } from '../models/config'; |
| 6 | +import { LintCommandOptions } from '../commands/lint'; |
| 7 | +import { oneLine } from 'common-tags'; |
4 | 8 |
|
5 | 9 | export default Task.extend({
|
6 |
| - run: function () { |
| 10 | + run: function (commandOptions: LintCommandOptions) { |
7 | 11 | const ui = this.ui;
|
| 12 | + const projectRoot = this.project.root; |
8 | 13 |
|
9 |
| - return new Promise(function(resolve, reject) { |
10 |
| - exec('npm run lint', (err, stdout) => { |
11 |
| - ui.writeLine(stdout); |
12 |
| - if (err) { |
13 |
| - ui.writeLine(chalk.red('Lint errors found in the listed files.')); |
14 |
| - reject(); |
15 |
| - } else { |
16 |
| - ui.writeLine(chalk.green('All files pass linting.')); |
17 |
| - resolve(); |
18 |
| - } |
| 14 | + return new Promise(function (resolve, reject) { |
| 15 | + const tslint = requireDependency(projectRoot, 'tslint'); |
| 16 | + const Linter = tslint.Linter; |
| 17 | + const Configuration = tslint.Configuration; |
| 18 | + |
| 19 | + const lintConfigs = CliConfig.fromProject().config.lint || []; |
| 20 | + |
| 21 | + if (lintConfigs.length === 0) { |
| 22 | + ui.writeLine(chalk.yellow(oneLine` |
| 23 | + No lint config(s) found. |
| 24 | + If this is not intended, run "ng update". |
| 25 | + `)); |
| 26 | + return resolve(0); |
| 27 | + } |
| 28 | + |
| 29 | + let errors = 0; |
| 30 | + |
| 31 | + lintConfigs.forEach((config) => { |
| 32 | + const program = Linter.createProgram(config.project); |
| 33 | + const files: string[] = Linter.getFileNames(program); |
| 34 | + |
| 35 | + const linter = new Linter({ |
| 36 | + fix: commandOptions.fix, |
| 37 | + formatter: commandOptions.format |
| 38 | + }, program); |
| 39 | + |
| 40 | + files.forEach((file) => { |
| 41 | + const fileContents = program.getSourceFile(file).getFullText(); |
| 42 | + const configLoad = Configuration.findConfiguration(config.tslintConfig, file); |
| 43 | + linter.lint(file, fileContents, configLoad.results); |
| 44 | + }); |
| 45 | + |
| 46 | + const result = linter.getResult(); |
| 47 | + errors += result.failureCount; |
| 48 | + |
| 49 | + ui.writeLine(result.output.trim().concat('\n')); |
19 | 50 | });
|
| 51 | + |
| 52 | + if (errors > 0) { |
| 53 | + ui.writeLine(chalk.red('Lint errors found in the listed files.')); |
| 54 | + return commandOptions.force ? resolve(0) : resolve(2); |
| 55 | + } |
| 56 | + |
| 57 | + ui.writeLine(chalk.green('All files pass linting.')); |
| 58 | + return resolve(0); |
20 | 59 | });
|
21 | 60 | }
|
22 | 61 | });
|
0 commit comments