Contains methods to safely spawn and manage various file system processes. It differs from the standard node.js child_process (https://nodejs.org/docs/v0.11.13/api/child_process.html#child_process_child_process) module in that it intercepts and handles many common errors that might occur when invoking child processes that could cause an application to crash. Most commonly, errors such as ENOENT and EACCESS. This enables an application to be both cleaner and more robust.

safeps()

Open a file. Pass your callback to fire when it is safe to open the process

openProcess(fn: Function): this
Parameters
fn (Function) callback
Returns
this:

Returns whether or not we are running on a windows machine

isWindows(): Boolean
Returns
Boolean:

getLocaleCode

./source/index.js

Get locale code - eg: en-AU, fr-FR, zh-CN etc.

getLocaleCode(lang: String): String
Parameters
lang (String)
Returns
String:

getLanguageCode

./source/index.js

Given the localeCode, return the language code.

getLanguageCode(localeCode: String): String
Parameters
localeCode (String)
Returns
String:

getCountryCode

./source/index.js

Given the localeCode, return the country code.

getCountryCode(localeCode: String): String
Parameters
localeCode (String)
Returns
String:

hasSpawnSync

./source/index.js

Has spawn sync. Returns true if the child_process spawnSync method exists, otherwise false

hasSpawnSync(): Boolean
Returns
Boolean:

Has exec sync. Returns true if the child_process execSync method exists, otherwise false

hasExecSync(): Boolean
Returns
Boolean:

isExecutableSync

./source/index.js

Is the path to a file object an executable? Synchronised version of isExecutable

isExecutableSync(path: String, opts: Object, next: [Function]): Boolean
Parameters
path (String) path to test
opts (Object)
next ([Function])
Name Description
next.err Error
next.isExecutable Boolean
Returns
Boolean:

isExecutable

./source/index.js

Is the path to a file object an executable? Boolean result returned as the isExecutable parameter of the passed callback.

isExecutable(path: String, opts: [Object], next: Function): Boolean
Parameters
path (String) path to test
opts ([Object])
Name Description
opts.sync [Boolean] true to test sync rather than async
next (Function) callback
Name Description
next.err Error
next.isExecutable Boolean
Returns
Boolean: returned if opts.sync = true

Syncronised version of safeps.spawn. Will not return until the child process has fully closed. Results can be returned from the method call or via a passed callback. Even if a callback is passed to spawnSync, the method will still be syncronised with the child process and the callback will only return after the child process has closed.

Simple usage example:

var safeps = require('safeps'); var command = ['npm', 'install', 'jade', '--save'];

//a lot of the time you won't need the opts argument var opts = { cwd: __dirname //this is actually pointless in a real application };

var result = safeps.spawnSync(command, opts);

console.log(result.error); console.log(result.status); console.log(result.signal); console.log("I've finished...");

spawnSync(command: (Array | String), next.signal: String, opts.safe: [Boolean], opts.cwd: [String], opts.stdio: [(Array | String)], opts.customFds: [Array], opts.env: [Object], opts.detached: [Boolean], opts: [Object], next: [Function]): Object
Parameters
command ((Array | String))
next.signal (String) unix style signal such as SIGKILL or SIGHUP
opts.safe ([Boolean]) Whether to check the executable path.
opts.cwd ([String]) Current working directory of the child process
opts.stdio ([(Array | String)]) Child's stdio configuration.
opts.customFds ([Array]) Deprecated File descriptors for the child to use for stdio.
opts.env ([Object]) Environment key-value pairs.
opts.detached ([Boolean]) The child will be a process group leader.
opts ([Object])
Name Description
opts.gid [Number] Sets the group identity of the process
opts.uid [Number] Sets the user identity of the process.
next ([Function]) callback
Name Description
next.error Error
next.stdout Stream out stream
next.stderr Stream error stream
next.status Number node.js exit code
Returns
Object: {error, pid, output, stdout, stderr, status, signal}

Wrapper around node's spawn command for a cleaner, more robust and powerful API. Launches a new process with the given command. Command line arguments are part of the command parameter (unlike the node.js spawn). Command can be an array of command line arguments or a command line string. Opts allows additional options to be sent to the spawning action.

Simple usage example:

var safeps = require('safeps');

var command = ['npm', 'install','jade','--save'];

//a lot of the time you won't need the opts argument var opts = { cwd: __dirname //this is actually pointless in a real application } function myCallback(error, stdout, stderr, status, signal){ console.log(error); console.log(status); console.log(signal); console.log("I've finished..."); } safeps.spawn(command, opts, myCallback);

spawn(command: (Array | String), next.signal: String, opts.safe: [Boolean], opts.cwd: [String], opts.stdio: [(Array | String)], opts.customFds: [Array], opts.env: [Object], opts.detached: [Boolean], opts: [Object], next: Function): this
Parameters
command ((Array | String))
next.signal (String) unix style signal such as SIGKILL or SIGHUP
opts.safe ([Boolean]) Whether to check the executable path.
opts.cwd ([String]) Current working directory of the child process
opts.stdio ([(Array | String)]) Child's stdio configuration.
opts.customFds ([Array]) Deprecated File descriptors for the child to use for stdio.
opts.env ([Object]) Environment key-value pairs.
opts.detached ([Boolean]) The child will be a process group leader.
opts ([Object])
Name Description
opts.gid [Number] Sets the group identity of the process.
opts.uid [Number] Sets the user identity of the process.
next (Function) callback
Name Description
next.error Error
next.stdout Stream out stream
next.stderr Stream error stream
next.status Number node.js exit code
Returns
this:

spawnMultiple

./source/index.js

Spawn multiple processes in the one method call. Launches new processes with the given array of commands. Each item in the commands array represents a command parameter sent to the safeps.spawn method, so each item can be a command line string or an array of command line inputs. It is also possible to pass a single command string and in this case calling spawnMultiple will be effectively the same as calling safeps.spawn.

spawnMultiple(commands: (Array | String), next.results: String, opts.concurrency: [Boolean], opts.cwd: String, opts.stdio: (Array | String), opts.customFds: Array, opts.env: Object, opts.detached: Boolean, opts: [Object], next: Function): this
Parameters
commands ((Array | String))
next.results (String) [i] .signal unix style signal such as SIGKILL or SIGHUP
opts.concurrency ([Boolean] (default 1) ) Whether to spawn processes concurrently.
opts.cwd (String) Current working directory of the child process.
opts.stdio ((Array | String)) Child's stdio configuration.
opts.customFds (Array) Deprecated File descriptors for the child to use for stdio.
opts.env (Object) Environment key-value pairs.
opts.detached (Boolean) The child will be a process group leader.
opts ([Object])
Name Description
opts.gid Number Sets the group identity of the process.
opts.uid Number Sets the user identity of the process.
next (Function) callback
Name Description
next.error Error
next.results Array array of spawn results
next.results Stream [i] .stdout out stream
next.results Stream [i] .stderr error stream
next.results Number [i] .status node.js exit code
Returns
this:

Syncronised version of safeps.exec. Runs a command in a shell and buffers the output. Will not return until the child process has fully closed. Results can be returned from the method call or via a passed callback. Even if a callback is passed to execSync, the method will still be syncronised with the child process and the callback will only return after the child process has closed. Note: Stdout and stderr should be Buffers but they are strings unless encoding:null for now, nothing we should do, besides wait for joyent to reply https://github.com/joyent/node/issues/5833#issuecomment-82189525.

execSync(command: Object, next.stderr: (Buffer | String), opts.sync: [Boolean], opts.cwd: [String], opts.env: [Object], opts.encoding: [String], opts.shell: [String], opts.timeout: [Number], opts: [Object], next: Function): Object
Parameters
command (Object)
next.stderr ((Buffer | String)) error buffer
opts.sync ([Boolean]) true to execute sync rather than async
opts.cwd ([String]) Current working directory of the child process
opts.env ([Object]) Environment key-value pairs
opts.encoding ([String] (default 'utf8') )
opts.shell ([String]) Shell to execute the command with (Default: '/bin/sh' on UNIX, 'cmd.exe' on Windows, The shell should understand the -c switch on UNIX or /s /c on Windows. On Windows, command line parsing should be compatible with cmd.exe.)
opts.timeout ([Number] (default 0) )
opts ([Object])
Name Description
opts.killSignal [String] (default 'SIGTERM')
opts.uid [Number] Sets the user identity of the process.
opts.gid [Number] Sets the group identity of the process.
opts.maxBuffer [Number] (default 200*1024) Largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed.
next (Function)
Name Description
next.err Error
next.stdout (Buffer | String) out buffer
Returns
Object: {error, stdout, stderr}

Wrapper around node's exec command for a cleaner, more robust and powerful API. Runs a command in a shell and buffers the output. Note: Stdout and stderr should be Buffers but they are strings unless encoding:null for now, nothing we should do, besides wait for joyent to reply https://github.com/joyent/node/issues/5833#issuecomment-82189525.

exec(command: Object, next.stderr: (Buffer | String), opts.sync: [Boolean], opts.cwd: [String], opts.env: [Object], opts.encoding: [String], opts.shell: [String], opts.timeout: [Number], opts: [Object], next: Function): this
Parameters
command (Object)
next.stderr ((Buffer | String)) error buffer
opts.sync ([Boolean]) true to execute sync rather than async
opts.cwd ([String]) Current working directory of the child process
opts.env ([Object]) Environment key-value pairs
opts.encoding ([String] (default 'utf8') )
opts.shell ([String]) Shell to execute the command with (Default: '/bin/sh' on UNIX, 'cmd.exe' on Windows, The shell should understand the -c switch on UNIX or /s /c on Windows. On Windows, command line parsing should be compatible with cmd.exe.)
opts.timeout ([Number] (default 0) )
opts ([Object])
Name Description
opts.killSignal [String] (default 'SIGTERM')
opts.uid [Number] Sets the user identity of the process.
opts.gid [Number] Sets the group identity of the process.
opts.maxBuffer [Number] (default 200*1024) Largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed.
next (Function)
Name Description
next.err Error
next.stdout (Buffer | String) out buffer
Returns
this:

execMultiple

./source/index.js

Exec multiple processes in the one method call. Launches new processes with the given array of commands. Each item in the commands array represents a command parameter sent to the safeps.exec method, so each item can be a command line string or an array of command line inputs. It is also possible to pass a single command string and in this case calling execMultiple will be effectively the same as calling safeps.exec.

execMultiple(commands: (Array | String), next.results: Stream, opts.concurrency: [Boolean], opts.cwd: String, opts.stdio: (Array | String), opts.customFds: Array, opts.env: Object, opts: [Object], next: Function): this
Parameters
commands ((Array | String))
next.results (Stream) [i] .stderr error buffer
opts.concurrency ([Boolean] (default 1) ) Whether to exec processes concurrently.
opts.cwd (String) Current working directory of the child process.
opts.stdio ((Array | String)) Child's stdio configuration.
opts.customFds (Array) Deprecated File descriptors for the child to use for stdio.
opts.env (Object) Environment key-value pairs.
opts ([Object])
Name Description
opts.uid Number Sets the user identity of the process.
opts.gid Number Sets the group identity of the process.
opts.detached Boolean The child will be a process group leader.
next (Function) callback
Name Description
next.error Error
next.results Array array of exec results
next.results Stream [i] .stdout out buffer
Returns
this:

getEnvironmentPaths

./source/index.js

Get the system's environment paths.

getEnvironmentPaths(): Array
Returns
Array: string array of file paths

Given an executable name, search and find its actual path. Will search the standard file paths defined by the environment to see if the executable is in any of those paths.

getExecPath(execName: Object, opts: [Object], next: Function): this
Parameters
execName (Object)
opts ([Object])
Name Description
opts.cache [Boolean] (default true)
next (Function)
Name Description
next.err Error
next.foundPath String path to the executable
Returns
this:

Get home path. Returns the user's home directory. Based upon home function from: https://github.com/isaacs/osenv

getHomePath(opts: [Object], next: Function): this
Parameters
opts ([Object])
Name Description
opts.cache [Object] (default true)
next (Function)
Name Description
next.err Error
next.homePath String
Returns
this:

Path to the evironment's temporary directory. Based upon tmpdir function from: https://github.com/isaacs/osenv

getTmpPath(opts: [Object], next: Function): this
Parameters
opts ([Object])
Name Description
opts.cache [Object] (default true)
next (Function)
Name Description
next.err Error
next.tmpPath String
Returns
this:

Path to the evironment's GIT directory. As 'git' is not always available in the environment path, we should check common path locations and if we find one that works, then we should use it.

getGitPath(opts: [Object], next: Function): this
Parameters
opts ([Object])
Name Description
opts.cache [Object] (default true)
next (Function)
Name Description
next.err Error
next.gitPath String
Returns
this:

Path to the evironment's Node directory. As 'node' is not always available in the environment path, we should check common path locations and if we find one that works, then we should use it

getNodePath(opts: [Object], next: Function): this
Parameters
opts ([Object])
Name Description
opts.cache [Object] (default true)
next (Function)
Name Description
next.err Error
next.nodePath String
Returns
this:

Path to the evironment's NPM directory. As 'npm' is not always available in the environment path, we should check common path locations and if we find one that works, then we should use it

getNpmPath(opts: [Object], next: Function): this
Parameters
opts ([Object])
Name Description
opts.cache [Object] (default true)
next (Function)
Name Description
next.err Error
next.npmPath String
Returns
this:

Initialize a git Repository. Requires internet access.

initGitRepo(opts: Object, next.results: String, next: Function): this
Parameters
opts (Object)
Name Description
opts.remote [String] (default 'origin')
opts.url String url to git remote repository
opts.branch [String] (default 'master')
opts.log String
opts.output String
opts.path String path to initiate local repository
opts.cwd [String] (default process.cwd()) Current working directory of the child process.
next.results (String) [i] .signal unix style signal such as SIGKILL or SIGHUP
next (Function)
Name Description
next.err Error
next.results Array array of spawn results
next.results Stream [i] .stdout out stream
next.results Stream [i] .stderr error stream
next.results Number [i] .status node.js exit code
Returns
this:

initOrPullGitRepo

./source/index.js

Pull from a git repository if it already exists on the file system else initialize new Git repository. Requires internet access.

initOrPullGitRepo(opts: Object, next.results: String, next: Function): this
Parameters
opts (Object)
Name Description
opts.remote [String] (default 'origin')
opts.url String url to git remote repository
opts.branch [String] (default 'master')
opts.log String
opts.output String
opts.path String path to local repository
opts.cwd [String] (default process.cwd()) Current working directory of the child process.
next.results (String) [i] .signal unix style signal such as SIGKILL or SIGHUP
next (Function)
Name Description
next.err Error
next.results Array array of spawn results
next.results Stream [i] .stdout out stream
next.results Stream [i] .stderr error stream
next.results Number [i] .status node.js exit code
Returns
this:

initNodeModules

./source/index.js

Init Node Modules with cross platform support supports linux, heroku, osx, windows

initNodeModules(opts: Object, next: Function): this
Parameters
opts (Object)
next (Function)
Name Description
next.err Object
next.stdout Stream out stream
next.stderr Stream error stream
next.status Number node.js exit code
next.signal String unix style signal such as SIGKILL or SIGHUP
Returns
this:

spawnNodeModule

./source/index.js

Spawn Node Modules with cross platform support supports linux, heroku, osx, windows spawnNodeModule(name:string, args:array, opts:object, next:function) Better than https://github.com/mafintosh/npm-execspawn as it uses safeps

spawnNodeModule(name: String, next.signal: String, opts: Object, args: Array, next.err: Object, next.stdout: Stream, next.stderr: Stream, next.status: Number, next: Function): this
Parameters
name (String)
next.signal (String) unix style signal such as SIGKILL or SIGHUP
opts (Object)
Name Description
opts.name String name of node module
opts.cwd [String] (default process.cwd()) Current working directory of the child process.
args (Array)
next.err (Object)
next.stdout (Stream) out stream
next.stderr (Stream) error stream
next.status (Number) node.js exit code
next (Function)
Returns
this: