-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn check_app_installed(app: &str) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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<()> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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<()> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod js; | ||
pub mod program; |
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")] | ||
|
@@ -12,6 +12,26 @@ struct CargoCommands { | |
enum SailsCommands { | ||
#[command(name = "sails", subcommand)] | ||
Sails(Commands), | ||
#[command(name = "sails-js", subcommand)] | ||
SailsJs(JsCommands), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That won't work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
} | ||
|
||
#[derive(Subcommand)] | ||
enum JsCommands { | ||
#[command(name = "generate-client")] | ||
GenerateClient { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is better to make
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)] | ||
|
@@ -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 { | ||
|
There was a problem hiding this comment.
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