-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
63 lines (56 loc) · 1.32 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { spawn } from 'child_process';
import dotenv from 'dotenv';
import dotenvExpand from 'dotenv-expand';
import minimist from 'minimist';
import { existsSync, realpathSync } from 'node:fs';
function resolveFile(file: string) {
const path = realpathSync(process.cwd());
return `${path}/${file}`;
}
type Argv = {
e: string;
env: string;
p: string;
path: string;
};
function getEnvFiles(argv: minimist.ParsedArgs & Argv) {
const envKey = argv.e || argv.env || '';
const envVal = process.env[envKey] ?? '';
const path = argv.p || argv.path || '';
return [
resolveFile(path),
resolveFile(`.env.${envVal}`),
resolveFile('.env.local'),
resolveFile('.env'),
].filter(Boolean);
}
export const main = (args: string[]) => {
const argv = minimist<Argv>(args, {
'--': true,
alias: {
e: 'env',
p: 'path',
},
default: {
e: 'APP_ENV',
p: '',
},
});
const dotenvFiles = getEnvFiles(argv);
dotenvFiles.forEach((dotenvFile) => {
if (existsSync(dotenvFile)) {
dotenvExpand.expand(
dotenv.config({
path: dotenvFile,
})
);
}
});
if (argv['--'] && argv['--'].length) {
spawn(argv['--'][0], argv['--'].slice(1), {
stdio: 'inherit',
}).on('exit', function (exitCode) {
process.exit(exitCode);
});
}
};