Skip to content

Commit

Permalink
feat(utils/exec): adds exec
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 11, 2019
1 parent 0dbdbd0 commit 74e1ed2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
17 changes: 15 additions & 2 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@pika/pack": "^0.3.6",
"@types/find-up": "^2.1.1",
"@types/jest": "^24.0.9",
"@types/lodash.clone": "^4.5.6",
"@types/pify": "^3.0.2",
"@typescript-eslint/eslint-plugin": "^1.6.0",
"@typescript-eslint/parser": "^1.6.0",
Expand Down Expand Up @@ -100,7 +101,9 @@
},
"dependencies": {
"find-up": "^3.0.0",
"pify": "^4.0.1"
"lodash.clone": "^4.5.0",
"pify": "^4.0.1",
"spawn-command": "0.0.2-1"
},
"husky": {
"hooks": {
Expand Down
8 changes: 8 additions & 0 deletions src/@types/spawn-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare module 'spawn-command' {
import { ChildProcess, SpawnOptions } from 'child_process';

export default function sc(
command: string,
options?: SpawnOptions
): ChildProcess;
}
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// Default configuration file name
export const FILE_NAME = 'kpo.scripts';
// Default stdio for spawned commands
export const DEFAULT_STDIO = 'inherit';
30 changes: 30 additions & 0 deletions src/utils/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sc from 'spawn-command';
import ensure from './ensure';
import clone from 'lodash.clone';
import { ChildProcess, SpawnOptions } from 'child_process';
import { DEFAULT_STDIO } from '~/constants';

export interface IExec {
ps: ChildProcess;
promise: Promise<void>;
}

export default function exec(command: string, options: SpawnOptions): IExec {
const opts: SpawnOptions = options ? Object.assign({}, options) : {};

if (!opts.stdio) opts.stdio = DEFAULT_STDIO;
if (!opts.env) opts.env = clone(process.env);

const ps = sc(command, opts);
return {
ps,
promise: new Promise((resolve, reject) => {
ps.on('close', (code: number) => {
return code ? reject(Error(`Failed: ${command}`)) : resolve();
});
ps.on('error', (err: any) => {
return reject(ensure.error(err));
});
})
};
}

0 comments on commit 74e1ed2

Please sign in to comment.