Created
November 11, 2016 13:24
-
-
Save somebody1234/474cf51af70b31c57d8825985ea519e1 to your computer and use it in GitHub Desktop.
hpm
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
//note: requires minimist (npm i minimist) | |
'use strict'; | |
const {accessSync, mkdirSync, unlinkSync, readFileSync, writeFileSync, F_OK} = require('fs'), | |
name = 'hpm', | |
{join} = require('path'), | |
homeDir = require('os').homedir(), | |
dir = join(homeDir, '.' + name), | |
path = join(homeDir, '.hyper.js'), | |
numberPath = join(dir, 'number'); | |
let redo = true; | |
try { | |
mkdirSync(dir); | |
} catch (e) { | |
//ignored | |
} | |
try { | |
accessSync(numberPath, F_OK); | |
} catch (e) { | |
writeFileSync(numberPath, '0'); | |
} | |
function writeJSSync (path, object) { | |
writeFileSync(path, `module.exports = ${JS.stringify(object)};`); | |
} | |
let options = require(path), | |
config = options.config; | |
const JS = { | |
stringify: function stringify (object, padding='') { | |
if (object === null) | |
return object; | |
let type = typeof object; | |
if (type !== 'object') { | |
switch (type) { | |
case 'string': | |
if (object.indexOf('\n') === -1) | |
return '\'' + JSON.stringify(object).slice(1,-1).replace(/\\"/g, '"').replace(/'/g, '\\\'') + '\''; | |
return '`' + object.replace(/\\/g, '\\\\').replace(/`/g, '\\`') + '`'; | |
default: | |
return object; | |
} | |
} | |
let newPadding = padding + ' '; | |
if (object instanceof Array) { | |
let string = '[\n'; | |
for (let item of object) | |
string += newPadding + JS.stringify(item, newPadding) + ',\n'; | |
return (object.length ? string.slice(0, -2) : string) + '\n' + padding + ']'; | |
} | |
let string = '{\n', | |
empty = true; | |
for (let key in object) { | |
string += `${newPadding}${key}: ${JS.stringify(object[key], newPadding)}, | |
`; | |
empty = false; | |
} | |
return (empty ? string : string.slice(0, -2)) + '\n' + padding + '}'; | |
} | |
}; | |
let argvIndex = process.argv.indexOf(name); | |
if (argvIndex === -1) | |
argvIndex = process.argv.indexOf('index.js'); | |
const argv = require('minimist')(process.argv.slice(argvIndex)); | |
if (argv.h) { | |
if (argv._) { | |
switch (argv._[0].toLowerCase()) { | |
case 'i': | |
case 'install': | |
case 'isntall': | |
console.log(`\ | |
hpm install {<npm package name>} | |
aliases: i, isntall`); | |
break; | |
case 'il': | |
case 'installlocal': | |
case 'isntalllocal': | |
console.log(`\ | |
hpm installlocal {<npm package name>} | |
aliases: il, isntalllocal`); | |
break; | |
case 'un': | |
case 'uninstall': | |
case 'unlink': | |
case 'r': | |
case 'rm': | |
case 'remove': | |
console.log(`\ | |
hpm uninstall {<npm package name>} | |
aliases: un, unlink, remove, rm, r`); | |
break; | |
case 'unl': | |
case 'uninstalllocal': | |
case 'unlinklocal': | |
case 'rl': | |
case 'rml': | |
case 'removelocal': | |
console.log(`\ | |
hpm uninstalllocal {<npm package name>} | |
aliases: unl, unlinklocal, removelocal, rml, rl`); | |
break; | |
case 'fs': | |
case 'fontsize': | |
console.log(`\ | |
hpm fontsize | |
hpm fontsize <size in pixels> | |
aliases: fs`); | |
break; | |
case 'ff': | |
case 'fontfamily': | |
console.log(`\ | |
hpm fontfamily | |
hpm fontfamily <font family + optional fallbacks> | |
aliases: ff`); | |
break; | |
case 'cc': | |
case 'cursorcolor': | |
console.log(`\ | |
hpm cursorcolor | |
hpm cursorcolor <css color> | |
aliases: cc`); | |
break; | |
case 'cs': | |
case 'cursorshape': | |
console.log(`\ | |
hpm cursorshape | |
hpm cursorshape <BEAM or UNDERLINE or BLOCK> | |
aliases: cs`); | |
break; | |
case 'fgc': | |
case 'foregroundcolor': | |
console.log(`\ | |
hpm foregroundcolor | |
hpm foregroundcolor <css color> | |
aliases: fgc`); | |
break; | |
case 'bgc': | |
case 'backgroundcolor': | |
console.log(`\ | |
hpm backgroundcolor | |
hpm backgroundcolor <css color> | |
aliases: bgc`); | |
break; | |
case 'bc': | |
case 'bordercolor': | |
console.log(`\ | |
hpm bordercolor | |
hpm bordercolor <css color> | |
aliases: bc`); | |
break; | |
case 'css': | |
console.log(`\ | |
hpm css | |
hpm css <css>`); | |
break; | |
case 'termcss': | |
console.log(`\ | |
hpm termcss | |
hpm termcss <css>`); | |
break; | |
case 'padding': | |
console.log(`\ | |
hpm padding | |
hpm padding <padding> | |
hpm padding <vertical horizontal> | |
hpm padding <top horizontal bottom> | |
hpm padding <top right bottom left>`); | |
break; | |
case 'color': | |
console.log(`\ | |
hpm color | |
hpm color <color name> <css color>`); | |
break; | |
case 'shell': | |
console.log(`\ | |
hpm cursorshape | |
hpm cursorshape <BEAM or UNDERLINE or BLOCK> | |
aliases: cs`); | |
break; | |
case 'se': | |
case 'setenv': | |
console.log(`\ | |
hpm setenv | |
hpm setenv <key value> | |
aliases: se`); | |
break; | |
case 'ue': | |
case 'unsetenv': | |
console.log(`\ | |
hpm unsetenv | |
hpm unsetenv <key> | |
aliases: ue`); | |
break; | |
case 'ws': | |
case 'windowsize': | |
console.log(`\ | |
hpm windowsize | |
hpm windowsize <width> <height> | |
aliases: ws`); | |
break; | |
case 'cos': | |
case 'copyonselect': | |
console.log(`\ | |
hpm copyonselect | |
hpm copyonselect <boolean> | |
aliases: cos`); | |
break; | |
case 'bell': | |
console.log(`\ | |
hpm bell <SOUND or false>`); | |
case 'bellurl': | |
case 'bellsound': | |
case 'bellsoundurl': | |
console.log(`\ | |
hpm bellsoundurl | |
hpm bellsoundurl <url> | |
aliases: bellurl, bellsound`); | |
case 'undo': | |
console.log(`\ | |
hpm undo`); | |
break; | |
default: | |
console.log(`\ | |
Usage: hpm <command> | |
where <command> is one of: | |
i, install, il, installlocal, | |
un, uninstall, unlink, r, rm, remove, | |
unl, uninstalllocal, unlinklocal, rl, rml, removelocal, | |
fs, fontsize, ff, fontfamily, | |
cc, cursorcolor, cs, cursorshape, | |
fgc, foregroundcolor, bgc, backgroundcolor, bc, bordercolor, | |
css, termcss, padding, color, shell, se, setenv, ue, unsetenv, | |
ws, windowsize, cos, copyonselect, | |
bell, bellurl, bellsound, bellsoundurl, | |
undo | |
hpm <cmd> -h quick help on <cmd>`); | |
} | |
process.exit(0); | |
} | |
} | |
let _ = argv._; | |
const plugins = options.plugins, | |
localPlugins = options.localPlugins; | |
function get(fallback) { | |
let result = _[1] || fallback; | |
if (_[1]) | |
_.splice(0, 2); | |
else | |
_.splice(0, 1); | |
return result; | |
} | |
while (_[0]) { | |
switch (_[0].toLowerCase()) { | |
case 'i': | |
case 'install': | |
case 'isntall': | |
for (let i = 1, item = _[1]; i < _.length; item = _[++i]) | |
if (plugins.indexOf(item) === -1) | |
plugins.push(item); | |
_.splice(0, _.length); | |
break; | |
case 'il': | |
case 'installlocal': | |
case 'isntalllocal': | |
for (let i = 1, item = _[1]; i < _.length; item = _[++i]) | |
if (localPlugins.indexOf(item) === -1) | |
localPlugins.push(item); | |
_.splice(0, _.length); | |
break; | |
case 'un': | |
case 'uninstall': | |
case 'unlink': | |
case 'r': | |
case 'rm': | |
case 'remove': | |
for (let i = 1, index = plugins.indexOf(_[1]); i < _.length; index = plugins.indexOf(_[++i])) | |
if (index !== -1) | |
plugins.splice(index, 1); | |
_.splice(0, _.length); | |
break; | |
case 'unl': | |
case 'uninstalllocal': | |
case 'unlinklocal': | |
case 'rl': | |
case 'rml': | |
case 'removelocal': | |
for (let i = 1, index = localPlugins.indexOf(_[1]); i < _.length; index = localPlugins.indexOf(_[++i])) | |
if (index !== -1) | |
localPlugins.splice(index, 1); | |
_.splice(0, _.length); | |
break; | |
case 'fs': | |
case 'fontsize': | |
config.fontSize = get(12); | |
break; | |
case 'ff': | |
case 'fontfamily': | |
config.fontFamily = get('Menlo, "DejaVu Sans Mono", "Lucida Console", monospace'); | |
break; | |
case 'cc': | |
case 'cursorcolor': | |
config.cursorColor = get('#F81CE5'); | |
break; | |
case 'cs': | |
case 'cursorshape': | |
config.cursorShape = get('BLOCK'); | |
break; | |
case 'fgc': | |
case 'foregroundcolor': | |
config.foregroundColor = get('#fff'); | |
break; | |
case 'bgc': | |
case 'backgroundcolor': | |
config.backgroundColor = get('#000'); | |
break; | |
case 'bc': | |
case 'bordercolor': | |
config.borderColor = get('#333'); | |
break; | |
case 'css': | |
config.css = get(''); | |
break; | |
case 'termcss': | |
config.termCSS = get(''); | |
break; | |
case 'padding': | |
config.padding = get('12px 14px'); | |
break; | |
case 'c': | |
case 'color': | |
if (_[2]) { | |
config.colors[_[1]] = _[2]; | |
_.splice(0, 3); | |
} else { | |
config.colors = { | |
black: '#000000', | |
red: '#ff0000', | |
green: '#33ff00', | |
yellow: '#ffff00', | |
blue: '#0066ff', | |
magenta: '#cc00ff', | |
cyan: '#00ffff', | |
white: '#d0d0d0', | |
lightBlack: '#808080', | |
lightRed: '#ff0000', | |
lightGreen: '#33ff00', | |
lightYellow: '#ffff00', | |
lightBlue: '#0066ff', | |
lightMagenta: '#cc00ff', | |
lightCyan: '#00ffff', | |
lightWhite: '#ffffff' | |
}; | |
_.splice(0, 1); | |
} | |
break; | |
case 'shell': | |
config.shell = get(''); | |
break; | |
case 'se': | |
case 'setenv': | |
if (_[2]) { //TODO | |
config.colors[_[1]] = _[2]; | |
_.splice(0, 3); | |
} else | |
_.splice(0, 1); | |
break; | |
case 'ue': | |
case 'unsetenv': | |
if (_[1]) { | |
delete config.env[_[1]]; | |
_.splice(0, 2); | |
} else { | |
config.env = {}; | |
_.splice(0, 1); | |
} | |
break; | |
case 'ws': | |
case 'windowsize': | |
if (_[2]) { | |
config.windowSize = [Number(_[1]), Number(_[2])]; | |
_.splice(0, 3); | |
} else { | |
config.windowSize = null; | |
_.splice(0, 1); | |
} | |
break; | |
case 'cos': | |
case 'copyonselect': | |
config.copyOnSelect = Boolean(get(false)); | |
break; | |
case 'bell': | |
config.bell = get('SOUND') || false; | |
break; | |
case 'bellurl': | |
case 'bellsound': | |
case 'bellsoundurl': | |
if (_[1]) { | |
config.bellSoundURL = _[1]; | |
_.splice(0, 2); | |
} else { | |
delete config.bellSoundURL; | |
_.splice(0, 1); | |
} | |
break; | |
case 'undo': | |
redo = false; | |
try { | |
let number = +readFileSync(numberPath); | |
if (!number) | |
throw 'Error: Cannot undo changes'; | |
let oldPath = join(dir, '.hyper.js.' + number); | |
accessSync(oldPath, F_OK); | |
writeFileSync(path, readFileSync(oldPath)); | |
unlinkSync(oldPath); | |
writeFileSync(numberPath, number - 1 + ''); | |
} catch (e) { | |
console.log('Error: Cannot undo changes'); | |
} | |
_.splice(0, 1); | |
break; | |
} | |
} | |
console.log(readFileSync(numberPath).toString().charCodeAt(0)); | |
if (redo) { | |
let number = +readFileSync(numberPath) + 1; | |
writeFileSync(join(dir, '.hyper.js.' + number), readFileSync(path)); | |
writeFileSync(numberPath, number + ''); | |
writeJSSync(path, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment