@@ -4,27 +4,37 @@ import * as glob from 'glob';
44import * as path from 'path' ;
55import * as ts from 'typescript' ;
66import { requireProjectModule } from '../utilities/require-project-module' ;
7- import { CliConfig } from '../models/config' ;
8- import { LintCommandOptions } from '../commands/lint' ;
97
108const SilentError = require ( 'silent-error' ) ;
119const Task = require ( '../ember-cli/lib/models/task' ) ;
1210
13- interface CliLintConfig {
11+ export interface CliLintConfig {
1412 files ?: ( string | string [ ] ) ;
1513 project ?: string ;
1614 tslintConfig ?: string ;
1715 exclude ?: ( string | string [ ] ) ;
1816}
1917
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+
2027export default Task . extend ( {
21- run : function ( commandOptions : LintCommandOptions ) {
28+ run : function ( options : LintTaskOptions ) {
29+ options = { ...new LintTaskOptions ( ) , ...options } ;
2230 const ui = this . ui ;
2331 const projectRoot = this . project . root ;
24- const lintConfigs : CliLintConfig [ ] = CliConfig . fromProject ( ) . config . lint || [ ] ;
32+ const lintConfigs = options . configs || [ ] ;
2533
2634 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+ }
2838 return Promise . resolve ( 0 ) ;
2939 }
3040
@@ -37,15 +47,17 @@ export default Task.extend({
3747 let program : ts . Program ;
3848 if ( config . project ) {
3949 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+ }
4254 }
4355 const files = getFilesToLint ( program , config , Linter ) ;
4456 const lintOptions = {
45- fix : commandOptions . fix ,
46- formatter : commandOptions . format
57+ fix : options . fix ,
58+ formatter : options . format
4759 } ;
48- const lintProgram = commandOptions . typeCheck ? program : undefined ;
60+ const lintProgram = options . typeCheck ? program : undefined ;
4961 const linter = new Linter ( lintOptions , lintProgram ) ;
5062
5163 let lastDirectory : string ;
@@ -82,42 +94,51 @@ export default Task.extend({
8294 fixes : undefined
8395 } ) ;
8496
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+ }
91108 }
92109
93110 // 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 )
96113 ? Promise . resolve ( 0 ) : Promise . resolve ( 2 ) ;
97114 }
98115
99116 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 ) ;
102121 }
103122
104- ui . writeLine ( chalk . green ( 'All files pass linting.' ) ) ;
123+ if ( ! options . silent ) {
124+ ui . writeLine ( chalk . green ( 'All files pass linting.' ) ) ;
125+ }
105126 return Promise . resolve ( 0 ) ;
106127 }
107128} ) ;
108129
109130function getFilesToLint ( program : ts . Program , lintConfig : CliLintConfig , Linter : any ) : string [ ] {
110131 let files : string [ ] = [ ] ;
111132
112- if ( lintConfig . files !== null ) {
133+ if ( lintConfig . files ) {
113134 files = Array . isArray ( lintConfig . files ) ? lintConfig . files : [ lintConfig . files ] ;
114135 } else if ( program ) {
115136 files = Linter . getFileNames ( program ) ;
116137 }
117138
118139 let globOptions = { } ;
119140
120- if ( lintConfig . exclude !== null ) {
141+ if ( lintConfig . exclude ) {
121142 const excludePatterns = Array . isArray ( lintConfig . exclude )
122143 ? lintConfig . exclude
123144 : [ lintConfig . exclude ] ;
0 commit comments