Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@untool/core

Package Overview
Dependencies
Maintainers
4
Versions
93
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@untool/core - npm Package Compare versions

Comparing version 0.25.1 to 0.26.0

lib/loader.js

9

CHANGELOG.md

@@ -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)

2

index.js

@@ -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"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc