33const fs = require ( 'fs-extra' )
44const path = require ( 'path' )
55
6- const ora = require ( 'ora' )
76const prettyHrtime = require ( 'pretty-hrtime' )
87const stdin = require ( 'get-stdin' )
98const read = require ( 'read-cache' )
@@ -25,8 +24,6 @@ const output = argv.output
2524
2625if ( argv . map ) argv . map = { inline : false }
2726
28- const spinner = ora ( )
29-
3027let config = {
3128 options : {
3229 map : argv . map !== undefined ? argv . map : { inline : true } ,
@@ -86,35 +83,31 @@ Promise.resolve()
8683 } )
8784 . then ( results => {
8885 if ( argv . watch ) {
86+ const printMessage = ( ) =>
87+ printVerbose ( chalk . dim ( '\nWaiting for file changes...' ) )
8988 const watcher = chokidar . watch ( input . concat ( dependencies ( results ) ) , {
9089 usePolling : argv . poll ,
9190 interval : argv . poll && typeof argv . poll === 'number' ? argv . poll : 100
9291 } )
9392
9493 if ( config . file ) watcher . add ( config . file )
9594
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 = [ ]
10297
103- if ( ~ input . indexOf ( file ) ) recompile . push ( file )
98+ if ( ~ input . indexOf ( file ) ) recompile . push ( file )
10499
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+ )
108103
109- if ( ! recompile . length ) recompile = input
104+ if ( ! recompile . length ) recompile = input
110105
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+ } )
118111 }
119112 } )
120113 . catch ( err => {
@@ -179,8 +172,7 @@ function css(css, file) {
179172
180173 const time = process . hrtime ( )
181174
182- spinner . text = `Processing ${ relativePath } `
183- spinner . start ( )
175+ printVerbose ( chalk `{cyan Processing {bold ${ relativePath } }...}` )
184176
185177 return rc ( ctx , argv . config )
186178 . then ( ( ) => {
@@ -205,7 +197,6 @@ function css(css, file) {
205197 }
206198
207199 if ( ! options . to && config . options . map && ! config . options . map . inline ) {
208- spinner . fail ( )
209200 error (
210201 'Output Error: Cannot output external sourcemaps when writing to STDOUT'
211202 )
@@ -220,39 +211,29 @@ function css(css, file) {
220211 tasks . push ( fs . outputFile ( options . to , result . css ) )
221212
222213 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`
231217 )
218+ tasks . push ( fs . outputFile ( mapfile , result . map ) )
232219 }
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' )
240221
241222 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 } }}`
244226 )
227+
245228 if ( result . warnings ( ) . length ) {
246- spinner . fail ( )
247229 console . warn ( reporter ( result ) )
248- } else spinner . succeed ( )
230+ }
249231
250232 return result
251233 } )
252234 } )
253235 } )
254236 . catch ( err => {
255- spinner . fail ( )
256237 throw err
257238 } )
258239}
@@ -274,25 +255,18 @@ function dependencies(results) {
274255 return messages
275256}
276257
258+ function printVerbose ( message ) {
259+ if ( argv . verbose ) console . warn ( message )
260+ }
261+
277262function error ( err ) {
263+ // Seperate error from logging output
264+ if ( argv . verbose ) console . error ( )
265+
278266 if ( typeof err === 'string' ) {
279- spinner . fail ( chalk . bold . red ( err ) )
267+ console . error ( chalk . red ( err ) )
280268 } 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 ( ) )
296270 } else {
297271 console . error ( err )
298272 }
0 commit comments