Switch from stdout stream based to tmp file approach

This commit is contained in:
Philipp Muens 2017-07-06 15:59:03 +02:00
parent bb5e62e61a
commit cb00caed84

View File

@ -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)}/**`);