Skip to content
This repository was archived by the owner on Jul 28, 2021. It is now read-only.

Commit 5b8fc0a

Browse files
committed
feat(config): add support for loading configs from npm itself
1 parent f8ce4f1 commit 5b8fc0a

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

lib/config.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,56 @@
11
'use strict'
22

33
const figgyPudding = require('figgy-pudding')
4+
const spawn = require('child_process').spawn
45

5-
module.exports = figgyPudding({
6+
const frogConfig = module.exports = figgyPudding({
67
also: {},
78
cache: {},
89
dev: {},
910
development: {},
1011
force: {},
12+
global: {},
1113
'ignore-scripts': {},
1214
log: {},
15+
loglevel: {},
1316
only: {},
1417
prefix: {},
1518
production: {},
19+
then: {}, // omfg
1620
umask: {}
1721
})
22+
23+
module.exports.fromNpm = getNpmConfig
24+
async function getNpmConfig (argv) {
25+
const npmBin = process.platform === 'win32' ? 'npm.cmd' : 'npm'
26+
const child = spawn(npmBin, [
27+
'config', 'ls', '--json', '-l'
28+
// We add argv here to get npm to parse those options for us :D
29+
].concat(argv || []), {
30+
env: process.env,
31+
cwd: process.cwd(),
32+
stdio: [0, 'pipe', 2]
33+
})
34+
35+
let stdout = ''
36+
if (child.stdout) {
37+
child.stdout.on('data', (chunk) => {
38+
stdout += chunk
39+
})
40+
}
41+
42+
return await new Promise((resolve, reject) => {
43+
child.on('error', reject)
44+
child.on('close', (code) => {
45+
if (code === 127) {
46+
reject(new Error('`npm` command not found. Please ensure you have npm@5.4.0 or later installed.'))
47+
} else {
48+
try {
49+
resolve(frogConfig(JSON.parse(stdout)))
50+
} catch (e) {
51+
reject(new Error('`npm config ls --json` failed to output json. Please ensure you have npm@5.4.0 or later installed.'))
52+
}
53+
}
54+
})
55+
})
56+
}

0 commit comments

Comments
 (0)