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

@delucis/if-env

v1.1.2

Published

Run an npm script if an environment variable matches a pattern

Downloads

2,651

Readme

@delucis/if-env

latest npm release version Build Status Coverage Status Greenkeeper badge npms.io package score

Run an npm script if an environment variable matches a pattern

This is a fork of Eric Clemmons’s if-env. It allows you to match environment variables using wildcards (*) rather than using simple string equality. See Sindre Sorhus’s matcher for details about how the matching works.

Installation

npm install --save @delucis/if-env

Usage

This package installs if-env, a script which lets you easily match environment variable values against a pattern using wildcards. It also includes if-env-cs, which is case sensitive.

If you want to to run an NPM script conditionally, depending on the value of an environment variable, you can use if-env in your package.json. In this example, we only want to run our test script when the SOME_VAR variable starts with new:

"scripts": {
  "test": "if-env SOME_VAR=new* && npm run test-suite"
}

Multiple conditions

If you want several conditions to be met you can pass them all to if-env:

"scripts": {
  "yay": "if-env VAR1=woo* VAR2=*hoo && echo yay"
}

If you want to run a script if either one or another condition is met, you can use the || operator:

"scripts": {
  "moo": "if-env ANIMAL=cow || if-env ANIMAL=bull* && echo moo"
}

If you want to do different things depending on the value of a variable, you can also use the || operator:

"scripts": {
  "joy": "if-env MOOD=happy && echo 😄 || echo 😭"
}

Negating conditions

If you want to do something if a variable does not match a pattern, you have two options. Note the single quotes in the first example; they escape ! for your shell.

"scripts": {
  "ifenv": "if-env MOOD='!happy' && echo 😭",
  "shell": "if-env MOOD=happy || echo 😭"
}

Escaping characters

If you need to have spaces in your pattern, make sure you wrap it in quotes:

"scripts": {
  "spacious": "if-env SENTENCE='I want * spaces.' && echo You get spaces!"
}

Case sensitivity

if-env is case insensitive, which means if-env VAR=foo will match both foo and FOO. If you need to test a variable and case is important, use the if-env-cs command:

"scripts": {
  "case": "if-env-cs WORD=lower && echo nice! || echo STOP SHOUTING!"
}