(feat) js executor

This commit is contained in:
b1ek 2024-02-19 23:10:08 +10:00
parent 63b9f40815
commit 6a33c3a244
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 44 additions and 1 deletions

View File

@ -1,5 +1,6 @@
use crate::executor::{php::PhpExecutor, python::PythonExecutor}; use crate::executor::{javascript::NodeJSExecutor, php::PhpExecutor, python::PythonExecutor};
pub mod javascript;
pub mod php; pub mod php;
pub mod python; pub mod python;
@ -7,6 +8,7 @@ pub mod helper;
pub fn executors() -> Vec<Box<dyn Executor>> { pub fn executors() -> Vec<Box<dyn Executor>> {
vec![ vec![
Box::new(NodeJSExecutor {}),
Box::new(PhpExecutor {}), Box::new(PhpExecutor {}),
Box::new(PythonExecutor {}), Box::new(PythonExecutor {}),
] ]

View File

@ -0,0 +1,41 @@
use std::fs;
use std::process::Command;
use std::process::Stdio;
use super::Executor;
use super::helper::*;
pub struct NodeJSExecutor {}
impl Executor for NodeJSExecutor {
fn id(&self) -> String {
"javascript".into()
}
fn exec(&self, code: String) -> Result<String, String> {
assure_dir_exists().map_err(|x| x.to_string())?;
let path = create_path();
// 1. Save the code
fs::write(path.clone(), code).map_err(|x| x.to_string())?;
// 2. Run it!
let mut out = Command::new("node")
.arg(path)
.stdout(Stdio::piped())
.spawn()
.map_err(|x| x.to_string())?;
// 3. Grab the exit code
let exit_status = out.wait().map_err(|x| x.to_string())?;
let mut stdout: String = get_stdout(out.stdout)?;
stdout += "\n\n";
stdout += exit_code_msg(exit_status).as_str();
Ok(stdout)
}
}