-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
107 lines (86 loc) · 2.89 KB
/
config.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const prompts = require('prompts')
const envfile = require('envfile')
const colors = require('colors')
const path = require('path')
const fs = require('fs')
const os = require('os')
const REFERENCE_ENV = '.env.example'
const DEVELOPMENT_ENV = '.env'
const PRODUCTION_ENV = '.env.production'
const SERVER_PORT = 8081
function getIPs() {
const interfaces = os.networkInterfaces()
const ipAddresses = []
Object.keys(interfaces).forEach((name) => {
interfaces[name].forEach((network) => {
if (network.family === 'IPv4') {
ipAddresses.push(network.address)
}
})
})
return ipAddresses
}
async function selectIP(...ips) {
switch (ips.length) {
case 0:
return undefined
case 1:
return ips[0]
default:
console.log('\n')
const response = await prompts({
type: 'select',
name: 'value',
message: 'Which IP address do you want to serve your application on?',
initial: 0,
choices: ips.map((ip) => ({ value: ip })),
})
return response.value
}
}
function createEnvFile(fullPath, fileName, overwriteVars = {}) {
const targetFile = path.join(fullPath, fileName)
const isUpdating = fs.existsSync(targetFile)
const sourceFile = isUpdating ? targetFile : path.join(fullPath, REFERENCE_ENV)
const envVars = fs.readFileSync(sourceFile)
const parsedEnvVars = envfile.parse(envVars)
const finalEnv = envfile.stringify({ ...parsedEnvVars, ...overwriteVars })
fs.writeFileSync(targetFile, finalEnv)
isUpdating
? console.log(` File "${fileName}" already exists in "${fullPath}" and has been updated.`)
: console.log(` File "${fileName}" has been created in "${fullPath}".`)
}
function configure(dirName, makeEnvDevelopment, makeEnvProduction, varOverwrite) {
console.log('\n', colors.inverse(` ${dirName.toUpperCase()} `))
const fullPath = path.join(__dirname, dirName)
makeEnvDevelopment && createEnvFile(fullPath, DEVELOPMENT_ENV, varOverwrite)
makeEnvProduction && createEnvFile(fullPath, PRODUCTION_ENV)
}
function configureServer(ip) {
configure('server', true, false, ip && {
APP_ROOT: `http://${ip}:${SERVER_PORT}`,
PORT: SERVER_PORT
})
}
function configureMobile(ip) {
configure('mobile', true, true, ip && {
API_URL: `http://${ip}:${SERVER_PORT}/api`,
})
}
function configureWeb(ip) {
configure('web', true, true, ip && {
NEXT_PUBLIC_API_URL: `http://${ip}:${SERVER_PORT}/api`,
})
}
const ipAddresses = getIPs()
if (!ipAddresses.length) {
console.log(colors.yellow(' No IPV4 address found in this machine.'))
console.log(colors.yellow(' If you have an appropriate IP to use, enter it manually in the ".env.local" files inside each subproject.'))
process.exit(1)
}
selectIP(...ipAddresses).then((ip) => {
console.log('\n ', colors.black.bgGreen(`Selected "${ip}" as app root!`))
configureServer(ip)
configureMobile(ip)
configureWeb(ip)
})