Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rodrigoespinosa/lookenv

Set rules for the environment variables in your project. Works great with dotenv
https://github.com/rodrigoespinosa/lookenv

dotenv javascript lookenv

Last synced: 3 months ago
JSON representation

Set rules for the environment variables in your project. Works great with dotenv

Awesome Lists containing this project

README

        

# lookenv

lookenv

> Set rules for the environment variables in your project.

[![NPM Version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Downloads Stats][npm-downloads]][npm-url]

`lookenv` can check if all the variables you need are present before starting your app. It also can set defaults for those variables that are not present. Works fine with `dotenv` or `dotenv-safe`.

## Installation

```sh
npm install lookenv --save

# Or with yarn
yarn add lookenv
```

## Usage example

Create a `lookenv.config.js` file, or `.lookenvrc` that exposes a JSON like the following:

```js
module.exports = {
MY_ENV_VAR: {
required: true
},
MY_SECOND_ENV_VAR: {
default: 'testing'
}
}
```

Then, add `lookenv` to the `package.json` start script, before the app starts but after dotenv (if you are using it!).

```json
{
"start": "lookenv -- node index.js"
}
```

You can also specify a path to the config file, or the directory where the config file by passing `--path` or `-p`.

```json
{
"start": "lookenv --path=lookenv.config.js -- node index.js"
}
```

### With `dotenv`

You can pass a `--dotenv` (or `-d` for short) to the cli to load `dotenv` before
validating the env vars.

```json
"start": "lookenv --dotenv -- node index.js"
```

_You can optionally pass the location of your `.env` in the `--dotenv` option,
like `lookenv --dotenv=/path/to/custom/env -- node index.js`._

### With [`Joi`](https://github.com/hapijs/joi)

_Joi, the object schema description language and validator for JavaScript objects._

Lookenv recognizes and supports Joi schemas from the config files. In order to do so, please remember to install (`npm install --save joi`) in your project. And then, export the Joi schema in your `lookenv.config.js` file.

```js
const Joi = require('joi');

module.exports = Joi.object().keys({
A_NUMBER: Joi.number().required(),
A_STRING: Joi.string().required(),
AN_OBJECT: Joi.string().required(),

A_PORT: Joi.number()
.positive()
.default(3000),

A_NUMBER_WITH_DEFAULTS: Joi.number().default(7),
A_STRING_WITH_DEFAULTS: Joi.string().default('seven')
})
```

This means that you can use the entire [Joi Schema API](https://github.com/hapijs/joi/blob/v13.0.2/API.md) to validate your env vars.

### Using it just for setting defaults

Everything would be the same, but you can use the simplified `lookenv.config.js` (or `.lookenvrc`) json that matches every key with a default.

```json
{
"MY_ENV_VAR": "my-default",
"MY_2ND_ENV_VAR": "other-default"
}
```

You can also combine them!

```json
{
"MY_ENV_VAR": "my-default",
"MY_2ND_ENV_VAR": {
"required": true
}
}
```

## API

**`lookenv.config({ path })`**
This method will only call the config and return the set of rules, it won't do any validation.

**`lookenv.validate({ path, context })`**
This method will get the config for the `lookenv.config.js` (or `.lookenvrc`) from the current working directory (using `process.cwd()`), unless you specify a `path` to the config file in question.

After that, it will validate the `context` (that is `process.env` as default) and apply all the defaults.

If there is a required variable that isn't present, it will throw an error specifying the missing variables.

## Programmatic use

```js
const lookenv = require('lookenv')

lookenv.validate()
.then(() => {
// ... your app goes here, basically...
})
.catch(error => {
console.error(error)
process.exit(1)
})
```

_Remember that `lookenv.validate` is async._

## Development setup

This project use `ava` to run tests. Just [fork it](https://github.com/RodrigoEspinosa/lookenv/fork).

```sh
npm test

# Or with yarn
yarn test
```

## Release History

See [`CHANGELOG.md`](https://github.com/RodrigoEspinosa/lookenv/blob/master/CHANGELOG.md)

## Meta

REC – [@reciam](https://twitter.com/reciam) – [email protected]

Distributed under the MIT license. See ``LICENSE`` for more information.

[https://github.com/RodrigoEspinosa/lookenv](https://github.com/RodrigoEspinosa/lookenv)

**Credits of the logo goes to [@guillecura](http://guillecura.co/).**

## Contributing

1. Fork it ()
2. Create your feature branch (`git checkout -b feature/foo-bar`)
3. Commit your changes (`git commit -am 'Add some foo and bar'`)
4. Push to the branch (`git push origin feature/foo-bar`)
5. Create a new Pull Request

[npm-image]: https://img.shields.io/npm/v/lookenv.svg?style=flat-square
[npm-url]: https://npmjs.org/package/lookenv
[npm-downloads]: https://img.shields.io/npm/dm/lookenv.svg?style=flat-square
[travis-image]: https://img.shields.io/travis/RodrigoEspinosa/lookenv/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/RodrigoEspinosa/lookenv