@percy/config
Advanced tools
Comparing version 1.0.0-beta.20 to 1.0.0-beta.21
@@ -8,14 +8,11 @@ "use strict"; | ||
var _deepmerge = _interopRequireDefault(require("deepmerge")); | ||
var _normalize = require("./normalize"); | ||
var _validate = require("./validate"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const { | ||
assign, | ||
entries, | ||
freeze | ||
entries | ||
} = Object; // Recursively walks a schema and collects defaults. When no schema is provided, | ||
// the default config schema is used. Returned defaults are frozen. | ||
// the default config schema is used. | ||
@@ -29,7 +26,7 @@ function getDefaultFromSchema(schema) { | ||
} else if (schema.default != null) { | ||
// return the frozen default for this schema | ||
return freeze(schema.default); | ||
// return the default for this schema | ||
return schema.default; | ||
} else if (schema.type === 'object' && schema.properties) { | ||
// return a frozen object of default properties | ||
return freeze(entries(schema.properties).reduce((acc, [prop, schema]) => { | ||
// return an object of default properties | ||
return entries(schema.properties).reduce((acc, [prop, schema]) => { | ||
let def = getDefaultFromSchema(schema); | ||
@@ -39,3 +36,3 @@ return def != null ? assign(acc || {}, { | ||
}) : acc; | ||
}, undefined)); | ||
}, undefined); | ||
} else { | ||
@@ -47,6 +44,5 @@ return undefined; | ||
function getDefaults(overrides = {}) { | ||
return _deepmerge.default.all([getDefaultFromSchema(), overrides], { | ||
// overwrite default arrays, do not merge | ||
arrayMerge: (_, arr) => arr | ||
return (0, _normalize.merge)(getDefaultFromSchema(), overrides, { | ||
replaceArrays: true | ||
}); | ||
} |
@@ -15,4 +15,2 @@ "use strict"; | ||
var _deepmerge = _interopRequireDefault(require("deepmerge")); | ||
var _logger = _interopRequireDefault(require("@percy/logger")); | ||
@@ -69,4 +67,3 @@ | ||
} else { | ||
// normalize to remove empty values and convert snake-case to camelCase | ||
config = (0, _normalize.default)(result.config); | ||
config = result.config; | ||
cache.set(path, config); | ||
@@ -85,7 +82,22 @@ } | ||
config = (0, _deepmerge.default)(config || {}, overrides); | ||
if (!(0, _validate.default)(config, { | ||
scrub: true | ||
}) && bail) return; // normalize again to remove empty values from overrides and validation scrubbing | ||
config = (0, _normalize.default)(config, overrides); | ||
let validation = config && (0, _validate.default)(config); | ||
if (validation && !validation.result) { | ||
_logger.default.warn('Invalid config:'); | ||
for (let { | ||
message, | ||
path | ||
} of validation.errors) { | ||
_logger.default.warn(`- ${path.join('.')}: ${message}`); | ||
let [k, t] = [path.pop(), path.reduce((d, p) => d[p], config)]; | ||
if (t && k in t) delete t[k]; | ||
} | ||
if (bail) return; | ||
} // normalize again to remove empty values for logging | ||
config = (0, _normalize.default)(config); | ||
@@ -92,0 +104,0 @@ if (config) _logger.default.debug(`Using config:\n${(0, _stringify.inspect)(config)}`); // merge with defaults |
@@ -6,26 +6,12 @@ "use strict"; | ||
}); | ||
exports.merge = merge; | ||
exports.default = normalize; | ||
const { | ||
isArray | ||
} = Array; | ||
const { | ||
entries, | ||
assign | ||
} = Object; // Edge case camelizations | ||
// recursively reduces config objects and arrays to remove undefined and empty | ||
// values and rename kebab-case properties to camelCase. | ||
function normalize(subject) { | ||
if (typeof subject === 'object') { | ||
let isArray = Array.isArray(subject); | ||
return Object.entries(subject).reduce((result, [key, value]) => { | ||
value = normalize(value); | ||
if (typeof value !== 'undefined') { | ||
return isArray ? (result || []).concat(value) : Object.assign(result || {}, { | ||
[camelize(key)]: value | ||
}); | ||
} else { | ||
return result; | ||
} | ||
}, undefined); | ||
} else { | ||
return subject; | ||
} | ||
} // Edge case camelizations | ||
const CAMELIZE_MAP = { | ||
@@ -36,4 +22,24 @@ css: 'CSS', | ||
function camelize(s) { | ||
return s.replace(/-([^-]+)/g, (_, w) => CAMELIZE_MAP[w] || w[0].toUpperCase() + w.slice(1)); | ||
function camelize(str) { | ||
return str.replace(/-([^-]+)/g, (_, w) => CAMELIZE_MAP[w] || w[0].toUpperCase() + w.slice(1)); | ||
} // Merges source values into the target object unless empty. When `options.replaceArrays` is truthy, | ||
// target arrays are replaced by their source arrays rather than concatenated together. | ||
function merge(target, source, options) { | ||
let isSourceArray = isArray(source); | ||
if ((options === null || options === void 0 ? void 0 : options.replaceArrays) && isSourceArray) return source; | ||
if (typeof source !== 'object') return source != null ? source : target; | ||
return entries(source).reduce((result, [key, value]) => { | ||
value = merge(result === null || result === void 0 ? void 0 : result[key], value, options); | ||
return value == null ? result : isSourceArray ? (result || []).concat(value) : assign(result || {}, { | ||
[camelize(key)]: value | ||
}); | ||
}, target); | ||
} // Recursively reduces config objects and arrays to remove undefined and empty values and rename | ||
// kebab-case properties to camelCase. Optionally allows deep merging of override values | ||
function normalize(object, overrides) { | ||
return merge(merge(undefined, object), overrides); | ||
} |
@@ -13,4 +13,2 @@ "use strict"; | ||
var _logger = _interopRequireDefault(require("@percy/logger")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -78,10 +76,7 @@ | ||
function validate(config, { | ||
scrub | ||
} = {}) { | ||
function validate(config) { | ||
let result = ajv.validate('config', config); | ||
let errors = []; | ||
if (!result) { | ||
_logger.default.warn('Invalid config:'); | ||
for (let error of ajv.errors) { | ||
@@ -95,25 +90,26 @@ let { | ||
} = error; | ||
let pre = dataPath ? `'${dataPath.substr(1)}' ` : ''; | ||
let path = dataPath ? dataPath.substr(1).split('.') : []; | ||
if (keyword === 'required') { | ||
message = `is missing required property '${params.missingProperty}'`; | ||
message = 'missing required property'; | ||
path.push(params.missingProperty); | ||
} else if (keyword === 'additionalProperties') { | ||
pre = pre ? `${pre}has ` : ''; | ||
message = `unknown property '${params.additionalProperty}'`; | ||
if (scrub) delete data[params.additionalProperty]; | ||
message = 'unknown property'; | ||
path.push(params.additionalProperty); | ||
} else if (keyword === 'type') { | ||
let dataType = Array.isArray(data) ? 'array' : typeof data; | ||
message = `should be ${a(params.type)}, received ${a(dataType)}`; | ||
if (scrub) { | ||
let [key, ...path] = dataPath.substr(1).split('.').reverse(); | ||
delete path.reduceRight((d, k) => d[k], config)[key]; | ||
} | ||
} | ||
_logger.default.warn(`- ${pre}${message}`); | ||
errors.push({ | ||
message, | ||
path | ||
}); | ||
} | ||
} | ||
return result; | ||
return { | ||
result, | ||
errors | ||
}; | ||
} // Adds "a" or "an" to a word for readability. | ||
@@ -120,0 +116,0 @@ |
{ | ||
"name": "@percy/config", | ||
"version": "1.0.0-beta.20", | ||
"version": "1.0.0-beta.21", | ||
"license": "MIT", | ||
@@ -25,6 +25,5 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@percy/logger": "^1.0.0-beta.20", | ||
"@percy/logger": "^1.0.0-beta.21", | ||
"ajv": "^6.12.5", | ||
"cosmiconfig": "^7.0.0", | ||
"deepmerge": "^4.2.2", | ||
"path-type": "^4.0.0", | ||
@@ -36,3 +35,3 @@ "yaml": "^1.10.0" | ||
}, | ||
"gitHead": "3729ffcbbb67e73e4964c140b892bc0416eaea07" | ||
"gitHead": "43ed5bf362ed4c7f3cacf73dd57284493bb43b71" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16371
5
321
- Removeddeepmerge@^4.2.2
- Removeddeepmerge@4.3.1(transitive)
Updated@percy/logger@^1.0.0-beta.21