diff --git a/.gitignore b/.gitignore index ea8c4bf..ccb5166 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.vscode \ No newline at end of file diff --git a/README.md b/README.md index 8fca565..4512e4b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Make sure you have Rust installed first! - Run `cargo run --release install package` or `cargo run --release install package@version` **IMPORTANT ⚠️** -In order for the symlinks to work you need to use the `--preserve-symlinks` flag when running `node myfile.js`. If development continues I'll make this easier so you don't have to do this extra step! +In order for the symlinks to work you need to use the `--preserve-symlinks` flag when running `node myfile.js`. You can also use the command `click exec myfile.js` ## How fast? diff --git a/src/cache.rs b/src/cache.rs index 9d3ac1c..2203df9 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -2,7 +2,6 @@ use std::{ collections::HashMap, fs::{self as fs_sync, File}, io::{ErrorKind, Read, Seek, SeekFrom}, - path::Path, str::FromStr, }; diff --git a/src/commands/command_handler.rs b/src/commands/command_handler.rs index e9397c1..cddb52e 100644 --- a/src/commands/command_handler.rs +++ b/src/commands/command_handler.rs @@ -7,6 +7,7 @@ use crate::errors::{ }; use super::install::InstallHandler; +use super::exec::RunFileHandler; #[async_trait] pub trait CommandHandler { @@ -20,14 +21,14 @@ pub async fn handle_args(mut args: Args) -> Result<(), ParseError> { let command = match args.next() { Some(command) => command, None => { - // TODO(conaticus): Implement help menu - println!("No help menu implemented yet."); + println!("Use: click [options]\n click install [semver]\n click exec "); return Ok(()); } }; let mut command_handler: Box = match command.to_lowercase().as_str() { "install" => Box::::default(), + "exec" => Box::::default(), _ => return Err(CommandNotFound(command.to_string())), }; diff --git a/src/commands/exec.rs b/src/commands/exec.rs new file mode 100644 index 0000000..67dbca1 --- /dev/null +++ b/src/commands/exec.rs @@ -0,0 +1,40 @@ +use crate::errors::{CommandError, ParseError}; +use async_trait::async_trait; +use std::env::Args; +use std::process::Command; +use std::io; + +use super::command_handler::CommandHandler; + +#[derive(Default)] +pub struct RunFileHandler { + file_name: String, +} + +#[async_trait] +impl CommandHandler for RunFileHandler { + fn parse(&mut self, args: &mut Args) -> Result<(), ParseError> { + let parsed_args = args + .next() + .ok_or(ParseError::MissingArgument(String::from("file name")))?; + + self.file_name = parsed_args; + + Ok(()) + } + async fn execute(&self) -> Result<(), CommandError> { + let cmd = Command::new("node") + .args(["--preserve-symlinks", &self.file_name]) + .status() + .map_err(CommandError::ComandFailedError)?; + + if !(cmd.success()) { + let error_message = "Something went wrong"; + + let error = io::Error::new(io::ErrorKind::Other, error_message); + return Err(CommandError::ComandFailedError(error)); + } + + Ok(()) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0892db0..050d5e2 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,2 +1,3 @@ pub mod command_handler; pub mod install; +pub mod exec; \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs index 863556f..295e647 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -35,4 +35,6 @@ pub enum CommandError { FailedToWriteFile(Error), #[error("failed to serialize package lock ({0})")] FailedToSerializePackageLock(serde_json::Error), + #[error("command failed ({0})")] + ComandFailedError(Error) }