Skip to content

Commit

Permalink
chore: use shared package @brandonocasey/spawn-promise
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Aug 18, 2021
1 parent 3e095b8 commit bcc2a9c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
"prepublishOnly": "npm run test:verify"
},
"devDependencies": {
"@brandonocasey/spawn-promise": "^0.2.0",
"ava": "^3.15.0",
"shelljs": "^0.8.4",
"uuid": "^8.3.2",
"conventional-changelog-cli": "^2.1.1",
"conventional-changelog-videojs": "^3.0.0",
"doctoc": "^2.0.0",
"husky": "^6.0.0",
"lint-staged": "^10.5.4",
"not-prerelease": "^1.0.1",
"shelljs": "^0.8.4",
"uuid": "^8.3.2",
"videojs-generator-verify": "~3.0.3",
"videojs-standard": "^8.0.4"
},
Expand Down
43 changes: 8 additions & 35 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const test = require('ava');
const childProcess = require('child_process');
const path = require('path');
const shell = require('shelljs');
const uuid = require('uuid');
const spawnPromise = require('@brandonocasey/spawn-promise');

const BASE_DIR = path.join(__dirname);
const TEMP_DIR = shell.tempdir();
Expand Down Expand Up @@ -35,44 +35,17 @@ const isInstalled = function(dir) {
};

const promiseSpawn = function(bin, args, options = {}) {
return new Promise((resolve, reject) => {
options = Object.assign({shell: true, stdio: 'pipe'}, options);
const ignoreExitCode = options.ignoreExitCode;

const child = childProcess.spawn(bin, args, options);
delete options.ignoreExitCode;
options = Object.assign({shell: true, stdio: 'pipe', encoding: 'utf8'}, options);

let stdout = '';
let stderr = '';
let out = '';

if (child.stdout) {
child.stdout.on('data', function(chunk) {
const str = chunk.toString();

out += str;
stdout += str;
});
return spawnPromise(bin, args, options).then(function({status, stderr, stdout, combined}) {
if (!ignoreExitCode && status !== 0) {
return Promise.reject(`command ${bin} ${args.join(' ')} failed with code ${status}\n` + combined);
}
return Promise.resolve({exitCode: status, stderr, stdout});

if (child.stderr) {
child.stderr.on('data', function(chunk) {
const str = chunk.toString();

out += str;
stderr += str;
});
}
const kill = () => child.kill();

process.on('SIGINT', kill);
process.on('SIGQUIT', kill);
process.on('exit', kill);

child.on('close', (exitCode) => {
if (!options.ignoreExitCode && exitCode !== 0) {
return reject(`command ${bin} ${args.join(' ')} failed with code ${exitCode}\n` + out);
}
return resolve({exitCode, stderr, stdout});
});
});
};

Expand Down

0 comments on commit bcc2a9c

Please sign in to comment.