@karmaniverous/get-dotenv 1.1.1 → 2.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/README.md CHANGED
@@ -73,27 +73,31 @@ dotenv files. You can:
73
73
  * Specify the directories containing your dotenv files.
74
74
  * Specify the token that identifies dotenv files (e.g. '.env').
75
75
  * Specify the token that identifies private vatiables (e.g. '.local').
76
+ * Load variables for a specific environment or none.
76
77
  * Specify a default environment, override the default with an existing
77
78
  environment variable, and override both with a direct setting.
78
79
  * Exclude public or private variables.
80
+ * Exclude global & dynamic or environment-specific variables.
79
81
  * Define dynamic variables progressively in terms of other variables and
80
82
  other logic.
81
- * Execute a shell command after loading variables.
82
- * Place the shell command inside the invocation to support npm script
83
+ * Execute a &&-delimited series of shell commands after loading variables.
84
+ * Place the shell commands inside the invocation to support npm script
83
85
  arguments for other options.
84
86
 
85
87
  Options:
86
88
  -p, --paths <strings...> space-delimited paths to dotenv directory (default './')
87
- -t, --dotenv-token <string> token indicating a dotenv file (default: '.env')
88
- -i, --private-token <string> token indicating private variables (default: 'local')
89
- -d, --defaultEnvironment <string> default environment
90
- -e, --environment <string> designated environment
91
- -v, --variable <string> environment from variable
89
+ -y, --dynamic-path <string> dynamic variables path
90
+ -d, --defaultEnvironment <string> default environment (prefix with $ to use environment variable)
91
+ -e, --environment <string> designated environment (prefix with $ to use environment variable)
92
+ -n, --exclude-env exclude environment-specific variables (default: false)
93
+ -g, --exclude-global exclude global & dynamic variables (default: false)
92
94
  -r, --exclude-private exclude private variables (default: false)
93
95
  -u, --exclude-public exclude public variables (default: false)
94
- -y, --dynamic-path <string> dynamic variables path
96
+ -z, --exclude-dynamic exclude dynamic variables (default: false)
95
97
  -c, --command <string> shell command string
96
98
  -l, --log log extracted variables (default: false)
99
+ -t, --dotenv-token <string> token indicating a dotenv file (default: '.env')
100
+ -i, --private-token <string> token indicating private variables (default: 'local')
97
101
  -h, --help display help for command
