forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.ts
More file actions
91 lines (75 loc) · 2.67 KB
/
Copy pathcache.ts
File metadata and controls
91 lines (75 loc) · 2.67 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
import * as fs from "node:fs";
import * as path from "node:path";
import { CACHE_DIR, PACKAGE_NAME } from "./constants";
interface BunLockfile {
workspaces?: {
""?: {
dependencies?: Record<string, string>;
};
};
packages?: Record<string, unknown>;
}
function stripTrailingCommas(json: string): string {
return json.replace(/,(\s*[}\]])/g, "$1");
}
function removeFromBunLock(packageName: string): boolean {
const lockPath = path.join(CACHE_DIR, "bun.lock");
if (!fs.existsSync(lockPath)) return false;
try {
const content = fs.readFileSync(lockPath, "utf-8");
const lock = JSON.parse(stripTrailingCommas(content)) as BunLockfile;
let modified = false;
if (lock.workspaces?.[""]?.dependencies?.[packageName]) {
delete lock.workspaces[""].dependencies[packageName];
modified = true;
}
if (lock.packages?.[packageName]) {
delete lock.packages[packageName];
modified = true;
}
if (modified) {
fs.writeFileSync(lockPath, JSON.stringify(lock, null, 2));
console.log(`[auto-update-checker] Removed from bun.lock: ${packageName}`);
}
return modified;
} catch {
return false;
}
}
export function invalidatePackage(packageName: string = PACKAGE_NAME): boolean {
try {
const pkgDir = path.join(CACHE_DIR, "node_modules", packageName);
const pkgJsonPath = path.join(CACHE_DIR, "package.json");
let packageRemoved = false;
let dependencyRemoved = false;
let lockRemoved = false;
if (fs.existsSync(pkgDir)) {
fs.rmSync(pkgDir, { recursive: true, force: true });
console.log(`[auto-update-checker] Package removed: ${pkgDir}`);
packageRemoved = true;
}
if (fs.existsSync(pkgJsonPath)) {
const content = fs.readFileSync(pkgJsonPath, "utf-8");
const pkgJson = JSON.parse(content);
if (pkgJson.dependencies?.[packageName]) {
delete pkgJson.dependencies[packageName];
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2));
console.log(`[auto-update-checker] Dependency removed from package.json: ${packageName}`);
dependencyRemoved = true;
}
}
lockRemoved = removeFromBunLock(packageName);
if (!packageRemoved && !dependencyRemoved && !lockRemoved) {
console.log(`[auto-update-checker] Package not found, nothing to invalidate: ${packageName}`);
return false;
}
return true;
} catch (err) {
console.error("[auto-update-checker] Failed to invalidate package:", err);
return false;
}
}
export function invalidateCache(): boolean {
console.warn("[auto-update-checker] WARNING: invalidateCache is deprecated, use invalidatePackage");
return invalidatePackage();
}