From 6a33c3a2442c0a85d08eaea2a754b0c6884252b4 Mon Sep 17 00:00:00 2001 From: b1ek Date: Mon, 19 Feb 2024 23:10:08 +1000 Subject: [PATCH] (feat) js executor --- src/executor.rs | 4 +++- src/executor/javascript.rs | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/executor/javascript.rs diff --git a/src/executor.rs b/src/executor.rs index 6a12203..cefbed1 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -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> { vec![ + Box::new(NodeJSExecutor {}), Box::new(PhpExecutor {}), Box::new(PythonExecutor {}), ] diff --git a/src/executor/javascript.rs b/src/executor/javascript.rs new file mode 100644 index 0000000..e13bbfa --- /dev/null +++ b/src/executor/javascript.rs @@ -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 { + 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) + } +} \ No newline at end of file