-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathcheck-tauri-plugin-versions.js
More file actions
executable file
·337 lines (287 loc) · 8.85 KB
/
check-tauri-plugin-versions.js
File metadata and controls
executable file
·337 lines (287 loc) · 8.85 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Check for debug flag
const DEBUG = process.argv.includes("--debug") || process.env.DEBUG === "true";
function debug(message) {
if (DEBUG) {
console.log(`[DEBUG] ${message}`);
}
}
// Helper function to parse semantic version
function parseVersion(version) {
const match = version.match(/^(\d+)\.(\d+)\.(\d+)/);
if (!match) {
throw new Error(`Invalid version format: ${version}`);
}
return {
major: parseInt(match[1], 10),
minor: parseInt(match[2], 10),
patch: parseInt(match[3], 10),
full: version,
};
}
// Parse pnpm-lock.yaml to extract Tauri plugin versions
function parsePnpmLock(lockfilePath) {
const content = fs.readFileSync(lockfilePath, "utf8");
const plugins = {};
const lines = content.split("\n");
let currentPlugin = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Look for @tauri-apps/plugin- packages (with or without quotes)
const pluginMatch = line.match(
/^\s*['"]?(@tauri-apps\/plugin-[^'":\s]+)['"]?:\s*$/,
);
if (pluginMatch) {
currentPlugin = pluginMatch[1];
debug(`Found JS plugin: ${currentPlugin} at line ${i + 1}`);
continue;
}
// Look for version when we're tracking a plugin
if (currentPlugin) {
const versionMatch = line.match(/^\s*version:\s*['"]?([^'"]+)['"]?\s*$/);
if (versionMatch) {
plugins[currentPlugin] = versionMatch[1];
debug(` Version: ${versionMatch[1]}`);
currentPlugin = null;
}
// Reset if we hit another top-level key
if (
line.match(/^\s*[^'":\s]+:\s*$/) &&
!line.match(/^\s*(specifier|version):/)
) {
currentPlugin = null;
}
}
// Also check for inline version specifications in dependency lists
const inlineMatch = line.match(/@tauri-apps\/plugin-([^@]+)@([^)]+)/);
if (inlineMatch) {
const pluginName = inlineMatch[1];
const version = inlineMatch[2];
plugins[`@tauri-apps/plugin-${pluginName}`] = version;
debug(
`Found inline JS plugin: @tauri-apps/plugin-${pluginName}@${version}`,
);
}
}
return plugins;
}
// Parse Cargo.lock to extract Tauri plugin versions
function parseCargoLock(lockfilePath) {
const content = fs.readFileSync(lockfilePath, "utf8");
const plugins = {};
const lines = content.split("\n");
let currentPackage = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Look for package declarations
const packageMatch = line.match(/^name = "tauri-plugin-(.+)"$/);
if (packageMatch) {
currentPackage = `tauri-plugin-${packageMatch[1]}`;
debug(`Found Rust plugin: ${currentPackage} at line ${i + 1}`);
continue;
}
// Look for version when we're in a tauri-plugin package
if (currentPackage && line.match(/^version = "(.+)"$/)) {
const versionMatch = line.match(/^version = "(.+)"$/);
if (versionMatch) {
plugins[currentPackage] = versionMatch[1];
debug(` Version: ${versionMatch[1]}`);
currentPackage = null;
}
}
// Reset when we hit a new [[package]] block
if (line.match(/^\[\[package\]\]$/)) {
currentPackage = null;
}
}
return plugins;
}
// Map plugin names between JS and Rust
function mapPluginNames(jsName, rustPlugins) {
// Convert @tauri-apps/plugin-clipboard-manager to tauri-plugin-clipboard-manager
const rustName = jsName.replace("@tauri-apps/plugin-", "tauri-plugin-");
return rustPlugins[rustName] ? rustName : null;
}
// Compare versions and report mismatches
function compareVersions(jsPlugins, rustPlugins) {
const results = {
matching: [],
mismatched: [],
jsOnly: [],
rustOnly: [],
};
// Check JS plugins against Rust plugins
for (const [jsName, jsVersion] of Object.entries(jsPlugins)) {
const rustName = mapPluginNames(jsName, rustPlugins);
if (!rustName) {
results.jsOnly.push({ name: jsName, version: jsVersion });
continue;
}
const rustVersion = rustPlugins[rustName];
try {
const jsParsed = parseVersion(jsVersion);
const rustParsed = parseVersion(rustVersion);
if (
jsParsed.major === rustParsed.major &&
jsParsed.minor === rustParsed.minor
) {
results.matching.push({
jsName,
rustName,
jsVersion,
rustVersion,
majorMinor: `${jsParsed.major}.${jsParsed.minor}`,
});
} else {
results.mismatched.push({
jsName,
rustName,
jsVersion,
rustVersion,
jsMajorMinor: `${jsParsed.major}.${jsParsed.minor}`,
rustMajorMinor: `${rustParsed.major}.${rustParsed.minor}`,
});
}
} catch (error) {
console.warn(
`Warning: Could not parse versions for ${jsName} (${jsVersion}) / ${rustName} (${rustVersion}): ${error.message}`,
);
}
}
// Check for Rust-only plugins
for (const [rustName, rustVersion] of Object.entries(rustPlugins)) {
const jsName = `@tauri-apps/plugin-${rustName.replace("tauri-plugin-", "")}`;
if (!jsPlugins[jsName]) {
results.rustOnly.push({ name: rustName, version: rustVersion });
}
}
return results;
}
// Main function
function main() {
const rootDir = path.resolve(__dirname, "..");
const pnpmLockPath = path.join(rootDir, "pnpm-lock.yaml");
const cargoLockPath = path.join(rootDir, "Cargo.lock");
// Check if files exist
if (!fs.existsSync(pnpmLockPath)) {
console.error(`❌ Error: pnpm-lock.yaml not found at ${pnpmLockPath}`);
console.error("Please run this script from the project root directory.");
process.exit(1);
}
if (!fs.existsSync(cargoLockPath)) {
console.error(`❌ Error: Cargo.lock not found at ${cargoLockPath}`);
console.error("Please run this script from the project root directory.");
process.exit(1);
}
debug(`Reading pnpm lockfile: ${pnpmLockPath}`);
debug(`Reading Cargo lockfile: ${cargoLockPath}`);
console.log("🔍 Checking Tauri plugin version consistency...\n");
try {
// Parse both lockfiles
const jsPlugins = parsePnpmLock(pnpmLockPath);
const rustPlugins = parseCargoLock(cargoLockPath);
console.log(`Found ${Object.keys(jsPlugins).length} JS Tauri plugins`);
console.log(
`Found ${Object.keys(rustPlugins).length} Rust Tauri plugins\n`,
);
if (DEBUG) {
console.log("JS Plugins found:");
Object.entries(jsPlugins).forEach(([name, version]) => {
console.log(` ${name}@${version}`);
});
console.log("\nRust Plugins found:");
Object.entries(rustPlugins).forEach(([name, version]) => {
console.log(` ${name}@${version}`);
});
console.log();
}
// Compare versions
const results = compareVersions(jsPlugins, rustPlugins);
// Report results
if (results.matching.length > 0) {
console.log("✅ Matching versions (major.minor):");
results.matching.forEach(
({ jsName, rustName, jsVersion, rustVersion, majorMinor }) => {
console.log(
` ${majorMinor}: ${jsName}@${jsVersion} ↔ ${rustName}@${rustVersion}`,
);
},
);
console.log();
}
let hasErrors = false;
if (results.mismatched.length > 0) {
hasErrors = true;
console.log("❌ Version mismatches (major.minor):");
results.mismatched.forEach(
({
jsName,
rustName,
jsVersion,
rustVersion,
jsMajorMinor,
rustMajorMinor,
}) => {
console.log(
` ${jsName}@${jsVersion} (${jsMajorMinor}) ↔ ${rustName}@${rustVersion} (${rustMajorMinor})`,
);
},
);
console.log();
}
if (results.jsOnly.length > 0) {
console.log("⚠️ JS-only plugins (no Rust equivalent found):");
results.jsOnly.forEach(({ name, version }) => {
console.log(` ${name}@${version}`);
});
console.log();
}
if (results.rustOnly.length > 0) {
console.log("⚠️ Rust-only plugins (no JS equivalent found):");
results.rustOnly.forEach(({ name, version }) => {
console.log(` ${name}@${version}`);
});
console.log();
}
// Summary
if (hasErrors) {
console.log("💥 Version consistency check failed!");
console.log(
"Please ensure that Tauri plugins have matching major.minor versions between package.json and Cargo.toml",
);
process.exit(1);
} else {
console.log("🎉 All Tauri plugin versions are consistent!");
}
} catch (error) {
console.error(`❌ Error: ${error.message}`);
if (DEBUG) {
console.error("Stack trace:", error.stack);
}
process.exit(1);
}
}
// Show usage if help flag is passed
if (process.argv.includes("--help") || process.argv.includes("-h")) {
console.log(`
Usage: node check-tauri-plugin-versions.js [options]
Options:
--debug Enable debug logging
--help Show this help message
Description:
Checks that all @tauri-apps/plugin-* packages have matching major.minor
versions with their corresponding tauri-plugin-* crates.
The script reads pnpm-lock.yaml and Cargo.lock from the project root
and compares versions to ensure compatibility.
Exit codes:
0 - All versions match
1 - Version mismatches found or error occurred
`);
process.exit(0);
}
main();