Skip to content

Commit f7256ee

Browse files
authored
feat: improve error messages (#471)
ref #467
1 parent 9c3686c commit f7256ee

4 files changed

Lines changed: 63 additions & 7 deletions

File tree

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+
Improve the error messages for unsupported package manager, unsupported template or when a supported template is used with a package manager that is not intended to be used with.
7+

packages/cli/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ where
259259
// is valid, otherwise, we error and exit
260260
if !pkg_manager.templates().contains(&template) {
261261
eprintln!(
262-
"{BOLD}{RED}error{RESET}: the {GREEN}{}{RESET} template is not suppported for the {GREEN}{pkg_manager}{RESET} package manager\n possible templates for {GREEN}{pkg_manager}{RESET} are: [{}]",
263-
template,
264-
templates_no_flavors.iter().map(|e|format!("{GREEN}{e}{RESET}")).collect::<Vec<_>>().join(", ")
262+
"{BOLD}{RED}error{RESET}: the {GREEN}{template}{RESET} template is not suppported for the {GREEN}{pkg_manager}{RESET} package manager\n possible templates for {GREEN}{pkg_manager}{RESET} are: [{}]\n or maybe you meant to use another package manager\n possible package managers for {GREEN}{template}{RESET} are: [{}]" ,
263+
templates_no_flavors.iter().map(|e|format!("{GREEN}{e}{RESET}")).collect::<Vec<_>>().join(", "),
264+
template.possible_package_managers().iter().map(|e|format!("{GREEN}{e}{RESET}")).collect::<Vec<_>>().join(", "),
265265
);
266266
exit(1);
267267
}

packages/cli/src/package_manager.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use std::{fmt::Display, str::FromStr};
66

7-
use crate::template::Template;
7+
use crate::{colors::*, template::Template};
88

99
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1010
#[non_exhaustive]
@@ -28,6 +28,13 @@ impl<'a> PackageManager {
2828
PackageManager::Yarn,
2929
PackageManager::Npm,
3030
];
31+
32+
/// Node.js managers
33+
pub const NODE: &'a [PackageManager] = &[
34+
PackageManager::Pnpm,
35+
PackageManager::Yarn,
36+
PackageManager::Npm,
37+
];
3138
}
3239
impl PackageManager {
3340
/// Returns templates without flavors
@@ -122,7 +129,14 @@ impl FromStr for PackageManager {
122129
"pnpm" => Ok(PackageManager::Pnpm),
123130
"yarn" => Ok(PackageManager::Yarn),
124131
"npm" => Ok(PackageManager::Npm),
125-
_ => Err("Invalid package manager".to_string()),
132+
_ => Err(format!(
133+
"{YELLOW}{s}{RESET} is not a valid package manager. Valid package mangers are [{}]",
134+
PackageManager::ALL
135+
.iter()
136+
.map(|e| format!("{GREEN}{e}{RESET}"))
137+
.collect::<Vec<_>>()
138+
.join(", ")
139+
)),
126140
}
127141
}
128142
}

packages/cli/src/template.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{fmt::Display, fs, io::Write, path, str::FromStr};
77
use anyhow::Context;
88
use rust_embed::RustEmbed;
99

10-
use crate::{manifest::Manifest, package_manager::PackageManager};
10+
use crate::{colors::*, manifest::Manifest, package_manager::PackageManager};
1111

1212
#[derive(RustEmbed)]
1313
#[folder = "fragments"]
@@ -102,7 +102,14 @@ impl FromStr for Template {
102102
"angular" => Ok(Template::Angular),
103103
"preact" => Ok(Template::Preact),
104104
"preact-ts" => Ok(Template::PreactTs),
105-
_ => Err("Invalid template".to_string()),
105+
_ => Err(format!(
106+
"{YELLOW}{s}{RESET} is not a valid template. Valid templates are [{}]",
107+
Template::ALL
108+
.iter()
109+
.map(|e| format!("{GREEN}{e}{RESET}"))
110+
.collect::<Vec<_>>()
111+
.join(", ")
112+
)),
106113
}
107114
}
108115
}
@@ -169,15 +176,43 @@ impl<'a> Template {
169176
}
170177
}
171178

179+
pub const fn possible_package_managers(&self) -> &[PackageManager] {
180+
match self {
181+
Template::Vanilla => &[
182+
PackageManager::Cargo,
183+
PackageManager::Pnpm,
184+
PackageManager::Yarn,
185+
PackageManager::Npm,
186+
],
187+
Template::VanillaTs => PackageManager::NODE,
188+
Template::Vue => PackageManager::NODE,
189+
Template::VueTs => PackageManager::NODE,
190+
Template::Svelte => PackageManager::NODE,
191+
Template::SvelteTs => PackageManager::NODE,
192+
Template::React => PackageManager::NODE,
193+
Template::ReactTs => PackageManager::NODE,
194+
Template::Solid => PackageManager::NODE,
195+
Template::SolidTs => PackageManager::NODE,
196+
Template::Yew => &[PackageManager::Cargo],
197+
Template::Leptos => &[PackageManager::Cargo],
198+
Template::Sycamore => &[PackageManager::Cargo],
199+
Template::Angular => PackageManager::NODE,
200+
Template::Preact => PackageManager::NODE,
201+
Template::PreactTs => PackageManager::NODE,
202+
}
203+
}
204+
172205
pub const fn needs_trunk(&self) -> bool {
173206
matches!(self, Template::Sycamore | Template::Yew | Template::Leptos)
174207
}
208+
175209
pub const fn needs_tauri_cli(&self) -> bool {
176210
matches!(
177211
self,
178212
Template::Sycamore | Template::Yew | Template::Leptos | Template::Vanilla
179213
)
180214
}
215+
181216
pub const fn needs_wasm32_target(&self) -> bool {
182217
matches!(self, Template::Sycamore | Template::Yew | Template::Leptos)
183218
}

0 commit comments

Comments
 (0)