Table of Contents
System
This module implement a 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"` to use it.* import
Functions
system.system( command )
Spawn process of executing command
. Command might either be a string
or a 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 of executing command
. Command might either be a string or
a array of strings. The result is an object with two fields:
status
: the evaluation result.data
: the output (stdout and stderr) of the execution
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 { status, data } = system( "ls -l /tmp" );
if( status ) {
console.log( "error", status, data );
} else {
console.log( data.split() );
}