API-Version: 1.9

system

Static object for system functions

Source:

Methods

<static> fileTest(path, flags) → {Boolean}

Checks for existence of a file or directory

Parameters:
Name Type Description
path String

Path to a file to check

flags FileTest

Bitmask of FileTest flags

Source:
Returns:

true if any of the flags is set

Type
Boolean

<static> getEnv(name) → {String}

Get the current process id

Parameters:
Name Type Description
name String

The name of the environment variable

Source:
Returns:

The environment variable or null if the variable wasn't found

Type
String

<static> getPid() → {Number}

Gets the process if of the dwb instance

Source:
Returns:

The process id of the dwb instance

Type
Number

<static> mkdir(path, mode) → {Boolean}

Creates a directory and all parent directories

Parameters:
Name Type Description
path Path

Path to create

mode Number

The permissions the directory will get

Source:
Returns:

true if creation was successful or if the diretory already existed

Type
Boolean

<static> spawn(command, options) → {Deferred}

Spawn a process asynchronously

Parameters:
Name Type Argument Description
command String

The command to execute

options Object <optional>
Properties
Name Type Argument Description
onStdout Function <optional>

A callback function that is called when a line from stdout was read. The function takes one parameter, the line that has been read. To get the complete stdout use either onFinished or the Deferred returned from system.spawn.

cacheStdout Boolean <optional>

Whether stdout should be cached. If onStdout is defined stdout will be cached automatically. If it isn't defined and stdout is required in onFinished or in the Deferred's resolve function cacheStdout must be set to true, default: false.

onStderr Function <optional>

A callback function that is called when a line from stderr was read. The function takes one parameter, the line that has been read. To get the complete stderr use either onFinished or the Deferred returned from system.spawn.

cacheStderr Boolean <optional>

Whether stderr should be cached. If onStderr is defined stderr will be cached automatically. If it isn't defined and stderr is required in onFinished or in the Deferred's reject function cacheStderr must be set to true, default: false.

onFinished Function <optional>

A callback that will be called when the child process has terminated. The callback takes one argument, an object that contains stdout if caching of stdout is enabled (see cacheStdout), stderr if caching of stderr is enabled (see cacheStderr) and status, i.e. the return code of the child process.

stdin String <optional>

String that will be piped to stdin of the child process.

environment Object <optional>

Hash of environment variables that will be set in the childs environment

Source:
Returns:

A deferred, it will be resolved if the child exits normally, it will be rejected if the child process exits abnormally. The argument passed to resolve and reject is an Object containing stdout if caching of stdout is enabled (see cacheStdout), stderr if caching of stderr is enabled (see cacheStderr) and status, i.e. the return code of the child process.

Type
Deferred
Example
 
// Simple spawning without using stdout/stderr
system.spawn("foo");
// Using stdout/stderr
system.spawn("foo", {
     cacheStdout : true, 
     cacheStderr : true, 
     onFinished : function(result) {
         io.print("Process terminated with status " + result.status);
         io.print("Stdout is :" + result.stdout);
         io.print("Stderr is :" + result.stderr);
     }
});
// Equivalently using the deferred
system.spawn("foo", { cacheStdout : true, cacheStderr : true }).always(function(result) {
     io.print("Process terminated with status " + result.status);
     io.print("Stdout is :" + result.stdout);
     io.print("Stderr is :" + result.stderr);
});
// Only use stdout if the process terminates successfully
system.spawn("foo", { cacheStdout : true }).done(function(result) {
    io.print(result.stdout);
});
// Only use stderr if the process terminates with an error
system.spawn("foo", { cacheStderr : true }).fail(function(result) {
    io.print(result.stderr);
});
// Using environment variables with automatic caching
system.spawn('sh -c "echo $foo"', {
     onStdout : function(stdout) {
         io.print("stdout: " + stdout);
     },
     environment : { foo : "bar" }
}).then(function(result) { 
     io.print(result.stdout); 
});

<static> spawnSync(command, environ) → {Object}

Spawn a process synchronously. This function should be used with care. The execution is single threaded so longer running processes will block the whole execution

Parameters:
Name Type Argument Description
command String

The command to execute

environ Object <optional>

Object that can be used to add environment variables to the childs environment

Source:
Returns:

An object that contains stdout, stderr and the status code.

Type
Object