98
102
  ```
99
103
 
@@ -155,6 +159,9 @@ get-dotenv options type
155
159
  | [dotenvToken] | <code>string</code> | token indicating a dotenv file (default: '.env') |
156
160
  | [dynamicPath] | <code>string</code> | path to file exporting an object keyed to dynamic variable functions |
157
161
  | [env] | <code>string</code> | target environment |
162
+ | [excludeDynamic] | <code>bool</code> | exclude dynamic variables (default: false) |
163
+ | [excludeEnv] | <code>bool</code> | exclude environment-specific variables (default: false) |
164
+ | [excludeGlobal] | <code>bool</code> | exclude global & dynamic variables (default: false) |
158
165
  | [excludePrivate] | <code>bool</code> | exclude private variables (default: false) |
159
166
  | [excludePublic] | <code>bool</code> | exclude public variables (default: false) |
160
167
  | [loadProcess] | <code>bool</code> | load dotenv to process.env (default: false) |
@@ -2,6 +2,7 @@
2
2
 
3
3
  // Import npm packages.
4
4
  import spawn from 'cross-spawn';
5
+ import _ from 'lodash';
5
6
  import { parseArgsStringToArgv } from 'string-argv';
6
7
 
7
8
  // Import package exports.
@@ -10,6 +11,11 @@ import { getDotenv } from '@karmaniverous/get-dotenv';
10
11
  // Create CLI.
11
12
  import { program } from 'commander';
12
13
 
14
+ const envMerge = (value) =>
15
+ !_.isUndefined(value) && value.startsWith('$')
16
+ ? process.env[value.slice(1)]
17
+ : value;
18
+
13
19
  // CLI description.
14
20
  program
15
21
  .name('getdotenv')
@@ -26,6 +32,7 @@ program
26
32
  `* Specify a default environment, override the default with an existing`,
27
33
  ` environment variable, and override both with a direct setting.`,
28
34
  `* Exclude public or private variables.`,
35
+ `* Exclude global & dynamic or environment-specific variables.`,
29
36
  `* Define dynamic variables progressively in terms of other variables and`,
30
37
  ` other logic.`,
31
38
  `* Execute a &&-delimited series of shell commands after loading variables.`,
@@ -40,22 +47,38 @@ program
40
47
  '-p, --paths <strings...>',
41
48
  "space-delimited paths to dotenv directory (default './')"
42
49
  )
50
+ .option('-y, --dynamic-path <string>', 'dynamic variables path')
43
51
  .option(
44
- '-t, --dotenv-token <string>',
45
- "token indicating a dotenv file (default: '.env')"
52
+ '-d, --defaultEnvironment <string>',
53
+ 'default environment (prefix with $ to use environment variable)',
54
+ envMerge
46
55
  )
47
56
  .option(
48
- '-i, --private-token <string>',
49
- "token indicating private variables (default: 'local')"
57
+ '-e, --environment <string>',
58
+ 'designated environment (prefix with $ to use environment variable)',
59
+ envMerge
60
+ )
61
+ .option(
62
+ '-n, --exclude-env',
63
+ 'exclude environment-specific variables (default: false)'
64
+ )
65
+ .option(
66
+ '-g, --exclude-global',
67
+ 'exclude global & dynamic variables (default: false)'
50
68
  )
51
- .option('-d, --defaultEnvironment <string>', 'default environment')
52
- .option('-e, --environment <string>', 'designated environment')
53
- .option('-v, --variable <string>', 'environment from variable')
54
69
  .option('-r, --exclude-private', 'exclude private variables (default: false)')
55
70
  .option('-u, --exclude-public', 'exclude public variables (default: false)')
56
- .option('-y, --dynamic-path <string>', 'dynamic variables path')
71
+ .option('-z, --exclude-dynamic', 'exclude dynamic variables (default: false)')
57
72
  .option('-c, --command <string>', 'shell command string')
58
- .option('-l, --log', 'log extracted variables (default: false)');
73
+ .option('-l, --log', 'log extracted variables (default: false)')
74
+ .option(
75
+ '-t, --dotenv-token <string>',
76
+ "token indicating a dotenv file (default: '.env')"
77
+ )
78
+ .option(
79
+ '-i, --private-token <string>',
80
+ "token indicating private variables (default: 'local')"
81
+ );
59
82
 
60
83
  // Parse CLI options from command line.
61
84
  program.parse();
@@ -64,24 +87,27 @@ const {
64
87
  defaultEnvironment,
65
88
  dotenvToken,
66
89
  environment,
90
+ excludeEnv,
91
+ excludeGlobal,
67
92
  excludePrivate,
68
93
  excludePublic,
69
94
  dynamicPath,
70
95
  log,
71
96
  paths,
72
97
  privateToken,
73
- variable,
74
98
  } = program.opts();
75
99
 
76
100
  if (command && program.args.length) program.error('command specified twice');
77
101
 
78
102
  // Get environment.
79
- const env = environment ?? process.env[variable] ?? defaultEnvironment;
103
+ const env = environment ?? defaultEnvironment;
80
104
 
81
105
  // Load dotenvs.
82
106
  await getDotenv({
83
107
  dotenvToken,
84
108
  env,
109
+ excludeEnv,
110
+ excludeGlobal,
85
111
  excludePrivate,
86
112
  excludePublic,
87
113
  loadProcess: true,
@@ -21,6 +21,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
21
21
  * @property {string} [dotenvToken] - token indicating a dotenv file (default: '.env')
22
22
  * @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
23
23
  * @property {string} [env] - target environment
24
+ * @property {bool} [excludeDynamic] - exclude dynamic variables (default: false)
25
+ * @property {bool} [excludeEnv] - exclude environment-specific variables (default: false)
26
+ * @property {bool} [excludeGlobal] - exclude global & dynamic variables (default: false)
24
27
  * @property {bool} [excludePrivate] - exclude private variables (default: false)
25
28
  * @property {bool} [excludePublic] - exclude public variables (default: false)
26
29
  * @property {bool} [loadProcess] - load dotenv to process.env (default: false)
@@ -44,6 +47,9 @@ const getDotenv = async function () {
44
47
  dotenvToken = '.env',
45
48
  env,
46
49
  dynamicPath,
50
+ excludeDynamic = false,
51
+ excludeEnv = false,
52
+ excludeGlobal = false,
47
53
  excludePrivate = false,
48
54
  excludePublic = false,
49
55
  loadProcess = false,
@@ -55,12 +61,12 @@ const getDotenv = async function () {
55
61
  const parsed = await paths.reduce(async (e, p) => ({
56
62
  ...(await e),
57
63
  ...(excludePublic ? {} : {
58
- ...(await (0, _readDotenv.readDotenv)(_path.default.resolve(p, dotenvToken))),
59
- ...(env ? await (0, _readDotenv.readDotenv)(_path.default.resolve(p, `${dotenvToken}.${env}`)) : {})
64
+ ...(excludeGlobal ? {} : await (0, _readDotenv.readDotenv)(_path.default.resolve(p, dotenvToken))),
65
+ ...(env && !excludeEnv ? await (0, _readDotenv.readDotenv)(_path.default.resolve(p, `${dotenvToken}.${env}`)) : {})
60
66
  }),
61
67
  ...(excludePrivate ? {} : {
62
- ...(await (0, _readDotenv.readDotenv)(_path.default.resolve(p, `${dotenvToken}.${privateToken}`))),
63
- ...(env ? await (0, _readDotenv.readDotenv)(_path.default.resolve(p, `${dotenvToken}.${env}.${privateToken}`)) : {})
68
+ ...(excludeGlobal ? {} : await (0, _readDotenv.readDotenv)(_path.default.resolve(p, `${dotenvToken}.${privateToken}`))),
69
+ ...(env && !excludeEnv ? await (0, _readDotenv.readDotenv)(_path.default.resolve(p, `${dotenvToken}.${env}.${privateToken}`)) : {})
64
70
  })
65
71
  }), []);
66
72
  const {
@@ -73,7 +79,7 @@ const getDotenv = async function () {
73
79
  if (error) throw new Error(error);
74
80
 
75
81
  // Process dynamic variables.
76
- if (!excludePublic && dynamicPath) {
82
+ if (dynamicPath && !excludeDynamic) {
77
83
  const dynamic = new Function(`return ${(await _fsExtra.default.readFile(dynamicPath)).toString()}`)()(dotenv);
78
84
  Object.keys(dynamic).forEach(key => {
79
85
  Object.assign(dotenv, {
@@ -105,6 +111,9 @@ const getDotenvSync = function () {
105
111
  dotenvToken = '.env',
106
112
  dynamicPath,
107
113
  env,
114
+ excludeDynamic = false,
115
+ excludeEnv = false,
116
+ excludeGlobal = false,
108
117
  excludePrivate = false,
109
118
  excludePublic = false,
110
119
  loadProcess = false,
@@ -116,12 +125,12 @@ const getDotenvSync = function () {
116
125
  const parsed = paths.reduce((e, p) => ({
117
126
  ...e,
118
127
  ...(excludePublic ? {} : {
119
- ...(0, _readDotenv.readDotenvSync)(_path.default.resolve(p, dotenvToken)),
120
- ...(env ? (0, _readDotenv.readDotenvSync)(_path.default.resolve(p, `${dotenvToken}.${env}`)) : {})
128
+ ...(excludeGlobal ? {} : (0, _readDotenv.readDotenvSync)(_path.default.resolve(p, dotenvToken))),
129
+ ...(env && !excludeEnv ? (0, _readDotenv.readDotenvSync)(_path.default.resolve(p, `${dotenvToken}.${env}`)) : {})
121
130
  }),
122
131
  ...(excludePrivate ? {} : {
123
- ...(0, _readDotenv.readDotenvSync)(_path.default.resolve(p, `${dotenvToken}.${privateToken}`)),
124
- ...(env ? (0, _readDotenv.readDotenvSync)(_path.default.resolve(p, `${dotenvToken}.${env}.${privateToken}`)) : {})
132
+ ...(excludeGlobal ? {} : (0, _readDotenv.readDotenvSync)(_path.default.resolve(p, `${dotenvToken}.${privateToken}`))),
133
+ ...(env && !excludeEnv ? (0, _readDotenv.readDotenvSync)(_path.default.resolve(p, `${dotenvToken}.${env}.${privateToken}`)) : {})
125
134
  })
126
135
  }), []);
127
136
  const {
@@ -134,7 +143,7 @@ const getDotenvSync = function () {
134
143
  if (error) throw new Error(error);
135
144
 
136
145
  // Process dynamic variables.
137
- if (dynamicPath) {
146
+ if (dynamicPath && !excludeDynamic) {
138
147
  const dynamic = new Function(`return ${_fsExtra.default.readFileSync(dynamicPath).toString()}`)()(dotenv);
139
148
  Object.keys(dynamic).forEach(key => {
140
149
  Object.assign(dotenv, {
@@ -14,6 +14,9 @@ import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
14
14
  * @property {string} [dotenvToken] - token indicating a dotenv file (default: '.env')
15
15
  * @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
16
16
  * @property {string} [env] - target environment
17
+ * @property {bool} [excludeDynamic] - exclude dynamic variables (default: false)
18
+ * @property {bool} [excludeEnv] - exclude environment-specific variables (default: false)
19
+ * @property {bool} [excludeGlobal] - exclude global & dynamic variables (default: false)
17
20
  * @property {bool} [excludePrivate] - exclude private variables (default: false)
18
21
  * @property {bool} [excludePublic] - exclude public variables (default: false)
19
22
  * @property {bool} [loadProcess] - load dotenv to process.env (default: false)
@@ -36,6 +39,9 @@ export const getDotenv = async ({
36
39
  dotenvToken = '.env',
37
40
  env,
38
41
  dynamicPath,
42
+ excludeDynamic = false,
43
+ excludeEnv = false,
44
+ excludeGlobal = false,
39
45
  excludePrivate = false,
40
46
  excludePublic = false,
41
47
  loadProcess = false,
@@ -50,18 +56,22 @@ export const getDotenv = async ({
50
56
  ...(excludePublic
51
57
  ? {}
52
58
  : {
53
- ...(await readDotenv(path.resolve(p, dotenvToken))),
54
- ...(env
59
+ ...(excludeGlobal
60
+ ? {}
61
+ : await readDotenv(path.resolve(p, dotenvToken))),
62
+ ...(env && !excludeEnv
55
63
  ? await readDotenv(path.resolve(p, `${dotenvToken}.${env}`))
56
64
  : {}),
57
65
  }),
58
66
  ...(excludePrivate
59
67
  ? {}
60
68
  : {
61
- ...(await readDotenv(
62
- path.resolve(p, `${dotenvToken}.${privateToken}`)
63
- )),
64
- ...(env
69
+ ...(excludeGlobal
70
+ ? {}
71
+ : await readDotenv(
72
+ path.resolve(p, `${dotenvToken}.${privateToken}`)
73
+ )),
74
+ ...(env && !excludeEnv
65
75
  ? await readDotenv(
66
76
  path.resolve(p, `${dotenvToken}.${env}.${privateToken}`)
67
77
  )
@@ -79,7 +89,7 @@ export const getDotenv = async ({
79
89
  if (error) throw new Error(error);
80
90
 
81
91
  // Process dynamic variables.
82
- if (!excludePublic && dynamicPath) {
92
+ if (dynamicPath && !excludeDynamic) {
83
93
  const dynamic = new Function(
84
94
  `return ${(await fs.readFile(dynamicPath)).toString()}`
85
95
  )()(dotenv);
@@ -110,6 +120,9 @@ export const getDotenvSync = ({
110
120
  dotenvToken = '.env',
111
121
  dynamicPath,
112
122
  env,
123
+ excludeDynamic = false,
124
+ excludeEnv = false,
125
+ excludeGlobal = false,
113
126
  excludePrivate = false,
114
127
  excludePublic = false,
115
128
  loadProcess = false,
@@ -124,18 +137,22 @@ export const getDotenvSync = ({
124
137
  ...(excludePublic
125
138
  ? {}
126
139
  : {
127
- ...readDotenvSync(path.resolve(p, dotenvToken)),
128
- ...(env
140
+ ...(excludeGlobal
141
+ ? {}
142
+ : readDotenvSync(path.resolve(p, dotenvToken))),
143
+ ...(env && !excludeEnv
129
144
  ? readDotenvSync(path.resolve(p, `${dotenvToken}.${env}`))
130
145
  : {}),
131
146
  }),
132
147
  ...(excludePrivate
133
148
  ? {}
134
149
  : {
135
- ...readDotenvSync(
136
- path.resolve(p, `${dotenvToken}.${privateToken}`)
137
- ),
138
- ...(env
150
+ ...(excludeGlobal
151
+ ? {}
152
+ : readDotenvSync(
153
+ path.resolve(p, `${dotenvToken}.${privateToken}`)
154
+ )),
155
+ ...(env && !excludeEnv
139
156
  ? readDotenvSync(
140
157
  path.resolve(p, `${dotenvToken}.${env}.${privateToken}`)
141
158
  )
@@ -153,7 +170,7 @@ export const getDotenvSync = ({
153
170
  if (error) throw new Error(error);
154
171
 
155
172
  // Process dynamic variables.
156
- if (dynamicPath) {
173
+ if (dynamicPath && !excludeDynamic) {
157
174
  const dynamic = new Function(
158
175
  `return ${fs.readFileSync(dynamicPath).toString()}`
159
176
  )()(dotenv);
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "bin": {
4
4
  "getdotenv": "bin/getdotenv/index.js"
5
5
  },
6
- "version": "1.1.1",
6
+ "version": "2.0.0",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
@@ -82,6 +82,7 @@
82
82
  }
83
83
  },
84
84
  "scripts": {
85
+ "cli": "node ./bin/getdotenv",
85
86
  "lint": "eslint lib/** bin/**",
86
87
  "test": "mocha",
87
88
  "build": "babel lib -d dist/default/lib --delete-dir-on-start --config-file ./dist/default/.babelrc",