From cb00caed84f1329d223fca88167ccf3bf2d3131f Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Thu, 6 Jul 2017 15:59:03 +0200 Subject: [PATCH] Switch from stdout stream based to tmp file approach --- lib/plugins/package/lib/zipService.js | 55 ++++++++++----------------- 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/lib/plugins/package/lib/zipService.js b/lib/plugins/package/lib/zipService.js index 84447b85c..6d9192b1c 100644 --- a/lib/plugins/package/lib/zipService.js +++ b/lib/plugins/package/lib/zipService.js @@ -4,11 +4,13 @@ /* eslint-disable no-param-reassign */ /* eslint-disable consistent-return */ -const childProcess = require('child_process'); -const archiver = require('archiver'); const BbPromise = require('bluebird'); +const archiver = require('archiver'); +const os = require('os'); const path = require('path'); -const fs = require('fs'); +const crypto = require('crypto'); +const fs = BbPromise.promisifyAll(require('fs')); +const childProcess = BbPromise.promisifyAll(require('child_process')); const globby = require('globby'); const _ = require('lodash'); @@ -124,6 +126,12 @@ function excludeNodeDevDependencies(servicePath) { exclude: [], }; + // the file where we'll write the dependencies into + const nodeDevDepFile = path.join( + os.tmpdir(), + `node-dev-dependencies-${crypto.randomBytes(8).toString('hex')}` + ); + try { const packageJsonFilePaths = globby.sync([ '**/package.json', @@ -142,47 +150,24 @@ function excludeNodeDevDependencies(servicePath) { return !isNodeModulesDir; }); - return BbPromise.map(packageJsonPaths, (packageJsonPath) => { + // NOTE: using mapSeries here for a sequential computation (w/o race conditions) + return BbPromise.mapSeries(packageJsonPaths, (packageJsonPath) => { // the path where the package.json file lives const fullPath = path.join(servicePath, packageJsonPath); const dirWithPackageJson = fullPath.replace(path.join(path.sep, 'package.json'), ''); - const childProc = childProcess.spawn('npm', - ['ls', '--dev=true', '--parseable=true', '--silent'], + return childProcess.execAsync( + `npm ls --dev=true --parseable=true --silent >> ${nodeDevDepFile}`, { cwd: dirWithPackageJson } ); - - let result = []; - - return new BbPromise((resolve) => { - if (childProc.error) { - return resolve(result); - } - - const chunks = []; - - childProc.stdout.on('data', (chunk) => { - chunks.push(chunk); - }); - - childProc.stdout.on('end', () => { - result = Buffer.concat(chunks) - .toString() - .split('\n') - .filter((item) => item.length > 0); - return resolve(result); - }); - - childProc.stdout.on('error', () => resolve(result)); - }); }) - .then((results) => { - // turn the two result arrays into one - const result = [].concat.apply([], results); // eslint-disable-line + .then(() => fs.readFileAsync(nodeDevDepFile)) + .then((fileContent) => { + const dependencies = fileContent.toString('utf8').split('\n'); const nodeModulesRegex = new RegExp(`${path.join('node_modules', path.sep)}.*`, 'g'); - if (result.length) { - const globs = result + if (dependencies.length) { + const globs = dependencies .map((item) => item.replace(servicePath, '')) .filter((item) => item.length > 0 && item.match(nodeModulesRegex)) .map((item) => `${item.substring(1)}/**`);