Skip to content

Commit 62f1526

Browse files
authored
fix(cli): generate build script using NPM runner if it was used (#6233)
1 parent 6f8ba28 commit 62f1526

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Fixes the generated mobile build script when using an NPM runner.

tooling/cli/src/mobile/init.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ use tauri_mobile::{
1919
},
2020
};
2121

22-
use std::{env::current_dir, path::PathBuf};
22+
use std::{
23+
env::{current_dir, var, var_os},
24+
path::PathBuf,
25+
};
2326

2427
pub fn command(target: Target, ci: bool, reinstall_deps: bool) -> Result<()> {
2528
let wrapper = TextWrapper::with_splitter(textwrap::termwidth(), textwrap::NoHyphenation);
2629
exec(
2730
target,
2831
&wrapper,
29-
ci || std::env::var("CI").is_ok(),
32+
ci || var_os("CI").is_some(),
3033
reinstall_deps,
3134
)
3235
.map_err(|e| anyhow::anyhow!("{:#}", e))?;
@@ -85,7 +88,7 @@ pub fn exec(
8588
let (handlebars, mut map) = handlebars(&app);
8689

8790
let mut args = std::env::args_os();
88-
let tauri_binary = args
91+
let mut binary = args
8992
.next()
9093
.map(|bin| {
9194
let path = PathBuf::from(&bin);
@@ -114,7 +117,28 @@ pub fn exec(
114117
}
115118
}
116119
build_args.push(target.ide_build_script_name().into());
117-
map.insert("tauri-binary", tauri_binary.to_string_lossy());
120+
121+
let binary_path = PathBuf::from(&binary);
122+
let bin_stem = binary_path.file_stem().unwrap().to_string_lossy();
123+
let r = regex::Regex::new("(nodejs|node)([1-9]*)*$").unwrap();
124+
if r.is_match(&bin_stem) {
125+
if let Some(npm_execpath) = var_os("npm_execpath").map(PathBuf::from) {
126+
let manager_stem = npm_execpath.file_stem().unwrap().to_os_string();
127+
let manager = if manager_stem == "npm-cli" {
128+
"npm".into()
129+
} else {
130+
manager_stem
131+
};
132+
binary = manager;
133+
if !build_args.is_empty() {
134+
// remove script path, we'll use `npm_lifecycle_event` instead
135+
build_args.remove(0);
136+
}
137+
build_args.insert(0, var("npm_lifecycle_event").unwrap());
138+
build_args.insert(0, "run".into());
139+
}
140+
}
141+
map.insert("tauri-binary", binary.to_string_lossy());
118142
map.insert("tauri-binary-args", &build_args);
119143
map.insert("tauri-binary-args-str", build_args.join(" "));
120144

0 commit comments

Comments
 (0)