Skip to content

Commit

Permalink
fix(cli): generate build script using NPM runner if it was used (#6233)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Feb 10, 2023
1 parent 6f8ba28 commit 62f1526
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-build-script-mobile-runner-npm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Fixes the generated mobile build script when using an NPM runner.
32 changes: 28 additions & 4 deletions tooling/cli/src/mobile/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(" "));

Expand Down

0 comments on commit 62f1526

Please sign in to comment.