fix(cli): generate build script using NPM runner if it was used (#6233)

This commit is contained in:
Lucas Fernandes Nogueira 2023-02-10 03:31:13 -08:00 committed by GitHub
parent 6f8ba2850e
commit 62f1526592
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 4 deletions

View File

@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---
Fixes the generated mobile build script when using an NPM runner.

View File

@ -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(" "));