From 62f152659204ce1218178596f463f0bcfbd4e6dc Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Fri, 10 Feb 2023 03:31:13 -0800 Subject: [PATCH] fix(cli): generate build script using NPM runner if it was used (#6233) --- .../fix-build-script-mobile-runner-npm.md | 6 ++++ tooling/cli/src/mobile/init.rs | 32 ++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 .changes/fix-build-script-mobile-runner-npm.md diff --git a/.changes/fix-build-script-mobile-runner-npm.md b/.changes/fix-build-script-mobile-runner-npm.md new file mode 100644 index 000000000..e076d7dcb --- /dev/null +++ b/.changes/fix-build-script-mobile-runner-npm.md @@ -0,0 +1,6 @@ +--- +"cli.rs": patch +"cli.js": patch +--- + +Fixes the generated mobile build script when using an NPM runner. diff --git a/tooling/cli/src/mobile/init.rs b/tooling/cli/src/mobile/init.rs index c6aeaba17..1f8b87bad 100644 --- a/tooling/cli/src/mobile/init.rs +++ b/tooling/cli/src/mobile/init.rs @@ -19,14 +19,17 @@ use tauri_mobile::{ }, }; -use std::{env::current_dir, path::PathBuf}; +use std::{ + env::{current_dir, var, var_os}, + path::PathBuf, +}; pub fn command(target: Target, ci: bool, reinstall_deps: bool) -> Result<()> { let wrapper = TextWrapper::with_splitter(textwrap::termwidth(), textwrap::NoHyphenation); exec( target, &wrapper, - ci || std::env::var("CI").is_ok(), + ci || var_os("CI").is_some(), reinstall_deps, ) .map_err(|e| anyhow::anyhow!("{:#}", e))?; @@ -85,7 +88,7 @@ pub fn exec( let (handlebars, mut map) = handlebars(&app); let mut args = std::env::args_os(); - let tauri_binary = args + let mut binary = args .next() .map(|bin| { let path = PathBuf::from(&bin); @@ -114,7 +117,28 @@ pub fn exec( } } build_args.push(target.ide_build_script_name().into()); - map.insert("tauri-binary", tauri_binary.to_string_lossy()); + + let binary_path = PathBuf::from(&binary); + let bin_stem = binary_path.file_stem().unwrap().to_string_lossy(); + let r = regex::Regex::new("(nodejs|node)([1-9]*)*$").unwrap(); + if r.is_match(&bin_stem) { + if let Some(npm_execpath) = var_os("npm_execpath").map(PathBuf::from) { + let manager_stem = npm_execpath.file_stem().unwrap().to_os_string(); + let manager = if manager_stem == "npm-cli" { + "npm".into() + } else { + manager_stem + }; + binary = manager; + if !build_args.is_empty() { + // remove script path, we'll use `npm_lifecycle_event` instead + build_args.remove(0); + } + build_args.insert(0, var("npm_lifecycle_event").unwrap()); + build_args.insert(0, "run".into()); + } + } + map.insert("tauri-binary", binary.to_string_lossy()); map.insert("tauri-binary-args", &build_args); map.insert("tauri-binary-args-str", build_args.join(" "));