npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

haxec

v2.0.1

Published

Wrap a Node.js spawn() or exec() with before/after handlers

Downloads

352,521

Readme

npm version downloads build status greenkeeper badge Language grade: JavaScript

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' );

// run is defined as:
function run(
  wrapModule: string,
  cmdWithArgs: Array< string >,
  options?: Options
): void;

// where options can contain environment variables in the "env" property:
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" ] );
// which is same as
run( wrapModule, [ "file.js", "arg1" ] );
// or as a shebang script
run( wrapModule, [ "./script-name.sh", "arg1" ] );

Example

// main.js
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:

// child.js
module.exports = {
  before( ) {
    console.log( "Child has pid", process.pid );
    // Inject an environment into this process
    process.env.injected = "yes";

    // When this function returns, the main "some-app.js" will run, and it
    // will have { foo: "bar", injected: "yes" } as environment variables.
  },
  after( code ) {
    console.log( "Child exited with code", code );
  },
};