Skip to content

Commit 06aa6ee

Browse files
committed
BREAKING: Be silent, add --verbose, change logging format
- postcss doesn't output any status logging by default - Added --verbose flag to reenable status logging - Removed ora spinners, simpler logging - Changed wording of a few log messages; also changed colors - Use simpler error message printing
1 parent f16d0d4 commit 06aa6ee

File tree

5 files changed

+47
-63
lines changed

5 files changed

+47
-63
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Basic options:
3535
-r, --replace Replace (overwrite) the input file [boolean]
3636
--map, -m Create an external sourcemap
3737
--no-map Disable the default inline sourcemaps
38+
--verbose Be verbose [boolean]
3839
--watch, -w Watch files for changes and recompile as needed [boolean]
3940
--env A shortcut for setting NODE_ENV [string]
4041

index.js

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const fs = require('fs-extra')
44
const path = require('path')
55

6-
const ora = require('ora')
76
const prettyHrtime = require('pretty-hrtime')
87
const stdin = require('get-stdin')
98
const read = require('read-cache')
@@ -25,8 +24,6 @@ const output = argv.output
2524

2625
if (argv.map) argv.map = { inline: false }
2726

28-
const spinner = ora()
29-
3027
let 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+
277262
function 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
}

lib/args.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ Usage:
3535
$0 <input-directory> [OPTIONS] --dir <output-directory> [--watch|-w]
3636
$0 <input.css>... [OPTIONS] --replace`
3737
)
38-
.group(['o', 'd', 'r', 'map', 'no-map', 'watch', 'env'], 'Basic options:')
38+
.group(
39+
['o', 'd', 'r', 'map', 'no-map', 'verbose', 'watch', 'env'],
40+
'Basic options:'
41+
)
3942
.option('o', {
4043
alias: 'output',
4144
desc: 'Output file',
@@ -60,6 +63,10 @@ Usage:
6063
.alias('map', 'm')
6164
.describe('map', 'Create an external sourcemap')
6265
.describe('no-map', 'Disable the default inline sourcemaps')
66+
.option('verbose', {
67+
desc: 'Be verbose',
68+
type: 'boolean'
69+
})
6370
.option('watch', {
6471
alias: 'w',
6572
desc: 'Watch files for changes and recompile as needed',

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"fs-extra": "^5.0.0",
2424
"get-stdin": "^5.0.1",
2525
"globby": "^7.1.1",
26-
"ora": "1.3.0",
2726
"postcss": "^6.0.1",
2827
"postcss-load-config": "^1.1.0",
2928
"postcss-reporter": "^5.0.0",

test/error.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ test('CssSyntaxError', t => {
5656
return cli(['test/fixtures/a.css', '--parser', 'sugarss', '-o', tmp()]).then(
5757
({ err, code }) => {
5858
t.is(code, 1, 'expected non-zero error code')
59-
t.regex(err.toString(), /\[1:4] Unnecessary curly bracket/)
59+
t.regex(
60+
err.toString(),
61+
/CssSyntaxError: .*a.css:1:4: Unnecessary curly bracket/
62+
)
6063
}
6164
)
6265
})

0 commit comments

Comments
 (0)