forked from sakirsensoy/apache-to-nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
55 lines (39 loc) · 1.3 KB
/
main.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
var fs = require('graceful-fs');
var apacheconf = require('apacheconf');
var format = require('string-template');
// parameters
var args = process.argv;
var apacheConfFile = args[2];
var outputFolder = args[3];
var tplFile = args[4];
// parameter controls
if (! apacheConfFile || ! outputFolder) throw 'Error: Wrong parameters!';
// get nginx conf template
var configTpl = fs.readFileSync(tplFile || __dirname + '/default.tpl', 'utf8');
// read apache virtual.conf
apacheconf(apacheConfFile, function (err, config, parser) {
if (err) throw err;
config.VirtualHost.forEach(function (hostConf) {
// prepare server name
var serverName = [];
if (hostConf.ServerName) {
serverName.push(hostConf.ServerName[0]);
}
if (hostConf.ServerAlias && hostConf.ServerAlias.length > 0) {
serverName = serverName.concat(hostConf.ServerAlias);
}
// ip-port
var listen = hostConf.$args || '80';
// format template
var formattedTpl = format(configTpl, {
listen : listen,
serverName : serverName[0],
directory : hostConf.DocumentRoot[0]
});
// save nginx config file
fs.writeFile(outputFolder + '/' + serverName[0] + '.conf', formattedTpl, 'utf8', function (err) {
if (err) throw err;
console.log(serverName[0] + ' configuration saved.');
});
});
});