diff --git a/rs/cli/src/js.rs b/rs/cli/src/js.rs new file mode 100644 index 00000000..1c76fcbd --- /dev/null +++ b/rs/cli/src/js.rs @@ -0,0 +1,69 @@ +use anyhow::Result; +use std::process::Command; + +pub struct JsClientGenerator { + idl_path: String, + out_path: String, + program_name: String, +} + +impl JsClientGenerator { + pub fn new(idl_path: String, out_path: String, program_name: String) -> Self { + Self { + idl_path, + out_path, + program_name, + } + } + + pub fn generate(&self) -> Result<()> { + check_node()?; + check_npx()?; + + let out_path = self.out_path.clone(); + let program_name = self.program_name.clone(); + let idl_path = self.idl_path.clone(); + + let mut child = Command::new("npx") + .arg("sails-js-cli@0.1.0") + .arg("generate") + .arg(idl_path) + .arg("-o") + .arg(out_path) + .arg("-n") + .arg(program_name) + .spawn() + .expect("Failed to run npx sails-js-cli"); + + let status = child.wait().expect("An error occured"); + + if !status.success() { + panic!("Failed to generate JS client"); + } + Ok(()) + } +} + +pub fn check_app_installed(app: &str) -> bool { + Command::new("which") + .arg(app) + .output() + .map(|output| output.status.success()) + .unwrap_or(false) +} + +pub fn check_node() -> Result<()> { + if !check_app_installed("node") { + panic!("Node.js is not installed. Please install Node.js to continue.") + } + + Ok(()) +} + +pub fn check_npx() -> Result<()> { + if !check_app_installed("npx") { + panic!("npx is not installed. Please install npx to continue.") + } + + Ok(()) +} diff --git a/rs/cli/src/lib.rs b/rs/cli/src/lib.rs index e2c04eaa..11eee1a7 100644 --- a/rs/cli/src/lib.rs +++ b/rs/cli/src/lib.rs @@ -1 +1,2 @@ +pub mod js; pub mod program; diff --git a/rs/cli/src/main.rs b/rs/cli/src/main.rs index bb7605bd..1cd430b7 100644 --- a/rs/cli/src/main.rs +++ b/rs/cli/src/main.rs @@ -1,5 +1,5 @@ use clap::{Parser, Subcommand}; -use sails_cli::program::ProgramGenerator; +use sails_cli::{js::JsClientGenerator, program::ProgramGenerator}; #[derive(Parser)] #[command(bin_name = "cargo")] @@ -12,6 +12,26 @@ struct CargoCommands { enum SailsCommands { #[command(name = "sails", subcommand)] Sails(Commands), + #[command(name = "sails-js", subcommand)] + SailsJs(JsCommands), +} + +#[derive(Subcommand)] +enum JsCommands { + #[command(name = "generate-client")] + GenerateClient { + #[arg(help = "Path to the IDL file")] + idl: String, + #[arg( + short, + long, + help = "Path to the output directory", + default_value = "." + )] + out: String, + #[arg(short, long, help = "Name of the program", default_value = "program")] + program_name: String, + }, } #[derive(Subcommand)] @@ -48,6 +68,14 @@ fn main() -> Result<(), i32> { .with_gtest(!no_gtest); program_generator.generate() } + SailsCommands::SailsJs(JsCommands::GenerateClient { + idl, + out, + program_name, + }) => { + let generator = JsClientGenerator::new(idl, out, program_name); + generator.generate() + } }; if let Err(e) = result {