Skip to content

Commit

Permalink
fix: don't crash when can't resolve an env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Jul 31, 2022
1 parent 8223712 commit cf262b8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
16 changes: 10 additions & 6 deletions index.js
Expand Up @@ -8,6 +8,7 @@ module.exports = (opts, types, defaults) => {
const conf = new Conf(Object.assign({}, _defaults.defaults, defaults), types);

conf.add(Object.assign({}, opts), 'cli');
const warnings = [];

if (require.resolve.paths) {
const paths = require.resolve.paths('npm');
Expand All @@ -25,7 +26,7 @@ module.exports = (opts, types, defaults) => {
* According to https://github.com/npm/cli/blob/86f5bdb91f7a5971953a5171d32d6eeda6a2e972/lib/npm.js#L258
* and https://github.com/npm/cli/blob/86f5bdb91f7a5971953a5171d32d6eeda6a2e972/lib/config/core.js#L92
*/
conf.addFile(path.resolve(path.dirname(npmPath), '..', 'npmrc'), 'builtin');
warnings.push(conf.addFile(path.resolve(path.dirname(npmPath), '..', 'npmrc'), 'builtin'));
}
}

Expand All @@ -36,7 +37,7 @@ module.exports = (opts, types, defaults) => {
const userConf = conf.get('userconfig');

if (!conf.get('global') && projectConf !== userConf) {
conf.addFile(projectConf, 'project');
warnings.push(conf.addFile(projectConf, 'project'));
} else {
conf.add({}, 'project');
}
Expand All @@ -45,18 +46,18 @@ module.exports = (opts, types, defaults) => {
// than the ones in userconfig
if (conf.get('workspace-prefix') && conf.get('workspace-prefix') !== projectConf) {
const workspaceConf = path.resolve(conf.get('workspace-prefix'), '.npmrc');
conf.addFile(workspaceConf, 'workspace');
warnings.push(conf.addFile(workspaceConf, 'workspace'));
}

conf.addFile(conf.get('userconfig'), 'user');
warnings.push(conf.addFile(conf.get('userconfig'), 'user'));

if (conf.get('prefix')) {
const etc = path.resolve(conf.get('prefix'), 'etc');
conf.root.globalconfig = path.resolve(etc, 'npmrc');
conf.root.globalignorefile = path.resolve(etc, 'npmignore');
}

conf.addFile(conf.get('globalconfig'), 'global');
warnings.push(conf.addFile(conf.get('globalconfig'), 'global'));
conf.loadUser();

const caFile = conf.get('cafile');
Expand All @@ -65,7 +66,10 @@ module.exports = (opts, types, defaults) => {
conf.loadCAFile(caFile);
}

return conf;
return {
config: conf,
warnings: warnings.filter(Boolean),
};
};

Object.defineProperty(module.exports, 'defaults', {
Expand Down
4 changes: 1 addition & 3 deletions lib/conf.js
Expand Up @@ -44,11 +44,9 @@ class Conf extends ConfigChain {
if (error.code === 'ENOENT') {
this.add({}, marker);
} else {
throw error;
return `Issue while reading "${file}". ${error.message}`
}
}

return this;
}

// https://github.com/npm/cli/blob/latest/lib/config/core.js#L341-L357
Expand Down
2 changes: 1 addition & 1 deletion test.js
Expand Up @@ -8,7 +8,7 @@ const m = require('.');
delete npmDefaults.unicode;

test('mirror npm config', async () => {
const conf = m();
const { config: conf } = m();
const npmConf = await promisify(npmCore.load)();

expect(conf.globalPrefix).toBe(npmConf.globalPrefix);
Expand Down

0 comments on commit cf262b8

Please sign in to comment.