cli: interactive run with -t flag
This commit is contained in:
parent
3436648add
commit
4933577dae
|
@ -1,7 +1,10 @@
|
|||
//const fetch = require('node-fetch');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const chalk = require('chalk');
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const SIGNALS = ["SIGABRT","SIGALRM","SIGBUS","SIGCHLD","SIGCLD","SIGCONT","SIGEMT","SIGFPE","SIGHUP","SIGILL","SIGINFO","SIGINT","SIGIO","SIGIOT","SIGLOST","SIGPIPE","SIGPOLL","SIGPROF","SIGPWR","SIGQUIT","SIGSEGV","SIGSTKFLT","SIGTSTP","SIGSYS","SIGTERM","SIGTRAP","SIGTTIN","SIGTTOU","SIGUNUSED","SIGURG","SIGUSR1","SIGUSR2","SIGVTALRM","SIGXCPU","SIGXFSZ","SIGWINCH"]
|
||||
|
||||
|
||||
exports.command = ['execute <language> <file> [args..]'];
|
||||
exports.aliases = ['run'];
|
||||
|
@ -35,17 +38,115 @@ exports.builder = {
|
|||
alias: ['f'],
|
||||
array: true,
|
||||
desc: 'Additional files to add',
|
||||
},
|
||||
interactive: {
|
||||
boolean: true,
|
||||
alias: ['t'],
|
||||
desc: 'Run interactively using WebSocket transport'
|
||||
},
|
||||
status: {
|
||||
boolean: true,
|
||||
alias: ['s'],
|
||||
desc: 'Output additional status to stderr'
|
||||
}
|
||||
};
|
||||
|
||||
exports.handler = async (argv) => {
|
||||
const files = [...(argv.files || []),argv.file]
|
||||
.map(file_path => {
|
||||
return {
|
||||
name: path.basename(file_path),
|
||||
content: fs.readFileSync(file_path).toString()
|
||||
};
|
||||
});
|
||||
async function handle_interactive(files, argv){
|
||||
const ws = new WebSocket(argv.pistonUrl.replace("http", "ws") + "/api/v2/connect")
|
||||
|
||||
const log_message = (process.stderr.isTTY && argv.status) ? console.error : ()=>{};
|
||||
|
||||
process.on("exit", ()=>{
|
||||
ws.close();
|
||||
process.stdin.end();
|
||||
process.stdin.destroy();
|
||||
process.exit();
|
||||
})
|
||||
|
||||
for(const signal of SIGNALS){
|
||||
process.on(signal, ()=>{
|
||||
ws.send(JSON.stringify({type: 'signal', signal}))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
ws.on('open', ()=>{
|
||||
const request = {
|
||||
type: "init",
|
||||
language: argv.language,
|
||||
version: argv['language_version'],
|
||||
files: files,
|
||||
args: argv.args,
|
||||
compile_timeout: argv.ct,
|
||||
run_timeout: argv.rt
|
||||
}
|
||||
|
||||
ws.send(JSON.stringify(request))
|
||||
log_message(chalk.white.bold("Connected"))
|
||||
|
||||
process.stdin.resume();
|
||||
|
||||
process.stdin.on("data", (data) => {
|
||||
ws.send(JSON.stringify({
|
||||
type: "data",
|
||||
stream: "stdin",
|
||||
data: data.toString()
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
ws.on("close", (code, reason)=>{
|
||||
log_message(
|
||||
chalk.white.bold("Disconnected: "),
|
||||
chalk.white.bold("Reason: "),
|
||||
chalk.yellow(`"${reason}"`),
|
||||
chalk.white.bold("Code: "),
|
||||
chalk.yellow(`"${code}"`),
|
||||
)
|
||||
process.stdin.pause()
|
||||
})
|
||||
|
||||
ws.on('message', function(data){
|
||||
const msg = JSON.parse(data);
|
||||
|
||||
switch(msg.type){
|
||||
case "runtime":
|
||||
log_message(chalk.bold.white("Runtime:"), chalk.yellow(`${msg.language} ${msg.version}`))
|
||||
break;
|
||||
case "stage":
|
||||
log_message(chalk.bold.white("Stage:"), chalk.yellow(msg.stage))
|
||||
break;
|
||||
case "data":
|
||||
if(msg.stream == "stdout") process.stdout.write(msg.data)
|
||||
else if(msg.stream == "stderr") process.stderr.write(msg.data)
|
||||
else log_message(chalk.bold.red(`(${msg.stream}) `), msg.data)
|
||||
break;
|
||||
case "exit":
|
||||
if(msg.signal === null)
|
||||
log_message(
|
||||
chalk.white.bold("Stage"),
|
||||
chalk.yellow(msg.stage),
|
||||
chalk.white.bold("exited with code"),
|
||||
chalk.yellow(msg.code)
|
||||
)
|
||||
else
|
||||
log_message(
|
||||
chalk.white.bold("Stage"),
|
||||
chalk.yellow(msg.stage),
|
||||
chalk.white.bold("exited with signal"),
|
||||
chalk.yellow(msg.signal)
|
||||
)
|
||||
break;
|
||||
default:
|
||||
log_message(chalk.red.bold("Unknown message:"), msg)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
async function run_non_interactively(files, argv) {
|
||||
|
||||
|
||||
const stdin = (argv.stdin && await new Promise((resolve, _) => {
|
||||
let data = '';
|
||||
|
@ -99,3 +200,18 @@ exports.handler = async (argv) => {
|
|||
|
||||
step('Run', response.run);
|
||||
}
|
||||
|
||||
exports.handler = async (argv) => {
|
||||
const files = [...(argv.files || []),argv.file]
|
||||
.map(file_path => {
|
||||
return {
|
||||
name: path.basename(file_path),
|
||||
content: fs.readFileSync(file_path).toString()
|
||||
};
|
||||
});
|
||||
|
||||
if(argv.interactive) await handle_interactive(files, argv);
|
||||
else await run_non_interactively(files, argv);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
"minimatch": "^3.0.4",
|
||||
"nocamel": "^1.0.2",
|
||||
"semver": "^7.3.5",
|
||||
"ws": "^7.5.3",
|
||||
"yargs": "^16.2.0"
|
||||
}
|
||||
},
|
||||
|
@ -243,6 +244,26 @@
|
|||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/ws": {
|
||||
"version": "7.5.3",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-7.5.3.tgz",
|
||||
"integrity": "sha512-kQ/dHIzuLrS6Je9+uv81ueZomEwH0qVYstcAQ4/Z93K8zeko9gtAbttJWzoC5ukqXY1PpoouV3+VSOqEAFt5wg==",
|
||||
"engines": {
|
||||
"node": ">=8.3.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bufferutil": "^4.0.1",
|
||||
"utf-8-validate": "^5.0.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bufferutil": {
|
||||
"optional": true
|
||||
},
|
||||
"utf-8-validate": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/y18n": {
|
||||
"version": "5.0.5",
|
||||
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.5.tgz",
|
||||
|
@ -455,6 +476,12 @@
|
|||
"strip-ansi": "^6.0.0"
|
||||
}
|
||||
},
|
||||
"ws": {
|
||||
"version": "7.5.3",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-7.5.3.tgz",
|
||||
"integrity": "sha512-kQ/dHIzuLrS6Je9+uv81ueZomEwH0qVYstcAQ4/Z93K8zeko9gtAbttJWzoC5ukqXY1PpoouV3+VSOqEAFt5wg==",
|
||||
"requires": {}
|
||||
},
|
||||
"y18n": {
|
||||
"version": "5.0.5",
|
||||
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.5.tgz",
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
"minimatch": "^3.0.4",
|
||||
"nocamel": "^1.0.2",
|
||||
"semver": "^7.3.5",
|
||||
"ws": "^7.5.3",
|
||||
"yargs": "^16.2.0"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue