-
Notifications
You must be signed in to change notification settings - Fork 2
/
vh-config.js
355 lines (311 loc) · 8.23 KB
/
vh-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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* Apache Virtual Host config generator
* ====================================
*/
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const shell = require('shelljs')
const parser = require('tree-parser')
const { AutoComplete } = require('enquirer')
const args = process.argv
const version = {
major: 0,
minor: 3,
patch: 0
}
// Options
let options = {
force: 0,
restart: 0,
overwrite: 0,
php: 72
}
// Names
let projectName = ''
// Paths
const root = "/usr/local/etc/httpd"
const dest = "sites-available"
const clone = "sites-enabled"
const workDir = process.cwd()
const helpText = {
usage: 'Usage: vh-config [arguments]',
options: {
title: 'Options:',
list: [
[
'-d, --default',
'print path to the httpd configuration'
],
[
'-f, --force',
'force if project not found in working directory'
],
[
'-h, --help',
'print command line options'
],
[
'-r, --restart',
'restarts brew httpd service'
],
[
'-v',
'print vh-config version number'
],
[
'-x',
'overwrite existing config file'
],
[
'-R, --Remove',
'overwrite existing config file'
],
[
'--php=',
'specify php version (default is 7.2)'
]
]
}
}
/**
* Print version number
*/
const printVersion = () => {
let versionString = ''
// Check that version number is not empty
if (version) {
print(`v${version.major}.${version.minor}.${version.patch}`)
}
}
/**
* Delete config
*/
const configDel = (file) => {
const target = {
dest: path.join(root, dest, file),
clone: path.join(root, clone, file),
}
// Remove the original config file
print(
`Delete: ${chalk.yellow(target.dest)}`
)
shell.rm(target.dest)
// Remove the cloned config file
print(
`Delete: ${chalk.yellow(target.clone)}`
)
shell.rm(target.clone)
}
// Create symlink
const symLn = (fileName)=> {
const lnTarget = path.join(root, dest, fileName)
const lnRef = path.join(root, clone, fileName)
shell.ln('-s', lnTarget, lnRef)
}
const restartServices = (service) => {
const brewRestart = 'brew services restart'
if (service === 'httpd') {
return `${brewRestart} httpd`
}
}
/**
* Remove entry
*/
const remove = () => {
const prompt = new AutoComplete (
{
name: 'Config Remove',
message: 'Which config file should I remove? (Esc to cancel)',
choices: listFiles._contents
}
)
prompt.run()
.then(answer => {
configDel(answer)
end()
})
.catch(console.error)
}
/**
* Print help
*/
const printHelp = () => {
let argumentsList = []
print(helpText.usage + '\n')
print(helpText.options.title)
// Get the length of the longest argument
helpText.options.list.forEach(function(item){
argumentsList.push(item[0])
})
// Get max length of the arguments
const maxArgLen = charMax(argumentsList)
helpText.options.list.forEach(function(item){
print(`${item[0]} ${genWhiteSpace(maxArgLen - item[0].length)} ${item[1]}`)
})
}
/**
* Generate white space
*/
const genWhiteSpace = (padding) => {
const seed = ' '
const result = []
if (!isNaN(padding)) {
for (var i = 0; i < padding; i++) {
result.push(seed)
}
return result.join('')
}
}
/**
* Get longest word
*/
const charMax = (strings) => {
const charLen = []
if (Array.isArray(strings)) {
strings.forEach(function(string){
charLen.push(string.length)
})
return Math.max( ...charLen)
}
}
/**
* Print to console
*/
const print = (msg, type=null) => {
if (type === null) {
console.log(msg)
} else if (type === 'error') {
console.error(
chalk.red(`error: ${msg}`)
)
}
}
/**
* End program
*/
const end = () => {
process.exit(1)
}
// Write to config files
const configCreate = () => {
projectValidate()
// Config template
const template = `<VirtualHost *:8080>
DocumentRoot ${workDir}/web
ServerName ${projectName}.local.blee.ch
RewriteCond ${workDir}/web/%{REQUEST_FILENAME} -f
RewriteRule ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:90${options.php}/${workDir}/web/$1 [P,QSA,L]
<Directory ${workDir}/web>
Options -Indexes +FollowSymLinks -MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8443>
DocumentRoot ${workDir}/web
ServerName ${projectName}.local.blee.ch
RewriteCond ${workDir}/web/%{REQUEST_FILENAME} -f
RewriteRule ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:90${options.php}${workDir}/web/$1 [P,QSA,L]
Include "/usr/local/etc/httpd/ssl/ssl-shared-cert.inc"
<Directory ${workDir}/web>
Options -Indexes +FollowSymLinks -MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
`
// Write config file to disk
fs.writeFile(path.join(root, dest, fileName), template, (err) => {
if (err) throw err
if (!options.overwrite) {
symLn(fileName)
}
// File written successfully
console.log(`File named ${chalk.yellow(fileName)} was created.`)
if (options.restart) {
// Try to restart services
print('Sending restart command `httpd`:')
shell.exec(restartServices('httpd'))
}
})
}
/**
* Validate project
*/
const projectValidate = () => {
if (!fs.existsSync(path.join(workDir, 'web')) && options.force === 0) {
print('Current directory doesn\'t seem to be a valid project.')
print('Please use \'--force\' option to suppress this warning.')
end()
} else if (fs.existsSync(path.join(root, dest, fileName)) && !options.overwrite) {
print(`Looks like the config file (${fileName}) already exists. Please delete it before trying again or use the '-x' flag.`, 'error')
end()
}
}
/**
* Read arguments
*/
const readArgs = () => {
if (args.length < 2) {
print('Looks like something went wrong.', 'error')
// Kill the process
end()
} else if (args.length >= 2) {
// -v
if (args.indexOf('-v') != -1) {
printVersion()
end()
// -h, --help
} else if (args.indexOf('-h') != -1 || args.indexOf('--help') != -1) {
printHelp()
end()
// -d, --default
} else if (args.indexOf('-d') != -1 || args.indexOf('--default') != -1) {
print(`Target path: ${root}`)
end()
// -R, --Remove
} else if (args.indexOf('-R') != -1 || args.indexOf('--Remove') != -1) {
remove()
} else {
// -f, --force
if (args.indexOf('-f') != -1 || args.indexOf('--force') != -1) {
options.force = 1
}
// -r, --restart
if (args.indexOf('-r') != -1 || args.indexOf('--restart') != -1) {
options.restart = 1
}
// -x
if (args.indexOf('-x') != -1) {
options.overwrite = 1
}
// --php=
args.forEach((arg) => {
if (arg.search(/--php=.{2,3}/g) > -1) {
options.php = arg.match(/\d/g).join('')
}
})
configCreate()
}
}
}
/**
* Check environment
*/
const scan = () => {
if (!fs.existsSync(root)) {
print(`${root} path not found`, 'error')
end()
} else {
readArgs()
}
}
// MAIN
const listFiles = parser(path.join(root, dest), [/*file exceptions*/])
const pathNodes = workDir.split('/')
projectName = pathNodes[pathNodes.length - 1]
// Output file name
const fileName = `${projectName}.conf`
scan()
// MAIN ./end