Table of Contents
System
This module implements an equivalent to the Unix system command. It is
a simplified alternative to the process module for spawning and
getting the result of an external command.
Use require( hop.system ), or ` from "hop:system", or
import { system, systemSync } from "hop:system"` to use it.* import
Functions
system.system( command )
Spawns a process executing command. Command might either be a string
or an array of strings. The result is a promise that resolves if the
execution succeeds, i.e., if its return code is 0, and rejects
otherwise. On resolution, the output the process has generated is the
value of the resolution. On rejection, the value is an object of two
fields:
status: the evaluation result.data: the output (stdout and stderr) of the execution
Strings are converted into array of strings using the following function:
function stringToCmd( cmd ) {
return cmd
.match( /([^ "']+)|"([^"]*)"|'([^']*)'|(?:[ \t]+)/g )
.filter( s => !s.match( /^[ \t]+$/ ) )
.map( s => s[ 0 ] === "'" || s[ 0 ] === '"'
? s.substring( 1, s.length - 1 )
: s );
}That is:
"foo" -> [ 'foo' ]
"foo bar" -> [ 'foo', 'bar' ]
"foo bar gee" -> [ 'foo', 'bar', 'gee' ]
'foo "bar gee" hux' -> [ 'foo', 'bar gee', 'hux' ]system.systemSync( command )
The synchronous, i.e., blocking, version of system.system. It spawns
a process executing command. Command might either be a string or
an array of strings. The result is either:
- a string: the output (stdout and stderr) of the execution, if command succeeds.
- an object with two fields:
status, anddata: if the command fails.
Example
This example shows how to spawn an external process and get the result in a string.
system/system.js
import { systemSync as system } from hop.system;
const res = system("ls -l /tmp");
if (typeof res === "string") {
console.log(res.split("\n"));
} else {
console.log("error", res.status, res.data);
}