piston/cli/commands/execute.js

102 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-03-13 04:44:33 +01:00
//const fetch = require('node-fetch');
2021-02-22 11:38:11 +01:00
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
2021-04-23 05:27:50 +02:00
exports.command = ['execute <language> <file> [args..]'];
exports.aliases = ['run'];
exports.describe = 'Executes file with the specified runner';
2021-02-22 11:38:11 +01:00
exports.builder = {
2021-04-23 01:59:54 +02:00
languageVersion: {
string: true,
desc: 'Set the version of the language to use',
alias: ['l'],
default: '*'
},
2021-02-22 11:38:11 +01:00
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',
}
2021-04-23 05:27:50 +02:00
};
2021-02-22 11:38:11 +01:00
2021-04-23 05:27:50 +02:00
exports.handler = async function(argv) {
2021-02-22 11:38:11 +01:00
const files = [...(argv.files || []),argv.file]
2021-04-23 05:27:50 +02:00
.map(file_path => {
return {
name: path.basename(file_path),
content: fs.readFileSync(file_path).toString()
};
});
2021-02-22 11:38:11 +01:00
2021-04-23 05:27:50 +02:00
const stdin = (argv.stdin && await new Promise((resolve, _) => {
let data = '';
process.stdin.on('data', d => data += d);
process.stdin.on('end', _ => resolve(data));
})) || '';
2021-02-22 11:38:11 +01:00
2021-03-13 04:08:35 +01:00
const request = {
2021-03-05 07:40:47 +01:00
language: argv.language,
version: argv['language-version'],
files: files,
2021-03-05 12:44:35 +01:00
args: argv.args,
2021-02-22 11:38:11 +01:00
stdin,
2021-03-05 07:40:47 +01:00
compile_timeout: argv.ct,
run_timeout: argv.rt
2021-03-13 04:08:35 +01:00
};
2021-04-23 05:27:50 +02:00
let { data: response } = await argv.axios.post('/api/v1/execute', request);
2021-02-22 11:38:11 +01:00
2021-04-23 05:27:50 +02:00
const step = (name, ctx) => {
console.log(chalk.bold(`== ${name} ==`));
if (ctx.stdout) {
console.log(chalk.bold(`STDOUT`))
console.log(ctx.stdout.replace(/\n/g,'\n '))
2021-02-22 11:38:11 +01:00
}
2021-04-23 05:27:50 +02:00
if (ctx.stderr) {
2021-02-22 11:38:11 +01:00
console.log(chalk.bold(`STDERR`))
2021-04-23 05:27:50 +02:00
console.log(ctx.stderr.replace(/\n/g,'\n '))
2021-02-22 11:38:11 +01:00
}
2021-04-23 05:27:50 +02:00
if (ctx.code) {
2021-02-22 11:38:11 +01:00
console.log(
chalk.bold(`Exit Code:`),
chalk.bold[ctx.code > 0 ? 'red' : 'green'](ctx.code)
2021-04-23 05:27:50 +02:00
);
}
if (ctx.signal) {
2021-02-22 11:38:11 +01:00
console.log(
chalk.bold(`Signal:`),
chalk.bold.yellow(ctx.signal)
2021-04-23 05:27:50 +02:00
);
}
2021-02-22 11:38:11 +01:00
}
2021-03-14 07:36:12 +01:00
2021-04-23 05:27:50 +02:00
if (response.compile) {
step('Compile', response.compile);
}
step('Run', response.run);
}