3
3
const fs = require ( 'fs-extra' )
4
4
const path = require ( 'path' )
5
5
6
- const ora = require ( 'ora' )
7
6
const prettyHrtime = require ( 'pretty-hrtime' )
8
7
const stdin = require ( 'get-stdin' )
9
8
const read = require ( 'read-cache' )
@@ -25,8 +24,6 @@ const output = argv.output
25
24
26
25
if ( argv . map ) argv . map = { inline : false }
27
26
28
- const spinner = ora ( )
29
-
30
27
let config = {
31
28
options : {
32
29
map : argv . map !== undefined ? argv . map : { inline : true } ,
@@ -86,35 +83,31 @@ Promise.resolve()
86
83
} )
87
84
. then ( results => {
88
85
if ( argv . watch ) {
86
+ const printMessage = ( ) =>
87
+ printVerbose ( chalk . dim ( '\nWaiting for file changes...' ) )
89
88
const watcher = chokidar . watch ( input . concat ( dependencies ( results ) ) , {
90
89
usePolling : argv . poll ,
91
90
interval : argv . poll && typeof argv . poll === 'number' ? argv . poll : 100
92
91
} )
93
92
94
93
if ( config . file ) watcher . add ( config . file )
95
94
96
- watcher
97
- . on ( 'ready' , ( ) => {
98
- console . warn ( chalk . bold . cyan ( 'Waiting for file changes...' ) )
99
- } )
100
- . on ( 'change' , file => {
101
- let recompile = [ ]
95
+ watcher . on ( 'ready' , printMessage ) . on ( 'change' , file => {
96
+ let recompile = [ ]
102
97
103
- if ( ~ input . indexOf ( file ) ) recompile . push ( file )
98
+ if ( ~ input . indexOf ( file ) ) recompile . push ( file )
104
99
105
- recompile = recompile . concat (
106
- depGraph . dependantsOf ( file ) . filter ( file => ~ input . indexOf ( file ) )
107
- )
100
+ recompile = recompile . concat (
101
+ depGraph . dependantsOf ( file ) . filter ( file => ~ input . indexOf ( file ) )
102
+ )
108
103
109
- if ( ! recompile . length ) recompile = input
104
+ if ( ! recompile . length ) recompile = input
110
105
111
- return files ( recompile )
112
- . then ( results => watcher . add ( dependencies ( results ) ) )
113
- . then ( ( ) => {
114
- console . warn ( chalk . bold . cyan ( 'Waiting for file changes...' ) )
115
- } )
116
- . catch ( error )
117
- } )
106
+ return files ( recompile )
107
+ . then ( results => watcher . add ( dependencies ( results ) ) )
108
+ . then ( printMessage )
109
+ . catch ( error )
110
+ } )
118
111
}
119
112
} )
120
113
. catch ( err => {
@@ -179,8 +172,7 @@ function css(css, file) {
179
172
180
173
const time = process . hrtime ( )
181
174
182
- spinner . text = `Processing ${ relativePath } `
183
- spinner . start ( )
175
+ printVerbose ( chalk `{cyan Processing {bold ${ relativePath } }...}` )
184
176
185
177
return rc ( ctx , argv . config )
186
178
. then ( ( ) => {
@@ -205,7 +197,6 @@ function css(css, file) {
205
197
}
206
198
207
199
if ( ! options . to && config . options . map && ! config . options . map . inline ) {
208
- spinner . fail ( )
209
200
error (
210
201
'Output Error: Cannot output external sourcemaps when writing to STDOUT'
211
202
)
@@ -220,39 +211,29 @@ function css(css, file) {
220
211
tasks . push ( fs . outputFile ( options . to , result . css ) )
221
212
222
213
if ( result . map ) {
223
- tasks . push (
224
- fs . outputFile (
225
- options . to . replace (
226
- path . extname ( options . to ) ,
227
- `${ path . extname ( options . to ) } .map`
228
- ) ,
229
- result . map
230
- )
214
+ const mapfile = options . to . replace (
215
+ path . extname ( options . to ) ,
216
+ `${ path . extname ( options . to ) } .map`
231
217
)
218
+ tasks . push ( fs . outputFile ( mapfile , result . map ) )
232
219
}
233
- } else {
234
- spinner . text = chalk . bold . green (
235
- `Finished ${ relativePath } (${ prettyHrtime ( process . hrtime ( time ) ) } )`
236
- )
237
- spinner . succeed ( )
238
- return process . stdout . write ( result . css , 'utf8' )
239
- }
220
+ } else process . stdout . write ( result . css , 'utf8' )
240
221
241
222
return Promise . all ( tasks ) . then ( ( ) => {
242
- spinner . text = chalk . bold . green (
243
- `Finished ${ relativePath } (${ prettyHrtime ( process . hrtime ( time ) ) } )`
223
+ const prettyTime = prettyHrtime ( process . hrtime ( time ) )
224
+ printVerbose (
225
+ chalk `{green Finished {bold ${ relativePath } } in {bold ${ prettyTime } }}`
244
226
)
227
+
245
228
if ( result . warnings ( ) . length ) {
246
- spinner . fail ( )
247
229
console . warn ( reporter ( result ) )
248
- } else spinner . succeed ( )
230
+ }
249
231
250
232
return result
251
233
} )
252
234
} )
253
235
} )
254
236
. catch ( err => {
255
- spinner . fail ( )
256
237
throw err
257
238
} )
258
239
}
@@ -274,25 +255,18 @@ function dependencies(results) {
274
255
return messages
275
256
}
276
257
258
+ function printVerbose ( message ) {
259
+ if ( argv . verbose ) console . warn ( message )
260
+ }
261
+
277
262
function error ( err ) {
263
+ // Seperate error from logging output
264
+ if ( argv . verbose ) console . error ( )
265
+
278
266
if ( typeof err === 'string' ) {
279
- spinner . fail ( chalk . bold . red ( err ) )
267
+ console . error ( chalk . red ( err ) )
280
268
} else if ( err . name === 'CssSyntaxError' ) {
281
- console . error ( '\n' )
282
-
283
- spinner . text = spinner . text . replace ( 'Processing ' , '' )
284
- spinner . fail ( chalk . bold . red ( `Syntax Error: ${ spinner . text } ` ) )
285
-
286
- if ( err . file ) {
287
- err . message = err . message . substr ( err . file . length + 1 )
288
- } else {
289
- err . message = err . message . replace ( '<css input>:' , '' )
290
- }
291
-
292
- err . message = err . message . replace ( / : \s / , '] ' )
293
-
294
- console . error ( '\n' , chalk . bold . red ( `[${ err . message } ` ) )
295
- console . error ( '\n' , err . showSourceCode ( ) , '\n\n' )
269
+ console . error ( err . toString ( ) )
296
270
} else {
297
271
console . error ( err )
298
272
}
0 commit comments