Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): integrate sails-js-cli into sails-cli #523

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions rs/cli/src/js.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use anyhow::Result;
use std::process::Command;

const SAILS_JS_CLI_VERSION: &str = "0.1.0";

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(format!("sails-js-cli@{}", SAILS_JS_CLI_VERSION))
.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");
Comment on lines +38 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please instead of using panic, expect, etc., use Result to return the error s it can printed out by the program in standard way. This comment is applicable to the entire mod

}
Ok(())
}
}

pub fn check_app_installed(app: &str) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need to be pub

Command::new("which")
.arg(app)
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}

pub fn check_node() -> Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need to be pub

if !check_app_installed("node") {
panic!("Node.js is not installed. Please install Node.js to continue.")
}

Ok(())
}

pub fn check_npx() -> Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need to be pub

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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't work.
Only one command sails per cargo-sails executable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

}

#[derive(Subcommand)]
enum JsCommands {
#[command(name = "generate-client")]
GenerateClient {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is better to make js-client under sails, like

cargo sails js-client service.idl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

#[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