Skip to content

Commit 2c79338

Browse files
fix(index): better error handling (process.stdout)
1 parent d12a122 commit 2c79338

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

index.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
const fs = require('fs-promise')
44
const path = require('path')
55

6+
const ora = require('ora')
67
const stdin = require('get-stdin')
78
const chalk = require('chalk')
8-
const ora = require('ora')
99
const globber = require('globby')
1010
const chokidar = require('chokidar')
1111

@@ -134,6 +134,8 @@ let output = argv.output
134134

135135
if (argv.map) argv.map = { inline: false }
136136

137+
const spinner = ora()
138+
137139
let config = {
138140
options: {
139141
map: argv.map !== undefined ? argv.map : { inline: true },
@@ -146,7 +148,7 @@ let config = {
146148
try {
147149
return require(plugin)()
148150
} catch (e) {
149-
error(`PluginError: Cannot find module '${plugin}'`)
151+
error(`Plugin Error: Cannot find module '${plugin}'`)
150152
}
151153
})
152154
: []
@@ -159,23 +161,21 @@ Promise.resolve()
159161
.then(() => {
160162
if (input && input.length) return globber(input)
161163

162-
console.warn(chalk.bold.yellow('Warning: No files passed, reading from stdin\n'))
163-
164-
if (argv.replace || argv.dir) error('Cannot use --dir or --replace when reading from stdin')
164+
if (argv.replace || argv.dir) error('Input Error: Cannot use --dir or --replace when reading from stdin')
165165

166166
if (argv.watch) {
167-
error('Cannot run in watch mode when reading from stdin')
167+
error('Input Error: Cannot run in watch mode when reading from stdin')
168168
}
169169

170170
return ['stdin']
171171
})
172172
.then((i) => {
173173
if (!i || !i.length) {
174-
error('You must pass a valid list of files to parse')
174+
error('Input Error: You must pass a valid list of files to parse')
175175
}
176176

177177
if (i.length > 1 && !argv.dir && !argv.replace) {
178-
error('Must use --dir or --replace with multiple input files')
178+
error('Input Error: Must use --dir or --replace with multiple input files')
179179
}
180180

181181
input = i
@@ -227,8 +227,8 @@ function files (files) {
227227
if (file === 'stdin') {
228228
return stdin()
229229
.then((content) => {
230-
if (!content) return error('Error: Did not receive any stdin')
231-
css(content, 'stdin')
230+
if (!content) return error('Input Error: Did not receive any STDIN')
231+
return css(content, 'stdin')
232232
})
233233
}
234234

@@ -254,7 +254,8 @@ function css (css, file) {
254254

255255
const time = process.hrtime()
256256

257-
const spinner = ora(`Processing ${file}`).start()
257+
spinner.text = `Processing ${file}`
258+
spinner.start()
258259

259260
return rc(ctx, argv.config)
260261
.then(() => {
@@ -276,10 +277,9 @@ function css (css, file) {
276277
options.to = path.resolve(options.to)
277278
}
278279

279-
// Can't use external sourcemaps when writing to stdout:
280280
if (!options.to && config.options.map && !config.options.map.inline) {
281281
spinner.fail()
282-
error('Cannot output external sourcemaps when writing to stdout')
282+
error('Output Error: Cannot output external sourcemaps when writing to STDOUT')
283283
}
284284

285285
return postcss(config.plugins)
@@ -302,7 +302,13 @@ function css (css, file) {
302302
)
303303
)
304304
}
305-
} else process.stdout.write(result.css, 'utf8')
305+
} else {
306+
spinner.text = chalk.bold.green(
307+
`Finished ${file} (${Math.round(process.hrtime(time)[1] / 1e6)}ms)`
308+
)
309+
spinner.succeed()
310+
return process.stdout.write(result.css, 'utf8')
311+
}
306312

307313
return Promise.all(tasks)
308314
.then(() => {
@@ -317,8 +323,7 @@ function css (css, file) {
317323
return result
318324
})
319325
})
320-
}).catch(err => {
321-
// Fail spinner and send error up the promise chain
326+
}).catch((err) => {
322327
spinner.fail()
323328
throw err
324329
})
@@ -342,21 +347,26 @@ function dependencies (results) {
342347

343348
function error (err) {
344349
if (typeof err === 'string') {
345-
// Manual error
346-
console.error(chalk.bold.red(err))
350+
spinner.fail(chalk.bold.red(err))
347351
} else if (err.name === 'CssSyntaxError') {
348-
// CSS Syntax Error
349-
console.error(chalk.bold.red(`${err.file}`))
350-
err.message = err.message
351-
.substr(err.file.length + 1)
352-
.replace(/:\s/, '] ')
352+
console.error('\n')
353+
354+
spinner.text = spinner.text.replace('Processing ', '')
355+
spinner.fail(chalk.bold.red(`Syntax Error: ${spinner.text}`))
356+
357+
if (err.file) {
358+
err.message = err.message.substr(err.file.length + 1)
359+
} else {
360+
err.message = err.message.replace('<css input>:', '')
361+
}
362+
363+
err.message = err.message.replace(/:\s/, '] ')
364+
353365
console.error('\n', chalk.bold.red(`[${err.message}`))
354-
console.error('\n', err.showSourceCode(), '\n')
355-
// If watch mode, don't exit the process:
366+
console.error('\n', err.showSourceCode(), '\n\n')
367+
356368
if (argv.watch) return
357369
} else {
358-
// JS Error
359-
// Don't use chalk here; we want a JS stack trace:
360370
console.error(err)
361371
}
362372
process.exit(1)

0 commit comments

Comments
 (0)