-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
37 lines (34 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env node --harmony
import spawn from "cross-spawn";
import os from "os";
import { exec } from "child_process";
import exit from 'exit';
function normalize( args, isWindows ) {
return args.map( arg => {
Object.keys( process.env )
.sort( ( x, y ) => x.length < y.length ) // sort by descending length to prevent partial replacement
.forEach( key => {
const regex = new RegExp( `\\$${ key }|%${ key }%`, "ig" );
arg = arg.replace( regex, process.env[ key ] );
} );
return arg;
} )
}
let args = process.argv.slice( 2 );
if ( args.length === 1 ) {
const [ command ] = normalize( args );
const proc = exec( command, ( error, stdout, stderr ) => {
if ( error ) {
console.error( `exec error: ${ error }` );
return;
}
process.stdout.write( stdout );
process.stderr.write( stderr );
exit(proc.code);
});
} else {
args = normalize( args );
const command = args.shift();
const proc = spawn.sync( command, args, { stdio: "inherit" } );
exit(proc.status);
}