1
1
const Task = require ( '../ember-cli/lib/models/task' ) ;
2
2
import * as chalk from 'chalk' ;
3
3
import * as path from 'path' ;
4
+ import * as glob from 'glob' ;
5
+ import * as ts from 'typescript' ;
4
6
import { requireDependency } from '../utilities/require-project-module' ;
5
7
import { CliConfig } from '../models/config' ;
6
8
import { LintCommandOptions } from '../commands/lint' ;
7
9
import { oneLine } from 'common-tags' ;
8
10
11
+ interface CliLintConfig {
12
+ files ?: ( string | string [ ] ) ;
13
+ project ?: string ;
14
+ tslintConfig ?: string ;
15
+ exclude ?: ( string | string [ ] ) ;
16
+ }
17
+
9
18
export default Task . extend ( {
10
19
run : function ( commandOptions : LintCommandOptions ) {
11
20
const ui = this . ui ;
12
21
const projectRoot = this . project . root ;
22
+ const lintConfigs : CliLintConfig [ ] = CliConfig . fromProject ( ) . config . lint || [ ] ;
13
23
14
- return new Promise ( function ( resolve , reject ) {
15
- const tslint = requireDependency ( projectRoot , 'tslint' ) ;
16
- const Linter = tslint . Linter ;
17
- const Configuration = tslint . Configuration ;
24
+ if ( lintConfigs . length === 0 ) {
25
+ ui . writeLine ( chalk . yellow ( oneLine `
26
+ No lint config(s) found.
27
+ If this is not intended, run "ng update".
28
+ ` ) ) ;
18
29
19
- const lintConfigs = CliConfig . fromProject ( ) . config . lint || [ ] ;
30
+ return Promise . resolve ( 0 ) ;
31
+ }
20
32
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
- }
33
+ const tslint = requireDependency ( projectRoot , 'tslint' ) ;
34
+ const Linter = tslint . Linter ;
35
+ const Configuration = tslint . Configuration ;
28
36
29
- let errors = 0 ;
37
+ let errors = 0 ;
38
+ let results = '' ;
30
39
31
- lintConfigs . forEach ( ( config ) => {
32
- const program = Linter . createProgram ( config . project ) ;
33
- const files : string [ ] = Linter . getFileNames ( program ) ;
40
+ lintConfigs
41
+ . forEach ( ( config ) => {
42
+ const program : ts . Program = Linter . createProgram ( config . project ) ;
43
+ const files = getFilesToLint ( program , config , Linter ) ;
34
44
35
45
const linter = new Linter ( {
36
46
fix : commandOptions . fix ,
@@ -45,17 +55,42 @@ export default Task.extend({
45
55
46
56
const result = linter . getResult ( ) ;
47
57
errors += result . failureCount ;
48
-
49
- ui . writeLine ( result . output . trim ( ) . concat ( '\n' ) ) ;
58
+ results = results . concat ( result . output . trim ( ) . concat ( '\n' ) ) ;
50
59
} ) ;
51
60
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
- }
61
+ if ( errors > 0 ) {
62
+ ui . writeLine ( results . trim ( ) ) ;
63
+ ui . writeLine ( chalk . red ( 'Lint errors found in the listed files.' ) ) ;
64
+ return commandOptions . force ? Promise . resolve ( 0 ) : Promise . resolve ( 2 ) ;
65
+ }
56
66
57
- ui . writeLine ( chalk . green ( 'All files pass linting.' ) ) ;
58
- return resolve ( 0 ) ;
59
- } ) ;
67
+ ui . writeLine ( chalk . green ( 'All files pass linting.' ) ) ;
68
+ return Promise . resolve ( 0 ) ;
60
69
}
61
70
} ) ;
71
+
72
+ function getFilesToLint ( program : ts . Program , lintConfig : CliLintConfig , Linter : any ) : string [ ] {
73
+ let files : string [ ] = [ ] ;
74
+
75
+ if ( lintConfig . files !== null ) {
76
+ files = Array . isArray ( lintConfig . files ) ? lintConfig . files : [ lintConfig . files ] ;
77
+ } else {
78
+ files = Linter . getFileNames ( program ) ;
79
+ }
80
+
81
+ let globOptions = { } ;
82
+
83
+ if ( lintConfig . exclude !== null ) {
84
+ const excludePatterns = Array . isArray ( lintConfig . exclude )
85
+ ? lintConfig . exclude
86
+ : [ lintConfig . exclude ] ;
87
+
88
+ globOptions = { ignore : excludePatterns , nodir : true } ;
89
+ }
90
+
91
+ files = files
92
+ . map ( ( file : string ) => glob . sync ( file , globOptions ) )
93
+ . reduce ( ( a : string [ ] , b : string [ ] ) => a . concat ( b ) , [ ] ) ;
94
+
95
+ return files ;
96
+ }
0 commit comments