Skip to content

Commit af40cfc

Browse files
committed
feat(cli): add -f/--force flag
1 parent d1978aa commit af40cfc

3 files changed

Lines changed: 27 additions & 16 deletions

File tree

.changes/force-flag.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"create-tauri-app": "patch"
3+
"create-tauri-app-js": "patch"
4+
---
5+
6+
Add `-f/--force` flag to force creating directory even if it is empty.
7+

packages/cli/src/cli.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Args {
1414
pub manager: Option<PackageManager>,
1515
pub template: Option<Template>,
1616
pub skip: bool,
17+
pub force: bool,
1718
pub alpha: bool,
1819
pub mobile: Option<bool>,
1920
}
@@ -25,6 +26,7 @@ impl Default for Args {
2526
manager: Some(PackageManager::Npm),
2627
template: Some(Template::Vanilla),
2728
skip: false,
29+
force: false,
2830
alpha: false,
2931
mobile: Some(false),
3032
}
@@ -51,6 +53,7 @@ pub fn parse(argv: Vec<OsString>, bin_name: Option<String>) -> anyhow::Result<Ar
5153
{GREEN}-m{RESET}, {GREEN}--manager <MANAGER>{RESET} Specify preferred package manager [{managers}]
5254
{GREEN}-t{RESET}, {GREEN}--template <TEMPLATE>{RESET} Specify the UI template to use [{fragments}]
5355
{GREEN}-y{RESET}, {GREEN}--yes{RESET} Skip prompts and use defaults where applicable
56+
{GREEN}-f{RESET}, {GREEN}--force{RESET} Force create the directory even if it is not empty.
5457
{GREEN}--alpha{RESET} Bootstraps a project using tauri@2.0-alpha
5558
{GREEN}--mobile{RESET} Bootstraps a mobile project too. Only availabe with `--alpha` option.
5659
{GREEN}-h{RESET}, {GREEN}--help{RESET} Prints help information
@@ -84,6 +87,7 @@ pub fn parse(argv: Vec<OsString>, bin_name: Option<String>) -> anyhow::Result<Ar
8487
manager: pargs.opt_value_from_str(["-m", "--manager"])?,
8588
template: pargs.opt_value_from_str(["-t", "--template"])?,
8689
skip: pargs.contains(["-y", "--yes"]),
90+
force: pargs.contains(["-f", "--force"]),
8791
alpha: pargs.contains("--alpha"),
8892
mobile: if pargs.contains("--mobile") {
8993
Some(true)

packages/cli/src/lib.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ where
7171
manager,
7272
project_name,
7373
template,
74+
force,
7475
} = args;
7576
let cwd = std::env::current_dir()?;
7677

@@ -133,7 +134,9 @@ where
133134

134135
// Confirm deleting the target project directory if not empty
135136
if target_dir.exists() && target_dir.read_dir()?.next().is_some() {
136-
let overrwite = if skip {
137+
let overwrite = if force {
138+
true
139+
} else if skip {
137140
false
138141
} else {
139142
Confirm::with_theme(&ColorfulTheme::default())
@@ -152,18 +155,17 @@ where
152155
.default(false)
153156
.interact()?
154157
};
155-
if !overrwite {
156-
eprintln!("{BOLD}{RED}✘{RESET} Operation Cancelled");
158+
if !overwrite {
159+
eprintln!("{BOLD}{RED}✘{RESET} Directory is not empty, Operation Cancelled");
157160
exit(1);
158161
}
159162
};
160163

161-
// Prompt for category if a package manger is not passed on the command line
162-
let category = if manager.is_none() && !skip {
164+
// Detect category if a package manger is not passed on the command line
165+
let category = if manager.is_none() {
163166
// Filter managers if a template is passed on the command line
164167
let managers = PackageManager::ALL.to_vec();
165-
let managers = args
166-
.template
168+
let managers = template
167169
.map(|t| {
168170
managers
169171
.iter()
@@ -193,8 +195,8 @@ where
193195
)
194196
});
195197

196-
// If only one category is detected, skip prompt
197-
if categories.len() == 1 {
198+
// Skip prompt, if only one category is detected or explicit skip requested by `-y/--yes` flag
199+
if categories.len() == 1 || skip {
198200
Some(categories[0])
199201
} else {
200202
let index = Select::with_theme(&ColorfulTheme::default())
@@ -213,11 +215,7 @@ where
213215
let pkg_manager = match manager {
214216
Some(manager) => manager,
215217
None => {
216-
if skip {
217-
defaults.manager.context("default manager not set")?
218-
} else {
219-
let category = category.context("category shouldn't be None at this point")?;
220-
218+
if let Some(category) = category {
221219
let mut managers = category.package_managers().to_owned();
222220
// sort managers so the auto-detected package manager is selected first
223221
managers.sort_by(|a, b| {
@@ -226,8 +224,8 @@ where
226224
.unwrap_or(false)
227225
.cmp(&detected_manager.map(|p| p == *a).unwrap_or(false))
228226
});
229-
// If only one package manager is detected, skip prompt
230-
if managers.len() == 1 {
227+
// Skip prompt, if only one package manager is detected or explicit skip requested by `-y/--yes` flag
228+
if managers.len() == 1 || skip {
231229
managers[0]
232230
} else {
233231
let index = Select::with_theme(&ColorfulTheme::default())
@@ -237,6 +235,8 @@ where
237235
.interact()?;
238236
managers[index]
239237
}
238+
} else {
239+
defaults.manager.context("default manager not set")?
240240
}
241241
}
242242
};

0 commit comments

Comments
 (0)