@@ -28,13 +28,26 @@ const handleError = function(err) {
28
28
throw new Error ( err ) ;
29
29
} ;
30
30
31
+ /**
32
+ * Helper function to determine if output should be logged
33
+ *
34
+ * @param {boolean } quietFlag Flag indicating if output should be logged if no error occur
35
+ * @param {boolean } hasErrors True if the linter found errors. False if it didn't.
36
+ * @return {boolean } True if the message should be logged. False if the message shouldn't be logged.
37
+ */
38
+ const shouldLogOutput = function ( quietFlag , hasErrors ) {
39
+ /* eslint no-extra-parens: "off" */
40
+ return ! quietFlag || ( quietFlag && hasErrors ) ;
41
+ } ;
42
+
31
43
// configure cli options
32
44
cliApp . version ( pkg . version ) ;
33
45
cliApp . usage ( pkg . name ) ;
34
46
cliApp . option ( '-f, --file <filePath>' , `File path including name. Defaults to ${ DEFAULT_FILE_NAME } ` , DEFAULT_FILE_NAME ) ;
35
47
cliApp . option ( '-r, --rule <rule name>' , 'Valid rule name to check. Defaults to nothing' ) ;
36
48
cliApp . option ( '-s, --rule-severity <rule severity>' , `"error" or "warning". Defaults to ${ DEFAULT_RULE_SEVERITY } ` , DEFAULT_RULE_SEVERITY ) ;
37
49
cliApp . option ( '-c, --rules-file <filePath>' , 'File path of .npmpackagejsonlintrc' ) ;
50
+ cliApp . option ( '-q, --quiet' , 'Report errors only' ) ;
38
51
cliApp . option ( '-w, --ignore-warnings' , 'Ignore warnings' ) ;
39
52
cliApp . parse ( process . argv ) ;
40
53
@@ -90,21 +103,28 @@ try {
90
103
const npmPackageJsonLint = new NpmPackageJsonLint ( fileData , rulesConfig , options ) ;
91
104
const output = npmPackageJsonLint . lint ( ) ;
92
105
const reporter = new Reporter ( ) ;
106
+ let hasErrors = false ;
93
107
94
108
for ( const issueType in output ) {
95
109
const issues = output [ issueType ] ;
96
110
97
111
if ( issues . length > noIssues && issueType === 'errors' ) {
98
112
exitCode = issuesDetectedErrorCode ;
113
+ hasErrors = true ;
99
114
}
100
115
101
- reporter . write ( output [ issueType ] , issueType ) ;
116
+ if ( shouldLogOutput ( cliApp . quiet , hasErrors ) ) {
117
+ reporter . write ( output [ issueType ] , issueType ) ;
118
+ }
102
119
}
103
120
104
121
const formattedFileName = chalk . bold . green ( filePath ) ;
105
122
106
123
process . exitCode = exitCode ;
107
- console . log ( `${ formattedFileName } check complete` ) ;
124
+
125
+ if ( shouldLogOutput ( cliApp . quiet , hasErrors ) ) {
126
+ console . log ( `${ formattedFileName } check complete` ) ;
127
+ }
108
128
} catch ( err ) {
109
129
handleError ( err ) ;
110
130
}
0 commit comments