-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): integrate sails-js-cli into sails-cli
- Loading branch information
1 parent
f44fc56
commit 1815e68
Showing
3 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod js; | ||
pub mod program; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters