11const Task = require ( '../ember-cli/lib/models/task' ) ;
22import * as chalk from 'chalk' ;
33import * as path from 'path' ;
4+ import * as glob from 'glob' ;
5+ import * as ts from 'typescript' ;
46import { requireDependency } from '../utilities/require-project-module' ;
57import { CliConfig } from '../models/config' ;
68import { LintCommandOptions } from '../commands/lint' ;
79import { oneLine } from 'common-tags' ;
810
11+ interface CliLintConfig {
12+ files ?: ( string | string [ ] ) ;
13+ project ?: string ;
14+ tslintConfig ?: string ;
15+ exclude ?: ( string | string [ ] ) ;
16+ }
17+
918export default Task . extend ( {
1019 run : function ( commandOptions : LintCommandOptions ) {
1120 const ui = this . ui ;
1221 const projectRoot = this . project . root ;
22+ const lintConfigs : CliLintConfig [ ] = CliConfig . fromProject ( ) . config . lint || [ ] ;
1323
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+ ` ) ) ;
1829
19- const lintConfigs = CliConfig . fromProject ( ) . config . lint || [ ] ;
30+ return Promise . resolve ( 0 ) ;
31+ }
2032
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 ;
2836
29- let errors = 0 ;
37+ let errors = 0 ;
38+ let results = '' ;
3039
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 ) ;
3444
3545 const linter = new Linter ( {
3646 fix : commandOptions . fix ,
@@ -45,17 +55,42 @@ export default Task.extend({
4555
4656 const result = linter . getResult ( ) ;
4757 errors += result . failureCount ;
48-
49- ui . writeLine ( result . output . trim ( ) . concat ( '\n' ) ) ;
58+ results = results . concat ( result . output . trim ( ) . concat ( '\n' ) ) ;
5059 } ) ;
5160
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+ }
5666
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 ) ;
6069 }
6170} ) ;
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