@@ -14,6 +14,7 @@ const getStdin = require('get-stdin')
1414
1515/**
1616 * @param {Omit<import('../').LinterOptions, 'cmd'> & StandardCliOptions } rawOpts
17+ * @returns {void }
1718 */
1819function cli ( rawOpts ) {
1920 const opts = {
@@ -99,24 +100,50 @@ Flags (advanced):
99100 parser : argv . parser
100101 }
101102
102- /** @type {string } */
103- let stdinText
103+ const outputFixed = argv . stdin && argv . fix
104104
105- if ( argv . stdin ) {
106- getStdin ( ) . then ( function ( text ) {
107- stdinText = text
108- standard . lintText ( text , lintOpts , onResult )
109- } )
110- } else {
111- standard . lintFiles ( argv . _ , lintOpts , onResult )
105+ /**
106+ * Print lint errors to stdout -- this is expected output from `standard-engine`.
107+ * Note: When fixing code from stdin (`standard --stdin --fix`), the transformed
108+ * code is printed to stdout, so print lint errors to stderr in this case.
109+ * @type {typeof console.log }
110+ */
111+ const log = ( ...args ) => {
112+ if ( outputFixed ) {
113+ args [ 0 ] = opts . cmd + ': ' + args [ 0 ]
114+ console . error . apply ( console , args )
115+ } else {
116+ console . log . apply ( console , args )
117+ }
112118 }
113119
114- /** @type {import('../').LinterCallback } */
115- function onResult ( err , result ) {
116- if ( err ) return onError ( err )
120+ Promise . resolve ( argv . stdin ? getStdin ( ) : '' ) . then ( async stdinText => {
121+ /** @type {import('eslint').CLIEngine.LintReport } */
122+ let result
123+
124+ try {
125+ result = argv . stdin
126+ ? await standard . lintText ( stdinText , lintOpts )
127+ : await standard . lintFiles ( argv . _ , lintOpts )
128+ } catch ( err ) {
129+ console . error ( opts . cmd + ': Unexpected linter output:\n' )
130+ if ( err instanceof Error ) {
131+ console . error ( err . stack || err . message )
132+ } else {
133+ console . error ( err )
134+ }
135+ console . error (
136+ '\nIf you think this is a bug in `%s`, open an issue: %s' ,
137+ opts . cmd ,
138+ opts . bugs
139+ )
140+ process . exitCode = 1
141+ return
142+ }
143+
117144 if ( ! result ) throw new Error ( 'expected a result' )
118145
119- if ( argv . stdin && argv . fix ) {
146+ if ( outputFixed ) {
120147 if ( result . results [ 0 ] && result . results [ 0 ] . output ) {
121148 // Code contained fixable errors, so print the fixed code
122149 process . stdout . write ( result . results [ 0 ] . output )
@@ -134,11 +161,7 @@ Flags (advanced):
134161 console . error ( '%s: %s (%s)' , opts . cmd , opts . tagline , opts . homepage )
135162
136163 // Are any warnings present?
137- const isSomeWarnings = result . results . some ( function ( result ) {
138- return result . messages . some ( function ( message ) {
139- return message . severity === 1
140- } )
141- } )
164+ const isSomeWarnings = result . results . some ( item => item . messages . some ( message => message . severity === 1 ) )
142165
143166 if ( isSomeWarnings ) {
144167 const homepage = opts . homepage != null ? ` (${ opts . homepage } )` : ''
@@ -150,11 +173,7 @@ Flags (advanced):
150173 }
151174
152175 // Are any fixable rules present?
153- const isSomeFixable = result . results . some ( function ( result ) {
154- return result . messages . some ( function ( message ) {
155- return ! ! message . fix
156- } )
157- } )
176+ const isSomeFixable = result . results . some ( item => item . messages . some ( message => ! ! message . fix ) )
158177
159178 if ( isSomeFixable ) {
160179 console . error (
@@ -164,53 +183,23 @@ Flags (advanced):
164183 )
165184 }
166185
167- result . results . forEach ( function ( result ) {
168- result . messages . forEach ( function ( message ) {
186+ for ( const item of result . results ) {
187+ for ( const message of item . messages ) {
169188 log (
170189 ' %s:%d:%d: %s%s%s' ,
171- result . filePath ,
190+ item . filePath ,
172191 message . line || 0 ,
173192 message . column || 0 ,
174193 message . message ,
175194 ' (' + message . ruleId + ')' ,
176195 message . severity === 1 ? ' (warning)' : ''
177196 )
178- } )
179- } )
180-
181- process . exitCode = result . errorCount ? 1 : 0
182- }
183-
184- /** @param {Error|unknown } err */
185- function onError ( err ) {
186- console . error ( opts . cmd + ': Unexpected linter output:\n' )
187- if ( err instanceof Error ) {
188- console . error ( err . stack || err . message )
189- } else {
190- console . error ( err )
197+ }
191198 }
192- console . error (
193- '\nIf you think this is a bug in `%s`, open an issue: %s' ,
194- opts . cmd ,
195- opts . bugs
196- )
197- process . exitCode = 1
198- }
199199
200- /**
201- * Print lint errors to stdout -- this is expected output from `standard-engine`.
202- * Note: When fixing code from stdin (`standard --stdin --fix`), the transformed
203- * code is printed to stdout, so print lint errors to stderr in this case.
204- * @type {typeof console.log }
205- */
206- function log ( ...args ) {
207- if ( argv . stdin && argv . fix ) {
208- args [ 0 ] = opts . cmd + ': ' + args [ 0 ]
209- console . error . apply ( console , args )
210- } else {
211- console . log . apply ( console , args )
212- }
213- }
200+ process . exitCode = result . errorCount ? 1 : 0
201+ } )
202+ . catch ( err => process . nextTick ( ( ) => { throw err } ) )
214203}
215204
216205module . exports = cli
0 commit comments