From 18d70ccbe67e83b6c96c1b154cf35f9be0655807 Mon Sep 17 00:00:00 2001 From: Lauri Svan Date: Sat, 25 Jun 2016 11:08:11 +0300 Subject: [PATCH 1/3] Fix: Full dependencies not installed for externals --- lib/copyModules.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/copyModules.js b/lib/copyModules.js index 1fb4a8a..8dd34d2 100644 --- a/lib/copyModules.js +++ b/lib/copyModules.js @@ -1,13 +1,27 @@ 'use strict'; const path = require('path'); -const fs = require('fs-extra'); +const childProcess = require('child_process'); const Promise = require('bluebird'); -const copy = Promise.promisify(fs.copy); +const exec = Promise.promisify(childProcess.exec); module.exports = function copyModules(projectPath, moduleNames, dest) { + const pkg = require(path.join(projectPath, 'package.json')); + return Promise.all(moduleNames.map(moduleName => { - const modulePath = path.join(projectPath, 'node_modules', moduleName); - return copy(modulePath, path.join(dest, moduleName)); + const moduleVersion = pkg.dependencies[moduleName]; + + if (!moduleVersion) { + const error = new Error(`No module ${moduleName} found in package.json`); + return Promise.reject(error); + } + + // Run 'npm install' on each module to get a full set of dependencies, + // not just the directly copied ones. + const opts = { + cwd: path.join(dest) + } + + return exec(`npm install --production ${moduleName}@${moduleVersion}`, opts); })); }; From ca299e544f6c451a8210b4831a77cf345446aff8 Mon Sep 17 00:00:00 2001 From: Lauri Svan Date: Sun, 26 Jun 2016 17:59:49 +0300 Subject: [PATCH 2/3] Run all installs within one process --- lib/copyModules.js | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/lib/copyModules.js b/lib/copyModules.js index 8dd34d2..346fec7 100644 --- a/lib/copyModules.js +++ b/lib/copyModules.js @@ -7,21 +7,16 @@ const exec = Promise.promisify(childProcess.exec); module.exports = function copyModules(projectPath, moduleNames, dest) { const pkg = require(path.join(projectPath, 'package.json')); - - return Promise.all(moduleNames.map(moduleName => { + const modulesAndVersions = moduleNames.map(moduleName => { const moduleVersion = pkg.dependencies[moduleName]; + return `${moduleName}@${moduleVersion}`; + }); + const opts = { + cwd: path.join(dest) + }; + const installString = modulesAndVersions.join(' '); - if (!moduleVersion) { - const error = new Error(`No module ${moduleName} found in package.json`); - return Promise.reject(error); - } - - // Run 'npm install' on each module to get a full set of dependencies, - // not just the directly copied ones. - const opts = { - cwd: path.join(dest) - } - - return exec(`npm install --production ${moduleName}@${moduleVersion}`, opts); - })); + // Run 'npm install' on each module to get a full set of dependencies, + // not just the directly copied ones. + return exec(`npm install --production ${installString}`, opts); }; From 373051b9d3c0b6d3132c911d9013dbde27fa9219 Mon Sep 17 00:00:00 2001 From: Lauri Svan Date: Sun, 26 Jun 2016 21:32:15 +0300 Subject: [PATCH 3/3] Use execFile (speed), some error handling --- lib/copyModules.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/copyModules.js b/lib/copyModules.js index 346fec7..10030c4 100644 --- a/lib/copyModules.js +++ b/lib/copyModules.js @@ -3,20 +3,29 @@ const path = require('path'); const childProcess = require('child_process'); const Promise = require('bluebird'); -const exec = Promise.promisify(childProcess.exec); +const exec = Promise.promisify(childProcess.execFile); module.exports = function copyModules(projectPath, moduleNames, dest) { + // No dependencies, just return, so that npm install would not fail. + if (moduleNames.length === 0) { + return Promise.resolve(); + } + const pkg = require(path.join(projectPath, 'package.json')); const modulesAndVersions = moduleNames.map(moduleName => { const moduleVersion = pkg.dependencies[moduleName]; + + // If no module version was found, throw an error + if (!moduleVersion) { + throw new Error(`Error: Could not find module ${moduleName} in package.json!`); + } + return `${moduleName}@${moduleVersion}`; }); - const opts = { - cwd: path.join(dest) - }; - const installString = modulesAndVersions.join(' '); + const opts = { cwd: path.join(dest), env: process.env }; + const args = ['install', '--production'].concat(modulesAndVersions); // Run 'npm install' on each module to get a full set of dependencies, // not just the directly copied ones. - return exec(`npm install --production ${installString}`, opts); + return exec('npm', args, opts); };