src/index.js
import _ from 'lodash';
import path from 'path';
import net from 'net';
import { Observable, Disposable, AsyncSubject } from 'rx';
import sfs from 'fs';
const spawnOg = require('child_process').spawn;
const isWindows = process.platform === 'win32';
const d = require('debug')('surf:promise-array');
/**
* stat a file but don't throw if it doesn't exist
*
* @param {string} file The path to a file
* @return {Stats} The stats structure
*
* @private
*/
function statSyncNoException(file) {
try {
return sfs.statSync(file);
} catch (e) {
return null;
}
}
/**
* Search PATH to see if a file exists in any of the path folders.
*
* @param {string} exe The file to search for
* @return {string} A fully qualified path, or the original path if nothing
* is found
*
* @private
*/
function runDownPath(exe) {
// NB: Windows won't search PATH looking for executables in spawn like
// Posix does
// Files with any directory path don't get this applied
if (exe.match(/[\\\/]/)) {
d('Path has slash in directory, bailing');
return exe;
}
let target = path.join('.', exe);
if (statSyncNoException(target)) {
d(`Found executable in currect directory: ${target}`);
return target;
}
let haystack = process.env.PATH.split(isWindows ? ';' : ':');
for (let p of haystack) {
let needle = path.join(p, exe);
if (statSyncNoException(needle)) return needle;
}
d('Failed to find executable anywhere in path');
return exe;
}
/**
* Finds the actual executable and parameters to run on Windows. This method
* mimics the POSIX behavior of being able to run scripts as executables by
* replacing the passed-in executable with the script runner, for PowerShell,
* CMD, and node scripts.
*
* This method also does the work of running down PATH, which spawn on Windows
* also doesn't do, unlike on POSIX.
*
* @param {string} exe The executable to run
* @param {Array<string>} args The arguments to run
*
* @return {Object} The cmd and args to run
* @property {string} cmd The command to pass to spawn
* @property {Array<string>} args The arguments to pass to spawn
*/
export function findActualExecutable(exe, args) {
// POSIX can just execute scripts directly, no need for silly goosery
if (process.platform !== 'win32') return { cmd: exe, args: args };
if (!sfs.existsSync(exe)) {
// NB: When you write something like `surf-client ... -- surf-build` on Windows,
// a shell would normally convert that to surf-build.cmd, but since it's passed
// in as an argument, it doesn't happen
const possibleExts = ['.exe', '.bat', '.cmd', '.ps1'];
for (let ext of possibleExts) {
let possibleFullPath = runDownPath(`${exe}${ext}`);
if (sfs.existsSync(possibleFullPath)) {
return findActualExecutable(possibleFullPath, args);
}
}
}
if (exe.match(/\.ps1$/i)) {
let cmd = path.join(process.env.SYSTEMROOT, 'System32', 'WindowsPowerShell', 'v1.0', 'PowerShell.exe');
let psargs = ['-ExecutionPolicy', 'Unrestricted', '-NoLogo', '-NonInteractive', '-File', exe];
return { cmd: cmd, args: psargs.concat(args) };
}
if (exe.match(/\.(bat|cmd)$/i)) {
let cmd = path.join(process.env.SYSTEMROOT, 'System32', 'cmd.exe');
let cmdArgs = ['/C', exe];
return { cmd: cmd, args: cmdArgs.concat(args) };
}
if (exe.match(/\.(js)$/i)) {
let cmd = process.execPath;
let nodeArgs = [exe];
return { cmd: cmd, args: nodeArgs.concat(args) };
}
// Dunno lol
return { cmd: exe, args: args };
}
/**
* Spawns a process but detached from the current process. The process is put
* into its own Process Group that can be killed by unsubscribing from the
* return Observable.
*
* @param {string} exe The executable to run
* @param {Array<string>} params The parameters to pass to the child
* @param {Object} opts Options to pass to spawn.
*
* @return {Observable<string>} Returns an Observable that when subscribed
* to, will create a detached process. The
* process output will be streamed to this
* Observable, and if unsubscribed from, the
* process will be terminated early. If the
* process terminates with a non-zero value,
* the Observable will terminate with onError.
*/
export function spawnDetached(exe, params, opts=null) {
if (!isWindows) return spawn(exe, params, _.assign({}, opts || {}, {detached: true }));
const newParams = [exe].concat(params);
let target = path.join(__dirname, '..', 'vendor', 'jobber', 'jobber.exe');
let options = _.assign({}, opts || {}, { detached: true, jobber: true });
d(`spawnDetached: ${target}, ${newParams}`);
return spawn(target, newParams, options);
}
/**
* Spawns a process attached as a child of the current process.
*
* @param {string} exe The executable to run
* @param {Array<string>} params The parameters to pass to the child
* @param {Object} opts Options to pass to spawn.
*
* @return {Observable<string>} Returns an Observable that when subscribed
* to, will create a child process. The
* process output will be streamed to this
* Observable, and if unsubscribed from, the
* process will be terminated early. If the
* process terminates with a non-zero value,
* the Observable will terminate with onError.
*/
export function spawn(exe, params=[], opts=null) {
let spawnObs = Observable.create((subj) => {
let proc = null;
let { cmd, args } = findActualExecutable(exe, params);
if (!opts) {
d(`spawning process: ${cmd} ${args.join()}`);
proc = spawnOg(cmd, args);
} else {
d(`spawning process: ${cmd} ${args.join()}, ${JSON.stringify(opts)}`);
proc = spawnOg(cmd, args, _.omit(opts, 'jobber'));
}
let bufHandler = (b) => {
if (b.length < 1) return;
let chunk = "<< String sent back was too long >>";
try {
chunk = b.toString();
} catch (e) {
chunk = `<< Lost chunk of process output for ${exe} - length was ${b.length}>>`;
}
subj.onNext(chunk);
};
let stderrCompleted = null;
let stdoutCompleted = null;
let noClose = false;
if (proc.stdout) {
stdoutCompleted = new AsyncSubject();
proc.stdout.on('data', bufHandler);
proc.stdout.on('close', () => { stdoutCompleted.onNext(true); stdoutCompleted.onCompleted(); });
} else {
stdoutCompleted = Observable.just(true);
}
if (proc.stderr) {
stderrCompleted = new AsyncSubject();
proc.stderr.on('data', bufHandler);
proc.stderr.on('close', () => { stderrCompleted.onNext(true); stderrCompleted.onCompleted(); });
} else {
stderrCompleted = Observable.just(true);
}
proc.stderr.on('data', bufHandler);
proc.on('error', (e) => {
noClose = true;
subj.onError(e);
});
proc.on('close', (code) => {
noClose = true;
let pipesClosed = Observable.merge(stdoutCompleted, stderrCompleted)
.reduce((acc) => acc, true);
if (code === 0) {
pipesClosed.subscribe(() => subj.onCompleted());
} else {
pipesClosed.subscribe(() => subj.onError(new Error(`Failed with exit code: ${code}`)));
}
});
return Disposable.create(() => {
if (noClose) return;
d(`Killing process: ${cmd} ${args.join()}`);
if (!opts.jobber) {
proc.kill();
return;
}
// NB: Connecting to Jobber's named pipe will kill it
net.connect(`\\\\.\\pipe\\jobber-${proc.pid}`);
setTimeout(() => proc.kill(), 5*1000);
});
});
return spawnObs.publish().refCount();
}
/**
* Spawns a process but detached from the current process. The process is put
* into its own Process Group.
*
* @param {string} exe The executable to run
* @param {Array<string>} params The parameters to pass to the child
* @param {Object} opts Options to pass to spawn.
*
* @return {Promise<string>} Returns an Promise that represents a detached
* process. The value returned is the process
* output. If the process terminates with a
* non-zero value, the Promise will resolve with
* an Error.
*/
export function spawnDetachedPromise(exe, params, opts=null) {
return spawnDetached(exe, params, opts).toPromise();
}
/**
* Spawns a process as a child process.
*
* @param {string} exe The executable to run
* @param {Array<string>} params The parameters to pass to the child
* @param {Object} opts Options to pass to spawn.
*
* @return {Promise<string>} Returns an Promise that represents a child
* process. The value returned is the process
* output. If the process terminates with a
* non-zero value, the Promise will resolve with
* an Error.
*/
export function spawnPromise(exe, params, opts=null) {
return spawn(exe, params, opts).toPromise();
}