Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports.config_from_path = function(file_path) {
const config_path = path.join(process.cwd(), file_path);
let isConfigDirectory = false;
try {
let config = null;
isConfigDirectory = fs.lstatSync(config_path).isDirectory();
if (isConfigDirectory) {
config = cosmiconfigSync("linthtml", { stopDir: config_path, packageProp: "linthtmlConfig" }).search(config_path);
} else {
config = explorer.load(config_path);
}
if (config === null) {
throw new Error();
}
return config;
} catch (error) {
if (isConfigDirectory) {
throw new CustomError("CLI-01", { config_path });
// console.log(`{red Error:} Cannot read config file in directory: {underline ${config_path}}`);
}
throw new CustomError("CLI-02", { config_path });
}
};
getConfig() {
const { token } = this.getOptions();
const { config: cosmi = {} } =
cosmiconfig('iopipe', {
cache: false,
sync: true,
rcExtensions: true
}).load(process.cwd()) || {};
const plugins = (cosmi.plugins || []).map(plugin => {
// plugins can be specified as strings or as arrays with 2 entries
// ["@iopipe/trace", ["@iopipe/logger", {"enabled": true}]]
// create require calls for each scenario
const pluginModule = _.isArray(plugin) ? plugin[0] : plugin;
const pluginConfig = _.isArray(plugin) ? JSON.stringify(plugin[1]) : '';
return `require('${pluginModule}')(${pluginConfig})`;
});
const inlineConfigObject = _.pickBy(
_.assign({}, cosmi, {
function createCosmiConfig() {
// We need to wrap loaders in order to access and transform file content (as string)
// Cosmiconfig has transform option but at this point config is not a string but an object
return cosmiconfig('graphql', {
loaders: {
'.js': {sync: cosmi.loadJs, async: cosmi.loadJs},
'.json': loadJson,
'.yaml': loadYaml,
'.yml': loadYaml,
noExt: loadYaml,
},
});
}
type => types[type].type === Boolean
);
// Parse args
const args = parseArgs(rawArgs || [], {
boolean: boleans,
string: Object.keys(types).filter(
type => types[type].type === String || types[type].type === Object
),
default: boleans.reduce((total, bool) => ({ ...total, [bool]: null }), {})
});
let options = {};
try {
// Load from "noderize" key in package.json, .noderizerc, or noderize.config.js
const results = cosmiconfig("noderize").searchSync();
if (results) {
const configOptions = results.config || {};
Object.keys(configOptions).forEach(configOptionKey => {
if (types[configOptionKey] !== undefined) {
const type = types[configOptionKey];
let value = configOptions[configOptionKey];
if (type.type === Array && !Array.isArray(value)) {
value = [value];
}
value = run(type.run, value, { options });
options[configOptionKey] = value;
} else {
log.warn(`Config key '${configOptionKey}' doesn't do anything.`);
}
});
function getConfig(configPath, searchPath = process.cwd(), stopDir = homedir()) {
const useCustomPath = !!configPath
const explorer = cosmiconfig('cca', { sync: true, stopDir })
try {
const searchPathAbsolute = !useCustomPath && searchPath
const configPathAbsolute = useCustomPath && path.join(process.cwd(), configPath)
// search from the root of the process if the user didnt specify a config file,
// or use the custom path if a file is passed.
const result = explorer.load(searchPathAbsolute, configPathAbsolute)
// dont throw if the explorer didnt find a configfile,
// instead use default config
const config = result ? result.config : {}
const filepath = result ? result.filepath : {}
if (!result) Logger.log('No config file detected, using defaults.')
return { ...config, filepath }
} catch (error) {
function loadExtendedConfig(config, configDir, extendLookup) {
const extendPath = getModulePath(configDir, extendLookup)
const extendDir = path.dirname(extendPath)
return cosmiconfig(null, {
configPath: extendPath,
// In case `--config` was used: do not pay attention to it again
argv: false,
}).then(result => {
// Make sure to also augment the config that we're merging in
// ... but the `ignoreFiles` option only works with the
// config that is being directly invoked, not any
// extended configs
return augmentConfig(stripIgnoreFiles(result.config), extendDir)
})
}
function getConfigExporer() {
return (_configExplorer =
_configExplorer ||
// Lazy load cosmiconfig since it is a relatively large bundle
require('cosmiconfig').cosmiconfigSync('babel-plugin-macros', {
searchPlaces: [
'package.json',
'.babel-plugin-macrosrc',
'.babel-plugin-macrosrc.json',
'.babel-plugin-macrosrc.yaml',
'.babel-plugin-macrosrc.yml',
'.babel-plugin-macrosrc.js',
'babel-plugin-macros.config.js',
],
packageProp: 'babelMacros',
}))
}
function getConfigExporer() {
return (_configExplorer =
_configExplorer || // Lazy load cosmiconfig since it is a relatively large bundle
require('cosmiconfig').cosmiconfigSync('babel-plugin-macros', {
searchPlaces: [
'package.json',
'.babel-plugin-macrosrc',
'.babel-plugin-macrosrc.json',
'.babel-plugin-macrosrc.yaml',
'.babel-plugin-macrosrc.yml',
'.babel-plugin-macrosrc.js',
'babel-plugin-macros.config.js'
],
packageProp: 'babelMacros'
}))
}
module.exports = async (context, opts) => {
const {cwd, env} = context;
const {config, filepath} = (await cosmiconfig(CONFIG_NAME, {searchPlaces: CONFIG_FILES}).search(cwd)) || {};
debug('load config from: %s', filepath);
// Merge config file options and CLI/API options
let options = {...config, ...opts};
if (options.ci === false) {
options.noCi = true;
}
const pluginsPath = {};
let extendPaths;
({extends: extendPaths, ...options} = options);
if (extendPaths) {
// If `extends` is defined, load and merge each shareable config with `options`
options = {
...castArray(extendPaths).reduce((result, extendPath) => {
const getExplorer = _.memoize(opts =>
cosmiconfig(
RC_NAMESPACE,
_.defaultsDeep(
{
// @ts-ignore
loaders: {noExt: cosmiconfig.loadJs}
},
opts
)
)
);