|
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 | +}) |
0 commit comments