1 | [![Travis](https://travis-ci.org/toddbluhm/env-cmd.svg?branch=master)](https://travis-ci.org/toddbluhm/env-cmd)
|
2 | [![Coverage Status](https://coveralls.io/repos/github/toddbluhm/env-cmd/badge.svg?branch=master)](https://coveralls.io/github/toddbluhm/env-cmd?branch=master)
|
3 | [![npm](https://img.shields.io/npm/v/env-cmd.svg?maxAge=86400)](https://www.npmjs.com/package/env-cmd)
|
4 | [![npm](https://img.shields.io/npm/dm/env-cmd.svg?maxAge=86400)](https://www.npmjs.com/package/env-cmd)
|
5 | [![npm](https://img.shields.io/npm/l/env-cmd.svg?maxAge=2592000)](https://www.npmjs.com/package/env-cmd)
|
6 | [![TS-Standard - Typescript Standard Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/toddbluhm/ts-standard)
|
7 | [![Greenkeeper badge](https://badges.greenkeeper.io/toddbluhm/env-cmd.svg)](https://greenkeeper.io/)
|
8 |
|
9 | # env-cmd
|
10 |
|
11 | A simple node program for executing commands using an environment from an env file.
|
12 |
|
13 | ## 💾 Install
|
14 |
|
15 | `npm install env-cmd` or `npm install -g env-cmd`
|
16 |
|
17 | ## ⌨️ Basic Usage
|
18 |
|
19 | **Environment file `./.env`**
|
20 |
|
21 | ```text
|
22 | # This is a comment
|
23 | ENV1=THANKS
|
24 | ENV2=FOR ALL
|
25 | ENV3=THE FISH
|
26 | ```
|
27 |
|
28 | **Package.json**
|
29 |
|
30 | ```json
|
31 | {
|
32 | "scripts": {
|
33 | "test": "env-cmd mocha -R spec"
|
34 | }
|
35 | }
|
36 | ```
|
37 |
|
38 | **Terminal**
|
39 |
|
40 | ```sh
|
41 | ./node_modules/.bin/env-cmd node index.js
|
42 | ```
|
43 |
|
44 | ### Using custom env file path
|
45 |
|
46 | To use a custom env filename or path, pass the `-f` flag. This is a major breaking change from prior versions < 9.0.0
|
47 |
|
48 | **Terminal**
|
49 |
|
50 | ```sh
|
51 | ./node_modules/.bin/env-cmd -f ./custom/path/.env node index.js
|
52 | ```
|
53 |
|
54 | ## 📜 Help
|
55 |
|
56 | ```text
|
57 | Usage: _ [options] <command> [...args]
|
58 |
|
59 | Options:
|
60 | -v, --version output the version number
|
61 | -e, --environments [env1,env2,...] The rc file environment(s) to use
|
62 | -f, --file [path] Custom env file path (default path: ./.env)
|
63 | --fallback Fallback to default env file path, if custom env file path not found
|
64 | --no-override Do not override existing environment variables
|
65 | -r, --rc-file [path] Custom rc file path (default path: ./.env-cmdrc(|.js|.json)
|
66 | --silent Ignore any env-cmd errors and only fail on executed program failure.
|
67 | --use-shell Execute the command in a new shell with the given environment
|
68 | --verbose Print helpful debugging information
|
69 | -x, --expand-envs Replace $var in args and command with environment variables
|
70 | -h, --help output usage information
|
71 | ```
|
72 |
|
73 | ## 🔬 Advanced Usage
|
74 |
|
75 | ### `.rc` file usage
|
76 |
|
77 | For more complex projects, a `.env-cmdrc` file can be defined in the root directory and supports
|
78 | as many environments as you want. Simply use the `-e` flag and provide which environments you wish to
|
79 | use from the `.env-cmdrc` file. Using multiple environment names will merge the environment variables
|
80 | together. Later environments overwrite earlier ones in the list if conflicting environment variables
|
81 | are found.
|
82 |
|
83 | **.rc file `./.env-cmdrc`**
|
84 |
|
85 | ```json
|
86 | {
|
87 | "development": {
|
88 | "ENV1": "Thanks",
|
89 | "ENV2": "For All"
|
90 | },
|
91 | "test": {
|
92 | "ENV1": "No Thanks",
|
93 | "ENV3": "!"
|
94 | },
|
95 | "production": {
|
96 | "ENV1": "The Fish"
|
97 | }
|
98 | }
|
99 | ```
|
100 |
|
101 | **Terminal**
|
102 |
|
103 | ```sh
|
104 | ./node_modules/.bin/env-cmd -e production node index.js
|
105 | # Or for multiple environments (where `production` vars override `test` vars,
|
106 | # but both are included)
|
107 | ./node_modules/.bin/env-cmd -e test,production node index.js
|
108 | ```
|
109 |
|
110 | ### `--no-override` option
|
111 |
|
112 | Prevents overriding of existing environment variables on `process.env` and within the current
|
113 | environment.
|
114 |
|
115 | ### `--fallback` file usage option
|
116 |
|
117 | If the `.env` file does not exist at the provided custom path, then use the default
|
118 | fallback location `./.env` env file instead.
|
119 |
|
120 | ### `--use-shell`
|
121 |
|
122 | Executes the command within a new shell environment. This is useful if you want to string multiple
|
123 | commands together that share the same environment variables.
|
124 |
|
125 | **Terminal**
|
126 |
|
127 | ```sh
|
128 | ./node_modules/.bin/env-cmd -f ./test/.env --use-shell "npm run lint && npm test"
|
129 | ```
|
130 |
|
131 | ### Asynchronous env file support
|
132 |
|
133 | EnvCmd supports reading from asynchronous `.env` files. Instead of using a `.env` file, pass in a `.js`
|
134 | file that exports either an object or a `Promise` resolving to an object (`{ ENV_VAR_NAME: value, ... }`). Asynchronous `.rc`
|
135 | files are also supported using `.js` file extension and resolving to an object with top level environment
|
136 | names (`{ production: { ENV_VAR_NAME: value, ... } }`).
|
137 |
|
138 | **Terminal**
|
139 |
|
140 | ```sh
|
141 | ./node_modules/.bin/env-cmd -f ./async-file.js node index.js
|
142 | ```
|
143 |
|
144 | ### `-x` expands vars in arguments
|
145 |
|
146 | EnvCmd supports expanding `$var` values passed in as arguments to the command. The allows a user
|
147 | to provide arguments to a command that are based on environment variable values at runtime.
|
148 |
|
149 | **Terminal**
|
150 |
|
151 | ```sh
|
152 | # $USER will be expanded into the env value it contains at runtime
|
153 | ./node_modules/.bin/env-cmd -x node index.js --user=$USER
|
154 | ```
|
155 |
|
156 | ### `--silent` suppresses env-cmd errors
|
157 |
|
158 | EnvCmd supports the `--silent` flag the suppresses all errors generated by `env-cmd`
|
159 | while leaving errors generated by the child process and cli signals still usable. This
|
160 | flag is primarily used to allow `env-cmd` to run in environments where the `.env`
|
161 | file might not be present, but still execute the child process without failing
|
162 | due to a missing file.
|
163 |
|
164 |
|
165 | ## 💿 Examples
|
166 |
|
167 | You can find examples of how to use the various options above by visiting
|
168 | the examples repo [env-cmd-examples](https://github.com/toddbluhm/env-cmd-examples).
|
169 |
|
170 | ## 💽️ Environment File Formats
|
171 |
|
172 | These are the currently accepted environment file formats. If any other formats are desired please create an issue.
|
173 |
|
174 | - `.env` as `key=value`
|
175 | - `.env.json` Key/value pairs as JSON
|
176 | - `.env.js` JavaScript file exporting an `object` or a `Promise` that resolves to an `object`
|
177 | - `.env-cmdrc` as valid json or `.env-cmdrc.json` in execution directory with at least one environment `{ "dev": { "key1": "val1" } }`
|
178 | - `.env-cmdrc.js` JavaScript file exporting an `object` or a `Promise` that resolves to an `object` that contains at least one environment
|
179 |
|
180 | ## 🗂 Path Rules
|
181 |
|
182 | This lib attempts to follow standard `bash` path rules. The rules are as followed:
|
183 |
|
184 | Home Directory = `/Users/test`
|
185 |
|
186 | Working Directory = `/Users/test/Development/app`
|
187 |
|
188 | | Type | Input Path | Expanded Path |
|
189 | | -- | -- | ------------- |
|
190 | | Absolute | `/some/absolute/path.env` | `/some/absolute/path.env` |
|
191 | | Home Directory with `~` | `~/starts/on/homedir/path.env` | `/Users/test/starts/on/homedir/path.env` |
|
192 | | Relative | `./some/relative/path.env` or `some/relative/path.env` | `/Users/test/Development/app/some/relative/path.env` |
|
193 | | Relative with parent dir | `../some/relative/path.env` | `/Users/test/Development/some/relative/path.env` |
|
194 |
|
195 | ## 🛠 API Usage
|
196 |
|
197 | ### `EnvCmd`
|
198 |
|
199 | A function that executes a given command in a new child process with the given environment and options
|
200 |
|
201 | - **`options`** { `object` }
|
202 | - **`command`** { `string` }: The command to execute (`node`, `mocha`, ...)
|
203 | - **`commandArgs`** { `string[]` }: List of arguments to pass to the `command` (`['-R', 'Spec']`)
|
204 | - **`envFile`** { `object` }
|
205 | - **`filePath`** { `string` }: Custom path to .env file to read from (defaults to: `./.env`)
|
206 | - **`fallback`** { `boolean` }: Should fall back to default `./.env` file if custom path does not exist
|
207 | - **`rc`** { `object` }
|
208 | - **`environments`** { `string[]` }: List of environment to read from the `.rc` file
|
209 | - **`filePath`** { `string` }: Custom path to the `.rc` file (defaults to: `./.env-cmdrc(|.js|.json)`)
|
210 | - **`options`** { `object` }
|
211 | - **`expandEnvs`** { `boolean` }: Expand `$var` values passed to `commandArgs` (default: `false`)
|
212 | - **`noOverride`** { `boolean` }: Prevent `.env` file vars from overriding existing `process.env` vars (default: `false`)
|
213 | - **`silent`** { `boolean` }: Ignore any errors thrown by env-cmd, used to ignore missing file errors (default: `false`)
|
214 | - **`useShell`** { `boolean` }: Runs command inside a new shell instance (default: `false`)
|
215 | - **`verbose`** { `boolean` }: Prints extra debug logs to `console.info` (default: `false`)
|
216 | - **Returns** { `Promise<object>` }: key is env var name and value is the env var value
|
217 |
|
218 | ### `GetEnvVars`
|
219 |
|
220 | A function that parses environment variables from a `.env` or a `.rc` file
|
221 |
|
222 | - **`options`** { `object` }
|
223 | - **`envFile`** { `object` }
|
224 | - **`filePath`** { `string` }: Custom path to .env file to read from (defaults to: `./.env`)
|
225 | - **`fallback`** { `boolean` }: Should fall back to default `./.env` file if custom path does not exist
|
226 | - **`rc`** { `object` }
|
227 | - **`environments`** { `string[]` }: List of environment to read from the `.rc` file
|
228 | - **`filePath`** { `string` }: Custom path to the `.rc` file (defaults to: `./.env-cmdrc(|.js|.json)`)
|
229 | - **`verbose`** { `boolean` }: Prints extra debug logs to `console.info` (default: `false`)
|
230 | - **Returns** { `Promise<object>` }: key is env var name and value is the env var value
|
231 |
|
232 | ## 🧙 Why
|
233 |
|
234 | Because sometimes it is just too cumbersome passing a lot of environment variables to scripts. It is
|
235 | usually just easier to have a file with all the vars in them, especially for development and testing.
|
236 |
|
237 | 🚨**Do not commit sensitive environment data to a public git repo!** 🚨
|
238 |
|
239 | ## 🧬 Related Projects
|
240 |
|
241 | [`cross-env`](https://github.com/kentcdodds/cross-env) - Cross platform setting of environment scripts
|
242 |
|
243 | ## 🎊 Special Thanks
|
244 |
|
245 | Special thanks to [`cross-env`](https://github.com/kentcdodds/cross-env) for inspiration (uses the
|
246 | same `cross-spawn` lib underneath too).
|
247 |
|
248 | ## 📋 Contributing Guide
|
249 |
|
250 | I welcome all pull requests. Please make sure you add appropriate test cases for any features
|
251 | added. Before opening a PR please make sure to run the following scripts:
|
252 |
|
253 | - `npm run lint` checks for code errors and format according to [ts-standard](https://github.com/toddbluhm/ts-standard)
|
254 | - `npm test` make sure all tests pass
|
255 | - `npm run test-cover` make sure the coverage has not decreased from current master
|