diff --git a/package.json b/package.json index 1411350..32b6bce 100644 --- a/package.json +++ b/package.json @@ -35,8 +35,8 @@ "sinon": "^8.0.4" }, "dependencies": { + "@malept/cross-spawn-promise": "^1.0.0", "asar": "^2.0.1", - "cross-spawn": "^7.0.1", "debug": "^4.1.1", "fs-extra": "^8.0.1", "glob": "^7.1.4", diff --git a/src/index.js b/src/index.js index 5bbe5a8..ba70ce5 100644 --- a/src/index.js +++ b/src/index.js @@ -10,7 +10,7 @@ const readElectronVersion = require('./readelectronversion') const readMetadata = require('./readmetadata') const replaceScopeName = require('./replacescopename') const sanitizeName = require('./sanitizename') -const spawn = require('./spawn') +const { spawn } = require('@malept/cross-spawn-promise') const template = require('./template') const sandboxHelper = require('./sandboxhelper') diff --git a/src/spawn.js b/src/spawn.js deleted file mode 100644 index c6af101..0000000 --- a/src/spawn.js +++ /dev/null @@ -1,46 +0,0 @@ -'use strict' - -const spawn = require('cross-spawn') - -/** - * A wrapper around `cross-spawn`'s `spawn` function which can optionally log the command executed - * and/or change the error message via a callback. - * - * If logger is specified in options, it's usually a debug or console.log function pointer. - * Specify updateErrorCallback (a callback) in options to adjust the error object before it - * is rethrown. - */ -module.exports = async function (cmd, args, options = {}) { - const { logger, updateErrorCallback, ...spawnOptions } = options - if (logger) logger(`Executing command ${cmd} ${args.join(' ')}`) - - return new Promise((resolve, reject) => { - let stdout = '' - let stderr = '' - const process = spawn(cmd, args, spawnOptions) - if (process.stdout) { - process.stdout.on('data', data => { - stdout += data.toString() - }) - } - if (process.stderr) { - process.stderr.on('data', data => { - /* istanbul ignore next */ - stderr += data.toString() - }) - } - process.on('close', code => { - if (code === 0) { - resolve(stdout) - } else { - reject(new Error(`Command failed with a non-zero return code (${code}):\n${cmd} ${args.join(' ')}\n${stdout}\n${stderr}`)) - } - }) - process.on('error', err => { - if (updateErrorCallback) { - updateErrorCallback(err, !!logger) - } - reject(new Error(`Error executing command (${err.message || err}):\n${cmd} ${args.join(' ')}\n${stderr}`)) - }) - }) -} diff --git a/test/spawn.js b/test/spawn.js deleted file mode 100644 index 2905850..0000000 --- a/test/spawn.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict' - -const spawn = require('../src/spawn') -const test = require('ava') - -test('returns stdout', async t => { - const dir = process.platform === 'darwin' ? 'ls' : 'dir' - const output = await spawn(dir, [__dirname], { logger: log => null }) - t.regex(output, /spawn/) -}) - -test('returns empty string if stdio is ignored', async t => { - const dir = process.platform === 'darwin' ? 'ls' : 'dir' - const output = await spawn(dir, [__dirname], { stdio: 'ignore' }) - t.is(output, '') -}) - -test('throws an error when it cannot find an executable', t => { - return t.throwsAsync(spawn('does-not-exist', []), { message: /^Error executing command/ }) -}) - -test('updateErrorCallback modifies the exception', t => { - return t.throwsAsync(spawn('does-not-exist', [], { updateErrorCallback: err => { err.message = 'I am an error' } }), { message: /I am an error/ }) -})