Comparing version 1.0.2 to 1.0.3
module.exports = { | ||
extends: ['airbnb', 'prettier'], | ||
parser: '@typescript-eslint/parser', | ||
extends: [ | ||
'airbnb', | ||
'plugin:@typescript-eslint/recommended', | ||
'prettier', | ||
'prettier/@typescript-eslint', | ||
], | ||
rules: { | ||
@@ -4,0 +10,0 @@ 'no-console': 'off', |
158
index.js
#! /usr/bin/env node | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
const meow = require('meow'); | ||
const cli = meow( | ||
` | ||
Usage | ||
$ blocko | ||
$ blocko unblock | ||
$ blocko unblock <site> | ||
Options | ||
--hosts-file, -h Path to the hosts file | ||
`, | ||
{ | ||
flags: { | ||
'hosts-file': { | ||
type: 'string', | ||
alias: 'h', | ||
default: '/etc/hosts', | ||
}, | ||
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
exports.__esModule = true; | ||
var fs = __importStar(require("fs")); | ||
var os = __importStar(require("os")); | ||
var path = __importStar(require("path")); | ||
var lodash_1 = __importDefault(require("lodash")); | ||
var meow_1 = __importDefault(require("meow")); | ||
var execa_1 = __importDefault(require("execa")); | ||
var chalk_1 = __importDefault(require("chalk")); | ||
var cli = meow_1["default"]("\n\tUsage\n $ blockr\n $ blockr unblock\n $ blockr unblock <site>\n\n\tOptions\n --hosts-file, -h Path to the hosts file\n --config-file, -c Path to the config file\n --password, -p Sudo password\n", { | ||
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`; | ||
'config-file': { | ||
type: 'string', | ||
alias: 'c', | ||
"default": path.join(os.homedir(), 'blockr.json') | ||
}, | ||
password: { | ||
type: 'string', | ||
alias: 'p', | ||
"default": null | ||
} | ||
} | ||
}); | ||
var BEGIN_MARKER = '# BEGIN BLOCKR'; | ||
var END_MARKER = '# END BLOCKR'; | ||
var makeEntry = function (host) { return "0.0.0.0 " + host + "\n:: " + host + "\n"; }; | ||
function generateBlockString(hosts) { | ||
let str = `${BEGIN_MARKER}\n`; | ||
hosts.forEach(host => { | ||
var str = BEGIN_MARKER + "\n"; | ||
hosts.forEach(function (host) { | ||
str += makeEntry(host); | ||
str += makeEntry(`www.${host}`); | ||
str += makeEntry("www." + host); | ||
}); | ||
str += END_MARKER; | ||
return str; | ||
} | ||
function tryWrite(hosts) { | ||
try { | ||
fs.writeFileSync(cli.flags.hostsFile, hosts); | ||
} catch (error) { | ||
if (error.code === 'EACCES') { | ||
console.log('Please re-run with sudo to write to the hosts file'); | ||
function tryWrite(hostsString) { | ||
if (cli.flags.password) { | ||
fs.writeFileSync('/tmp/hosts', hostsString); | ||
execa_1["default"].sync("./runner.sh", [cli.flags.password, cli.flags.hostsFile]); | ||
} | ||
else { | ||
try { | ||
fs.writeFileSync(cli.flags.hostsFile, hostsString); | ||
} | ||
process.exit(1); | ||
catch (error) { | ||
if (error.code === 'EACCES') { | ||
console.log('Please re-run with sudo or use the `--password` option to write to the hosts file'); | ||
} | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
function updateBlock(hosts) { | ||
function updateBlock(hostsMap) { | ||
if (!fs.existsSync(cli.flags.hostsFile)) { | ||
fs.closeSync(fs.openSync(cli.flags.hostsFile, 'w')); | ||
} | ||
const currentHosts = fs.readFileSync(cli.flags.hostsFile, 'utf-8'); | ||
const blockString = generateBlockString(_.values(hosts)); | ||
const startLocation = currentHosts.indexOf(BEGIN_MARKER); | ||
const endLocation = currentHosts.indexOf(END_MARKER); | ||
let newHosts; | ||
var currentHosts = fs.readFileSync(cli.flags.hostsFile, 'utf-8'); | ||
var blockString = generateBlockString(lodash_1["default"].values(hostsMap)); | ||
var startLocation = currentHosts.indexOf(BEGIN_MARKER); | ||
var endLocation = currentHosts.indexOf(END_MARKER); | ||
var newHosts; | ||
if (startLocation > -1 && endLocation > -1) { | ||
newHosts = | ||
currentHosts.substr(0, startLocation) + | ||
blockString + | ||
currentHosts.substr(endLocation + END_MARKER.length); | ||
} else { | ||
newHosts = `${currentHosts}\n${blockString}`; | ||
blockString + | ||
currentHosts.substr(endLocation + END_MARKER.length); | ||
} | ||
else { | ||
newHosts = currentHosts + "\n" + blockString; | ||
} | ||
tryWrite(newHosts); | ||
} | ||
const command = cli.input[0]; | ||
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'blocko.json'), 'utf-8')); | ||
function loadConfig() { | ||
try { | ||
return JSON.parse(fs.readFileSync(cli.flags.configFile, 'utf-8')); | ||
} | ||
catch (error) { | ||
console.error("No config file found at " + cli.flags.configFile); | ||
process.exit(1); | ||
} | ||
return null; | ||
} | ||
var command = cli.input[0]; | ||
var config = loadConfig(); | ||
if (!command || command === 'block') { | ||
updateBlock(config.hosts); | ||
console.log('✅ All sites blocked'); | ||
} | ||
if (command === 'unblock') { | ||
if (cli.input[1]) { | ||
updateBlock(_.omit(config.hosts, cli.input[1])); | ||
} else { | ||
var site = cli.input[1]; | ||
if (site) { | ||
if (config.hosts[site]) { | ||
updateBlock(lodash_1["default"].omit(config.hosts, site)); | ||
console.log("\u2705 '" + chalk_1["default"].yellow(site) + "' unblocked"); | ||
} | ||
else { | ||
console.error("\u274C No site called '" + chalk_1["default"].yellow(site) + "'"); | ||
} | ||
} | ||
else { | ||
updateBlock({}); | ||
console.log('✅ All sites unblocked'); | ||
} | ||
} |
{ | ||
"name": "blockr", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "index.js", | ||
@@ -8,5 +8,9 @@ "license": "MIT", | ||
"scripts": { | ||
"lint": "eslint ." | ||
"lint": "eslint index.ts", | ||
"build": "tsc --esModuleInterop index.ts", | ||
"release": "yarn lint && yarn build && npm publish" | ||
}, | ||
"dependencies": { | ||
"chalk": "^2.4.2", | ||
"execa": "^2.0.3", | ||
"lodash": "^4.17.14", | ||
@@ -16,2 +20,5 @@ "meow": "^5.0.0" | ||
"devDependencies": { | ||
"@types/node": "^12.6.2", | ||
"@typescript-eslint/eslint-plugin": "^1.12.0", | ||
"@typescript-eslint/parser": "^1.12.0", | ||
"eslint": "^6.0.1", | ||
@@ -22,4 +29,5 @@ "eslint-config-airbnb": "^17.1.1", | ||
"eslint-plugin-jsx-a11y": "^6.2.3", | ||
"eslint-plugin-react": "^7.14.2" | ||
"eslint-plugin-react": "^7.14.2", | ||
"typescript": "^3.5.3" | ||
} | ||
} |
@@ -1,3 +0,9 @@ | ||
# Blocko | ||
# Blockr | ||
Block distracting sites from the command line | ||
## Install | ||
``` | ||
yarn global add blockr | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
77242
9
263
10
4
10
1
+ Addedchalk@^2.4.2
+ Addedexeca@^2.0.3
+ Addedansi-styles@3.2.1(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedcross-spawn@7.0.6(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedexeca@2.1.0(transitive)
+ Addedget-stream@5.2.0(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedis-stream@2.0.1(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedmerge-stream@2.0.0(transitive)
+ Addedmimic-fn@2.1.0(transitive)
+ Addednpm-run-path@3.1.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedonetime@5.1.2(transitive)
+ Addedp-finally@2.0.1(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedstrip-final-newline@2.0.0(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwrappy@1.0.2(transitive)