From 5b8fc0aff8f653a1b71cdd20084db0890d45a434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Tue, 31 Jul 2018 18:20:14 -0700 Subject: [PATCH] feat(config): add support for loading configs from npm itself --- lib/config.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/config.js b/lib/config.js index 666a526..f51a930 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,17 +1,56 @@ 'use strict' const figgyPudding = require('figgy-pudding') +const spawn = require('child_process').spawn -module.exports = figgyPudding({ +const frogConfig = module.exports = figgyPudding({ also: {}, cache: {}, dev: {}, development: {}, force: {}, + global: {}, 'ignore-scripts': {}, log: {}, + loglevel: {}, only: {}, prefix: {}, production: {}, + then: {}, // omfg umask: {} }) + +module.exports.fromNpm = getNpmConfig +async function getNpmConfig (argv) { + const npmBin = process.platform === 'win32' ? 'npm.cmd' : 'npm' + const child = spawn(npmBin, [ + 'config', 'ls', '--json', '-l' + // We add argv here to get npm to parse those options for us :D + ].concat(argv || []), { + env: process.env, + cwd: process.cwd(), + stdio: [0, 'pipe', 2] + }) + + let stdout = '' + if (child.stdout) { + child.stdout.on('data', (chunk) => { + stdout += chunk + }) + } + + return await new Promise((resolve, reject) => { + child.on('error', reject) + child.on('close', (code) => { + if (code === 127) { + reject(new Error('`npm` command not found. Please ensure you have npm@5.4.0 or later installed.')) + } else { + try { + resolve(frogConfig(JSON.parse(stdout))) + } catch (e) { + reject(new Error('`npm config ls --json` failed to output json. Please ensure you have npm@5.4.0 or later installed.')) + } + } + }) + }) +}