-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·303 lines (249 loc) · 7.9 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
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
#!/usr/bin/env node
/* eslint prefer-promise-reject-errors: ["error", {"allowEmptyReject": true}] */
'use strict'
const chalk = require('chalk')
const columnify = require('columnify')
const { execSync } = require('child_process')
const fs = require('fs')
const Fuse = require('fuse.js')
const inquirer = require('inquirer')
const lineNumber = require('line-number')
const os = require('os')
const program = require('commander')
const sort = require('fast-sort')
const version = require('./package.json').version
const reg = /^(#)?LoadModule[ \t](\S*)[ \t](\S*)$/
var config = {
apache_config: '/etc/apache2/httpd.conf',
apache_restart: '/usr/sbin/apachectl restart'
}
var apacheConfig
var lines
var mods
var userConfig
function changeModuleStatus (mod, status) {
return new Promise((resolve, reject) => {
if (status) {
var dupCheck = mods.filter(m => m.enabled && m.name === mod.name).length
if (dupCheck) {
console.log(chalk`{red ✘} There is already an {green enabled} module named {yellow ${mod.name}}`)
return reject()
}
}
return inquirer.prompt({
type: 'confirm',
name: 'confirm',
message: chalk`${status ? 'Enable' : 'Disable'} {cyan ${mod.name}} ({yellow ${mod.path}})?`
}).then(a => {
if (!a['confirm']) return reject()
var line = lines[mod.line - 1]
if (line.startsWith(status ? '#' : 'L')) {
mods[mod.id].enabled = status
if (status) {
editLine(mod.line, line.substr(1))
} else {
editLine(mod.line, '#' + line)
}
return resolve()
}
})
})
}
function chooseModule (find, list) {
if (!list) list = mods
var fuse = new Fuse(list, {
keys: ['name'],
threshold: 0.1
})
var search = fuse.search(find)
return new Promise((resolve, reject) => {
if (search.length === 0) {
console.log(chalk`{red ✘} No applicable modules found matching {yellow ${find}}`)
return reject()
} else if (search.length === 1) {
return resolve(search[0])
} else {
return inquirer.prompt({
name: 'mod',
type: 'list',
message: 'Which module are you looking for?',
choices: search.map((m, i) => Object({
key: i,
name: chalk`${m.name} {yellow ${m.path}}`,
value: m
}))
}).then(a => resolve(a.mod))
}
})
}
function editLine (lineNumber, line) {
lines[lineNumber - 1] = line
apacheConfig = lines.join('\n')
try {
fs.writeFileSync(config.apache_config, apacheConfig)
console.log(chalk`{green ✔} Changed line {cyan ${lineNumber}} to {yellow ${line}}`)
} catch (e) {
console.log(chalk`{red ✘} An error occured while trying to edit {yellow ${config.apache_config}}: ${e.code}`)
console.log(chalk`{yellow !} You may want to retry that command with the {cyan sudo} prefix`)
process.exit(1)
}
}
function getModules () {
var modList = apacheConfig.match(RegExp(reg, 'gm'))
var lineList = lineNumber(apacheConfig, reg)
modList = modList.map((m, id) => {
var mod = reg.exec(m)
return {
id,
name: mod[2],
path: mod[3],
enabled: !mod[1],
line: lineList[id].number
}
})
mods = modList
return modList
}
function promptRestart () {
return new Promise((resolve, reject) => {
if (!config.apache_restart) return resolve()
return inquirer.prompt({
name: 'confirm',
type: 'confirm',
message: chalk`Restart Apache ({yellow ${config.apache_restart}})?`
}).then(a => {
if (a['confirm']) execSync(config.apache_restart)
return resolve(a['confirm'])
})
})
}
function setup () {
var newUserConfig = os.homedir() + '/.amm.json'
if (process.env.AMM_CONFIG) newUserConfig = process.env.AMM_CONFIG
if (program.config) newUserConfig = program.config
if (fs.existsSync(newUserConfig)) {
try {
userConfig = newUserConfig
newUserConfig = require(newUserConfig)
Object.assign(config, newUserConfig)
} catch (e) {
console.log(e)
console.log(chalk`{red ✘} An error occured while loading specified user config file {yellow ${newUserConfig}}`)
}
} else if (process.env.AMM_CONFIG || program.config) {
console.log(chalk`{yellow !} Specified user config file {yellow ${newUserConfig}} was not found`)
}
var newApacheConfig = config.apache_config
if (process.env.AMM_APACHE_CONFIG) newApacheConfig = process.env.AMM_APACHE_CONFIG
if (program.apacheConfig) newApacheConfig = program.apacheConfig
if (fs.existsSync(newApacheConfig)) {
config.apache_config = newApacheConfig
} else {
console.log(chalk`{red ✘} Apache config file {yellow ${newApacheConfig}} was not found`)
process.exit(1)
}
apacheConfig = fs.readFileSync(config.apache_config, 'utf8')
lines = apacheConfig.split('\n')
getModules()
if (userConfig) console.log(chalk`{yellow AMM Config:} ${userConfig}`)
console.log(chalk`{yellow Apache Config:} ${config.apache_config}\n`)
}
program
.command('disable <module>')
.alias('d')
.description('Disable module(s)')
.action((search, cmd) => {
setup()
chooseModule(search, mods.filter(m => m.enabled))
.then(mod => changeModuleStatus(mod, false))
.then(() => promptRestart())
.catch(() => {})
})
program
.command('enable [module]')
.alias('e')
.description('Enable module(s)')
.action((search, cmd) => {
setup()
chooseModule(search, mods.filter(m => !m.enabled))
.then(mod => changeModuleStatus(mod, true))
.then(() => promptRestart())
.catch(() => {})
})
program
.command('list [search]')
.alias('l')
.description('List modules')
.option('-d, --disabled', 'Only display disabled modules')
.option('-e, --enabled', 'Only display enabled modules')
.option('-s, --sort <columns>', 'Sort results by column values')
.action((search, cmd) => {
setup()
var columns = ['id', 'name', 'path', 'enabled', 'line']
if (cmd.disabled) mods = mods.filter(m => !m.enabled)
if (cmd.enabled) mods = mods.filter(m => m.enabled)
if (search) {
var fuse = new Fuse(mods, {
keys: ['name'],
threshold: 0.1
})
mods = fuse.search(search)
}
if (!mods.length) {
console.log(chalk`{red ✘} No applicable modules found`)
return
}
var sortOrder = 'enabled,name,path'
var sortFuncs = []
if (cmd.sort) sortOrder = cmd.sort
sortOrder
.split(',')
.map(s => {
s = s.toLowerCase()
if (columns.includes(s)) {
var func = m => m[s]
if (s === 'enabled') func = m => m.enabled ? 0 : 1
sortFuncs.push(func)
}
})
if (sortFuncs.length) sort(mods).asc(sortFuncs)
var data = columnify(mods, {
columns,
config: {
enabled: {
dataTransform: enabled => {
if (enabled === 'true') {
return chalk.green(enabled)
} else {
return chalk.red(enabled)
}
}
}
}
})
console.log(data)
})
program
.command('switch <old-module> [new-module]')
.alias('s')
.description('Switch out module(s)')
.action((disable, enable, cmd) => {
setup()
if (!enable) enable = disable
var enabledMods = mods.filter(m => m.enabled)
var disabledMods = mods.filter(m => !m.enabled)
chooseModule(disable, enabledMods)
.then(mod => changeModuleStatus(mod, false))
.then(() => chooseModule(enable, disabledMods))
.then(mod => changeModuleStatus(mod, true))
.then(() => promptRestart())
.catch(() => {})
})
console.log(chalk`{greenBright Apache Module Manager v${version}} (https://github.com/kodie/apache-module-manager)`)
console.log(chalk`by {cyanBright Kodie Grantham} (http://kodieg.com)\n`)
program
.version(version)
.option('-a, --apache-config <path>', 'Apache config path')
.option('-c, --config <path>', 'AMM config path')
.parse(process.argv)
if (process.argv.length < 3) program.help()