From bf9521881d84f8be85bc9ab735c5dabc94d0d1c9 Mon Sep 17 00:00:00 2001 From: StarNumber12046 <64470722+StarNumber12046@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:58:19 +0200 Subject: [PATCH 1/3] feat: added first version of help menu --- .gitignore | 1 + src/commands/command_handler.rs | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) 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/src/commands/command_handler.rs b/src/commands/command_handler.rs index e9397c1..c0bc04b 100644 --- a/src/commands/command_handler.rs +++ b/src/commands/command_handler.rs @@ -20,8 +20,7 @@ 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]"); return Ok(()); } }; From 11882450a0567c3506cb66fcbf49f49f76418d4c Mon Sep 17 00:00:00 2001 From: StarNumber12046 <64470722+StarNumber12046@users.noreply.github.com> Date: Wed, 25 Oct 2023 19:36:13 +0200 Subject: [PATCH 2/3] feat: added "exec" command The command is used to execute a js file with preserve symlinks --- src/cache.rs | 1 - src/commands/command_handler.rs | 4 +++- src/commands/exec.rs | 40 +++++++++++++++++++++++++++++++++ src/commands/mod.rs | 1 + src/errors.rs | 2 ++ 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/commands/exec.rs 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 c0bc04b..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,13 +21,14 @@ pub async fn handle_args(mut args: Args) -> Result<(), ParseError> { let command = match args.next() { Some(command) => command, None => { - println!("Use: click [options]\n click install [semver]"); + 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) } From d41383d6d1a30d599fd23d934bb4f070c555f345 Mon Sep 17 00:00:00 2001 From: StarNumber12046 <64470722+StarNumber12046@users.noreply.github.com> Date: Wed, 25 Oct 2023 19:37:02 +0200 Subject: [PATCH 3/3] docs: updated README to include new command --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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?