Created
June 21, 2020 07:09
-
-
Save diginfo/9a8b73d9ebdd306a9d489784e6207808 to your computer and use it in GitHub Desktop.
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
module.exports = {} | |
const mex = module.exports; | |
const cl = console.log; | |
mex.cl = cl; | |
mex._rl = require('readline'); | |
mex._os = require('os'); | |
mex._path = require('path'); | |
mex._fs = require('fs'); | |
mex._async = require("async"); | |
mex._cp = require('child_process'); | |
mex._crypto = require('crypto'); | |
mex._promisify = require('util').promisify; | |
mex._arg = process.argv.splice(2); | |
mex._cmd = mex._arg.shift(); | |
mex._opt = []; | |
mex.ce = function(...args){ | |
args.map(function(e,i){args[i] = mex.style(e.trim(),'fgr')}) | |
console.error(...args); | |
} | |
mex.doargs = function(){ | |
var dels=[]; | |
mex._arg.map(function(arg,idx){ | |
if((/^-+/).test(arg)) { | |
mex._opt.push(arg.replace(/^-+/,'')); | |
dels.push(idx); | |
}; | |
}) | |
dels.reverse().map(function(idx){ | |
mex._arg.splice(idx,1) | |
}) | |
} | |
mex.exit = process.exit; | |
mex.quit = function(msg='Quit.'){ | |
if(msg) mex.cl(mex.style(msg,'fgr')); | |
mex.exit(); | |
} | |
// styling the console output. | |
mex.style = function(str,sty,noclr){ | |
var cols={res: "\x1b[0m",bright : "\x1b[1m",dim : "\x1b[2m",ul : "\x1b[4m",blink : "\x1b[5m",rev : "\x1b[7m",hide : "\x1b[8m",fgk : "\x1b[30m",fgr : "\x1b[31m",fgg : "\x1b[32m",fgy : "\x1b[33m",fgb : "\x1b[34m",fgv : "\x1b[35m",fgc : "\x1b[36m",fgw : "\x1b[37m",bgk : "\x1b[40m",bgr : "\x1b[41m",bgg : "\x1b[42m",bgy : "\x1b[43m",bgb : "\x1b[44m",bgv : "\x1b[45m",bgc : "\x1b[46m",bgw : "\x1b[47m"}; | |
if(typeof sty =='string') sty=[sty]; | |
sty.map(function(e){str = cols[e]+str;}) | |
if(!noclr) str += cols['res']; | |
return str; | |
} | |
mex.h1 = function(txt,len=10){ | |
const tlen = txt.length; | |
cl(); | |
cl(mex.style(mex.rpad(mex.lpad(` ${txt} `,tlen+len,'-'),tlen+len*2,'-'),'bgb')) | |
} | |
mex.h2 = function(txt){cl(mex.style(`\n${txt}`,['fgc','ul']))} | |
mex.list = function(list,sty){ | |
if(!Array.isArray(list)) return oblist(list,sty); | |
list.map(function(row,i){ | |
if(!row) return; | |
var line = `${mex.lpad(i+1)}. ${row}` | |
if(sty) cl(mex.style(line,sty)); | |
else cl(line); | |
}) | |
} | |
// return true if valid. | |
mex.listval = function(list,idx){ | |
var data = {vals:idx, error:false}; | |
if(idx=='*') data.vals = list.map(function(e,i){return i+1}); | |
else if((/^$|^x$|^q$/).test(idx)) data.error = true; | |
else if (!isNaN(idx)){ | |
if(idx > list.length || idx < 1) data.error=true; | |
} | |
else data.vals = idx.toString().split(/, |,/); | |
if(data.vals.length > list.length) data.error = true; | |
if(!Array.isArray(data.vals)) data.vals = [data.vals]; | |
data.vals.map(function(e,i){data.vals[i]--}); | |
if(data.error) data.vals = []; | |
return data; | |
} | |
mex.test = function(){ | |
cl(mex.listval(["a","b","c","d"],mex._arg[0])) | |
} | |
mex.oblist = function (obj,sty){ | |
var i =1; | |
for(var k in obj){ | |
cl(`${mex.lpad(i,2)} ${mex.lpad(k,6)} : ${JSON.stringify(obj[k]).replace(/"/g,'')}`); | |
i++; | |
} | |
} | |
mex.rpad = function(data,len=2,chr=' '){ | |
return data.toString().padEnd(len,chr) | |
} | |
mex.lpad = function(data,len=2,chr=' '){ | |
return data.toString().padStart(len,chr) | |
} | |
mex.pad = mex.lpad; | |
mex.newrl = function(){ | |
return mex._rl.createInterface({input: process.stdin,output: process.stdout}); | |
} | |
mex.isopt = function(opt){ | |
return (mex._opt.indexOf(opt)>-1) | |
} | |
mex.isarg = function(opt){ | |
return (mex._arg.indexOf(opt)>-1) | |
} | |
mex.str2bool = function(str){ | |
return str.toString()=='true'; | |
} | |
mex.str2array = function(str){ | |
if(!Array.isArray(str)) return str.split(/,\s*|\s/); | |
return str; | |
} | |
mex.funcargs = function(fn){ | |
return fn.toString() | |
.replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s))/mg,'') | |
.match(/^function\s*[^\(]*\(\s*([^\)]*)\)/m)[1] | |
.split(/,/); | |
} | |
mex.prompt = function(msg,cb,def){ | |
var rl = mex.newrl(); | |
rl.question(mex.style(`${msg} > `,'fgw'),function(res){ | |
setTimeout(function(){cb(res)}); | |
rl.close() | |
}); | |
if(def) rl.write(def); | |
} | |
mex.jsparse = function(str){ | |
try{return JSON.parse(str)} | |
catch(e){return({error:true,msg:e.message})} | |
} | |
mex.jsstring = function(obj){ | |
try{return JSON.stringify(obj)} | |
catch(e){return({error:true,msg:e.message})} | |
} | |
mex.exec = function(dir,cmd,cb,opts){ | |
//opts { maxBuffer: 10 * 1024 * 1024 * 1024}; | |
opts=opts||{}; | |
process.chdir(dir); | |
if((/^git/).test(cmd) && mex.isopt('test')) cmd = cmd.replace(/^git/g,'git --dry-run'); | |
mex._cp.exec(cmd,opts,function (error, stdout, stderr) { | |
if(typeof(cb)=="function") { | |
if(error){ return cb({error:true,'err':error.message,'std':stderr,'msg':stderr});} | |
else return cb(stdout); | |
} else return null; | |
}); | |
}; | |
mex.help = function(mod=mex,cb=cl){ | |
for(var k in mod){ | |
if(typeof mod[k]=='function') cl(`pacutils ${k} [${mex.funcargs(mod[k]).join('|')}]`); | |
} | |
} | |
/* COMMAND-LINE RUN */ | |
if(!module.parent){ | |
mex.doargs(); | |
// _arg are variables, _opt are control options. | |
cl(mex._cmd); | |
if(mex._cmd) mex[mex._cmd](...mex._arg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment