Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
lavelle committed Jul 13, 2019
0 parents commit ea23084
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 100,
"tabWidth": 4,
"trailingComma": "all",
"singleQuote": true
}
32 changes: 32 additions & 0 deletions blocko.json
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"
]
}
64 changes: 64 additions & 0 deletions index.js
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);
10 changes: 10 additions & 0 deletions package.json
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"
}
}
Loading

0 comments on commit ea23084

Please sign in to comment.