@@ -4,27 +4,37 @@ import * as glob from 'glob';
4
4
import * as path from 'path' ;
5
5
import * as ts from 'typescript' ;
6
6
import { requireProjectModule } from '../utilities/require-project-module' ;
7
- import { CliConfig } from '../models/config' ;
8
- import { LintCommandOptions } from '../commands/lint' ;
9
7
10
8
const SilentError = require ( 'silent-error' ) ;
11
9
const Task = require ( '../ember-cli/lib/models/task' ) ;
12
10
13
- interface CliLintConfig {
11
+ export interface CliLintConfig {
14
12
files ?: ( string | string [ ] ) ;
15
13
project ?: string ;
16
14
tslintConfig ?: string ;
17
15
exclude ?: ( string | string [ ] ) ;
18
16
}
19
17
18
+ export class LintTaskOptions {
19
+ fix : boolean ;
20
+ force : boolean ;
21
+ format ? = 'prose' ;
22
+ silent ? = false ;
23
+ typeCheck ? = false ;
24
+ configs : Array < CliLintConfig > ;
25
+ }
26
+
20
27
export default Task . extend ( {
21
- run : function ( commandOptions : LintCommandOptions ) {
28
+ run : function ( options : LintTaskOptions ) {
29
+ options = { ...new LintTaskOptions ( ) , ...options } ;
22
30
const ui = this . ui ;
23
31
const projectRoot = this . project . root ;
24
- const lintConfigs : CliLintConfig [ ] = CliConfig . fromProject ( ) . config . lint || [ ] ;
32
+ const lintConfigs = options . configs || [ ] ;
25
33
26
34
if ( lintConfigs . length === 0 ) {
27
- ui . writeLine ( chalk . yellow ( 'No lint configuration(s) found.' ) ) ;
35
+ if ( ! options . silent ) {
36
+ ui . writeLine ( chalk . yellow ( 'No lint configuration(s) found.' ) ) ;
37
+ }
28
38
return Promise . resolve ( 0 ) ;
29
39
}
30
40
@@ -37,15 +47,17 @@ export default Task.extend({
37
47
let program : ts . Program ;
38
48
if ( config . project ) {
39
49
program = Linter . createProgram ( config . project ) ;
40
- } else if ( commandOptions . typeCheck ) {
41
- ui . writeLine ( chalk . yellow ( 'A "project" must be specified to enable type checking.' ) ) ;
50
+ } else if ( options . typeCheck ) {
51
+ if ( ! options . silent ) {
52
+ ui . writeLine ( chalk . yellow ( 'A "project" must be specified to enable type checking.' ) ) ;
53
+ }
42
54
}
43
55
const files = getFilesToLint ( program , config , Linter ) ;
44
56
const lintOptions = {
45
- fix : commandOptions . fix ,
46
- formatter : commandOptions . format
57
+ fix : options . fix ,
58
+ formatter : options . format
47
59
} ;
48
- const lintProgram = commandOptions . typeCheck ? program : undefined ;
60
+ const lintProgram = options . typeCheck ? program : undefined ;
49
61
const linter = new Linter ( lintOptions , lintProgram ) ;
50
62
51
63
let lastDirectory : string ;
@@ -82,42 +94,51 @@ export default Task.extend({
82
94
fixes : undefined
83
95
} ) ;
84
96
85
- const Formatter = tslint . findFormatter ( commandOptions . format ) ;
86
- const formatter = new Formatter ( ) ;
87
-
88
- const output = formatter . format ( result . failures , result . fixes ) ;
89
- if ( output ) {
90
- ui . writeLine ( output ) ;
97
+ if ( ! options . silent ) {
98
+ const Formatter = tslint . findFormatter ( options . format ) ;
99
+ if ( ! Formatter ) {
100
+ throw new SilentError ( chalk . red ( `Invalid lint format "${ options . format } ".` ) ) ;
101
+ }
102
+ const formatter = new Formatter ( ) ;
103
+
104
+ const output = formatter . format ( result . failures , result . fixes ) ;
105
+ if ( output ) {
106
+ ui . writeLine ( output ) ;
107
+ }
91
108
}
92
109
93
110
// print formatter output directly for non human-readable formats
94
- if ( [ 'prose' , 'verbose' , 'stylish' ] . indexOf ( commandOptions . format ) == - 1 ) {
95
- return ( result . failures . length == 0 || commandOptions . force )
111
+ if ( [ 'prose' , 'verbose' , 'stylish' ] . indexOf ( options . format ) == - 1 ) {
112
+ return ( result . failures . length == 0 || options . force )
96
113
? Promise . resolve ( 0 ) : Promise . resolve ( 2 ) ;
97
114
}
98
115
99
116
if ( result . failures . length > 0 ) {
100
- ui . writeLine ( chalk . red ( 'Lint errors found in the listed files.' ) ) ;
101
- return commandOptions . force ? Promise . resolve ( 0 ) : Promise . resolve ( 2 ) ;
117
+ if ( ! options . silent ) {
118
+ ui . writeLine ( chalk . red ( 'Lint errors found in the listed files.' ) ) ;
119
+ }
120
+ return options . force ? Promise . resolve ( 0 ) : Promise . resolve ( 2 ) ;
102
121
}
103
122
104
- ui . writeLine ( chalk . green ( 'All files pass linting.' ) ) ;
123
+ if ( ! options . silent ) {
124
+ ui . writeLine ( chalk . green ( 'All files pass linting.' ) ) ;
125
+ }
105
126
return Promise . resolve ( 0 ) ;
106
127
}
107
128
} ) ;
108
129
109
130
function getFilesToLint ( program : ts . Program , lintConfig : CliLintConfig , Linter : any ) : string [ ] {
110
131
let files : string [ ] = [ ] ;
111
132
112
- if ( lintConfig . files !== null ) {
133
+ if ( lintConfig . files ) {
113
134
files = Array . isArray ( lintConfig . files ) ? lintConfig . files : [ lintConfig . files ] ;
114
135
} else if ( program ) {
115
136
files = Linter . getFileNames ( program ) ;
116
137
}
117
138
118
139
let globOptions = { } ;
119
140
120
- if ( lintConfig . exclude !== null ) {
141
+ if ( lintConfig . exclude ) {
121
142
const excludePatterns = Array . isArray ( lintConfig . exclude )
122
143
? lintConfig . exclude
123
144
: [ lintConfig . exclude ] ;
0 commit comments