Last active
February 18, 2020 16:47
-
-
Save isurfer21/bc4fc6698acdb3f53015484c9e939dcb to your computer and use it in GitHub Desktop.
Web Shell Package Manager
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
/* | |
WSPM | |
Web Shell Package Manager | |
Copyright (c) 2020 Abhishek Kumar. | |
Protected by Creative Commons license (CC BY 4.0). | |
@file wspm.js | |
@author Abhishek Kumar | |
*/ | |
const APP_VER = '1.2.1'; | |
const Config = { | |
url: 'https://docs.google.com/spreadsheets/d/e/2PACX-1vTVr6IFAZwCake8zGd97zlc8CBd3Xozxm93xukMo5ZgvinEmR7vLOhI-mgM1R0kT4xvsCriBMvmA5lW/pub?gid=2142129124&single=true&output=csv', | |
form: { | |
url: 'https://docs.google.com/forms/d/e/1FAIpQLSf-XVIOf1NWFNnIaQ1Jlyx8zv45W8QfP7CtXk9mxIMi3QDaNg/viewform' | |
} | |
}; | |
export function wspm(argv) { | |
if (argv.h || argv.help) { | |
return ` | |
Package manager for Web Shell | |
Syntax: | |
wspm | |
Options: | |
-h --help show help options | |
-v --ver show version | |
-r --reg register new app | |
`; | |
} else if (argv.v || argv.ver) { | |
return `WSPM (Version ${APP_VER})`; | |
} else if (argv.r || argv.reg) { | |
return registerPkg(); | |
} else { | |
return fetchRegPkg(Config.url); | |
} | |
} | |
function registerPkg() { | |
window.open(Config.form.url, '_blank'); | |
return `Click <a href="${Config.form.url}">here</a> to open the form.`; | |
} | |
function fetchRegPkg(url) { | |
fetch(url, { | |
headers: { | |
"Content-Type": "text/csv; charset=utf-8" | |
} | |
}).then(response => { | |
if (response.ok) { | |
response.text().then(text => { | |
let tbl = csvToArray(text); | |
if (!!tbl) { | |
Shell.write(renderArray(tbl)); | |
Shell.read('Choose (index) to install: ', (id) => { | |
if (!!id && !isNaN(id)) { | |
Shell.exit(`install ${tbl[id][4]}`); | |
} else { | |
Shell.exit(); | |
} | |
}); | |
} else { | |
Shell.exit(); | |
} | |
}); | |
} else { | |
Shell.write('Error: failed to fetch registered package'); | |
Shell.exit(); | |
} | |
}).catch(err => { | |
Shell.write(err); | |
Shell.exit(); | |
}); | |
} | |
function renderArray(list) { | |
let output = []; | |
for (let i = 1; i < list.length; i++) { | |
output.push(`<b>${list[i][2]}</b> <br>${list[i][3]}`); | |
} | |
return '<ol><li>' + output.join('</li><li>') + '</li></ol>'; | |
} | |
function csvToArray(strData, strDelimiter) { | |
strDelimiter = (strDelimiter || ","); | |
var objPattern = new RegExp(("(\\" + strDelimiter + "|\\r?\\n|\\r|^)" + "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" + "([^\"\\" + strDelimiter + "\\r\\n]*))"), "gi"); | |
var arrData = [ | |
[] | |
]; | |
var arrMatches = null; | |
while (arrMatches = objPattern.exec(strData)) { | |
var strMatchedDelimiter = arrMatches[1]; | |
if (strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter)) { | |
arrData.push([]) | |
} | |
if (arrMatches[2]) { | |
var strMatchedValue = arrMatches[2].replace(new RegExp("\"\"", "g"), "\"") | |
} else { | |
var strMatchedValue = arrMatches[3] | |
} | |
arrData[arrData.length - 1].push(strMatchedValue) | |
} | |
return (arrData) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Production
Click to Install in WebShell
CDN supported CDN URL
Development
Click to Install in WebShell
CDN supported CDN URL