env-has

0.0.3 • Public • Published

ENV-HAS

Checks that variable names specified as parameters on the command line are defined in the ENVIRONMENT.

If any of parameters is undefined, null or empty string, process exits with code 1 (Error), otherwise returns with code 0 (Success).

Usage

You can use env-has command in package.json scripts to check presence of certain environment variables. You can do a simple guard that allows you to run your script succesfully only if you specify all environment variables before run.

You can easily block unwanted npm publish with env-has during your npm package development.

Example

If you want to use env-has to block unwanted package publication, you only need to do those things:

  1. Put env-has into devDependencies in package.json:
  "devDependencies": {
    "env-has": "^0.0.1"
  }
  1. Add env-has check for ALLOW_NPM_PUBLISH environment variable into prepublishOnly script in package.json (or any other lifecycle script that is called before npm publish):
  "scripts": {
    "prepublishOnly": "env-has ALLOW_NPM_PUBLISH"
  }

Now if you suddenly use npm publish, it will fail in prepublishOnly with this error:

$ npm publish

> my-package@x.y.z prepublishOnly .
> env-has ALLOW_NPM_PUBLISH

Environment variable "ALLOW_NPM_PUBLISH" is not defined.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-package@x.y.z prepublishOnly: `env-has ALLOW_NPM_PUBLISH`
npm ERR! Exit status 1

If you are ok with your package and want to publish it, call npm publish with arbitrary value in ALLOW_NPM_PUBLISH like this (here we use value yes):

$ ALLOW_NPM_PUBLISH=yes npm publish

This is really usefull if you can't/don't want to use private: true or publishConfig in your package.json.

Package Sidebar

Install

npm i env-has

Weekly Downloads

2

Version

0.0.3

License

MIT

Unpacked Size

2.94 kB

Total Files

3

Last publish

Collaborators

  • tomas.kukol