-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}) | ||
}; | ||
} |