-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathupdate-plugin-version.ts
More file actions
101 lines (84 loc) · 2.89 KB
/
Copy pathupdate-plugin-version.ts
File metadata and controls
101 lines (84 loc) · 2.89 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
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
interface PluginConfig {
version: string;
[key: string]: any;
}
/**
* Updates both plugin.json files with the specified version
* @param version - The new version to set
* @param pluginFiles - Array of paths to plugin.json files (defaults to standard locations)
* @returns true if there was an error, false otherwise
*/
export function updatePluginVersion(
version: string,
pluginFiles: string[] = [
"../../plugin/.claude-plugin/plugin.json",
"../../plugin/.cursor-plugin/plugin.json",
"../../plugin/.plugin/plugin.json"
]
): boolean {
console.log(`Updating plugin files to version: ${version}`);
let hadError = false;
for (const filePath of pluginFiles) {
let fd: number | undefined;
try {
const fullPath = path.resolve(__dirname, filePath);
// Open file for reading and writing
fd = fs.openSync(fullPath, "r+");
// Read current content
const content: string = fs.readFileSync(fd, "utf8");
const pluginConfig: PluginConfig = JSON.parse(content);
// Update version
pluginConfig.version = version;
// Prepare new content
const newContent: string = JSON.stringify(pluginConfig, null, 2) + "\n";
// Truncate and write back with formatted JSON
fs.ftruncateSync(fd, 0);
// Seek back to the beginning of the file after truncation
fs.writeSync(fd, newContent, 0);
console.log(`✓ Updated ${filePath} to version ${version}`);
} catch (error: unknown) {
const err = error as NodeJS.ErrnoException;
if (err.code === "ENOENT") {
console.error(`File not found: ${filePath}`);
} else {
console.error(`Failed to update ${filePath}:`, err.message);
}
hadError = true;
break; // Stop processing further files if there's an error
} finally {
// Always close the file descriptor if it was opened
if (fd !== undefined) {
try {
fs.closeSync(fd);
} catch (closeError: unknown) {
const err = closeError as NodeJS.ErrnoException;
console.error(`Failed to close file ${filePath}:`, err.message);
}
}
}
}
return hadError;
}
/**
* Main function for CLI execution
*/
function main(): void {
const version: string | undefined = process.argv[2];
if (!version) {
console.error("Usage: npx tsx update-plugin-version.ts <version>");
process.exit(1);
}
const hadError = updatePluginVersion(version);
if (hadError) {
process.exit(1);
}
}
// Only run main if this file is executed directly (not imported)
// Convert import.meta.url to file path and compare with resolved argv[1]
if (fileURLToPath(import.meta.url) === path.resolve(process.argv[1])) {
main();
}