forked from foxglove/extension-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-changed-extensions.ts
More file actions
82 lines (70 loc) · 2.19 KB
/
get-changed-extensions.ts
File metadata and controls
82 lines (70 loc) · 2.19 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
import { execFileSync } from "node:child_process";
import fs from "node:fs";
interface Extension {
id: string;
[key: string]: unknown;
}
/**
* Get the extensions.json content from the base branch (origin/main or specified ref).
* Returns an empty array if the file doesn't exist in the base branch.
*/
function getBaseExtensions(baseRef: string): Extension[] {
try {
const content = execFileSync("git", ["show", `${baseRef}:extensions.json`], {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
});
return JSON.parse(content) as Extension[];
} catch (err) {
// Only treat as empty if the file doesn't exist; rethrow other errors
if (err instanceof Error && err.message.includes("does not exist")) {
return [];
}
throw err;
}
}
/**
* Get the current extensions.json content from the working directory.
*/
function getCurrentExtensions(): Extension[] {
const content = fs.readFileSync("./extensions.json", "utf-8");
return JSON.parse(content) as Extension[];
}
/**
* Compare two extension objects to check if they differ.
*/
function extensionsEqual(a: Extension, b: Extension): boolean {
return JSON.stringify(a) === JSON.stringify(b);
}
/**
* Find extensions that were added or modified compared to the base branch.
*/
function getChangedExtensionIds(
baseExtensions: Extension[],
currentExtensions: Extension[]
): string[] {
const baseMap = new Map(baseExtensions.map((ext) => [ext.id, ext]));
const changedIds: string[] = [];
for (const current of currentExtensions) {
const base = baseMap.get(current.id);
if (!base) {
// New extension
changedIds.push(current.id);
} else if (!extensionsEqual(base, current)) {
// Modified extension
changedIds.push(current.id);
}
}
return changedIds;
}
function main() {
const baseRef = process.argv[2] ?? "origin/main";
const baseExtensions = getBaseExtensions(baseRef);
const currentExtensions = getCurrentExtensions();
const changedIds = getChangedExtensionIds(baseExtensions, currentExtensions);
// Output the changed IDs, one per line (empty output if no changes)
if (changedIds.length > 0) {
console.log(changedIds.join("\n"));
}
}
main();