fix(cli): tauri add NPM packages for community plugins (#12246)

It currently isn't possible to simply add a community plugin the same was as adding official plugins.
Trying to perform  `npm run tauri add tauri-plugin-python` is trying to install npm package `@tauri-apps/plugin-python`.
But the npm scope `@tauri-apps/` is reserved for official tauri plugins.

The official documentation recommends to name the npm package `tauri-plugin-{name}-api` and it should be possible to have a parameter that makes it possible to install that package.

- closes #12217

This changes the command to check if the plugin is an official tauri plugin or not, using the appropriate npm package name format

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Marco Mengelkoch
2025-01-07 14:16:35 +01:00
committed by GitHub
parent c130af6b06
commit 98f62e65a2
3 changed files with 23 additions and 3 deletions

View File

@@ -0,0 +1,6 @@
---
"tauri-cli": patch:bug
"@tauri-apps/cli": patch:bug
---
Properly add NPM packages for community plugins when using the `tauri add` command.

View File

@@ -49,12 +49,25 @@ pub fn run(options: Options) -> Result<()> {
.map(|(p, v)| (p, Some(v)))
.unwrap_or((&options.plugin, None));
let mut plugins = crate::helpers::plugins::known_plugins();
let (metadata, is_known) = plugins
.remove(plugin)
.map(|metadata| (metadata, true))
.unwrap_or_default();
let plugin_snake_case = plugin.replace('-', "_");
let crate_name = format!("tauri-plugin-{plugin}");
let npm_name = format!("@tauri-apps/plugin-{plugin}");
let npm_name = if is_known {
format!("tauri-apps/plugin-{plugin}")
} else {
format!("tauri-plugin-{plugin}-api")
};
let mut plugins = crate::helpers::plugins::known_plugins();
let metadata = plugins.remove(plugin).unwrap_or_default();
if !is_known && (options.tag.is_some() || options.rev.is_some() || options.branch.is_some()) {
anyhow::bail!(
"Git options --tag, --rev and --branch can only be used with official Tauri plugins"
);
}
let frontend_dir = resolve_frontend_dir();
let tauri_dir = tauri_dir();

View File

@@ -69,6 +69,7 @@ pub fn known_plugins() -> HashMap<&'static str, PluginMetadata> {
"shell",
"upload",
"websocket",
"opener",
] {
plugins.entry(p).or_default();
}