-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhosts.js
55 lines (42 loc) · 1.17 KB
/
hosts.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('fs'),
hostsManager = require('./lib/hostsManager'),
platform = process.platform;
var lineBreak = {
'win32': '\r\n',
'linux': '\n',
'darwin': '\r'
}[platform];
exports.summary = 'Remapping of requests for one host to a different IP';
exports.usage = '<hosts> [options]';
exports.options = {
hosts: {
describe : 'the hosts'
},
action: {
describe : 'the action',
default: 'set'
}
};
exports.run = function (options, callback) {
var hosts = options.hosts;
var group = options.group;
var _ = exports._;
if(_.isString(hosts)){
if (fs.existsSync(hosts)){ // to support file path
hosts = fs.readFileSync(hosts).toString().split(lineBreak).filter(function(hosts){return hosts !== ''}); // to filter null line
}
else {
hosts = [hosts];
}
}
if(!_.isArray(hosts)){
exports.error('hosts option must be string or array');
}
hosts.forEach(function(host){
var m= host.match(/^\s*([^\s]+)\s+([^#\s]+)/);
hostsManager.set(m[1], m[2], group);
exports.log(m[1], m[2]);
});
hostsManager.save();
callback();
};