diff --git a/.changes/cli-init-before-commands.md b/.changes/cli-init-before-commands.md new file mode 100644 index 000000000..cca090bd2 --- /dev/null +++ b/.changes/cli-init-before-commands.md @@ -0,0 +1,7 @@ +--- +"cli.rs": "minor" +"cli.js": "minor" +--- + +Prompt for `beforeDevCommand` and `beforeBuildCommand` in `tauri init`. + diff --git a/tooling/cli/src/init.rs b/tooling/cli/src/init.rs index 11796de6b..cfe67d475 100644 --- a/tooling/cli/src/init.rs +++ b/tooling/cli/src/init.rs @@ -60,6 +60,12 @@ pub struct Options { /// Url of your dev server #[clap(short = 'P', long)] dev_path: Option, + /// A shell command to run before `tauri dev` kicks in. + #[clap(long)] + before_dev_command: Option, + /// A shell command to run before `tauri build` kicks in. + #[clap(long)] + before_build_command: Option, } #[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 "/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(prompt: &str, default: Option, skip: bool) -> Result> +fn request_input( + prompt: &str, + initial: Option, + skip: bool, + allow_empty: bool, +) -> Result> 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()); } diff --git a/tooling/cli/templates/tauri.conf.json b/tooling/cli/templates/tauri.conf.json index 79829f625..403fa5e73 100644 --- a/tooling/cli/templates/tauri.conf.json +++ b/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": {