@achs/env 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +797 -0
- package/arguments.d.ts +22 -0
- package/arguments.d.ts.map +1 -0
- package/arguments.js +105 -0
- package/arguments.js.map +1 -0
- package/commands/env.command.d.ts +8 -0
- package/commands/env.command.d.ts.map +1 -0
- package/commands/env.command.js +64 -0
- package/commands/env.command.js.map +1 -0
- package/commands/export.command.d.ts +8 -0
- package/commands/export.command.d.ts.map +1 -0
- package/commands/export.command.js +51 -0
- package/commands/export.command.js.map +1 -0
- package/commands/index.d.ts +6 -0
- package/commands/index.d.ts.map +1 -0
- package/commands/index.js +14 -0
- package/commands/index.js.map +1 -0
- package/commands/pull.command.d.ts +7 -0
- package/commands/pull.command.d.ts.map +1 -0
- package/commands/pull.command.js +39 -0
- package/commands/pull.command.js.map +1 -0
- package/commands/push.command.d.ts +7 -0
- package/commands/push.command.d.ts.map +1 -0
- package/commands/push.command.js +38 -0
- package/commands/push.command.js.map +1 -0
- package/commands/schema.command.d.ts +4 -0
- package/commands/schema.command.d.ts.map +1 -0
- package/commands/schema.command.js +18 -0
- package/commands/schema.command.js.map +1 -0
- package/exec.d.ts +3 -0
- package/exec.d.ts.map +1 -0
- package/exec.js +142 -0
- package/exec.js.map +1 -0
- package/index.d.ts +4 -0
- package/index.d.ts.map +1 -0
- package/index.js +20 -0
- package/index.js.map +1 -0
- package/interfaces/index.d.ts +2 -0
- package/interfaces/index.d.ts.map +1 -0
- package/interfaces/index.js +18 -0
- package/interfaces/index.js.map +1 -0
- package/interfaces/loader.interface.d.ts +21 -0
- package/interfaces/loader.interface.d.ts.map +1 -0
- package/interfaces/loader.interface.js +3 -0
- package/interfaces/loader.interface.js.map +1 -0
- package/main.d.ts +3 -0
- package/main.d.ts.map +1 -0
- package/main.js +6 -0
- package/main.js.map +1 -0
- package/package.json +85 -0
- package/providers/app-settings.provider.d.ts +9 -0
- package/providers/app-settings.provider.d.ts.map +1 -0
- package/providers/app-settings.provider.js +53 -0
- package/providers/app-settings.provider.js.map +1 -0
- package/providers/azure-key-vault.provider.d.ts +16 -0
- package/providers/azure-key-vault.provider.d.ts.map +1 -0
- package/providers/azure-key-vault.provider.js +159 -0
- package/providers/azure-key-vault.provider.js.map +1 -0
- package/providers/index.d.ts +8 -0
- package/providers/index.d.ts.map +1 -0
- package/providers/index.js +28 -0
- package/providers/index.js.map +1 -0
- package/providers/package-json.provider.d.ts +8 -0
- package/providers/package-json.provider.d.ts.map +1 -0
- package/providers/package-json.provider.js +29 -0
- package/providers/package-json.provider.js.map +1 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/utils/command.util.d.ts +12 -0
- package/utils/command.util.d.ts.map +1 -0
- package/utils/command.util.js +136 -0
- package/utils/command.util.js.map +1 -0
- package/utils/index.d.ts +7 -0
- package/utils/index.d.ts.map +1 -0
- package/utils/index.js +23 -0
- package/utils/index.js.map +1 -0
- package/utils/interpolate.util.d.ts +4 -0
- package/utils/interpolate.util.d.ts.map +1 -0
- package/utils/interpolate.util.js +33 -0
- package/utils/interpolate.util.js.map +1 -0
- package/utils/json.util.d.ts +5 -0
- package/utils/json.util.d.ts.map +1 -0
- package/utils/json.util.js +47 -0
- package/utils/json.util.js.map +1 -0
- package/utils/logger.d.ts +3 -0
- package/utils/logger.d.ts.map +1 -0
- package/utils/logger.js +18 -0
- package/utils/logger.js.map +1 -0
- package/utils/normalize.util.d.ts +2 -0
- package/utils/normalize.util.d.ts.map +1 -0
- package/utils/normalize.util.js +44 -0
- package/utils/normalize.util.js.map +1 -0
- package/utils/schema.util.d.ts +9 -0
- package/utils/schema.util.d.ts.map +1 -0
- package/utils/schema.util.js +69 -0
- package/utils/schema.util.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,797 @@
|
|
|
1
|
+
<div id="top" align="center">
|
|
2
|
+
<img
|
|
3
|
+
alt="logo"
|
|
4
|
+
src="https://nodejs.org/static/images/logo.svg"
|
|
5
|
+
width="256px"
|
|
6
|
+
/>
|
|
7
|
+
|
|
8
|
+
</br>
|
|
9
|
+
|
|
10
|
+
<h1 align="center"><b>env</b></h1>
|
|
11
|
+
<h4 align="center">¡Environment variables made easy!</h4>
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
<br />
|
|
15
|
+
|
|
16
|
+
<p align="center">
|
|
17
|
+
<img
|
|
18
|
+
src="https://img.shields.io/badge/version-1.0.0-blue?style=flat-square"
|
|
19
|
+
alt="version"
|
|
20
|
+
/>
|
|
21
|
+
|
|
22
|
+
<img
|
|
23
|
+
src="https://img.shields.io/badge/TypeScript-007ACC?style=flat-square&logo=typescript&logoColor=white"
|
|
24
|
+
alt="typescript"
|
|
25
|
+
/>
|
|
26
|
+
|
|
27
|
+
<img
|
|
28
|
+
src="https://img.shields.io/badge/nodejs-~14.0.0_||_^16.14.2-darkgreen?style=flat-square"
|
|
29
|
+
alt="nodejs engine"
|
|
30
|
+
/>
|
|
31
|
+
|
|
32
|
+
<img
|
|
33
|
+
src="https://img.shields.io/badge/npm->=7.5.6-darkgreen?style=flat-square"
|
|
34
|
+
alt="npm engine"
|
|
35
|
+
/>
|
|
36
|
+
</p>
|
|
37
|
+
|
|
38
|
+
<br />
|
|
39
|
+
|
|
40
|
+
<!-- ABOUT THE PROJECT -->
|
|
41
|
+
|
|
42
|
+
## 📖 **About**
|
|
43
|
+
|
|
44
|
+
Eases NodeJS <b>environment variable handling</b>, like [env-cmd](https://www.npmjs.com/package/env-cmd) or [dotenv](https://www.npmjs.com/package/dotenv), but with <b>powerfull features and extensibility</b> for adding custom providers (as plugins) for <u>load</u>, <u>pull</u> and <u>push</u> the variables from different stores.
|
|
45
|
+
|
|
46
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
47
|
+
|
|
48
|
+
<!-- REQUIREMENTS -->
|
|
49
|
+
|
|
50
|
+
## 📌 **Requirements**
|
|
51
|
+
|
|
52
|
+
First, [download](https://nodejs.org/) and install **NodeJS**. Version `16` or higher is required.
|
|
53
|
+
|
|
54
|
+
Validate installed versions of node and npm with:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
> node -v
|
|
58
|
+
v16.14.2
|
|
59
|
+
|
|
60
|
+
> npm -v
|
|
61
|
+
8.3.0
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
You can initialize a new npm project using:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
> npm init
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
71
|
+
|
|
72
|
+
<!-- QUICK START -->
|
|
73
|
+
|
|
74
|
+
## ⚡️ **Quick start**
|
|
75
|
+
|
|
76
|
+
> 🔔 Make sure that you have [NodeJS 14+](https://nodejs.org/) installed on your computer.
|
|
77
|
+
|
|
78
|
+
- Installs the package:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
> npm install @achs/env
|
|
82
|
+
|
|
83
|
+
added 1 packages, and audited 1 packages in 1s
|
|
84
|
+
|
|
85
|
+
found 0 vulnerabilities
|
|
86
|
+
> _
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
- Executes binary directly:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
> node_modules/.bin/env --help
|
|
93
|
+
|
|
94
|
+
Usage: env [command] [options..] [: subcmd [:]] [options..]
|
|
95
|
+
|
|
96
|
+
Commands:
|
|
97
|
+
env [options..] [: <subcmd> :]
|
|
98
|
+
env pull [options..]
|
|
99
|
+
env push [options..]
|
|
100
|
+
env schema [options..]
|
|
101
|
+
> _
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
- Or add desired commands in your **npm script** in `package.json`:
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
{
|
|
108
|
+
...,
|
|
109
|
+
"scripts": {
|
|
110
|
+
// starts project injecting "dev" environment variables and debug log level
|
|
111
|
+
"start:dev": "env -e dev -m debug : node dist/main.js : --log debug",
|
|
112
|
+
// starts project injecting "prod" environment variables
|
|
113
|
+
"start:prod": "env -e prod -m debug : node dist/main.js",
|
|
114
|
+
...,
|
|
115
|
+
// builds project injecting "prod" environment variables
|
|
116
|
+
"build:prod": "env -e prod -m build : tsc",
|
|
117
|
+
...,
|
|
118
|
+
"env:schema": "env schema -e dev",
|
|
119
|
+
// uploads environment "dev" variables
|
|
120
|
+
"env:push:dev": "env push -e dev",
|
|
121
|
+
// downloads environment "dev" variables
|
|
122
|
+
"env:pull:dev": "env pull -e dev"
|
|
123
|
+
},
|
|
124
|
+
...
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
- Execs your command:
|
|
129
|
+
|
|
130
|
+
**file**: _dist/main.js_
|
|
131
|
+
|
|
132
|
+
```javascript
|
|
133
|
+
console.log(`My environment loaded is: ${process.env.ENV}`);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
> npm run start:dev
|
|
138
|
+
|
|
139
|
+
13:31:59.865 INFO loading dev environment in debug mode
|
|
140
|
+
13:31:59.911 DEBUG using package-json provider
|
|
141
|
+
13:31:59.912 DEBUG using app-settings provider
|
|
142
|
+
13:31:59.914 DEBUG using secrets provider
|
|
143
|
+
13:32:00.109 DEBUG environment loaded:
|
|
144
|
+
{
|
|
145
|
+
NODE_ENV: 'development',
|
|
146
|
+
ENV: 'dev',
|
|
147
|
+
VERSION: '1.0.0',
|
|
148
|
+
NAME: '@my-app',
|
|
149
|
+
VAR1: true,
|
|
150
|
+
VAR2: true,
|
|
151
|
+
GROUP1__VAR1: 'G1V2',
|
|
152
|
+
ARR1: '1,val,true',
|
|
153
|
+
SECRET: '***'
|
|
154
|
+
}
|
|
155
|
+
13:32:00.116 INFO executing command > node dist/main.js
|
|
156
|
+
My environment loaded is: dev
|
|
157
|
+
13:32:00.232 INFO process finished successfully
|
|
158
|
+
> _
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
162
|
+
|
|
163
|
+
## ⛩ **Structure**
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
├── src/
|
|
167
|
+
│ ├── commands/ # lib commands handlers
|
|
168
|
+
│ │ ├── env.command.ts
|
|
169
|
+
│ │ ├── export.command.ts
|
|
170
|
+
│ │ ├── pull.command.ts
|
|
171
|
+
│ │ ├── push.command.ts
|
|
172
|
+
│ │ └── schema.command.ts
|
|
173
|
+
│ ├── interfaces/ # provider interfaces
|
|
174
|
+
│ ├── providers/ # integrated providers
|
|
175
|
+
│ │ ├── package-json.provider.ts
|
|
176
|
+
│ │ ├── app-settings.provider.ts
|
|
177
|
+
│ │ └── azure-key-vault.provider.ts
|
|
178
|
+
│ ├── utils/
|
|
179
|
+
│ │ ├── command.util.ts
|
|
180
|
+
│ │ ├── interpolate.util.ts
|
|
181
|
+
│ │ ├── json.util.ts
|
|
182
|
+
│ │ ├── normalize.util.ts
|
|
183
|
+
│ │ ├── schema.util.ts
|
|
184
|
+
│ │ └── logger.ts
|
|
185
|
+
│ ├── arguments.ts # global arguments
|
|
186
|
+
│ ├── exec.ts # initialization logic (load config, commands, etc.)
|
|
187
|
+
│ └── main.ts
|
|
188
|
+
├── tests/ # integration tests
|
|
189
|
+
├── .eslintrc.json
|
|
190
|
+
├── jest.config.json
|
|
191
|
+
├── tsconfig.build.json
|
|
192
|
+
└── tsconfig.json
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
196
|
+
|
|
197
|
+
<!-- COMMANDS AND OPTIONS -->
|
|
198
|
+
|
|
199
|
+
## ⚙️ **Commands & Options**
|
|
200
|
+
|
|
201
|
+
Options handling has the ability of **replace arguments itself**, using `[[` and `]]` as delimiters.
|
|
202
|
+
So, in example for define your config file path, you must use your _root_ argument,
|
|
203
|
+
supposing root has the value of "config", this definition _`[[root]]/any-config-file.json`_ will be
|
|
204
|
+
_`config/any-config-file.json`_, or if your _env_ argument is "dev", this definition
|
|
205
|
+
_`[[root]]/config-file.[[env]].json`_ will be _`config/config-file.dev.json`_.
|
|
206
|
+
|
|
207
|
+
<div align="center">
|
|
208
|
+
<span style="font-size:20px;font-weight:bold" align="center">Options</span>
|
|
209
|
+
</div>
|
|
210
|
+
|
|
211
|
+
### Global options
|
|
212
|
+
|
|
213
|
+
| Option | Description | Type | Default | Required? |
|
|
214
|
+
| ---------------------------------- | -------------------------------------- | ---------- | ------- | --------- |
|
|
215
|
+
| `--help` | Shows help | `boolean` | | No |
|
|
216
|
+
| `--e, --env` | Environment for load | `string` | | Yes |
|
|
217
|
+
| `-m, --modes` | Execution modes | `string[]` | `[]` | No |
|
|
218
|
+
| `--nd, --nestingDelimiter` | Nesting level delimiter for flatten | `string` | `__` | No |
|
|
219
|
+
| `--arrDesc, --arrayDescomposition` | Whether serialize or break down arrays | `boolean` | `false` | No |
|
|
220
|
+
|
|
221
|
+
</br>
|
|
222
|
+
|
|
223
|
+
### Workspace options
|
|
224
|
+
|
|
225
|
+
| Option | Description | Type | Default | Required? |
|
|
226
|
+
| ------------------ | --------------------------------- | -------- | -------------------------- | --------- |
|
|
227
|
+
| `--root` | Default environment folder path | `string` | `env` | No |
|
|
228
|
+
| `-c, --configFile` | Config JSON file path | `string` | `[[root]]/env.config.json` | No |
|
|
229
|
+
| `-s, --schemaFile` | Environment Schema JSON file path | `string` | `[[root]]/env.schema.json` | No |
|
|
230
|
+
|
|
231
|
+
### JSON Schema options
|
|
232
|
+
|
|
233
|
+
| Option | Description | Type | Default | Required? |
|
|
234
|
+
| ---------------------- | ---------------------------------------------------------- | ----------------- | ------- | --------- |
|
|
235
|
+
| `-r, --resolve` | Whether merges new schema or override | `merge, override` | `merge` | No |
|
|
236
|
+
| `--null, --nullable` | Whether variables are nullable by default | `boolean` | `true` | No |
|
|
237
|
+
| `--df, --detectFormat` | Whether format of strings variables are included in schema | `boolean` | `true` | No |
|
|
238
|
+
|
|
239
|
+
### Logger options
|
|
240
|
+
|
|
241
|
+
| Option | Description | Type | Default | Required? |
|
|
242
|
+
| ------------------- | ----------- | ---------------------------------------- | ------- | --------- |
|
|
243
|
+
| `--log, --logLevel` | Log level | `silly, trace, debug, info, warn, error` | `info` | No |
|
|
244
|
+
|
|
245
|
+
<div align="center">
|
|
246
|
+
<span style="font-size:20px;font-weight:bold" align="center">Commands</span>
|
|
247
|
+
</div>
|
|
248
|
+
|
|
249
|
+
- ## **`env`**
|
|
250
|
+
|
|
251
|
+
Inject your environment variables into `process.env` and executes a command.
|
|
252
|
+
|
|
253
|
+
```bash
|
|
254
|
+
env -e [env] [options..] [: subcmd [:]] [options..]
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Examples:
|
|
258
|
+
|
|
259
|
+
```bash
|
|
260
|
+
> env -e dev -m test unit : npm test
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
> env -e dev -m debug : npm start : -c [[root]]/[[env]].env.json
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
> env -e prod -m build optimize : npm build
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
- ## **`pull`**
|
|
272
|
+
|
|
273
|
+
Pulls environment variables from providers stores.
|
|
274
|
+
|
|
275
|
+
```bash
|
|
276
|
+
env pull -e [env] [options..]
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
| Option | Description | Type | Default | Required? |
|
|
280
|
+
| ----------------- | ------------------------- | --------- | ------- | --------- |
|
|
281
|
+
| `-o, --overwrite` | Overwrite local variables | `boolean` | `false` | No |
|
|
282
|
+
|
|
283
|
+
Examples:
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
> env pull -e dev
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
- ## **`push`**
|
|
290
|
+
|
|
291
|
+
Pushes environment variables to providers stores.
|
|
292
|
+
|
|
293
|
+
```bash
|
|
294
|
+
env push -e [env] [options..]
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
| Option | Description | Type | Default | Required? |
|
|
298
|
+
| ------------- | ------------------------------------ | --------- | ------- | --------- |
|
|
299
|
+
| `-f, --force` | Force push for secrets (replace all) | `boolean` | `false` | No |
|
|
300
|
+
|
|
301
|
+
Examples:
|
|
302
|
+
|
|
303
|
+
```bash
|
|
304
|
+
> env push -e dev
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
- ## **`schema`**
|
|
308
|
+
|
|
309
|
+
Generates validation schema from providers output variables.
|
|
310
|
+
|
|
311
|
+
```bash
|
|
312
|
+
env schema -e [env] -m [modes] [options..]
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
Examples:
|
|
316
|
+
|
|
317
|
+
```bash
|
|
318
|
+
> env schema -e dev -m build
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
- ## **`export`**
|
|
322
|
+
|
|
323
|
+
Export unified environment variables to a file from providers.
|
|
324
|
+
|
|
325
|
+
```bash
|
|
326
|
+
env export -e [env] -m [modes] [options..]
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
| Option | Description | Type | Default | Required? |
|
|
330
|
+
| --------------- | ---------------------------------- | -------- | -------- | --------- |
|
|
331
|
+
| `-u, -p, --uri` | Uri for export file with variables | `string` | `.env` | No |
|
|
332
|
+
| `-f, --format` | Format for export variables | `string` | `dotenv` | No |
|
|
333
|
+
|
|
334
|
+
Examples:
|
|
335
|
+
|
|
336
|
+
```bash
|
|
337
|
+
> env export -e dev -m build -f json --uri [[env]].env.json
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
341
|
+
|
|
342
|
+
<!-- PROVIDERS -->
|
|
343
|
+
|
|
344
|
+
## 📡 **Providers**
|
|
345
|
+
|
|
346
|
+
Main feature of this library is using providers for get and set environment variables.
|
|
347
|
+
So, you cand define your own provider, but lib came with 3 integrated providers:
|
|
348
|
+
|
|
349
|
+
- ## **`package-json`**
|
|
350
|
+
|
|
351
|
+
Load some info from your project `package.json`.
|
|
352
|
+
|
|
353
|
+
Info read is:
|
|
354
|
+
|
|
355
|
+
```json
|
|
356
|
+
{
|
|
357
|
+
"version": "1.0.0",
|
|
358
|
+
"project": "project-name",
|
|
359
|
+
"name": "@package-name",
|
|
360
|
+
"title": "app-name",
|
|
361
|
+
"description": "any description"
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
| Option | Description | Type | Default | Required? |
|
|
366
|
+
| ------------------- | --------------------------- | -------- | ------- | --------- |
|
|
367
|
+
| `--vp, --varPrefix` | Prefix for loaded variables | `string` | `""` | No |
|
|
368
|
+
|
|
369
|
+
Examples:
|
|
370
|
+
|
|
371
|
+
```bash
|
|
372
|
+
> env -e dev -m build : react-script build : --vp REACT_APP_
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
</br>
|
|
376
|
+
|
|
377
|
+
- ## **`app-settings`**
|
|
378
|
+
|
|
379
|
+
Non secrets loader for `appsettings.json`.
|
|
380
|
+
|
|
381
|
+
`appsettings.json` file has the format below:
|
|
382
|
+
|
|
383
|
+
```json
|
|
384
|
+
{
|
|
385
|
+
"|DEFAULT|": {},
|
|
386
|
+
"|MODE|": {},
|
|
387
|
+
"|ENV|": {}
|
|
388
|
+
}
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
In example:
|
|
392
|
+
|
|
393
|
+
```json
|
|
394
|
+
{
|
|
395
|
+
"|DEFAULT|": {
|
|
396
|
+
"VAR1": "v1_default"
|
|
397
|
+
},
|
|
398
|
+
"|MODE|": {
|
|
399
|
+
"::build": {
|
|
400
|
+
"NODE_ENV": "production"
|
|
401
|
+
},
|
|
402
|
+
"::debug": {
|
|
403
|
+
"NODE_ENV": "development"
|
|
404
|
+
},
|
|
405
|
+
"::test": {
|
|
406
|
+
"NODE_ENV": "test"
|
|
407
|
+
}
|
|
408
|
+
},
|
|
409
|
+
"|ENV|": {
|
|
410
|
+
"::dev": {
|
|
411
|
+
"C1": "V1",
|
|
412
|
+
"C2": "V2",
|
|
413
|
+
"C3": 3,
|
|
414
|
+
"GROUP1": {
|
|
415
|
+
"VAR1": null,
|
|
416
|
+
"VAR2": "G1V2",
|
|
417
|
+
"VAR3": true,
|
|
418
|
+
"GROUP2": {
|
|
419
|
+
"VAR1": "G1G2V1"
|
|
420
|
+
}
|
|
421
|
+
},
|
|
422
|
+
"C4": "23"
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
| Option | Description | Type | Default | Required? |
|
|
429
|
+
| ----------------------- | ------------------------------------ | -------- | --------------------------- | --------- |
|
|
430
|
+
| `--ef, --envFile` | Environment variables file path | `string` | `[[root]]/appsettings.json` | No |
|
|
431
|
+
| `--sp, --sectionPrefix` | Prefix for env and modes in env file | `string` | `::` | No |
|
|
432
|
+
|
|
433
|
+
</br>
|
|
434
|
+
|
|
435
|
+
- ## **`package-json`**
|
|
436
|
+
|
|
437
|
+
Load some info from your project `package.json`.
|
|
438
|
+
|
|
439
|
+
Info read is:
|
|
440
|
+
|
|
441
|
+
```json
|
|
442
|
+
{
|
|
443
|
+
"version": "1.0.0",
|
|
444
|
+
"project": "project-name",
|
|
445
|
+
"name": "@package-name",
|
|
446
|
+
"title": "app-name",
|
|
447
|
+
"description": "any description"
|
|
448
|
+
}
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
| Option | Description | Type | Default | Required? |
|
|
452
|
+
| ------------------- | --------------------------- | -------- | ------- | --------- |
|
|
453
|
+
| `--vp, --varPrefix` | Prefix for loaded variables | `string` | `""` | No |
|
|
454
|
+
|
|
455
|
+
Examples:
|
|
456
|
+
|
|
457
|
+
```bash
|
|
458
|
+
> env -e dev -m build : react-script build : --vp REACT_APP_
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
- ## **`azure-key-vault`**
|
|
462
|
+
|
|
463
|
+
Azure Key Vault provider, allows to load secrets from vault store to `env/secrets/[[env]].env.json` per environment.
|
|
464
|
+
Also, handles `env/secrets/[[env]].local.env.json` for load local variables with precedence over base.
|
|
465
|
+
|
|
466
|
+
| Option | Description | Type | Default | Required? |
|
|
467
|
+
| --------------------------------------- | ---------------------------------------------- | ---------- | ----------------------------------------- | --------- |
|
|
468
|
+
| `--sf, --secretFile` | Secret variables file path | `string` | `[[root]]/secrets/[[env]].env.json` | No |
|
|
469
|
+
| `--lsf, --localSecretFile` | Local secret variables file path | `string` | `[[root]]/secrets/[[env]].local.env.json` | No |
|
|
470
|
+
| `-k, --keys, --keysFile` | Azure Key Vault SPN credentials files paths | `string[]` | `['../keys.json', '[[root]]/keys.json']` | No |
|
|
471
|
+
| `--url, --vaultUrl` | Azure Key Vault server URL | `string` | | Yes |
|
|
472
|
+
| `--spn, --clientId, --id` | SPN Client ID | `string` | | Yes |
|
|
473
|
+
| `-p --password, --pass, --clientSecret` | SPN Client Secret Password | `string` | | Yes |
|
|
474
|
+
| `-t, --tenant` | Azure Tenant ID | `string` | | Yes |
|
|
475
|
+
| `--mock` | Mocks Azure Key Vault client (testing purpose) | `string` | `false` | No |
|
|
476
|
+
|
|
477
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
478
|
+
|
|
479
|
+
<!-- PROVIDERS -->
|
|
480
|
+
|
|
481
|
+
## ✒ **Creating Custom Providers**
|
|
482
|
+
|
|
483
|
+
You can create your custom providers, in two ways:
|
|
484
|
+
|
|
485
|
+
- **Local Script**: you must create a JavaScript file (.js), exporting by default your "provider" following standard interface exported by this lib.
|
|
486
|
+
- **NPM Package**: you must create your custom NPM library and export by default your "provider" using standard interface exported by this lib.
|
|
487
|
+
|
|
488
|
+
How to load your provider is shown in Config Section.
|
|
489
|
+
|
|
490
|
+
In example, a provider exported by your NPM package written in TypeScript should be like:
|
|
491
|
+
|
|
492
|
+
```typescript
|
|
493
|
+
import { CommandArguments, EnvProvider } from '@achs/env';
|
|
494
|
+
import { logger, readJson, writeJson } from '@achs/env/utils';
|
|
495
|
+
|
|
496
|
+
const KEY = 'my-unique-provider-key';
|
|
497
|
+
|
|
498
|
+
interface MyProviderCommandArguments extends CommandArguments {
|
|
499
|
+
anyExtraOption: boolean;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export const MyProvider: EnvProvider<MyProviderCommandArguments> = {
|
|
503
|
+
// unique identifier for provider
|
|
504
|
+
key: KEY,
|
|
505
|
+
|
|
506
|
+
// (optional) allows to provider adds new arguments/options
|
|
507
|
+
// to commands using yargs for builder
|
|
508
|
+
builder: (builder) => {
|
|
509
|
+
builder.options({
|
|
510
|
+
anyExtraOption: {
|
|
511
|
+
group: KEY,
|
|
512
|
+
alias: ['a', 'aeo'],
|
|
513
|
+
type: 'boolean',
|
|
514
|
+
default: false,
|
|
515
|
+
describe: 'Any option description',
|
|
516
|
+
},
|
|
517
|
+
});
|
|
518
|
+
},
|
|
519
|
+
|
|
520
|
+
// call on environment variables loading,
|
|
521
|
+
// may be a Promise
|
|
522
|
+
load: ({ env, modes, ...options }) => {
|
|
523
|
+
if (env === 'dev')
|
|
524
|
+
return {
|
|
525
|
+
NODE_ENV: 'development',
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
// you can return a list of JSON environment variables for merge
|
|
529
|
+
return [
|
|
530
|
+
{
|
|
531
|
+
NODE_ENV: 'production',
|
|
532
|
+
},
|
|
533
|
+
{
|
|
534
|
+
ANY_VAR: 'ANY_VALUE',
|
|
535
|
+
ANY_GROUP: {
|
|
536
|
+
INNER_VAR: 12,
|
|
537
|
+
},
|
|
538
|
+
},
|
|
539
|
+
];
|
|
540
|
+
},
|
|
541
|
+
|
|
542
|
+
// (optional) call on pulling variables from provider store,
|
|
543
|
+
// config may pass in your config file
|
|
544
|
+
pull: ({ env, modes, ...options }, config) => {
|
|
545
|
+
// anyway you want for pulling variables to cache
|
|
546
|
+
},
|
|
547
|
+
|
|
548
|
+
// (optional) call on pushing/updating variables to provider store,
|
|
549
|
+
// config may pass in your config file
|
|
550
|
+
push: ({ env, modes, ...options }, config) => {
|
|
551
|
+
// anyway you should do for pushing or updating your variables
|
|
552
|
+
},
|
|
553
|
+
};
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
557
|
+
|
|
558
|
+
<!-- CONFIG -->
|
|
559
|
+
|
|
560
|
+
## 📥 **Config**
|
|
561
|
+
|
|
562
|
+
You can configure any config argument inside you config file, but commonly providers are designed for this purpose.
|
|
563
|
+
|
|
564
|
+
```javascript
|
|
565
|
+
{
|
|
566
|
+
"log": "silly",
|
|
567
|
+
// will hide values of keys SECRET and MY_API_KEY in logging
|
|
568
|
+
"logMaskValuesOfKeys": ["SECRET", "MY_API_KEY"],
|
|
569
|
+
// integrated providers and custom providers together
|
|
570
|
+
"providers": [
|
|
571
|
+
{
|
|
572
|
+
"path": "package-json",
|
|
573
|
+
"type": "integrated"
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
"path": "app-settings",
|
|
577
|
+
"type": "integrated"
|
|
578
|
+
},
|
|
579
|
+
{
|
|
580
|
+
"path": "azure-key-vault",
|
|
581
|
+
"type": "integrated"
|
|
582
|
+
},
|
|
583
|
+
{
|
|
584
|
+
// custom NPM package
|
|
585
|
+
"path": "@npm-package",
|
|
586
|
+
"type": "module",
|
|
587
|
+
"config": {
|
|
588
|
+
"any-config": "any value"
|
|
589
|
+
}
|
|
590
|
+
},
|
|
591
|
+
{
|
|
592
|
+
// custom script inside project
|
|
593
|
+
"path": "scripts/custom-loader.js",
|
|
594
|
+
"type": "script"
|
|
595
|
+
}
|
|
596
|
+
]
|
|
597
|
+
}
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
<!-- AZURE KEY VAULT -->
|
|
601
|
+
|
|
602
|
+
## 💽 **Azure Key Vault**
|
|
603
|
+
|
|
604
|
+
Allows you to store your secrets in Azure Key Vault.
|
|
605
|
+
|
|
606
|
+
#### 1.2. NPM Scripts
|
|
607
|
+
|
|
608
|
+
For load desired environment, add you npm script like **`env -e <env> -m <mode1[ mode2]> : <your-command>`**.
|
|
609
|
+
|
|
610
|
+
- **mode**: (build|debug|test) execution mode base variables.
|
|
611
|
+
- **env**: (dev|qa|stg|prod) environment variables.
|
|
612
|
+
|
|
613
|
+
_In example: `env -e dev -m debug : npm start`_
|
|
614
|
+
|
|
615
|
+
## 2. Structure
|
|
616
|
+
|
|
617
|
+
#### 2.1. Environments
|
|
618
|
+
|
|
619
|
+
Your `env/secrets` folder will contain files below:
|
|
620
|
+
|
|
621
|
+
- **dev.env.json**: development environment.
|
|
622
|
+
- **dev.local.env.json**: local development environment (takes precedence).
|
|
623
|
+
- **qa.env.json**: quality assurance environment.
|
|
624
|
+
- **qa.local.env.json**: local qa environment (takes precedence).
|
|
625
|
+
- **prod.env.json**: production environment.
|
|
626
|
+
- **prod.local.env.json**: local production environment (takes precedence).
|
|
627
|
+
|
|
628
|
+
_This folder should contains environment variables files for system environments._
|
|
629
|
+
|
|
630
|
+
#### 2.2. Keys (env/keys.json)
|
|
631
|
+
|
|
632
|
+
Your `keys.json` file should contains you Azure Key Vault SPN credentials per environment:
|
|
633
|
+
|
|
634
|
+
```json
|
|
635
|
+
{
|
|
636
|
+
"<env-name>": {
|
|
637
|
+
"keyVaultUri": "<azure-key-vault-uri>",
|
|
638
|
+
"clientId": "<spn-client-id>",
|
|
639
|
+
"clientSecret": "<spn-secret-password>",
|
|
640
|
+
"tenantId": "<tenant-id>"
|
|
641
|
+
},
|
|
642
|
+
...
|
|
643
|
+
}
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
In example:
|
|
647
|
+
|
|
648
|
+
```json
|
|
649
|
+
{
|
|
650
|
+
"dev": {
|
|
651
|
+
"keyVaultUri": "https://kv-desa-ittec-sti.vault.azure.net",
|
|
652
|
+
"clientId": "f176a774-239e-4cd3-8551-88fd9fb9b441",
|
|
653
|
+
"clientSecret": "WyBwkmcL8rGQe9B2fvRLDrqDuannE4Ku",
|
|
654
|
+
"tenantId": "6d4bbe0a-5654-4c69-a682-bf7dcdaed8e7"
|
|
655
|
+
},
|
|
656
|
+
"qa": {
|
|
657
|
+
"keyVaultUri": "https://kv-qa-ittec-sti.vault.azure.net",
|
|
658
|
+
"clientId": "5dcd9f45-7067-4387-94d8-e5e7066ba630",
|
|
659
|
+
"clientSecret": "60ec5e16430a46eba70dfea80d721b66",
|
|
660
|
+
"tenantId": "6d4bbe0a-5654-4c69-a682-bf7dcdaed8e7"
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
Your secrets will be grouped, using your "name" and "project" variables from `package.json` file.
|
|
666
|
+
|
|
667
|
+
_This file allows to load environment files locally first run time._
|
|
668
|
+
|
|
669
|
+
## 3. Commands
|
|
670
|
+
|
|
671
|
+
You can use two command scripts for refresh your local env files
|
|
672
|
+
or publish/updates env files in the azure key vault from your local files.
|
|
673
|
+
|
|
674
|
+
- **Pulls Secrets File**: `env pull -e <env> [-o]`. (-o forces to replace your local file).
|
|
675
|
+
- **Pushes/Publishes Secrets File**: `env push -e <env>`.
|
|
676
|
+
|
|
677
|
+
#### 3.1. Environment Variables for Credentials
|
|
678
|
+
|
|
679
|
+
You can set your credentials variables from node environment variables.
|
|
680
|
+
|
|
681
|
+
In example:
|
|
682
|
+
|
|
683
|
+
```bash
|
|
684
|
+
user@machine:/mnt/c/Users/user$ AZURE_URI=https://kv-desa-ittec-sti.vault.azure.net \
|
|
685
|
+
AZURE_CLIENT_ID=f176a774-239e-4cd3-8551-88fd9fb9b441 \
|
|
686
|
+
AZURE_CLIENT_SECRET=WyBwkmcL8rGQe9B2fvRLDrqDuannE4Ku \
|
|
687
|
+
AZURE_TENANT_ID=6d4bbe0a-5654-4c69-a682-bf7dcdaed8e7 \
|
|
688
|
+
node_modules/.bin/env pull -e dev -o
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
## 4. Schema
|
|
692
|
+
|
|
693
|
+
This lib uses JSON schema for validate and retrieve secrets from store.
|
|
694
|
+
|
|
695
|
+
For each property in the file, loader will retrieve the value from Azure Key
|
|
696
|
+
Vault.
|
|
697
|
+
|
|
698
|
+
When you push a new variable from any of your environment secrets
|
|
699
|
+
file, `env.schema.json` will be updated automatically.
|
|
700
|
+
|
|
701
|
+
If you want to ignore to load some variable without delete it, you can remove
|
|
702
|
+
the variable from `env.schema.json`.
|
|
703
|
+
|
|
704
|
+
## 5. Shared and Nested Keys
|
|
705
|
+
|
|
706
|
+
You can organize your keys in nested objects,
|
|
707
|
+
and declare project shared secrets (skips group
|
|
708
|
+
separation) prefixing with '\$' like:
|
|
709
|
+
|
|
710
|
+
```json
|
|
711
|
+
{
|
|
712
|
+
// .dev.env.json
|
|
713
|
+
"$SHARED": "sharedValue",
|
|
714
|
+
"GROUP1": {
|
|
715
|
+
"$SHARED": "sharedValue2",
|
|
716
|
+
"VAR": "anyValue1",
|
|
717
|
+
...
|
|
718
|
+
},
|
|
719
|
+
"GROUP2": {
|
|
720
|
+
"VAR": "anyValue2",
|
|
721
|
+
"SUBGROUP1": {
|
|
722
|
+
"VAR": "anyValue1",
|
|
723
|
+
...
|
|
724
|
+
},
|
|
725
|
+
...
|
|
726
|
+
},
|
|
727
|
+
"VAR3": "anyValue3",
|
|
728
|
+
...
|
|
729
|
+
}
|
|
730
|
+
```
|
|
731
|
+
|
|
732
|
+
So, in your application you can use the variables as shown below:
|
|
733
|
+
|
|
734
|
+
```javascript
|
|
735
|
+
const myVar1 = process.env.GROUP1_VAR;
|
|
736
|
+
const myVar2 = process.env.GROUP2_VAR;
|
|
737
|
+
const myVar2 = process.env.GROUP2_SUBGROUP1_VAR;
|
|
738
|
+
const myVar3 = process.env.VAR3;
|
|
739
|
+
// shared vars will load in every project
|
|
740
|
+
const mySharedVar1 = process.env.SHARED;
|
|
741
|
+
const mySharedNestedVar2 = process.env.GROUP1_SHARED;
|
|
742
|
+
```
|
|
743
|
+
|
|
744
|
+
## 6. Priority
|
|
745
|
+
|
|
746
|
+
### From lowest to highest.
|
|
747
|
+
|
|
748
|
+
- `(dev|qa|prod).env.json`
|
|
749
|
+
- `(dev|qa|prod).local.env.json` (takes precedence over previous)
|
|
750
|
+
|
|
751
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
752
|
+
|
|
753
|
+
<!-- LINTING -->
|
|
754
|
+
|
|
755
|
+
## 🧿 **Linting**
|
|
756
|
+
|
|
757
|
+
Project uses ESLint, for code formatting and code styling normalizing.
|
|
758
|
+
|
|
759
|
+
- **eslint**: linter integrated with TypeScript.
|
|
760
|
+
|
|
761
|
+
For correct interpretation of linters, is recommended to use [Visual Studio Code](https://code.visualstudio.com/) as IDE and install the plugins in .vscode folder at 'extensions.json', as well as use the config provided in 'settings.json'
|
|
762
|
+
|
|
763
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
764
|
+
|
|
765
|
+
<!-- CHANGELOG -->
|
|
766
|
+
|
|
767
|
+
## 📋 Changelog
|
|
768
|
+
|
|
769
|
+
For last changes see [CHANGELOG.md](CHANGELOG.md) file for details.
|
|
770
|
+
|
|
771
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
772
|
+
|
|
773
|
+
<!-- BUILT WITH -->
|
|
774
|
+
|
|
775
|
+
## 🛠️ Built with
|
|
776
|
+
|
|
777
|
+
- [yargs](http://yargs.js.org/)
|
|
778
|
+
- [tslog](https://tslog.js.org/#/)
|
|
779
|
+
- [subslate](https://github.com/josh-hemphill/subslate)
|
|
780
|
+
- [merge-deep](https://github.com/jonschlinkert/merge-deep)
|
|
781
|
+
- [ajv](https://ajv.js.org/)
|
|
782
|
+
- [to-json-schema](https://www.npmjs.com/package/to-json-schema)
|
|
783
|
+
|
|
784
|
+
<p align="right">(<a href="#top">back to top</a>)</p>
|
|
785
|
+
|
|
786
|
+
---
|
|
787
|
+
|
|
788
|
+
<br />
|
|
789
|
+
|
|
790
|
+
<p align="center">
|
|
791
|
+
<img
|
|
792
|
+
width="15%"
|
|
793
|
+
src="https://upload.wikimedia.org/wikipedia/commons/0/09/Logo_ACHS.svg"
|
|
794
|
+
/>
|
|
795
|
+
<h2 align="center">ASOCIACIÓN CHILENA DE SEGURIDAD</h2>
|
|
796
|
+
<h3 align="center">Tranformación Digital ▪ Equipo de Desarrollo</h3>
|
|
797
|
+
</p>
|