From b72031df7b5eb80774783121eb8d9fd8868ecd3f Mon Sep 17 00:00:00 2001 From: Joao Figueiredo Date: Mon, 13 Jul 2015 18:13:27 +0200 Subject: [PATCH 1/4] Adds some error handling to pagesHelpers. --- gulp/utils/pagesHelpers.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/gulp/utils/pagesHelpers.js b/gulp/utils/pagesHelpers.js index 4496ff1..80b821a 100644 --- a/gulp/utils/pagesHelpers.js +++ b/gulp/utils/pagesHelpers.js @@ -29,6 +29,12 @@ module.exports = function (config) { // if the passed options has any value for this option // just take that value if (hasValue(optionValue)) { + if (optionSchema.indexOf(optionValue) === -1) { + throw new Error( + '\'' + optionValue + '\' is not one of the following \'' + + optionSchema.join('\', \'') + '\'.\n' + ); + } return optionValue; } // otherwise take the default value (first value) @@ -89,13 +95,21 @@ module.exports = function (config) { options = options || {}; schema = yamljs.load(srcDir + path + '/definition.yml'); optionsSchema = schema.options; + return _.mapValues(optionsSchema, function(o, oKey) { - // Handle simple option (options that are just an array) - if (Array.isArray(o)) { - return mergeSimpleOptionDefault(options[oKey], o); - } + try { + // Handle simple option (options that are just an array) + if (Array.isArray(o)) { + return mergeSimpleOptionDefault(options[oKey], o); + } - return mergeComplexOptionDefault(options[oKey] || {}, o); + return mergeComplexOptionDefault(options[oKey] || {}, o); + } catch (e) { + throw new Error( + 'ERROR Invalid instanciation of ' + path + '.\n' + + 'Option \'' + oKey + '\': '+ e.message + ); + } }); }; From 1d6f519268825ebb3895e5064c466eba933d9520 Mon Sep 17 00:00:00 2001 From: Joao Figueiredo Date: Mon, 13 Jul 2015 18:13:27 +0200 Subject: [PATCH 2/4] Adds some error handling to pagesHelpers. --- gulp/utils/pagesHelpers.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/gulp/utils/pagesHelpers.js b/gulp/utils/pagesHelpers.js index 4496ff1..88ad527 100644 --- a/gulp/utils/pagesHelpers.js +++ b/gulp/utils/pagesHelpers.js @@ -29,6 +29,12 @@ module.exports = function (config) { // if the passed options has any value for this option // just take that value if (hasValue(optionValue)) { + if (optionSchema.indexOf(optionValue) === -1) { + throw new Error( + '\'' + optionValue + '\' is not one of the following \'' + + optionSchema.join('\', \'') + '\'.\n' + ); + } return optionValue; } // otherwise take the default value (first value) @@ -89,13 +95,21 @@ module.exports = function (config) { options = options || {}; schema = yamljs.load(srcDir + path + '/definition.yml'); optionsSchema = schema.options; + return _.mapValues(optionsSchema, function(o, oKey) { - // Handle simple option (options that are just an array) - if (Array.isArray(o)) { - return mergeSimpleOptionDefault(options[oKey], o); - } + try { + // Handle simple option (options that are just an array) + if (Array.isArray(o)) { + return mergeSimpleOptionDefault(options[oKey], o); + } - return mergeComplexOptionDefault(options[oKey] || {}, o); + return mergeComplexOptionDefault(options[oKey] || {}, o); + } catch (e) { + throw new Error( + 'ERROR Invalid instanciation of ' + path + '.\n' + + 'Option \'' + oKey + '\': ' + e.message + ); + } }); }; From 0c9f8143577d4f980396db56549f3fd126567315 Mon Sep 17 00:00:00 2001 From: Joao Figueiredo Date: Mon, 13 Jul 2015 19:02:39 +0200 Subject: [PATCH 3/4] Removes value validation and replaces by a warning when one tries to use an option that is not defined on the definition files. --- gulp/utils/pagesHelpers.js | 42 ++++++++++++++++++++++++++------------ package.json | 1 + 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/gulp/utils/pagesHelpers.js b/gulp/utils/pagesHelpers.js index 88ad527..2b97b5d 100644 --- a/gulp/utils/pagesHelpers.js +++ b/gulp/utils/pagesHelpers.js @@ -2,6 +2,7 @@ var yamljs = require('yamljs'); var _ = require('lodash'); var markdown = require('marked'); +var chalk = require('chalk'); module.exports = function (config) { var srcDir = config.basePaths.src; @@ -15,6 +16,25 @@ module.exports = function (config) { return value !== null && value !== undefined; } + function generateExtraOptionsError (extraOptions) { + throw new Error( + 'Option' + (extraOptions.length > 1 ? 's ' : ' ') + + '\'' + chalk.blue(extraOptions.join('\', \'')) + '\' ' + + (extraOptions.length > 1 ? 'aren\'t' : 'isn\'t') + ' defined.' + ); + } + + function validatesOptionsKeysPresentOnSchema(options, optionsSchema) { + var optionsKeys = Object.keys(options || {}); + var optionsSchemaKeys = Object.keys(optionsSchema || {}); + + var extraKeys = _.difference(optionsKeys, optionsSchemaKeys); + + if (extraKeys.length > 0) { + generateExtraOptionsError(extraKeys); + } + } + /** * Merges a simple component option with its default * @@ -29,12 +49,6 @@ module.exports = function (config) { // if the passed options has any value for this option // just take that value if (hasValue(optionValue)) { - if (optionSchema.indexOf(optionValue) === -1) { - throw new Error( - '\'' + optionValue + '\' is not one of the following \'' + - optionSchema.join('\', \'') + '\'.\n' - ); - } return optionValue; } // otherwise take the default value (first value) @@ -96,20 +110,22 @@ module.exports = function (config) { schema = yamljs.load(srcDir + path + '/definition.yml'); optionsSchema = schema.options; + try { + validatesOptionsKeysPresentOnSchema(options, optionsSchema); + } catch (e) { + console.warn( + chalk.yellow('Error while instanciating ') + chalk.blue(path) + + chalk.yellow('. ' + e.message) + ); + } + return _.mapValues(optionsSchema, function(o, oKey) { - try { // Handle simple option (options that are just an array) if (Array.isArray(o)) { return mergeSimpleOptionDefault(options[oKey], o); } return mergeComplexOptionDefault(options[oKey] || {}, o); - } catch (e) { - throw new Error( - 'ERROR Invalid instanciation of ' + path + '.\n' + - 'Option \'' + oKey + '\': ' + e.message - ); - } }); }; diff --git a/package.json b/package.json index 83cdfc3..d8abd83 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "devDependencies": { "bower": "^1.3.12", "browser-sync": "^2.6.5", + "chalk": "^1.1.0", "del": "^1.1.1", "gulp": "^3.8.11", "gulp-autoprefixer": "^2.2.0", From f68c578ec17cdf301c2188f16a743ef84be1e5fa Mon Sep 17 00:00:00 2001 From: Joao Figueiredo Date: Mon, 13 Jul 2015 19:08:33 +0200 Subject: [PATCH 4/4] Fixes identation. --- gulp/utils/pagesHelpers.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gulp/utils/pagesHelpers.js b/gulp/utils/pagesHelpers.js index 2b97b5d..3be9ea2 100644 --- a/gulp/utils/pagesHelpers.js +++ b/gulp/utils/pagesHelpers.js @@ -120,12 +120,12 @@ module.exports = function (config) { } return _.mapValues(optionsSchema, function(o, oKey) { - // Handle simple option (options that are just an array) - if (Array.isArray(o)) { - return mergeSimpleOptionDefault(options[oKey], o); - } + // Handle simple option (options that are just an array) + if (Array.isArray(o)) { + return mergeSimpleOptionDefault(options[oKey], o); + } - return mergeComplexOptionDefault(options[oKey] || {}, o); + return mergeComplexOptionDefault(options[oKey] || {}, o); }); };