Skip to content

Commit bb09837

Browse files
feat(lib): add cli (#68)
* feat(lib): add CLI * docs: tune README
1 parent a98fcb6 commit bb09837

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,18 @@ webpack-validator makes it easy:
4444
![validation-example](https://cloud.githubusercontent.com/assets/3755413/14134087/b3279738-f654-11e5-9752-367b01ac123d.png)
4545

4646
### Usage
47-
In your `webpack.config.js`:
47+
There are two ways to use webpack-validator: a) "programmatically" by wrapping your webpack config object with a validation function or b) using a command line interface.
48+
49+
For the first approach, add this in your `webpack.config.js`:
4850
```js
4951
const validate = require('webpack-validator')
5052

5153
module.exports = validate({ /* ... your webpack config */ })
5254
```
5355
Now run webpack. Either everything is green and the build continues or `joi` will let you know what's wrong and the build won't continue.
5456

55-
Alternatively just run `node webpack.config.js` to only validate your config and not run webpack.
57+
#### CLI
58+
For CLI usage you probably want to install the tool globally (`npm install -g webpack-validator`) first. Then just run `webpack-validator <your-config>`.
5659

5760
#### Customizing
5861
If you need to extend the schema, for example for custom top level properties or properties added by third party plugins like `eslint-loader` (which adds a toplevel `eslint` property), do it like this:

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "webpack-validator",
33
"description": "Validate your webpack config with joi",
44
"main": "dist",
5+
"bin": "./dist/bin/webpack-validator.js",
56
"files": [
67
"dist"
78
],
@@ -41,8 +42,10 @@
4142
},
4243
"dependencies": {
4344
"chalk": "1.1.3",
45+
"commander": "2.9.0",
4446
"joi": "8.0.5",
45-
"lodash": "4.11.1"
47+
"lodash": "4.11.1",
48+
"npmlog": "2.0.3"
4649
},
4750
"devDependencies": {
4851
"autoprefixer": "6.3.6",

src/bin/webpack-validator.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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+
.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+
})

0 commit comments

Comments
 (0)