Skip to content

Commit

Permalink
chore: abstract validation
Browse files Browse the repository at this point in the history
  • Loading branch information
evenstensberg committed Aug 28, 2019
1 parent 55b770c commit 065e87e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
19 changes: 2 additions & 17 deletions bin/utils/convert-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ const fs = require("fs");
fs.existsSync = fs.existsSync || path.existsSync;
const interpret = require("interpret");
const prepareOptions = require("./prepareOptions");
const webpackConfigurationSchema = require("../config/webpackConfigurationSchema.json");
const validateSchema = require("webpack").validateSchema;
const findup = require("findup-sync");
const validateOptions = require("./validate-options");

module.exports = function(...args) {
const argv = args[1] || args[0];
Expand Down Expand Up @@ -136,21 +135,7 @@ module.exports = function(...args) {

function processConfiguredOptions(options) {
if (options) {
let error;
try {
const errors = validateSchema(webpackConfigurationSchema, options);
if (errors && errors.length > 0) {
const { WebpackOptionsValidationError } = require("webpack");
error = new WebpackOptionsValidationError(errors);
}
} catch (err) {
error = err;
}

if (error) {
console.error(error.message);
process.exit(-1);
}
validateOptions(options);
} else {
options = {};
}
Expand Down
20 changes: 20 additions & 0 deletions bin/utils/validate-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const webpackConfigurationSchema = require("../config/webpackConfigurationSchema.json");
const validateSchema = require("webpack").validateSchema;

module.exports = function validateOptions(options) {
let error;
try {
const errors = validateSchema(webpackConfigurationSchema, options);
if (errors && errors.length > 0) {
const { WebpackOptionsValidationError } = require("webpack");
error = new WebpackOptionsValidationError(errors);
}
} catch (err) {
error = err;
}

if (error) {
console.error(error.message);
process.exit(-1);
}
};
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
"babel-preset-jest": "24.6.0",
"codecov": "3.5.0",
"commitizen": "4.0.3",
"commitlint": "^8.1.0",
"commitlint-config-cz": "0.12.0",
"conventional-changelog-cli": "2.0.21",
"cz-customizable": "6.2.0",
Expand Down

0 comments on commit 065e87e

Please sign in to comment.