@karmaniverous/get-dotenv 0.0.1 → 0.1.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/README.md CHANGED
@@ -12,11 +12,31 @@ 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
+ ## Installation
17
+
18
+ ```bash
19
+ npm install @karmaniverous/get-dotenv
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```js
25
+ import { getDotenv, getDotenvSync } from '@karmaniverous/get-dotenv';
26
+
27
+ // asynchronous
28
+ const dotenv = await getDotenv(options);
29
+
30
+ // synchronous
31
+ const dotenvSync = await getDotenvSync(options);
32
+ ```
33
+
34
+ See [OptionsType](#optionstype--object) below for configuration options.
15
35
 
16
36
  # Command Line Interface
17
37
 
18
38
  ```text
19
- Usage: getdotenv [options]
39
+ Usage: getdotenv [options] [-- [command]]
20
40
 
21
41
  Load environment variables with a cascade of environment-aware
22
42
  dotenv files. You can:
@@ -24,10 +44,12 @@ dotenv files. You can:
24
44
  * Specify the directory containing your dotenv files.
25
45
  * Specify the token that identifies dotenv files (e.g. '.env').
26
46
  * Specify the token that identifies private vatiables (e.g. '.local').
27
- * Specify a default environment, override the default with an
47
+ * Specify a default environment, override the default with an existing
28
48
  environment variable, and override both with a direct setting.
29
49
  * Exclude public or private variables.
30
50
  * Execute a shell command after loading variables.
51
+ * Place the shell command inside the invocation to support npm script
52
+ arguments for other options.
31
53
 
32
54
  Options:
33
55
  -p, --path <string> path to dotenv directory (default './')
@@ -38,25 +60,21 @@ Options:
38
60
  -v, --variable <string> environment from variable
39
61
  -r, --exclude-private exclude private variables (default: false)
40
62
  -u, --exclude-public exclude public variables (default: false)
41
- -c, --command <string> shell command
63
+ -c, --command <string> shell command string
42
64
  -l, --log log extracted variables (default: false)
43
65
  -h, --help display help for command
44
66
  ```
45
67
 
46
68
  # API Documentation
47
69
 
48
- ```js
49
- import { foo, PACKAGE_INFO } from '@karmaniverous/npm-package-template`;
50
- ```
51
-
52
70
  ## Functions
53
71
 
54
72
  <dl>
55
73
  <dt><a href="#getDotenv">getDotenv([options])</a> ⇒ <code>Object</code></dt>
56
74
  <dd><p>Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]</p>
57
75
  </dd>
58
- <dt><a href="#getDotenv">getDotenv([options])</a> ⇒ <code>Object</code></dt>
59
- <dd><p>Synchronously process dotenv files of the form .env[.<ENV>][.&lt;PRIVATEEXT]</p>
76
+ <dt><a href="#getDotenvSync">getDotenvSync([options])</a> ⇒ <code>Object</code></dt>
77
+ <dd><p>Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]</p>
60
78
  </dd>
61
79
  </dl>
62
80
 
@@ -80,10 +98,10 @@ Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]
80
98
  | --- | --- | --- |
81
99
  | [options] | [<code>OptionsType</code>](#OptionsType) | options object |
82
100
 
83
- <a name="getDotenv"></a>
101
+ <a name="getDotenvSync"></a>
84
102
 
85
- ## getDotenv([options]) ⇒ <code>Object</code>
86
- Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATEEXT]
103
+ ## getDotenvSync([options]) ⇒ <code>Object</code>
104
+ Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]
87
105
 
88
106
  **Kind**: global function
89
107
  **Returns**: <code>Object</code> - The combined parsed dotenv object.
@@ -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
+ }
@@ -65,9 +65,9 @@ const getDotenv = async function () {
65
65
  };
66
66
 
67
67
  /**
68
- * Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATEEXT]
68
+ * Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]
69
69
  *
70
- * @function getDotenv
70
+ * @function getDotenvSync
71
71
  *
72
72
  * @param {OptionsType} [options] - options object
73
73
  *
@@ -76,9 +76,9 @@ export const getDotenv = async ({
76
76
  };
77
77
 
78
78
  /**
79
- * Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATEEXT]
79
+ * Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]
80
80
  *
81
- * @function getDotenv
81
+ * @function getDotenvSync
82
82
  *
83
83
  * @param {OptionsType} [options] - options object
84
84
  *
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "bin": {
4
4
  "getdotenv": "bin/getdotenv/index.js"
5
5
  },
6
- "version": "0.0.1",
6
+ "version": "0.1.0",
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
  }