소스 검색

feat(cli): prompt for before*Command, closes #4691 (#4721)

* feat(cli): prompt for before*Command, closes #4691

* fix default command

* add allow_empty argument

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Amr Bashir 3 년 전
부모
커밋
6d4945c9f0
3개의 변경된 파일60개의 추가작업 그리고 7개의 파일을 삭제
  1. 7 0
      .changes/cli-init-before-commands.md
  2. 51 5
      tooling/cli/src/init.rs
  3. 2 2
      tooling/cli/templates/tauri.conf.json

+ 7 - 0
.changes/cli-init-before-commands.md

@@ -0,0 +1,7 @@
+---
+"cli.rs": "minor"
+"cli.js": "minor"
+---
+
+Prompt for `beforeDevCommand` and `beforeBuildCommand` in `tauri init`.
+

+ 51 - 5
tooling/cli/src/init.rs

@@ -60,6 +60,12 @@ pub struct Options {
   /// Url of your dev server
   #[clap(short = 'P', long)]
   dev_path: Option<String>,
+  /// A shell command to run before `tauri dev` kicks in.
+  #[clap(long)]
+  before_dev_command: Option<String>,
+  /// A shell command to run before `tauri build` kicks in.
+  #[clap(long)]
+  before_build_command: Option<String>,
 }
 
 #[derive(Default)]
@@ -90,6 +96,7 @@ impl Options {
         "What is your app name?",
         init_defaults.app_name.clone(),
         self.ci,
+        false,
       )
     })?;
 
@@ -98,13 +105,15 @@ impl Options {
         "What should the window title be?",
         init_defaults.app_name.clone(),
         self.ci,
+        false,
       )
     })?;
 
     self.dist_dir = self.dist_dir.map(|s| Ok(Some(s))).unwrap_or_else(|| request_input(
       r#"Where are your web assets (HTML/CSS/JS) located, relative to the "<current dir>/src-tauri/tauri.conf.json" file that will be created?"#,
       init_defaults.framework.as_ref().map(|f| f.dist_dir()),
-      self.ci
+      self.ci,
+      false,
     ))?;
 
     self.dev_path = self.dev_path.map(|s| Ok(Some(s))).unwrap_or_else(|| {
@@ -112,9 +121,33 @@ impl Options {
         "What is the url of your dev server?",
         init_defaults.framework.map(|f| f.dev_path()),
         self.ci,
+        false,
       )
     })?;
 
+    self.before_dev_command = self
+      .before_dev_command
+      .map(|s| Ok(Some(s)))
+      .unwrap_or_else(|| {
+        request_input(
+          "What is your frontend dev command?",
+          Some("npm run dev".to_string()),
+          self.ci,
+          true,
+        )
+      })?;
+    self.before_build_command = self
+      .before_build_command
+      .map(|s| Ok(Some(s)))
+      .unwrap_or_else(|| {
+        request_input(
+          "What is your frontend build command?",
+          Some("npm run build".to_string()),
+          self.ci,
+          true,
+        )
+      })?;
+
     Ok(self)
   }
 }
@@ -178,6 +211,14 @@ pub fn command(mut options: Options) -> Result<()> {
       "window_title",
       to_json(options.window_title.unwrap_or_else(|| "Tauri".to_string())),
     );
+    data.insert(
+      "before_dev_command",
+      to_json(options.before_dev_command.unwrap_or_default()),
+    );
+    data.insert(
+      "before_build_command",
+      to_json(options.before_build_command.unwrap_or_default()),
+    );
 
     let mut config = serde_json::from_str(
       &handlebars
@@ -241,20 +282,25 @@ pub fn command(mut options: Options) -> Result<()> {
   Ok(())
 }
 
-fn request_input<T>(prompt: &str, default: Option<T>, skip: bool) -> Result<Option<T>>
+fn request_input<T>(
+  prompt: &str,
+  initial: Option<T>,
+  skip: bool,
+  allow_empty: bool,
+) -> Result<Option<T>>
 where
   T: Clone + FromStr + Display + ToString,
   T::Err: Display + std::fmt::Debug,
 {
   if skip {
-    Ok(default)
+    Ok(initial)
   } else {
     let theme = dialoguer::theme::ColorfulTheme::default();
     let mut builder = Input::with_theme(&theme);
     builder.with_prompt(prompt);
+    builder.allow_empty(allow_empty);
 
-    if let Some(v) = default {
-      builder.default(v.clone());
+    if let Some(v) = initial {
       builder.with_initial_text(v.to_string());
     }
 

+ 2 - 2
tooling/cli/templates/tauri.conf.json

@@ -6,8 +6,8 @@
   "build": {
     "distDir": "{{ dist_dir }}",
     "devPath": "{{ dev_path }}",
-    "beforeDevCommand": "",
-    "beforeBuildCommand": ""
+    "beforeDevCommand": "{{ before_dev_command }}",
+    "beforeBuildCommand": "{{ before_build_command }}"
   },
   "tauri": {
     "bundle": {