piston/cli/commands/ppman_commands/install.js

37 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-02-27 07:30:11 +01:00
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 = {
2021-02-27 13:16:33 +01:00
'color': p => `${p.language ? chalk.green.bold('✓') : chalk.red.bold('❌')} Installation ${p.language ? "succeeded" : "failed: " + p.message}`,
'monochrome': p => `Installation ${p.language ? "succeeded" : "failed: " + p.message}`,
2021-02-27 07:30:11 +01:00
'json': JSON.stringify
}
exports.handler = async function(argv){
const api = new PistonEngine(argv['piston-url']);
const repos = await api.list_repos();
2021-03-05 07:40:47 +01:00
2021-02-27 07:30:11 +01:00
const repos_obj = await Promise.all(repos.repos.map(({slug}) => api.get_repo(slug)));
2021-03-05 07:40:47 +01:00
2021-02-27 07:30:11 +01:00
const repo_pkgs = await Promise.all(repos_obj.map(
async repo => ({
repo: repo,
2021-03-05 07:40:47 +01:00
packages: await repo.list_packages().catch(_=>[])
2021-02-27 07:30:11 +01:00
})
))
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));
}