Skip to content

Commit 76e78e6

Browse files
committed
feat: add support for quiet mode
If `--quiet` or `{quiet: true}` is set, webpack-validator won't write to stdout. This is useful with webpack `--json` option as then you won't get extra output there. Closes #69.
1 parent 2dd7964 commit 76e78e6

File tree

4 files changed

+88
-62
lines changed

4 files changed

+88
-62
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ const config = { /* ... your webpack config */ }
7979
module.exports = validate(config, yourSchema)
8080
```
8181

82+
#### Quiet Mode
83+
If you want to mute console output apart from errors, set `--quiet` or `validate(config, yourSchema, {quiet: true})`. This is particularly useful if you are using webpack `--json` as you'll want to avoid writing additional text to the JSON output.
84+
8285
#### Advanced Usage
8386
If you need to access the validation results directly and want to control the side-effects (i.e. console.log output, `process.exit(1)` on fail behaviour) yourself, you can call the validation function like so: `validate(config, yourSchema, { returnValidation: true })`. This will make 1) the function return the validation results instead of your configuration and 2) not perform any side effects.
8487

src/bin/webpack-validator.js

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
1-
#!/usr/bin/env node
2-
3-
'use strict'
4-
const fs = require('fs')
5-
const path = require('path')
6-
const program = require('commander')
7-
const log = require('npmlog')
8-
const validate = require('../')
9-
const schema = require('../').schema
10-
let configFile
11-
12-
program
13-
.arguments('<configFileName>')
14-
.action((configFileName) => {
15-
configFile = configFileName
16-
})
17-
program.parse(process.argv)
18-
19-
function errorHandler(err) {
20-
if (err.isJoi && err.name === 'ValidationError' && err.annotate) {
21-
log.error(err.annotate())
22-
} else {
23-
log.error(err.message)
24-
}
25-
process.exit(1)
26-
}
27-
28-
function validateConfig(webpackConfigFile) {
29-
console.log(`Reading: ${webpackConfigFile}`)
30-
const config = require(path.join(process.cwd(), webpackConfigFile))
31-
const validationResult = validate(config, schema, { returnValidation: true })
32-
if (validationResult.error) {
33-
console.info(validationResult.error.annotate())
34-
process.exit(1)
35-
} else {
36-
console.info(`${webpackConfigFile} is valid`)
37-
process.exit(0)
38-
}
39-
}
40-
41-
if (! configFile) {
42-
const error = new Error(['No configuration file given',
43-
'Usage: webpack-validator-cli <configFileName>'].join('\n'))
44-
error.type = 'EUSAGE'
45-
errorHandler(error)
46-
}
47-
48-
fs.stat(configFile, (err, stats) => {
49-
if (err) {
50-
err.message = `Could not find file "${configFile}"` // eslint-disable-line no-param-reassign
51-
errorHandler(err)
52-
} else {
53-
if (stats.isFile()) {
54-
validateConfig(configFile)
55-
} else {
56-
const error = new Error(`Could not find file "${configFile}"`)
57-
error.type = 'EISDIR'
58-
errorHandler(error)
59-
}
60-
}
61-
})
1+
#!/usr/bin/env node
2+
3+
'use strict'
4+
const fs = require('fs')
5+
const path = require('path')
6+
const program = require('commander')
7+
const log = require('npmlog')
8+
const validate = require('../')
9+
const schema = require('../').schema
10+
let configFile
11+
12+
program
13+
.arguments('<configFileName>')
14+
.option('-q, --quiet', 'Quiet output')
15+
.action((configFileName) => {
16+
configFile = configFileName
17+
})
18+
program.parse(process.argv)
19+
20+
function errorHandler(err) {
21+
if (err.isJoi && err.name === 'ValidationError' && err.annotate) {
22+
log.error(err.annotate())
23+
} else {
24+
log.error(err.message)
25+
}
26+
process.exit(1)
27+
}
28+
29+
function validateConfig(webpackConfigFile, quiet) {
30+
if (!quiet) console.log(`Reading: ${webpackConfigFile}`)
31+
const config = require(path.join(process.cwd(), webpackConfigFile))
32+
const validationResult = validate(config, schema, {
33+
returnValidation: true,
34+
quiet,
35+
})
36+
if (validationResult.error) {
37+
console.info(validationResult.error.annotate())
38+
process.exit(1)
39+
} else {
40+
if (!quiet) console.info(`${webpackConfigFile} is valid`)
41+
process.exit(0)
42+
}
43+
}
44+
45+
if (! configFile) {
46+
const error = new Error(['No configuration file given',
47+
'Usage: webpack-validator-cli <configFileName>'].join('\n'))
48+
error.type = 'EUSAGE'
49+
errorHandler(error)
50+
}
51+
52+
fs.stat(configFile, (err, stats) => {
53+
if (err) {
54+
err.message = `Could not find file "${configFile}"` // eslint-disable-line no-param-reassign
55+
errorHandler(err)
56+
} else {
57+
if (stats.isFile()) {
58+
validateConfig(configFile, program.quiet)
59+
} else {
60+
const error = new Error(`Could not find file "${configFile}"`)
61+
error.type = 'EISDIR'
62+
errorHandler(error)
63+
}
64+
}
65+
})

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = function validate(config, schema_ = schema, options = {}) {
5252
const {
5353
// Don't return the config object and throw on error, but just return the validation result
5454
returnValidation, // bool
55+
quiet, // bool
5556
} = options
5657

5758
const validationResult = Joi.validate(config, schema_, { abortEarly: false })
@@ -61,7 +62,11 @@ module.exports = function validate(config, schema_ = schema, options = {}) {
6162
console.error(validationResult.error.annotate())
6263
process.exit(1)
6364
}
64-
console.info(chalk.green('[webpack-validator] Config is valid.'))
65+
66+
if (!quiet) {
67+
console.info(chalk.green('[webpack-validator] Config is valid.'))
68+
}
69+
6570
return config
6671
}
6772
module.exports.schema = schema

src/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ describe('.', () => {
4444
// process.exit should have been called
4545
assert(processExitStub.callCount === 1)
4646
})
47+
48+
it('should allow console output to be muted', () => {
49+
validate({}, {}, { quiet: true })
50+
51+
// The success message should not have been printed
52+
assert(consoleInfoStub.callCount === 0)
53+
54+
// The error message should not have been printed
55+
if (consoleErrorStub.callCount !== 0) {
56+
throw new Error(consoleErrorStub.args[0])
57+
}
58+
// process.exit should not have been called
59+
assert(processExitStub.callCount === 0)
60+
})
4761
})

0 commit comments

Comments
 (0)