Sub process encapsulation for different exec technics.
npm install @universal-packages/sub-process
SubProcess is the main interface to setup a process to run at whenever time and only once.
import { SubProcess } from '@universal-packages/sub-process'
const subProcess = new SubProcess({ command: 'echo', args: ['$VARIABLE'], env: { VARIABLE: 'value' } })
await subProcess.run()
console.log(subProcess.stdout.toString())
-
args
string[]
Arguments to pass to the command. -
command
string
Command to run. -
engine
Engine | 'spawn' | 'exec' | 'fork' | 'test'
default: spawn
Instance of the engine to be used to execute the process or a string identifying the engine adapter. -
engineOptions
Object
Options to pass to the engine if resolved as adapter. -
env
Object
Environment variables to pass to the process. -
input
string | Buffer | string[] | Buffer[] | Readable
Input to pass to the process. For example when a process requires any kind of input like a yes/no question. -
timeout
number
Time to wait in milliseconds before killing the process. -
workingDirectory
string
Working directory to run the process in.
Initialize the internal engine in case it needs preparation.
Releases the engine resources in case they need to be disposed after finishing the process.
Runs the process and waits for it to finish.
Kills the process if it is running.
Skips the process if it has not been started yet to mark it as skipped and do not let it run.
Waits for the process to reach a specific status or an status in the same level, for example success
or failure
will wait for the process to finish with any of those statuses.
Buffer containing the stdout of the process.
Buffer containing the stderr of the process.
Exit code of the process.
Signal that killed the process.
Process id of the process.
Status of the process.
SubProcess
will emit events regarding execution status and output.
subProcess.on('*', (event) => console.log(event))
subProcess.on('running', (event) => console.log(event))
subProcess.on('stdout', (event) => console.log(event))
subProcess.on('stderr', (event) => console.log(event))
subProcess.on('success', (event) => console.log(event))
subProcess.on('failure', (event) => console.log(event))
subProcess.on('stopping', (event) => console.log(event))
subProcess.on('stopped', (event) => console.log(event))
subProcess.on('skipped', (event) => console.log(event))
subProcess.on('end', (event) => console.log(event))
subProcess.on('timeout', (event) => console.log(event))
subProcess.on('error', (event) => console.log(event))
subProcess.on('warning', (event) => console.log(event))
To create an engine that suits your requirements you just need to implement new classes and use them as the following:
import MyEngine from './MyEngine'
const subProcess = new SubProcess({ engine: new MyEngine() })
You need to implement a engine process representation by subclassify the EngineProcess
class to provide a way to kill yur custom process.
import { EngineProcess } from '@universal-packages/sub-process'
export default class MyEngineProcess extends EngineProcess {
killObject(signal) {
this.object.sendKillSignal(signal)
}
}
The run method of the engine will be called with the command, args, input and env to execute the process and return an EngineProcess
instance.
export default class MyEngine {
constructor(options) {
// Options passed through the adapters sub system
}
prepare() {
// Initialize any connection using options
}
release() {
// Release any resources or close any connection
}
run(command, args, input, env) {
const myExecutableObject = myExecutionMethod.exec(command, args, input, env)
const engineProcess = new MyEngineProcess(myExecutableObject.processId, myExecutableObject)
// Now the SubProcess instance knows how to kill the process when needed as well as the process id.
return engineProcess
}
}
If you are using TypeScript just implement the EngineInterface
in your class to ensure the right implementation.
import { EngineInterface } from '@universal-packages/sub-process'
export default class MyEngine implements EngineInterface {}
This library is developed in TypeScript and shipped fully typed.
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.