-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (131 loc) · 4.4 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const fs = require('fs')
const { exec } = require('child_process')
const program = require('commander')
const jsonfile = require('jsonfile')
const prompt = require('prompt')
const packageJSON = jsonfile.readFileSync('./package.json')
const configJSON = jsonfile.readFileSync('./config.json')
function range (val) {
return val.split(',')
}
function switchApp (appName) {
const tmp = configJSON.dev[appName]
configJSON.dev[appName] = configJSON.prod[appName]
configJSON.prod[appName] = tmp
console.info(appName + ' PROD becomes: ', configJSON.prod[appName])
console.info(appName + ' DEV becomes: ', configJSON.dev[appName])
}
program
.version(packageJSON.version)
.command('switch <appNames...>')
.alias('s')
.description(`Switch between dev and prod for the specifed applications.
Supported app names: ${configJSON.apps}.
Examples:
'$ nginxswitch switch botStorage'
'$ nginxswitch switch signaling authProxy'
`)
.action((appNames) => {
if (appNames.every((app) => configJSON.apps.includes(app))) {
// Update config.json
appNames.forEach(switchApp)
jsonfile.writeFile('./config.json', configJSON)
// Regenerate nginx conf file
writeNginxConfFile()
console.info(`${configJSON.nginxConfFile} file has been updated successfully`)
// Reload Nginx
reloadNginx()
console.info('Nginx has been reloaded')
} else {
console.error('Unknown app names: ', appNames)
console.info('Supported app names: ', configJSON.apps)
process.exit(1)
}
})
program
.command('getdev <appName>')
.alias('g')
.description(`Provide DEV path to the specified app.
Supported app names: ${configJSON.apps}.
`)
.action((appName) => {
if (configJSON.apps.includes(appName)) {
console.log(getAppPath(appName))
} else {
console.error('Unknown app name: ', appName)
console.info('Supported app names: ', configJSON.apps)
process.exit(1)
}
})
//
// program
// .command('')
// .description(`Show config file`)
// .action((appName) => {
// console.log(JSON.stringify(configJSON, null, 4))
// })
program
.command('init')
.description('Initialize base options in config file')
.action(() => {
prompt.start()
prompt.get({
properties: {
root: {
message: 'Root path where A and B folder are located',
required: true,
default: '/root'
},
nginxConfFile: {
message: 'Absolute path to the Nginx conf file which is generated each time the switch is performed',
required: true
}
}
}, (err, result) => {
configJSON.root = result.root
configJSON.nginxConfFile = result.nginxConfFile
jsonfile.writeFile('./config.json', configJSON)
console.log('Initialized successfully. Current config file is: ')
prettyPrintConfigFile()
})
})
program.parse(process.argv)
// Init nginx conf file path
if (program.nginxConf !== undefined) {
configJSON.nginxConfFile = program.nginxConf
jsonfile.writeFile('./config.json', configJSON)
console.info('Nginx conf file path is set to :', program.nginxConf)
}
function writeNginxConfFile () {
const content =
`set $prod_localhost_port_signaling ${configJSON.prod.signaling.port};
set $prod_localhost_port_bot_storage ${configJSON.prod.botStorage.port};
set $prod_localhost_port_auth_proxy ${configJSON.prod.authProxy.port};
set $prod_root_mute ${getAppPath('mute', false)};
set $dev_localhost_port_signaling ${configJSON.dev.signaling.port};
set $dev_localhost_port_bot_storage ${configJSON.dev.botStorage.port};
set $dev_localhost_port_auth_proxy ${configJSON.dev.authProxy.port};
set $dev_root_mute ${getAppPath('mute')};
`
fs.writeFileSync(configJSON.nginxConfFile, content)
}
function getAppPath (appName, isDev = true) {
if (isDev) {
return `${configJSON.root}/${configJSON.dev[appName].folder}/${appName}`
} else {
return `${configJSON.root}/${configJSON.prod[appName].folder}/${appName}`
}
}
function reloadNginx () {
exec('nginx -s reload', (err, stdout, stderr) => {
if (err) {
console.error('Failed to reload Nginx: ', err)
} else {
}
console.log(stdout)
console.log(stderr)
})
}
function prettyPrintConfigFile () {
console.log(JSON.stringify(configJSON, null, 4))
}