@willbooster/wb 13.20.3 → 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 +74 -0
- package/bin/index.js +6 -1
- package/dist/index.js +3 -3
- package/package.json +1 -1
package/bin/dotenv.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import childProcess from 'node:child_process';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { config } from 'dotenv';
|
|
5
|
+
import { expand } from 'dotenv-expand';
|
|
6
|
+
|
|
7
|
+
export function runDotenvCommand(args) {
|
|
8
|
+
const { command } = parseDotenvArgs(args);
|
|
9
|
+
if (command.length === 0) {
|
|
10
|
+
console.error('Usage: wb dotenv -- <command> [args...]');
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const cwd = path.resolve(process.cwd());
|
|
15
|
+
readAndApplyEnvironmentVariables(cwd);
|
|
16
|
+
removeNpmAndYarnEnvironmentVariables(process.env);
|
|
17
|
+
|
|
18
|
+
const child = childProcess.spawn(command[0], command.slice(1), {
|
|
19
|
+
cwd,
|
|
20
|
+
env: process.env,
|
|
21
|
+
stdio: 'inherit',
|
|
22
|
+
});
|
|
23
|
+
child.on('error', (error) => {
|
|
24
|
+
console.error(error);
|
|
25
|
+
process.exit(1);
|
|
26
|
+
});
|
|
27
|
+
child.on('exit', (code, signal) => {
|
|
28
|
+
if (signal) {
|
|
29
|
+
process.kill(process.pid, signal);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
process.exit(code ?? 1);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function readAndApplyEnvironmentVariables(cwd) {
|
|
37
|
+
const envVars = readEnvFile(path.join(cwd, '.env'));
|
|
38
|
+
for (const [key, value] of Object.entries(envVars)) {
|
|
39
|
+
if (!(key in process.env)) {
|
|
40
|
+
process.env[key] = value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readEnvFile(filePath) {
|
|
46
|
+
const parsed = config({ path: path.resolve(filePath), processEnv: {}, quiet: true }).parsed ?? {};
|
|
47
|
+
return expand({ parsed, processEnv: {} }).parsed ?? parsed;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function removeNpmAndYarnEnvironmentVariables(envVars) {
|
|
51
|
+
if (envVars.PATH && envVars.BERRY_BIN_FOLDER) {
|
|
52
|
+
envVars.PATH = envVars.PATH.replace(`${envVars.BERRY_BIN_FOLDER}:`, '')
|
|
53
|
+
.replaceAll(/\/private\/var\/folders\/[^:]+:/g, '')
|
|
54
|
+
.replaceAll(/\/var\/tmp\/[^:]+:/g, '')
|
|
55
|
+
.replaceAll(/\/tmp\/[^:]+:/g, '');
|
|
56
|
+
}
|
|
57
|
+
for (const key of Object.keys(envVars)) {
|
|
58
|
+
const upperKey = key.toUpperCase();
|
|
59
|
+
if (
|
|
60
|
+
upperKey.startsWith('NPM_') ||
|
|
61
|
+
upperKey.startsWith('YARN_') ||
|
|
62
|
+
upperKey.startsWith('BERRY_') ||
|
|
63
|
+
upperKey === 'PROJECT_CWD' ||
|
|
64
|
+
upperKey === 'INIT_CWD'
|
|
65
|
+
) {
|
|
66
|
+
delete envVars[key];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function parseDotenvArgs(args) {
|
|
72
|
+
const separatorIndex = args.indexOf('--');
|
|
73
|
+
return { command: separatorIndex === -1 ? args : args.slice(separatorIndex + 1) };
|
|
74
|
+
}
|
package/bin/index.js
CHANGED