@willbooster/wb 13.20.3 → 13.21.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/bin/dotenv.js ADDED
@@ -0,0 +1,208 @@
1
+ import childProcess from 'node:child_process';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+
5
+ import { config } from 'dotenv';
6
+ import { expand } from 'dotenv-expand';
7
+
8
+ export function runDotenvCommand(args) {
9
+ const { command, options } = parseDotenvArgs(args);
10
+ if (command.length === 0) {
11
+ console.error('Usage: wb dotenv [-c <environment>] [--env <file>] -- <command> [args...]');
12
+ process.exit(1);
13
+ }
14
+
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);
23
+ removeNpmAndYarnEnvironmentVariables(process.env);
24
+
25
+ const child = childProcess.spawn(command[0], command.slice(1), {
26
+ cwd,
27
+ env: process.env,
28
+ stdio: 'inherit',
29
+ });
30
+ child.on('error', (error) => {
31
+ console.error(error);
32
+ process.exit(1);
33
+ });
34
+ child.on('exit', (code, signal) => {
35
+ if (signal) {
36
+ process.kill(process.pid, signal);
37
+ return;
38
+ }
39
+ process.exit(code ?? 1);
40
+ });
41
+ }
42
+
43
+ function readAndApplyEnvironmentVariables(options, cwd) {
44
+ const envVars = readEnvironmentVariables(options, cwd);
45
+ for (const [key, value] of Object.entries(envVars)) {
46
+ if (!(key in process.env)) {
47
+ process.env[key] = value;
48
+ }
49
+ }
50
+ }
51
+
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
+ function readEnvFile(filePath) {
134
+ return config({ path: path.resolve(filePath), processEnv: {}, quiet: true }).parsed ?? {};
135
+ }
136
+
137
+ function removeNpmAndYarnEnvironmentVariables(envVars) {
138
+ if (envVars.PATH && envVars.BERRY_BIN_FOLDER) {
139
+ envVars.PATH = envVars.PATH.replace(`${envVars.BERRY_BIN_FOLDER}:`, '')
140
+ .replaceAll(/\/private\/var\/folders\/[^:]+:/g, '')
141
+ .replaceAll(/\/var\/tmp\/[^:]+:/g, '')
142
+ .replaceAll(/\/tmp\/[^:]+:/g, '');
143
+ }
144
+ for (const key of Object.keys(envVars)) {
145
+ const upperKey = key.toUpperCase();
146
+ if (
147
+ upperKey.startsWith('NPM_') ||
148
+ upperKey.startsWith('YARN_') ||
149
+ upperKey.startsWith('BERRY_') ||
150
+ upperKey === 'PROJECT_CWD' ||
151
+ upperKey === 'INIT_CWD'
152
+ ) {
153
+ delete envVars[key];
154
+ }
155
+ }
156
+ }
157
+
158
+ 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;
208
+ }
package/bin/index.js CHANGED
@@ -1,3 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import '../dist/index.js';
3
+ if (process.argv[2] === 'dotenv') {
4
+ const { runDotenvCommand } = await import('./dotenv.js');
5
+ await runDotenvCommand(process.argv.slice(3));
6
+ } else {
7
+ await import('../dist/index.js');
8
+ }