cli: add ppman install and list

This commit is contained in:
Thomas Hobson 2021-02-27 19:30:11 +13:00
parent e5225f05c9
commit ff69a28a68
No known key found for this signature in database
GPG Key ID: 9F1FD9D87950DB6F
3 changed files with 73 additions and 0 deletions

7
cli/commands/ppman.js Normal file
View File

@ -0,0 +1,7 @@
exports.command = 'ppman'
exports.aliases = ['pkg']
exports.describe = 'Package Manager'
exports.builder = yargs => yargs
.commandDir('ppman_commands')
.demandCommand()

View File

@ -0,0 +1,35 @@
const {PistonEngine} = require('piston-api-client');
const chalk = require('chalk');
exports.command = ['install <language> <language-version>']
exports.aliases = ['i']
exports.describe = 'Installs the named package'
const msg_format = {
'color': p => `${p.success ? chalk.green.bold('✓') : chalk.red.bold('❌')} Installation ${p.success ? "succeeded" : "failed: " + p.message}`,
'monochrome': p => `Installation ${p.success ? "succeeded" : "failed: " + p.message}`,
'json': JSON.stringify
}
exports.handler = async function(argv){
const api = new PistonEngine(argv['piston-url']);
const repos = await api.list_repos();
const repos_obj = await Promise.all(repos.repos.map(({slug}) => api.get_repo(slug)));
const repo_pkgs = await Promise.all(repos_obj.map(
async repo => ({
repo: repo,
packages: await repo.list_packages().catch(x=>[])
})
))
const repo = repo_pkgs.find(r => r.packages.find(p=>p.language == argv['language'] && p.language_version == argv['language-version']))
if(!repo) throw Error("Package could not be located")
const package = await repo.repo.get_package(argv['language'], argv['language-version'])
const install = await package.install().catch(x=>x)
console.log(msg_format.color(install));
}

View File

@ -0,0 +1,31 @@
const {PistonEngine} = require('piston-api-client');
const chalk = require('chalk');
exports.command = ['list']
exports.aliases = ['l']
exports.describe = 'Lists all available packages'
const msg_format = {
'color': p => `${chalk[p.installed ? "green":"red"]("•")} ${p.language} ${p.language_version}`,
'monochrome': p => `${p.language} ${p.language_version} ${p.installed ? "(INSTALLED)": ""}`,
'json': JSON.stringify
}
exports.handler = async function(argv){
const api = new PistonEngine(argv['piston-url']);
const repos = await api.list_repos();
const repos_obj = await Promise.all(repos.repos.map(({slug}) => api.get_repo(slug)));
const packages = await repos_obj.reduce(async (a, c) => [
...await a,
...await c.list_packages().catch(x=>{console.log(x); return []})
], []);
const pkg_msg = packages
.map(msg_format.color)
.join('\n');
console.log(pkg_msg);
}