-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.mjs
88 lines (79 loc) · 2.12 KB
/
config.mjs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env node
import { readFile, stat, writeFile } from 'fs/promises';
import { parse, stringify } from 'envfile';
import { hideBin } from 'yargs/helpers';
import inquirer from 'inquirer';
import yargs from 'yargs';
const localServerUrl = 'http://localhost:3123';
const developmentEnvPath = '.env.development';
const localEnvFilePath = '.env.local';
const fileExists = (path) =>
stat(path).then(
() => true,
() => false
);
const args = yargs(hideBin(process.argv)).argv;
const localEnvFileExists = await fileExists(localEnvFilePath);
if (args.check && localEnvFileExists) process.exit(0);
const base = {
...((await fileExists(developmentEnvPath)) && parse(await readFile(developmentEnvPath))),
...(localEnvFileExists && parse(await readFile(localEnvFilePath)))
};
const { environment } = {
...args,
...(await inquirer.prompt(
[
{
message: 'Choose an environment',
name: 'environment',
type: 'list',
choices: [
{ name: 'Local', value: 'local' },
{ name: 'Development', value: 'development' },
{ name: 'Production', value: 'production' }
],
default: 'local',
describe: 'Environment',
demandOption: true
}
].filter((v) => !args[v.name])
))
};
const { baseurl } = {
...args,
...(await inquirer.prompt(
[
{
message: 'Base URL',
name: 'baseurl',
type: 'input',
default: '',
describe: 'URL',
demandOption: true
}
].filter((v) => !args[v.name])
))
};
const { server } =
(environment.toLowerCase() !== 'standalone' &&
(await inquirer.prompt([
{
message: 'Server location?',
name: 'server',
type: 'input',
default: localServerUrl,
describe: 'Server',
// validate: (v) => v.match(/^[a-z0-9]+$/i), // should return promise
demandOption: true
}
]))) ||
{};
const data = {
...base,
...(environment.toLowerCase() !== 'standalone' && {
BASE_URL: baseurl ?? '',
VITE_SERVER: server
}),
ENVIRONMENT: environment
};
await writeFile(`.env.${environment}`, stringify(data));