瀏覽代碼

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

Lucas Fernandes Nogueira 2 年之前
父節點
當前提交
62f1526592
共有 2 個文件被更改,包括 34 次插入4 次删除
  1. 6 0
      .changes/fix-build-script-mobile-runner-npm.md
  2. 28 4
      tooling/cli/src/mobile/init.rs

+ 6 - 0
.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.

+ 28 - 4
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(" "));