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

npm-failsafe

v1.2.1

Published

Executes a sequence of npm scripts.

Downloads

35,153

Readme

NPM Failsafe

npm version build status Coverage Status npm

The npm-failsafe lets you execute a sequence of NPM scripts and return the correct exit code should any of them fail.

Installation

npm install --save-dev npm-failsafe

Usage

Failsafe is a command line tool. To view the available options, run the following command in your terminal:

npx failsafe --help

Configuration

You can use failsafe to run scripts defined in your package.json file.

Configuring Failsafe in package.json

If your scripts require any arguments, those can be specified upfront in your package.json file.

For example, given the below package.json file:

{
    "scripts": {
        "test": "jest",
        "test:coverage": "npm run test -- --coverage",
        "lint": "eslint 'src/*'",
        "start": "pm2 start server.js --port=3000",
        "ci": "failsafe start lint test:coverage"
    }
}

Running: npm run ci will execute:

  • npm run start
  • npm run lint
  • npm run test:coverage, in that order

It will also return the highest exit code of all the executed scripts.

Configuring Failsafe at runtime

If you need to pass arguments to your scripts at runtime, pass them to failsafe directly, and then Failsafe will pass them to your script as per the configuration in your package.json file.

For example, given the below package.json file:

{
    "scripts": {
        "test:clean": "rimraf reports",
        "test:execute": "cucumber-js",
        "test:report": "serenity-bdd run",
        "test": "failsafe test:clean test:execute [--name,--tags] test:report"
    }
}

Running npm test -- --name="Authentication" will execute:

  • npm run test:clean
  • npm run test:execute -- --name="Authentication", which in turn will execute cucumber-js --name="Authentication"
  • npm run test:report

The same applies to npm test -- --tags="@smoke", which will execute:

  • npm run test:clean
  • npm run test:execute -- --tags="@smoke", which in turn will execute cucumber-js --name="@smoke"
  • npm run test:report

Using wildcards

To help you avoid configuration errors, Failsafe will complain if you try to execute a script that doesn't exist, or use an argument that is not configured for a given script.

However, you can configure Failsafe with a wildcard of [...]. This instructs Failsafe to pass any arguments it receives to the script configured with the wildcard.

For example, given the below package.json file:

{
    "scripts": {
        "test:clean": "rimraf reports",
        "test:execute": "cucumber-js",
        "test:report": "serenity-bdd run",
        "test": "failsafe test:clean test:execute [...] test:report"
    }
}

Running npm test -- --name="Authentication" or npm test -- --tags="@smoke" will instruct Failsafe to pass those arguments to the test:execute script - the receiver of the wildcard.

Motivation

Assume a package.json with the following scripts defined:

"scripts": {
    "preintegration": "bin/start_the_server.sh",
    "integration": "bin/run_some_tests_that_require_the_server.sh",
    "cleanup": "bin/shutdown_the_server.sh"
}

In this example, we want to execute the integration script. The script runs some integration tests against some server, which means that we need to start the server up before the tests and shut it down afterwards.

The server itself is started in the preintegration phase (check out the node docs to learn more about the pre- and post- commands).

The question is: how do we shut it down?

We could add the following test script to our package.json: "test": "integration && cleanup".

The problem with this is that because of how the && operator works, the cleanup script will only get executed when the integration script succeeds. This is no good because we need to shut down the server even if the integration tests fail.

We could try to use the || operator instead, which executes the second script no matter the result of the first one: "test": "integration || cleanup". However, the problem with this approach is that the exit code of the "integration || cleanup" combo will always take the value of 0, incorrectly indicating that the test script has succeeded. This could for example cause a continuous integration server to publish your project even if the tests have failed...

Enter npm-failsafe!

With npm-failsafe you can execute a sequence of arbitrary NPM scripts and return the correct exit code should any of them fail:

"scripts": {
     "preintegration": "bin/start_the_server.sh",
     "integration": "bin/run_some_tests_that_require_the_server.sh",
     "cleanup": "bin/shutdown_the_server.sh",

     "test": "failsafe integration cleanup"
}

Your feedback matters!

Did you find this project useful? Give it a star on github! ★

Found a bug? Raise an issue or submit a pull request.

Have feedback? Let me know on twitter: @JanMolak