-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ea23084
Showing
6 changed files
with
430 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"printWidth": 100, | ||
"tabWidth": 4, | ||
"trailingComma": "all", | ||
"singleQuote": true | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"hosts": [ | ||
"wsj.com", | ||
"news.ycombinator.com", | ||
"reddit.com", | ||
"xkcd.com", | ||
"bbc.com", | ||
"bbc.co.uk", | ||
"news.google.com", | ||
"theguardian.com", | ||
"twitter.com", | ||
"imgur.com", | ||
"instagram.com", | ||
"wired.com", | ||
"theverge.com", | ||
"youtube.com", | ||
"genius.com", | ||
"buzzfeed.com", | ||
"facebook.com", | ||
"vox.com", | ||
"medium.com", | ||
"hckrnews.com", | ||
"outline.com", | ||
"theatlantic.com", | ||
"telegraph.co.uk", | ||
"linkedin.com", | ||
"forbes.com", | ||
"minimal.news", | ||
"techcrunch.com", | ||
"businessinsider.com" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
const meow = require('meow'); | ||
|
||
const cli = meow( | ||
` | ||
Usage | ||
$ blocko | ||
Options | ||
--hosts-file, -h Path to the hosts file | ||
`, | ||
{ | ||
flags: { | ||
'hosts-file': { | ||
type: 'string', | ||
alias: 'h', | ||
default: '/etc/hosts', | ||
}, | ||
}, | ||
}, | ||
); | ||
|
||
const BEGIN_MARKER = '# BEGIN BLOCKO'; | ||
const END_MARKER = '# END BLOCKO'; | ||
|
||
const makeEntry = host => `0.0.0.0 ${host}\n:: ${host}\n`; | ||
|
||
function generateBlockString(hosts) { | ||
let str = `${BEGIN_MARKER}\n`; | ||
|
||
hosts.forEach(host => { | ||
str += makeEntry(host); | ||
str += makeEntry(`www.${host}`); | ||
}); | ||
|
||
str += END_MARKER; | ||
|
||
return str; | ||
} | ||
|
||
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'blocko.json'), 'utf-8')); | ||
|
||
// fs.closeSync(fs.openSync(cli.flags.hostsFile, 'w')); | ||
|
||
const currentHosts = fs.readFileSync(cli.flags.hostsFile, 'utf-8'); | ||
|
||
const blockString = generateBlockString(config.hosts); | ||
|
||
const startLocation = currentHosts.indexOf(BEGIN_MARKER); | ||
const endLocation = currentHosts.indexOf(END_MARKER); | ||
|
||
let newHosts; | ||
if (startLocation > -1 && endLocation > -1) { | ||
newHosts = | ||
currentHosts.substr(0, startLocation) + | ||
blockString + | ||
currentHosts.substr(endLocation + END_MARKER.length); | ||
} else { | ||
newHosts = `${currentHosts}\n${blockString}`; | ||
} | ||
|
||
fs.writeFileSync(cli.flags.hostsFile, newHosts); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "blocko", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"lodash": "^4.17.14", | ||
"meow": "^5.0.0" | ||
} | ||
} |
Oops, something went wrong.