haxec
Run a (JavaScript) program with before and after handlers inside the child process. The command can be either a .js
file or a node file.js [...]
command line, or a Node shebang script.
If the wrapped script spawns further sub processes, the wrapping will occur to these as well!
The invoked program will take over standard I/O and become foreground. When the invoked program exits, the parent exits too.
API
Interface
The interface for haxec
is a module exporting a function run
:
const { run } = require( 'haxec' );
function run(
wrapModule: string,
cmdWithArgs: Array< string >,
options?: Options
): void;
interface Options
{
env?: { [ key: string ]: string; };
beforeExit?: ( ) => void | Promise< void >;
}
The wrapModule
is your module (it will make sense to provide an absolut path to it) which haxec
will require()
. This module is supposed to export a before
function and/or a after
function. The module will be injected into the command that is being run, so these functions will be called in that process.
run
returns nothing. At this time, the program shouldn't do much else, as the child will take over terminal I/O and exit the parent when the child exits.
Environment variables can be added to the child, using the env
property in the options object.
To perform any final/cleanup work right before the child has exited, provide a beforeExit
callback. If this returns a promise, it will be awaited before exiting the child (and thereby the parent).
Usage
const path = require( "path" );
const { run } = require( 'haxec' );
const wrapModule = path.join( __dirname, "child.js" );
run( wrapModule, [ "node", "file.js", "arg1" ] );
run( wrapModule, [ "file.js", "arg1" ] );
run( wrapModule, [ "./script-name.sh", "arg1" ] );
Example
const { run } = require( 'haxec' );
run(
path.join( __dirname, "child.js" ),
[ "node", "some-app.js", "arg1" ],
{
env: { foo: "bar" }
}
);
This will run some-app.js
with arg1
as argument. It will inject this file into it and call its hooks before and after some-app.js
is invoked, inside its process:
module.exports = {
before( ) {
console.log( "Child has pid", process.pid );
process.env.injected = "yes";
},
after( code ) {
console.log( "Child exited with code", code );
},
};