hs.task
ModuleModule for running external processes
Types
This module provides the following types:
Properties
hs.task.sequence
function | null
Run multiple tasks in sequence. Swift-retained storage for the JS implementation.
hs.task.TaskBuilder
function | null
TaskBuilder class. Swift-retained storage for the JS implementation.
Methods
hs.task.create(launchPath, arguments, onTermination, environment, onOutput) -> HSTask
Create a new task
Declaration
hs.task.create(launchPath, arguments, onTermination, environment, onOutput) -> HSTask
Parameters
| Name | Type | Description |
|---|---|---|
| launchPath | string | The full path to the executable to run |
| arguments | string[] | An array of arguments to pass to the executable |
| onTermination optional | ((exitCode: number, exitReason: string, error?: string) => void) | null | Optional callback called when the task terminates with exit code and reason. If the program could not be started, exitReason is "launchFailed", exitCode is -1, and error describes why |
| environment optional | [String: String] | Optional dictionary of environment variables for the task |
| onOutput optional | ((stream: string, data: string) => void) | null | Optional callback called when the task produces output; stream is "stdout" or "stderr" |
Returns
A task object. Call start() to begin execution.
Example
const task = hs.task.create("/usr/bin/env", ["printenv", "PATH"], (code, reason) => {
console.log("exited", code)
})
task.start()
hs.task.runAsync(launchPath, args, options, legacyStreamCallback) -> Promise<object>
Create and run a task asynchronously
Declaration
hs.task.runAsync(launchPath, args, options, legacyStreamCallback) -> Promise<{exitCode: number, stdout: string, stderr: string}>
Parameters
| Name | Type | Description |
|---|---|---|
| launchPath | string | - Full path to the executable |
| args | string[] | - Array of arguments |
| options optional | Object|Function | - Options object or legacy callback |
| legacyStreamCallback optional | Function | - Legacy streaming callback |
Returns
Promise<object>
The task's result. If the task exits with a non-zero status or is killed by a signal, the promise rejects with an Error that also has the result's exitCode, stdout, stderr and reason. If the program could not be started, the promise rejects with an Error whose reason is "launchFailed"
| Field | Type | Description |
|---|---|---|
| exitCode | number | |
| stdout | string | |
| stderr | string |
hs.task.shell(command, options) -> Promise<object>
Run a shell command asynchronously
Declaration
hs.task.shell(command, options) -> Promise<{exitCode: number, stdout: string, stderr: string}>
Parameters
| Name | Type | Description |
|---|---|---|
| command | string | - Shell command to execute |
| options optional | Object | - Options (same as hs.task.runAsync()) |
Returns
Promise<object>
The command's result
| Field | Type | Description |
|---|---|---|
| exitCode | number | |
| stdout | string | |
| stderr | string |
hs.task.parallel(tasks) -> Promise<Array<object>>
Run multiple tasks in parallel
Declaration
hs.task.parallel(tasks) -> Promise<Array<{exitCode: number, stdout: string, stderr: string}>>
Parameters
| Name | Type | Description |
|---|---|---|
| tasks | Array<{path?: string, launchPath?: string, args?: string[], options?: Object}> | - Array of task specifications: [{path, args, options}, ...] |
Returns
Promise<Array<object>>
Array of results
| Field | Type | Description |
|---|---|---|
| exitCode | number | |
| stdout | string | |
| stderr | string |
hs.task.builder(launchPath) -> TaskBuilder
Create a task builder for fluent API
Declaration
hs.task.builder(launchPath) -> TaskBuilder
Parameters
| Name | Type | Description |
|---|---|---|
| launchPath | string | - Full path to the executable |
Returns
A new task builder