-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
frontend.rs
76 lines (65 loc) · 2.39 KB
/
frontend.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use crate::{helpers::app_paths::walk_builder, Result};
use anyhow::Context;
use std::{fs, path::Path};
// (from, to)
const RENAMED_MODULES: &[(&str, &str)] = &[("tauri", "core"), ("window", "webviewWindow")];
const PLUGINIFIED_MODULES: &[&str] = &[
"cli",
"clipboard",
"dialog",
"fs",
"globalShortcut",
"http",
"notification",
"os",
"process",
"shell",
"updater",
];
const JS_EXTENSIONS: &[&str] = &["js", "mjs", "jsx", "ts", "mts", "tsx"];
/// Returns a list of paths that could not be migrated
pub fn migrate(app_dir: &Path) -> Result<Vec<String>> {
let mut new_plugins = Vec::new();
let tauri_api_import_regex = regex::bytes::Regex::new(r"@tauri-apps/api/(\w+)").unwrap();
for entry in walk_builder(app_dir).build().flatten() {
if entry.file_type().map(|t| t.is_file()).unwrap_or_default() {
let path = entry.path();
let ext = path.extension().unwrap_or_default();
if JS_EXTENSIONS.iter().any(|e| e == &ext) {
let js_contents = fs::read(path)?;
let new_contents =
tauri_api_import_regex.replace_all(&js_contents, |cap: ®ex::bytes::Captures<'_>| {
let module = cap.get(1).unwrap().as_bytes();
let module = String::from_utf8_lossy(module).to_string();
let original = cap.get(0).unwrap().as_bytes();
let original = String::from_utf8_lossy(original).to_string();
let new = if let Some((_, renamed_to)) =
RENAMED_MODULES.iter().find(|(from, _to)| *from == module)
{
format!("@tauri-apps/api/{renamed_to}")
} else if PLUGINIFIED_MODULES.contains(&module.as_str()) {
let plugin = format!("@tauri-apps/plugin-{module}");
new_plugins.push(if module == "clipboard" {
"clipboard-manager".to_string()
} else {
module
});
plugin
} else {
return original;
};
log::info!("Replacing `{original}` with `{new}` on {}", path.display());
new
});
if new_contents != js_contents {
fs::write(path, new_contents)
.with_context(|| format!("Error writing {}", path.display()))?;
}
}
}
}
Ok(new_plugins)
}