Skip to content

Commit

Permalink
fix(argv): try use js process for argv
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Jul 23, 2024
1 parent 6223b4a commit ce477e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn define_command(options: Command) -> Command {

#[napi]
pub fn run(env: Env, cmd: Command, args: Option<Vec<String>>) -> Result<()> {
let raw_args = resolve_option_args(args);
let raw_args = resolve_option_args(env, args)?;
let clap = resolve_command(clap::Command::default(), Default::default(), &cmd);
let matches = clap.clone().get_matches_from(&raw_args);

Expand Down
20 changes: 17 additions & 3 deletions src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ use crate::{
utils::{leak_borrowed_str, leak_borrowed_str_or_default, leak_str},
};

pub(crate) fn resolve_option_args(args: Option<Vec<String>>) -> Vec<String> {
let mut args = args.unwrap_or(std::env::args().collect());
pub(crate) fn resolve_option_args(
env: napi::Env,
args: Option<Vec<String>>,
) -> napi::Result<Vec<String>> {
let mut args = match args {
Some(args) => args,
None => {
let mut args = std::env::args().collect::<Vec<String>>();
if args.is_empty() {
let process: napi::JsObject = env.get_global()?.get_named_property("process")?;
let argv: Vec<String> = process.get_named_property("argv")?;
args = argv;
}
args
}
};
args.remove(0); // remove `node.exe`
args
Ok(args)
}

pub(crate) fn resolve_command_meta(
Expand Down

0 comments on commit ce477e2

Please sign in to comment.