@willbooster/wb 13.21.0 → 13.21.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/bin/dotenv.js CHANGED
@@ -1,25 +1,18 @@
1
1
  import childProcess from 'node:child_process';
2
- import fs from 'node:fs';
3
2
  import path from 'node:path';
4
3
 
5
4
  import { config } from 'dotenv';
6
5
  import { expand } from 'dotenv-expand';
7
6
 
8
7
  export function runDotenvCommand(args) {
9
- const { command, options } = parseDotenvArgs(args);
8
+ const { command } = parseDotenvArgs(args);
10
9
  if (command.length === 0) {
11
- console.error('Usage: wb dotenv [-c <environment>] [--env <file>] -- <command> [args...]');
10
+ console.error('Usage: wb dotenv -- <command> [args...]');
12
11
  process.exit(1);
13
12
  }
14
13
 
15
- const cwd = path.resolve(options.workingDir ?? process.cwd());
16
- if (options.workingDir) {
17
- process.chdir(cwd);
18
- }
19
- if (options.cascadeEnv) {
20
- process.env.WB_ENV ||= options.cascadeEnv;
21
- }
22
- readAndApplyEnvironmentVariables(options, cwd);
14
+ const cwd = path.resolve(process.cwd());
15
+ readAndApplyEnvironmentVariables(cwd);
23
16
  removeNpmAndYarnEnvironmentVariables(process.env);
24
17
 
25
18
  const child = childProcess.spawn(command[0], command.slice(1), {
@@ -40,8 +33,8 @@ export function runDotenvCommand(args) {
40
33
  });
41
34
  }
42
35
 
43
- function readAndApplyEnvironmentVariables(options, cwd) {
44
- const envVars = readEnvironmentVariables(options, cwd);
36
+ function readAndApplyEnvironmentVariables(cwd) {
37
+ const envVars = readEnvFile(path.join(cwd, '.env'));
45
38
  for (const [key, value] of Object.entries(envVars)) {
46
39
  if (!(key in process.env)) {
47
40
  process.env[key] = value;
@@ -49,89 +42,9 @@ function readAndApplyEnvironmentVariables(options, cwd) {
49
42
  }
50
43
  }
51
44
 
52
- function readEnvironmentVariables(options, cwd) {
53
- let envPaths = (options.env ?? []).map((envPath) => path.resolve(cwd, envPath));
54
- if (options.cascadeEnv) {
55
- if (envPaths.length === 0) {
56
- envPaths.push(path.join(cwd, '.env'));
57
- if (options.includeRootEnv ?? true) {
58
- const rootPath = path.resolve(cwd, '..', '..');
59
- if (fs.existsSync(path.join(rootPath, 'package.json'))) {
60
- envPaths.push(path.join(rootPath, '.env'));
61
- }
62
- }
63
- }
64
- envPaths = envPaths.flatMap((envPath) => [
65
- `${envPath}.${options.cascadeEnv}.local`,
66
- `${envPath}.local`,
67
- `${envPath}.${options.cascadeEnv}`,
68
- envPath,
69
- ]);
70
- }
71
- const envVars = {};
72
- for (const envPath of envPaths) {
73
- if (!fs.existsSync(envPath)) continue;
74
-
75
- for (const [key, value] of Object.entries(readEnvFile(envPath))) {
76
- if (!(key in envVars) && !(key in process.env)) {
77
- envVars[key] = value;
78
- }
79
- }
80
- }
81
- Object.assign(envVars, readMiseEnvironmentVariables(cwd, options.cascadeEnv, envVars));
82
- if (options.checkEnv) {
83
- const missingKeys = Object.keys(readEnvFile(path.join(cwd, options.checkEnv))).filter(
84
- (key) => !(key in envVars) && !(key in process.env)
85
- );
86
- if (missingKeys.length > 0) {
87
- throw new Error(`Missing environment variables: [${missingKeys.join(', ')}]`);
88
- }
89
- }
90
- return expand({ parsed: envVars, processEnv: {} }).parsed ?? envVars;
91
- }
92
-
93
- function readMiseEnvironmentVariables(cwd, cascadeEnv, currentEnvVars) {
94
- if (!hasProjectMiseConfig(cwd)) return {};
95
-
96
- const args = ['env', '--json', '--cd', cwd];
97
- if (cascadeEnv) {
98
- args.push('--env', cascadeEnv);
99
- }
100
- const result = childProcess.spawnSync('mise', args, {
101
- encoding: 'utf8',
102
- stdio: ['ignore', 'pipe', 'pipe'],
103
- });
104
- if (result.error || result.status !== 0 || !result.stdout?.trim()) return {};
105
-
106
- let parsed;
107
- try {
108
- parsed = JSON.parse(result.stdout);
109
- } catch {
110
- return {};
111
- }
112
- if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
113
-
114
- const envVars = {};
115
- for (const [key, value] of Object.entries(parsed)) {
116
- if (typeof value !== 'string') continue;
117
- if (key in currentEnvVars || process.env[key] === value) continue;
118
- envVars[key] = value;
119
- }
120
- return envVars;
121
- }
122
-
123
- function hasProjectMiseConfig(cwd) {
124
- for (let currentPath = path.resolve(cwd); ; currentPath = path.dirname(currentPath)) {
125
- if (fs.existsSync(path.join(currentPath, 'mise.toml')) || fs.existsSync(path.join(currentPath, '.mise.toml'))) {
126
- return true;
127
- }
128
- const parentPath = path.dirname(currentPath);
129
- if (parentPath === currentPath) return false;
130
- }
131
- }
132
-
133
45
  function readEnvFile(filePath) {
134
- return config({ path: path.resolve(filePath), processEnv: {}, quiet: true }).parsed ?? {};
46
+ const parsed = config({ path: path.resolve(filePath), processEnv: {}, quiet: true }).parsed ?? {};
47
+ return expand({ parsed, processEnv: {} }).parsed ?? parsed;
135
48
  }
136
49
 
137
50
  function removeNpmAndYarnEnvironmentVariables(envVars) {
@@ -156,53 +69,6 @@ function removeNpmAndYarnEnvironmentVariables(envVars) {
156
69
  }
157
70
 
158
71
  function parseDotenvArgs(args) {
159
- const options = {};
160
- for (let index = 0; index < args.length; index++) {
161
- const arg = args[index];
162
- if (arg === '--') {
163
- return normalizeParsedDotenvArgs({ command: args.slice(index + 1), options });
164
- }
165
- if (!arg.startsWith('-')) {
166
- return normalizeParsedDotenvArgs({ command: args.slice(index), options });
167
- }
168
-
169
- const nextValue = () => {
170
- const value = args[++index];
171
- if (!value) {
172
- throw new Error(`Missing value for ${arg}`);
173
- }
174
- return value;
175
- };
176
- if (arg === '-c' || arg === '--cascade-env') {
177
- options.cascadeEnv = nextValue();
178
- } else if (arg.startsWith('--cascade-env=')) {
179
- options.cascadeEnv = arg.slice('--cascade-env='.length);
180
- } else if (arg === '-e' || arg === '--env') {
181
- options.env = [...(options.env ?? []), nextValue()];
182
- } else if (arg.startsWith('--env=')) {
183
- options.env = [...(options.env ?? []), arg.slice('--env='.length)];
184
- } else if (arg === '--check-env') {
185
- options.checkEnv = nextValue();
186
- } else if (arg.startsWith('--check-env=')) {
187
- options.checkEnv = arg.slice('--check-env='.length);
188
- } else if (arg === '--include-root-env') {
189
- options.includeRootEnv = true;
190
- } else if (arg === '--no-include-root-env') {
191
- options.includeRootEnv = false;
192
- } else if (arg === '--working-dir') {
193
- options.workingDir = nextValue();
194
- } else if (arg.startsWith('--working-dir=')) {
195
- options.workingDir = arg.slice('--working-dir='.length);
196
- } else {
197
- throw new Error(`Unknown wb dotenv option: ${arg}`);
198
- }
199
- }
200
- return normalizeParsedDotenvArgs({ command: [], options });
201
- }
202
-
203
- function normalizeParsedDotenvArgs(parsed) {
204
- if (!parsed.options.cascadeEnv && !parsed.options.env) {
205
- parsed.options.env = ['.env'];
206
- }
207
- return parsed;
72
+ const separatorIndex = args.indexOf('--');
73
+ return { command: separatorIndex === -1 ? args : args.slice(separatorIndex + 1) };
208
74
  }