This repository has been archived by the owner on Mar 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvhost.js
77 lines (70 loc) · 2.15 KB
/
vhost.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
var fs = require('fs'),
serverName = __dirname.substr(__dirname.lastIndexOf('/') + 1).replace(/\.(com|org|edu|io|net)/, '') + '.local';
var httpd = new Promise(function (resolve, reject) {
console.log('Updating httpd.conf if necessary...');
fs.readFile('/etc/apache2/httpd.conf', 'utf8', function (err, data) {
if (err) {
return reject(err);
}
var uncommented = data.replace(/[# ]*?Include \/private\/etc\/apache2\/extra\/httpd-vhosts\.conf/, 'Include /private/etc/apache2/extra/httpd-vhosts.conf');
fs.writeFile('/etc/apache2/httpd.conf', uncommented, 'utf8', function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
});
var vhosts = new Promise(function (resolve, reject) {
fs.readFile('/etc/apache2/extra/httpd-vhosts.conf', 'utf8', function (err, data) {
if (err) {
return reject(err);
}
if (~data.indexOf(process.cwd())) {
console.log('Virtual Host already exists');
resolve();
} else {
var host = '\n\n' +
'<VirtualHost *:80>\n' +
' DocumentRoot "' + process.cwd() + '"\n' +
' ServerName ' + serverName + '\n' +
'</VirtualHost>';
fs.appendFile('/etc/apache2/extra/httpd-vhosts.conf', host, function (err) {
if (err) {
reject(err);
} else {
resolve();
console.log('Added Virtual Host: ' + serverName);
}
});
}
});
});
var hosts = new Promise(function (resolve, reject) {
fs.readFile('/etc/hosts', 'utf8', function (err, data) {
if (err) {
return reject(err);
}
console.log(serverName);
if (~data.indexOf(serverName)) {
console.log('/etc/hosts already up to date');
resolve();
} else {
var host = '\n127.0.0.1 ' + serverName;
fs.appendFile('/etc/hosts', host, function (err) {
if (err) {
reject(err);
} else {
resolve();
console.log('Added entry to /etc/hosts');
}
});
}
});
});
Promise.all([httpd, vhosts, hosts]).then(function () {
console.log('\033[32mEverything looks good!');
}, function (err) {
console.error(err);
});