Skip to content

Commit

Permalink
feat(cli): integrate sails-js-cli into sails-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
osipov-mit committed Sep 12, 2024
1 parent f44fc56 commit 1815e68
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
69 changes: 69 additions & 0 deletions rs/cli/src/js.rs
Original file line number Diff line number Diff line change
@@ -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("[email protected]")
.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(())
}
1 change: 1 addition & 0 deletions rs/cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod js;
pub mod program;
30 changes: 29 additions & 1 deletion rs/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1815e68

Please sign in to comment.