@karmaniverous/get-dotenv 0.0.2 → 0.1.1

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/README.md CHANGED
@@ -12,11 +12,39 @@ Load environment variables with a cascade of environment-aware dotenv files. You
12
12
  - Log the result to the console.
13
13
 
14
14
  The command-line version can pull the environment designator from a number of sources, populate `process.env`, and execute a shell command.
15
+
16
+ `getdotenv` relies on the excellent [`dotenv`](https://www.npmjs.com/package/dotenv) parser and uses [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) for recursive variable expansion.
17
+
18
+ **So why not just use the very popular [`dotenv-cli`](https://www.npmjs.com/package/dotenv-cli) package to load dotenv file cascades?** A couple of reasons:
19
+
20
+ - `dotenv-cli` assumes all your `.env` files are located in your root directory. If you have a lot of environments, you probably want to sequester them into their own directory.
21
+
22
+ - The `dotenv-cli` syntax requires the environment to be set (the `-c` argument) _before_ the shell command is articulated (after the `--`). This doesn't work if you want to write an NPM script that takes the environment as an argument, since in that case it would have to go at the end.
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ npm install @karmaniverous/get-dotenv
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ```js
33
+ import { getDotenv, getDotenvSync } from '@karmaniverous/get-dotenv';
34
+
35
+ // asynchronous
36
+ const dotenv = await getDotenv(options);
37
+
38
+ // synchronous
39
+ const dotenvSync = await getDotenvSync(options);
40
+ ```
41
+
42
+ See [OptionsType](#optionstype--object) below for configuration options.
15
43
 
16
44
  # Command Line Interface
17
45
 
18
46
  ```text
19
- Usage: getdotenv [options]
47
+ Usage: getdotenv [options] [-- [command]]
20
48
 
21
49
  Load environment variables with a cascade of environment-aware
22
50
  dotenv files. You can:
@@ -24,10 +52,12 @@ dotenv files. You can:
24
52
  * Specify the directory containing your dotenv files.
25
53
  * Specify the token that identifies dotenv files (e.g. '.env').
26
54
  * Specify the token that identifies private vatiables (e.g. '.local').
27
- * Specify a default environment, override the default with an
55
+ * Specify a default environment, override the default with an existing
28
56
  environment variable, and override both with a direct setting.
29
57
  * Exclude public or private variables.
30
58
  * Execute a shell command after loading variables.
59
+ * Place the shell command inside the invocation to support npm script
60
+ arguments for other options.
31
61
 
32
62
  Options:
33
63
  -p, --path <string> path to dotenv directory (default './')
@@ -38,22 +68,18 @@ Options:
38
68
  -v, --variable <string> environment from variable
39
69
  -r, --exclude-private exclude private variables (default: false)
40
70
  -u, --exclude-public exclude public variables (default: false)
41
- -c, --command <string> shell command
71
+ -c, --command <string> shell command string
42
72
  -l, --log log extracted variables (default: false)
43
73
  -h, --help display help for command
44
74
  ```
45
75
 
46
76
  # API Documentation
47
77
 
48
- ```js
49
- import { foo, PACKAGE_INFO } from '@karmaniverous/npm-package-template`;
50
- ```
51
-
52
78
  ## Functions
53
79
 
54
80
  <dl>
55
81
  <dt><a href="#getDotenv">getDotenv([options])</a> ⇒ <code>Object</code></dt>
56
- <dd><p>Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]</p>
82
+ <dd><p>Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATE_TOKEN>]</p>
57
83
  </dd>
58
84
  <dt><a href="#getDotenvSync">getDotenvSync([options])</a> ⇒ <code>Object</code></dt>
59
85
  <dd><p>Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]</p>
@@ -71,7 +97,7 @@ import { foo, PACKAGE_INFO } from '@karmaniverous/npm-package-template`;
71
97
  <a name="getDotenv"></a>
72
98
 
73
99
  ## getDotenv([options]) ⇒ <code>Object</code>
74
- Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]
100
+ Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATE_TOKEN>]
75
101
 
76
102
  **Kind**: global function
77
103
  **Returns**: <code>Object</code> - The combined parsed dotenv object.
@@ -108,7 +134,7 @@ get-dotenv options type
108
134
  | [excludePublic] | <code>bool</code> | exclude public variables (default: false) |
109
135
  | [loadProcess] | <code>bool</code> | load dotenv to process.env (default: false) |
110
136
  | [log] | <code>bool</code> | log result to console (default: false) |
111
- | [path] | <code>string</code> | path to target directory |
137
+ | [path] | <code>string</code> | path to target directory (default './') |
112
138
  | [privateToken] | <code>string</code> | token indicating private variables (default: 'local'). |
113
139
 
114
140
 
@@ -1,26 +1,35 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ // Import npm packages.
4
+ import spawn from 'cross-spawn';
5
+ import { parseArgsStringToArgv } from 'string-argv';
6
+
3
7
  // Import package exports.
4
8
  import { getDotenvSync } from '@karmaniverous/get-dotenv';
5
- import { execSync } from 'child_process';
6
9
 
7
10
  // Create CLI.
8
11
  import { program } from 'commander';
9
12
 
10
13
  // CLI description.
11
- program.name('getdotenv');
12
- program.description(
13
- `Load environment variables with a cascade of environment-aware
14
- dotenv files. You can:
15
-
16
- * Specify the directory containing your dotenv files.
17
- * Specify the token that identifies dotenv files (e.g. '.env').
18
- * Specify the token that identifies private vatiables (e.g. '.local').
19
- * Specify a default environment, override the default with an
20
- environment variable, and override both with a direct setting.
21
- * Exclude public or private variables.
22
- * Execute a shell command after loading variables.`
23
- );
14
+ program
15
+ .name('getdotenv')
16
+ .usage('[options] [-- [command]]')
17
+ .description(
18
+ [
19
+ `Load environment variables with a cascade of environment-aware`,
20
+ `dotenv files. You can:`,
21
+ ``,
22
+ `* Specify the directory containing your dotenv files.`,
23
+ `* Specify the token that identifies dotenv files (e.g. '.env').`,
24
+ `* Specify the token that identifies private vatiables (e.g. '.local').`,
25
+ `* Specify a default environment, override the default with an existing`,
26
+ ` environment variable, and override both with a direct setting.`,
27
+ `* Exclude public or private variables.`,
28
+ `* Execute a shell command after loading variables.`,
29
+ `* Place the shell command inside the invocation to support npm script`,
30
+ ` arguments for other options.`,
31
+ ].join('\n')
32
+ );
24
33
 
25
34
  // CLI options.
26
35
  program
@@ -38,7 +47,7 @@ program
38
47
  .option('-v, --variable <string>', 'environment from variable')
39
48
  .option('-r, --exclude-private', 'exclude private variables (default: false)')
40
49
  .option('-u, --exclude-public', 'exclude public variables (default: false)')
41
- .option('-c, --command <string>', 'shell command')
50
+ .option('-c, --command <string>', 'shell command string')
42
51
  .option('-l, --log', 'log extracted variables (default: false)');
43
52
 
44
53
  // Parse CLI options from command line.
@@ -56,11 +65,13 @@ const {
56
65
  variable,
57
66
  } = program.opts();
58
67
 
68
+ if (command && program.args) program.error('command specified twice');
69
+
59
70
  // Get environment.
60
71
  const env = environment ?? process.env[variable] ?? defaultEnvironment;
61
72
 
62
73
  // Load dotenvs.
63
- const dotenv = getDotenvSync({
74
+ getDotenvSync({
64
75
  dotenvToken,
65
76
  env,
66
77
  excludePrivate,
@@ -72,4 +83,17 @@ const dotenv = getDotenvSync({
72
83
  });
73
84
 
74
85
  // Execute shell command.
75
- if (command) execSync(command, { env: dotenv, stdio: 'inherit' });
86
+ if (command || program.args) {
87
+ const argv = program.args ?? parseArgsStringToArgv(command);
88
+
89
+ spawn(argv[0], argv.slice(1), { stdio: 'inherit' }).on(
90
+ 'exit',
91
+ function (exitCode, signal) {
92
+ if (typeof exitCode === 'number') {
93
+ process.exit(exitCode);
94
+ } else {
95
+ process.kill(process.pid, signal);
96
+ }
97
+ }
98
+ );
99
+ }
@@ -19,12 +19,12 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
19
19
  * @property {bool} [excludePublic] - exclude public variables (default: false)
20
20
  * @property {bool} [loadProcess] - load dotenv to process.env (default: false)
21
21
  * @property {bool} [log] - log result to console (default: false)
22
- * @property {string} [path] - path to target directory
22
+ * @property {string} [path] - path to target directory (default './')
23
23
  * @property {string} [privateToken] - token indicating private variables (default: 'local').
24
24
  */
25
25
 
26
26
  /**
27
- * Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]
27
+ * Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATE_TOKEN>]
28
28
  *
29
29
  * @async
30
30
  * @function getDotenv
@@ -13,12 +13,12 @@ import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
13
13
  * @property {bool} [excludePublic] - exclude public variables (default: false)
14
14
  * @property {bool} [loadProcess] - load dotenv to process.env (default: false)
15
15
  * @property {bool} [log] - log result to console (default: false)
16
- * @property {string} [path] - path to target directory
16
+ * @property {string} [path] - path to target directory (default './')
17
17
  * @property {string} [privateToken] - token indicating private variables (default: 'local').
18
18
  */
19
19
 
20
20
  /**
21
- * Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]
21
+ * Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATE_TOKEN>]
22
22
  *
23
23
  * @async
24
24
  * @function getDotenv
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "bin": {
4
4
  "getdotenv": "bin/getdotenv/index.js"
5
5
  },
6
- "version": "0.0.2",
6
+ "version": "0.1.1",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
@@ -32,9 +32,11 @@
32
32
  "license": "BSD-3-Clause",
33
33
  "dependencies": {
34
34
  "commander": "^9.4.1",
35
+ "cross-spawn": "^7.0.3",
35
36
  "dotenv": "^16.0.3",
36
37
  "dotenv-expand": "^10.0.0",
37
- "fs-extra": "^11.1.0"
38
+ "fs-extra": "^11.1.0",
39
+ "string-argv": "^0.3.1"
38
40
  },
39
41
  "devDependencies": {
40
42
  "@babel/cli": "^7.20.7",
@@ -46,7 +48,6 @@
46
48
  "@types/node": "^18.11.18",
47
49
  "chai": "^4.3.7",
48
50
  "concat-md": "^0.5.0",
49
- "dotenv-cli": "^6.0.0",
50
51
  "eslint": "^8.31.0",
51
52
  "eslint-config-standard": "^17.0.0",
52
53
  "eslint-plugin-mocha": "^10.1.0",
@@ -85,7 +86,7 @@
85
86
  "build": "babel lib -d dist/default/lib --delete-dir-on-start --config-file ./dist/default/.babelrc",
86
87
  "doc": "jsdoc2md -c doc/jsdoc.config.json -f lib/**/*.* -t doc/api-template.hbs > doc/3-api.md && concat-md doc --hide-anchor-links > README.md",
87
88
  "package": "npm run lint && npm run test && npm run build && npm run doc",
88
- "release": "npm run package && dotenv -c -- release-it"
89
+ "release": "npm run package && node ./bin/getdotenv -- release-it"
89
90
  },
90
91
  "type": "module"
91
92
  }