@untool/core
Advanced tools
Comparing version 0.23.0 to 0.24.0
@@ -6,2 +6,14 @@ # Change Log | ||
<a name="0.24.0"></a> | ||
# [0.24.0](https://github.com/untool/untool/compare/v0.23.0...v0.24.0) (2018-09-26) | ||
### Features | ||
* **core:** make config namespace configurable ([b2df149](https://github.com/untool/untool/commit/b2df149)) | ||
<a name="0.23.0"></a> | ||
@@ -8,0 +20,0 @@ # [0.23.0](https://github.com/untool/untool/compare/v0.22.0...v0.23.0) (2018-09-25) |
@@ -19,2 +19,4 @@ 'use strict'; | ||
const defaultNamespace = process.env.UNTOOL_NSP || 'untool'; | ||
const merge = (...args) => | ||
@@ -89,62 +91,2 @@ mergeWith({}, ...args, (objValue, srcValue, key) => { | ||
const loadConfig = (context, config) => { | ||
const { loadSync, searchSync } = cosmiconfig( | ||
process.env.UNTOOL_NSP || 'untool', | ||
{ stopDir: context } | ||
); | ||
return config | ||
? loadSync(resolvePreset(context, config)) | ||
: searchSync(context); | ||
}; | ||
const loadSettings = (context, { dependencies = {}, devDependencies = {} }) => { | ||
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); | ||
}, {}); | ||
const placeholdify = (config) => { | ||
@@ -174,3 +116,3 @@ const flatConfig = flatten(config); | ||
exports.getConfig = (overrides) => { | ||
exports.getConfig = ({ configNamespace = defaultNamespace, ...overrides }) => { | ||
const pkgFile = findUp('package.json'); | ||
@@ -182,2 +124,71 @@ const pkgData = require(pkgFile); | ||
const mergeAndOverride = (...args) => { | ||
if (typeof overrides === 'function') { | ||
return overrides(merge(...args)); | ||
} else { | ||
return merge(...args, overrides); | ||
} | ||
}; | ||
const loadConfig = (context, config) => { | ||
const { loadSync, searchSync } = cosmiconfig(configNamespace, { | ||
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') }); | ||
@@ -189,16 +200,11 @@ | ||
const mergeAndOverride = (...args) => | ||
typeof overrides === 'function' | ||
? overrides(merge(...args)) | ||
: merge(...args, overrides); | ||
const raw = mergeAndOverride(defaults, presets, settings); | ||
delete raw.presets; | ||
const rawConfig = mergeAndOverride(defaults, presets, settings); | ||
delete rawConfig.presets; | ||
const config = { | ||
...placeholdify(raw), | ||
mixins: resolveMixins(rootDir, raw.mixins), | ||
...placeholdify(rawConfig), | ||
mixins: resolveMixins(rootDir, rawConfig.mixins), | ||
}; | ||
debug(config); | ||
return config; | ||
}; |
{ | ||
"name": "@untool/core", | ||
"version": "0.23.0", | ||
"version": "0.24.0", | ||
"description": "untool core", | ||
@@ -38,3 +38,3 @@ "jsnext": "lib/runtime.js", | ||
}, | ||
"gitHead": "56663d8a79cfb4573d250dc8daca0790f54c40ce" | ||
"gitHead": "8dbacec7c4782ee828b24832da7985924eda532f" | ||
} |
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
25315
253