Created
February 13, 2019 06:11
-
-
Save alanwei43/03d3ff1604dd4501d973f877ec7992a3 to your computer and use it in GitHub Desktop.
tidy hosts content
This file contains hidden or 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
#!/usr/bin/env node | |
const hostsFilePath = "/etc/hosts"; | |
const fs = require("fs"); | |
const path = require("path"); | |
fs.readFile(hostsFilePath, (err, data) => { | |
let lines = data.toString().split("\n"); | |
console.log("lines: " + lines.length); | |
let hosts = lines.map(line => line.replace(/\s+/g, " ")) | |
.filter(line => !/^\s*$/.test(line)) | |
.filter(line => !line.startsWith("#")) | |
.map(line => { | |
const parts = line.split(" "); | |
return { | |
host: parts[0], | |
domains: parts.slice(1, parts.length) | |
} | |
}) | |
.reduce((prev, next) => { | |
if (prev[next.host] && Array.isArray(prev[next.host])) { | |
prev[next.host] = prev[next.host].concat(next.domains); | |
} else { | |
prev[next.host] = next.domains; | |
} | |
return prev; | |
}, {}); | |
let hostsContent = Object.keys(hosts).sort().map(key => `${key} ${hosts[key].join(" ")}`).join("\n"); | |
fs.writeFile(__dirname + "/hosts", hostsContent, err => {}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment