Created
August 30, 2015 02:21
-
-
Save luveti/cd4eee59ebb81ee38a82 to your computer and use it in GitHub Desktop.
Enable or disable sites in /etc/hosts using nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var sites = [ | |
'example.com', | |
'test.com' | |
]; | |
if(process.argv[2]) { | |
if(process.argv[2] == 'enable' || process.argv[2] == 'disable') { | |
fs.readFile('/etc/hosts', function(err, data) { | |
var lines = data.toString('utf8').split('\n'); | |
lines.forEach(function(line, i) { | |
sites.forEach(function(domain) { | |
if(line.split(' ')[1] == domain) { | |
if(process.argv[2] == 'enable') { | |
if(line.substr(0, 1) == '#') { | |
lines[i] = line.substr(1); | |
} | |
} | |
else if(process.argv[2] == 'disable') { | |
if(line.substr(0, 1) != '#') { | |
lines[i] = '#' + line; | |
} | |
} | |
} | |
}); | |
}) | |
fs.writeFile('/etc/hosts', lines.join('\n'), function(err) { | |
if(err) throw err; | |
console.log("Sites " + process.argv[2] + 'd'); | |
}); | |
}); | |
} | |
else { | |
console.log("'" + process.argv[2] + "' is an invalid choice, please pass in 'enable' or 'disable'!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment