@untool/core
Advanced tools
Comparing version 0.25.1 to 0.26.0
@@ -6,2 +6,11 @@ # Change Log | ||
<a name="0.26.0"></a> | ||
# [0.26.0](https://github.com/untool/untool/compare/v0.25.1...v0.26.0) (2018-10-01) | ||
**Note:** Version bump only for package @untool/core | ||
<a name="0.25.1"></a> | ||
@@ -8,0 +17,0 @@ ## [0.25.1](https://github.com/untool/untool/compare/v0.25.0...v0.25.1) (2018-09-28) |
@@ -7,3 +7,3 @@ 'use strict'; | ||
const { getConfig } = require('./lib/config'); | ||
const { environmentalize } = require('./lib/env'); | ||
const { environmentalize } = require('./lib/utils'); | ||
@@ -10,0 +10,0 @@ exports.Mixin = class Mixin { |
'use strict'; | ||
const debug = require('debug')('untool:config'); | ||
const { basename, dirname, join } = require('path'); | ||
const mergeWith = require('lodash.mergewith'); | ||
const { | ||
sync: resolve, | ||
create: { sync: createResolver }, | ||
} = require('enhanced-resolve'); | ||
const debug = require('debug')('untool:config'); | ||
const { sync: findUp } = require('find-up'); | ||
const cosmiconfig = require('cosmiconfig'); | ||
const flatten = require('flat'); | ||
const escapeRegExp = require('escape-string-regexp'); | ||
const isPlainObject = require('is-plain-object'); | ||
const { load: loadEnv } = require('dotenv'); | ||
const { createLoader } = require('./loader'); | ||
const { resolveMixins } = require('./resolve'); | ||
const { placeholdify, merge } = require('./utils'); | ||
const defaultNamespace = process.env.UNTOOL_NSP || 'untool'; | ||
@@ -26,74 +20,2 @@ const defaultMixinTypes = { | ||
const merge = (...args) => | ||
mergeWith({}, ...args, (objValue, srcValue, key) => { | ||
if (Array.isArray(objValue)) { | ||
if ('mixins' === key) { | ||
return objValue.concat(srcValue); | ||
} | ||
return srcValue; | ||
} | ||
}); | ||
const resolvePreset = createResolver({ | ||
mainFiles: ['preset'], | ||
mainFields: ['preset'], | ||
}); | ||
const resolveMixin = (types, ...args) => { | ||
try { | ||
return createResolver({ | ||
mainFiles: [...types.map((type) => `mixin.${type}`), 'mixin'], | ||
mainFields: [...types.map((type) => `mixin:${type}`), 'mixin'], | ||
})(...args); | ||
} catch (error) { | ||
return null; | ||
} | ||
}; | ||
const resolveMixins = (context, mixins, types) => | ||
mixins.reduce((result, mixin) => { | ||
let found = false; | ||
Object.keys(types).forEach((type) => { | ||
const typeMixin = resolveMixin(types[type], context, mixin); | ||
if (typeMixin) { | ||
result[type] = result[type] || []; | ||
if (!result[type].includes(typeMixin)) { | ||
result[type].push(typeMixin); | ||
} | ||
found = true; | ||
} | ||
}); | ||
if (!found) { | ||
throw new Error(`Can't find mixin '${mixin}'`); | ||
} | ||
return result; | ||
}, {}); | ||
const isResolveError = (error) => | ||
error && error.message && error.message.startsWith("Can't resolve"); | ||
const placeholdify = (config) => { | ||
const flatConfig = flatten(config); | ||
const flatKeys = Object.keys(flatConfig); | ||
const regExp = new RegExp(`<(${flatKeys.map(escapeRegExp).join('|')})>`, 'g'); | ||
const replaceRecursive = (item) => { | ||
if (Array.isArray(item)) { | ||
return item.map(replaceRecursive); | ||
} | ||
if (isPlainObject(item)) { | ||
return Object.keys(item).reduce((result, key) => { | ||
result[key] = replaceRecursive(item[key]); | ||
return result; | ||
}, {}); | ||
} | ||
if (regExp.test(item)) { | ||
return item.replace(regExp, (_, key) => | ||
replaceRecursive(flatConfig[key] || '') | ||
); | ||
} | ||
return item; | ||
}; | ||
return replaceRecursive(config); | ||
}; | ||
exports.getConfig = ({ | ||
@@ -108,75 +30,7 @@ untoolNamespace: namespace = defaultNamespace, | ||
loadEnv({ path: join(rootDir, '.env') }); | ||
const { loadSettings, loadPresets } = createLoader(namespace); | ||
const { name = basename(rootDir), version = '0.0.0' } = pkgData; | ||
const mergeAndOverride = (...args) => { | ||
if (typeof overrides === 'function') { | ||
return overrides(merge(...args)); | ||
} else { | ||
return merge(...args, overrides); | ||
} | ||
}; | ||
const loadConfig = (context, config) => { | ||
const { loadSync, searchSync } = cosmiconfig(namespace, { | ||
stopDir: context, | ||
}); | ||
return config | ||
? loadSync(resolvePreset(context, config)) | ||
: searchSync(context); | ||
}; | ||
const loadSettings = (context, pkgData) => { | ||
const { dependencies = {}, devDependencies = {} } = pkgData; | ||
const result = loadConfig(context); | ||
const settings = { | ||
...(result ? result.config : {}), | ||
}; | ||
if (!settings.presets) { | ||
settings.presets = Object.keys(dependencies) | ||
.concat( | ||
process.env.NODE_ENV !== 'production' | ||
? Object.keys(devDependencies) | ||
: [] | ||
) | ||
.filter((key) => { | ||
try { | ||
return loadConfig(context, key); | ||
} catch (error) { | ||
if (!isResolveError(error)) throw error; | ||
return null; | ||
} | ||
}); | ||
} | ||
return settings; | ||
}; | ||
const loadPreset = (context, preset) => { | ||
try { | ||
return loadConfig(context, preset); | ||
} catch (error) { | ||
if (!isResolveError(error)) throw error; | ||
try { | ||
return loadConfig(dirname(resolve(context, `${preset}/package.json`))); | ||
} catch (error) { | ||
if (!isResolveError(error)) throw error; | ||
throw new Error(`Can't find preset '${preset}' in '${context}'`); | ||
} | ||
} | ||
}; | ||
const loadPresets = (context, presets = []) => | ||
presets.reduce((configs, preset) => { | ||
const { config, filepath } = loadPreset(context, preset); | ||
const presetContext = dirname(filepath); | ||
if (config.mixins) { | ||
config.mixins = config.mixins.map( | ||
(mixin) => | ||
mixin.startsWith('.') ? join(presetContext, mixin) : mixin | ||
); | ||
} | ||
return merge(configs, loadPresets(presetContext, config.presets), config); | ||
}, {}); | ||
loadEnv({ path: join(rootDir, '.env') }); | ||
const defaults = { rootDir, name, version, mixins: [] }; | ||
@@ -186,8 +40,7 @@ const settings = loadSettings(rootDir, pkgData); | ||
const rawConfig = mergeAndOverride(defaults, presets, settings); | ||
delete rawConfig.presets; | ||
// eslint-disable-next-line no-unused-vars | ||
const { presets: _, ...raw } = merge(defaults, presets, settings, overrides); | ||
const config = { | ||
...placeholdify(rawConfig), | ||
mixins: resolveMixins(rootDir, rawConfig.mixins, mixinTypes), | ||
...placeholdify(raw), | ||
mixins: resolveMixins(rootDir, raw.mixins, mixinTypes), | ||
}; | ||
@@ -194,0 +47,0 @@ debug(config); |
{ | ||
"name": "@untool/core", | ||
"version": "0.25.1", | ||
"version": "0.26.0", | ||
"description": "untool core", | ||
@@ -38,3 +38,3 @@ "jsnext": "index.js", | ||
}, | ||
"gitHead": "0628f95386413ae28cb9e2d5beaf7b215c55785f" | ||
"gitHead": "0a63533a040db7b2bf27447320541510682b740f" | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
25487
9
232
1