Skip to content

Commit

Permalink
feat(cli): prompt for before*Command, closes #4691 (#4721)
Browse files Browse the repository at this point in the history
* feat(cli): prompt for before*Command, closes #4691

* fix default command

* add allow_empty argument

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog authored Jul 25, 2022
1 parent b2a8930 commit 6d4945c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changes/cli-init-before-commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"cli.rs": "minor"
"cli.js": "minor"
---

Prompt for `beforeDevCommand` and `beforeBuildCommand` in `tauri init`.

56 changes: 51 additions & 5 deletions tooling/cli/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -90,6 +96,7 @@ impl Options {
"What is your app name?",
init_defaults.app_name.clone(),
self.ci,
false,
)
})?;

Expand All @@ -98,23 +105,49 @@ 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(|| {
request_input(
"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)
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}

Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/templates/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"build": {
"distDir": "{{ dist_dir }}",
"devPath": "{{ dev_path }}",
"beforeDevCommand": "",
"beforeBuildCommand": ""
"beforeDevCommand": "{{ before_dev_command }}",
"beforeBuildCommand": "{{ before_build_command }}"
},
"tauri": {
"bundle": {
Expand Down

0 comments on commit 6d4945c

Please sign in to comment.