(feat) js executor
This commit is contained in:
parent
63b9f40815
commit
6a33c3a244
|
@ -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 python;
|
||||
|
||||
|
@ -7,6 +8,7 @@ pub mod helper;
|
|||
|
||||
pub fn executors() -> Vec<Box<dyn Executor>> {
|
||||
vec![
|
||||
Box::new(NodeJSExecutor {}),
|
||||
Box::new(PhpExecutor {}),
|
||||
Box::new(PythonExecutor {}),
|
||||
]
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue