Skip to content

Commit

Permalink
feat(cli/init): allow empty responses non-crucial questions (#9183)
Browse files Browse the repository at this point in the history
closes #9181
  • Loading branch information
amrbashir committed Mar 19, 2024
1 parent 81b853b commit ac76a22
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-empty-responses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'tauri-cli': 'patch:enhance'
'@tauri-apps/cli': 'patch:enhance'
---

Allow empty responses for `devUrl`, `beforeDevCommand` and `beforeBuildCommands` questions in `tauri init`.
6 changes: 5 additions & 1 deletion tooling/cli/src/helpers/prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn input<T>(
where
T: Clone + FromStr + Display + ToString,
T::Err: Display + std::fmt::Debug,
T: PartialEq<str>,
{
if skip {
Ok(initial)
Expand All @@ -28,7 +29,10 @@ where
builder = builder.with_initial_text(v.to_string());
}

builder.interact_text().map(Some).map_err(Into::into)
builder
.interact_text()
.map(|t: T| if t.ne("") { Some(t) } else { None })
.map_err(Into::into)
}
}

Expand Down
47 changes: 22 additions & 25 deletions tooling/cli/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,28 @@ impl Options {
self.app_name = self.app_name.map(|s| Ok(Some(s))).unwrap_or_else(|| {
prompts::input(
"What is your app name?",
init_defaults.app_name.clone(),
Some(
init_defaults
.app_name
.clone()
.unwrap_or_else(|| "Tauri App".to_string()),
),
self.ci,
false,
true,
)
})?;

self.window_title = self.window_title.map(|s| Ok(Some(s))).unwrap_or_else(|| {
prompts::input(
"What should the window title be?",
init_defaults.app_name.clone(),
Some(
init_defaults
.app_name
.clone()
.unwrap_or_else(|| "Tauri".to_string()),
),
self.ci,
false,
true,
)
})?;

Expand All @@ -116,7 +126,7 @@ impl Options {
"What is the url of your dev server?",
init_defaults.framework.map(|f| f.dev_url()),
self.ci,
false,
true,
)
})?;

Expand All @@ -131,6 +141,7 @@ impl Options {
true,
)
})?;

self.before_build_command = self
.before_build_command
.map(|s| Ok(Some(s)))
Expand Down Expand Up @@ -186,35 +197,21 @@ pub fn command(mut options: Options) -> Result<()> {
data.insert("tauri_build_dep", to_json(tauri_build_dep));
data.insert(
"frontend_dist",
to_json(
options
.frontend_dist
.unwrap_or_else(|| "../dist".to_string()),
),
);
data.insert(
"dev_url",
to_json(
options
.dev_url
.unwrap_or_else(|| "http://localhost:4000".to_string()),
),
to_json(options.frontend_dist.as_deref().unwrap_or("../dist")),
);
data.insert("dev_url", to_json(options.dev_url));
data.insert(
"app_name",
to_json(options.app_name.unwrap_or_else(|| "Tauri App".to_string())),
to_json(options.app_name.as_deref().unwrap_or("Tauri App")),
);
data.insert(
"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()),
to_json(options.window_title.as_deref().unwrap_or("Tauri")),
);
data.insert("before_dev_command", to_json(options.before_dev_command));
data.insert(
"before_build_command",
to_json(options.before_build_command.unwrap_or_default()),
to_json(options.before_build_command),
);

let mut config = serde_json::from_str(
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/plugin/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT

use crate::{
helpers::{prompts::input, template},
helpers::{prompts, template},
Result,
};
use clap::{Parser, Subcommand};
Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn command(cli: Cli) -> Result<()> {
return Err(anyhow::anyhow!("android folder already exists"));
}

let plugin_id = input(
let plugin_id = prompts::input(
"What should be the Android Package ID for your plugin?",
Some(format!("com.plugin.{}", plugin_name)),
false,
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/plugin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use crate::helpers::prompts::input;
use crate::helpers::prompts;
use crate::Result;
use crate::{
helpers::{resolve_tauri_path, template},
Expand Down Expand Up @@ -141,7 +141,7 @@ pub fn command(mut options: Options) -> Result<()> {
}

let plugin_id = if options.android || options.mobile {
let plugin_id = input(
let plugin_id = prompts::input(
"What should be the Android Package ID for your plugin?",
Some(format!("com.plugin.{}", plugin_name)),
false,
Expand Down
8 changes: 4 additions & 4 deletions tooling/cli/templates/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"version": "0.1.0",
"identifier": "com.tauri.dev",
"build": {
"frontendDist": "{{ frontend_dist }}",
"devUrl": "{{ dev_url }}",
"beforeDevCommand": "{{ before_dev_command }}",
"beforeBuildCommand": "{{ before_build_command }}"
"frontendDist": "{{ frontend_dist }}"{{#if dev_url}},
"devUrl": "{{ dev_url }}"{{/if}}{{#if before_dev_command}},
"beforeDevCommand": "{{ before_dev_command }}"{{/if}}{{#if before_build_command}},
"beforeBuildCommand": "{{ before_build_command }}"{{/if}}
},
"app": {
"windows": [
Expand Down

0 comments on commit ac76a22

Please sign in to comment.