-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.mjs
71 lines (63 loc) · 1.81 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
#!/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 localEnvPath = '.env.local';
const fileExists = (path) =>
stat(path).then(
() => true,
() => false,
);
const args = yargs(hideBin(process.argv)).argv;
const localEnvFileExists = await fileExists(localEnvPath);
if (args.check && localEnvFileExists) process.exit(0);
const base = {
...((await fileExists(developmentEnvPath)) && parse(await readFile(developmentEnvPath))),
...(localEnvFileExists && parse(await readFile(localEnvPath))),
};
const { environment } = {
...args,
...(await inquirer.prompt(
[
{
message: 'Choose an environment',
name: 'environment',
type: 'list',
choices: [
{ name: 'Local', value: 'local' },
{ name: 'Production', value: 'production' },
{ name: 'Standalone', value: 'standalone' },
],
default: 'local',
describe: 'Environment',
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' && {
SERVER: server,
}),
ENVIRONMENT: environment,
};
await writeFile(localEnvPath, stringify(data));