mirror of
https://github.com/engineer-man/piston.git
synced 2025-04-20 20:16:26 +02:00
cil: execute command
This commit is contained in:
parent
16b86607b1
commit
1fd3dce31d
5 changed files with 257 additions and 0 deletions
90
cli/commands/execute.js
Normal file
90
cli/commands/execute.js
Normal file
|
@ -0,0 +1,90 @@
|
|||
const {PistonEngine} = require('piston-api-wrapper');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const chalk = require('chalk');
|
||||
|
||||
exports.command = ['execute <language> <language-version> <file> [args..]']
|
||||
exports.aliases = ['run']
|
||||
exports.describe = 'Executes file with the specified runner'
|
||||
|
||||
exports.builder = {
|
||||
stdin: {
|
||||
boolean: true,
|
||||
desc: 'Read input from stdin and pass to executor',
|
||||
alias: ['i']
|
||||
},
|
||||
run_timeout: {
|
||||
alias: ['rt', 'r'],
|
||||
number: true,
|
||||
desc: 'Milliseconds before killing run process',
|
||||
default: 3000
|
||||
},
|
||||
compile_timeout: {
|
||||
alias: ['ct', 'c'],
|
||||
number: true,
|
||||
desc: 'Milliseconds before killing compile process',
|
||||
default: 10000,
|
||||
},
|
||||
files: {
|
||||
alias: ['f'],
|
||||
array: true,
|
||||
desc: 'Additional files to add',
|
||||
}
|
||||
}
|
||||
|
||||
exports.handler = async function(argv){
|
||||
|
||||
const api = new PistonEngine(argv['piston-url']);
|
||||
|
||||
const files = [...(argv.files || []),argv.file]
|
||||
.map(file_path => ({
|
||||
name: path.basename(file_path),
|
||||
content: fs.readFileSync(file_path).toString()
|
||||
}));
|
||||
|
||||
|
||||
const stdin = (argv.stdin && await new Promise((resolve, _)=>{
|
||||
var data = "";
|
||||
process.stdin.on('data', d=> data += d)
|
||||
process.stdin.on('end', _ => resolve(data))
|
||||
})) || "";
|
||||
|
||||
|
||||
const response = await api.run_job(
|
||||
argv.language,
|
||||
argv['language-version'],
|
||||
files,
|
||||
argv.file,
|
||||
argv.args,
|
||||
stdin,
|
||||
argv.ct,
|
||||
argv.rt
|
||||
)
|
||||
|
||||
function step(name, ctx){
|
||||
console.log(chalk.bold(`== ${name} ==`))
|
||||
if(ctx.stdout){
|
||||
console.log(" ",chalk.bold(`STDOUT`))
|
||||
console.log(" ",ctx.stdout.replace(/\n/g,'\n '))
|
||||
}
|
||||
if(ctx.stderr){
|
||||
console.log(chalk.bold(`STDERR`))
|
||||
console.log(" ",ctx.stderr.replace(/\n/g,'\n '))
|
||||
}
|
||||
|
||||
if(ctx.code)
|
||||
console.log(
|
||||
chalk.bold(`Exit Code:`),
|
||||
chalk.bold[ctx.code > 0 ? 'red' : 'green'](ctx.code)
|
||||
)
|
||||
if(ctx.signal)
|
||||
console.log(
|
||||
chalk.bold(`Signal:`),
|
||||
chalk.bold.yellow(ctx.signal)
|
||||
)
|
||||
}
|
||||
|
||||
if(response.compile) step('Compile', response.compile)
|
||||
step('Run', response.run)
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue