# grunt-env

> Specify an ENV configuration for future tasks in the chain

Latest version **1.0.1** (published 2019-07-25) · 0 weekly downloads

## Install

```sh
npm install grunt-env
pnpm add grunt-env
yarn add grunt-env
bun add grunt-env
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-07-25 |
| First published | 2012-10-24 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 2 |
| Unpacked size | 14.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 224 |
| Author | Jarrod Overson |
| Maintainers | jsoverson, stephane.bachelier |
| Keywords | gruntplugin, env, config |

## Links

- npm: https://www.npmjs.com/package/grunt-env
- Repository: https://github.com/jsoverson/grunt-env
- Issues: https://github.com/jsoverson/grunt-env/issues
- npm.io page: https://npm.io/package/grunt-env

## Dependencies (2)

- [ini](https://npm.io/package/ini.md) ^1.3.5
- [lodash](https://npm.io/package/lodash.md) ^4.17.14

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-07-25
- 1.0.0 — 2019-07-16
- 0.4.4 — 2015-02-20
- 0.4.3 — 2015-02-20
- 0.4.2 — 2014-10-02
- 0.4.1 — 2013-12-14
- 0.4.0 — 2013-04-20
- 0.3.0 — 2013-04-20
- 0.2.1 — 2013-02-18
- 0.2.0 — 2013-02-18
- 0.1.0 — 2012-10-24

## README

# grunt-env [![Build Status](https://secure.travis-ci.org/jsoverson/grunt-env.png?branch=master)](http://travis-ci.org/onehealth/grunt-env)

Specify an ENV configuration as a task, e.g.

```
grunt.registerTask('dev', ['env:dev', 'lint', 'server', 'watch']);
grunt.registerTask('build', ['env:build', 'lint', 'other:build:tasks']);
```

## Getting Started
Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-env`

Then add this line to your project's `grunt.js` gruntfile:

```javascript
grunt.loadNpmTasks('grunt-env');
```

[grunt]: http://gruntjs.com/
[getting_started]: https://github.com/gruntjs/grunt/blob/master/docs/getting_started.md

## Configuration

```js
  env : {
    options : {
 	//Shared Options Hash
    },
    dev : {
      NODE_ENV : 'development',
      DEST     : 'temp'
    },
    build : {
      NODE_ENV : 'production',
      DEST     : 'dist',
      concat   : {
        PATH     : {
          'value': 'node_modules/.bin',
          'delimiter': ':'
        }
      }
    },
    functions: {
      BY_FUNCTION: function() {
        var value = '123';
        grunt.log.writeln('setting BY_FUNCTION to ' + value);
        return value;
      }
    }
  }
```
## Using external files

You can specify environment values in INI, JSON or YAML style and load them via the src option.

```js
  env : {
    dev : {
      src : "dev.json"
    },
    prod: {
      src: "settings.yaml"
    }
    heroku : {
      src : ".env"
    }
  }
```

## Using envdir

You can specify files to read environment variables from, similar to the daemontools [envdir](http://cr.yp.to/daemontools/envdir.html) utility.

```js
  env : {
    dev : {
      src : ["envdir/*"],
      options: {
        envdir: true
      }
    }
  }
```

## Dynamic ENV configuration

The following directives can be specified in the `options` to alter the environment in more specific ways

- `add`
  - This will add the variables *only* if they don't already exist
- `replace`
  - Will replace the variable with the value specified
- `unshift`
  - Will prepend the value to the variable specified, optionally specifying a 'delimiter'
- `push`
  - Same as unshift, but at the end of the value.
- `concat`
  - Functionally same as `push`, added for readability

```
yourtask : {
  USER : 'you',
  PATH : '/bin:/usr/bin'

  options : {
    add : {
      VERBOSE : '1' // will only be added if VERBOSE isn't already set
    },
    replace : {
      USER : 'me'
    },
    push : {
      PATH : {
        value : '~/bin',
        delimiter : ':'
      }
    },
    unshift : {
      PATH : '/sbin:'
    }
  }
}
```

## Environment-specific configuration

In order to configure your tasks based on the environment, you need to define a task and use templates:

```

grunt.initConfig({
  env: {
    dev: {
      MY_CONST: 'a'
    },
    prod: {
      MY_CONST: 'b'
    }
  },
  myTask: {
    options: {
      myOpt: <%= MY_CONST %>
    }
  }
});

grunt.registerTask('loadconst', 'Load constants', function() {
    grunt.config('MY_CONST', process.env.MY_CONST);
});

grunt.registerTask('default', [
    'env:dev',
    'loadconst',
    'myTask'
]);

```

## Important note on data types

Environment variables are strings only. If you attempt to assign complex objects, they will be converted to strings.

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt][grunt].

## Release History

- 0.4.0 Removed automatic parse, added ability to add ini or json style `src` files
- 0.3.0 Automatically parses .env files now
- 0.2.1 fixed npm install
- 0.2.0 grunt 0.4.0 support, simplified
- 0.1.0 Initial release

## License

Licensed under the Apache 2.0 license.

## Author

Jarrod Overson

---
_Source: https://npm.io/package/grunt-env · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
