|
| 1 | +import { existsSync, statSync } from "fs"; |
| 2 | +import { mkdir, readFile, unlink, writeFile } from "fs/promises"; |
| 3 | +import { dirname, join } from "path"; |
| 4 | +import type { OptionDef } from "bailian-cli-core"; |
| 5 | +import { getPluginsCachePath } from "./paths.ts"; |
| 6 | +import type { DiscoveredPlugin, PluginSourceType } from "./types.ts"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Disk cache schema version |
| 10 | + */ |
| 11 | +export const COMMANDS_CACHE_SCHEMA = 1; |
| 12 | + |
| 13 | +/** |
| 14 | + * is plugin commands cache disabled |
| 15 | + * @returns true if plugin commands cache is disabled, false otherwise |
| 16 | + */ |
| 17 | +export function isPluginCommandsCacheDisabled(): boolean { |
| 18 | + return process.env.BAILIAN_CLI_PLUGINS_NO_CACHE === "1"; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Serializable command metadata |
| 23 | + */ |
| 24 | +export interface CachedCommandMeta { |
| 25 | + commandPath: string; |
| 26 | + modulePath: string; |
| 27 | + name: string; |
| 28 | + description: string; |
| 29 | + usage?: string; |
| 30 | + options?: OptionDef[]; |
| 31 | + examples?: string[]; |
| 32 | + apiDocs?: string; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Plugin fingerprint: name + version + commands directory mtime |
| 37 | + */ |
| 38 | +export interface PluginFingerprint { |
| 39 | + name: string; |
| 40 | + version?: string; |
| 41 | + source: PluginSourceType; |
| 42 | + root: string; |
| 43 | + commandsMtimeMs: number; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Single plugin entry in the cache |
| 48 | + */ |
| 49 | +export interface CachedPluginEntry { |
| 50 | + fingerprint: PluginFingerprint; |
| 51 | + commands: CachedCommandMeta[]; |
| 52 | + /** |
| 53 | + * No-auth setup command paths (space-separated raw strings) |
| 54 | + */ |
| 55 | + noAuthSetup: string[]; |
| 56 | +} |
| 57 | + |
| 58 | +/** ~/.bailian/plugins/commands-cache.json */ |
| 59 | +export interface CommandsCache { |
| 60 | + schema: typeof COMMANDS_CACHE_SCHEMA; |
| 61 | + cliVersion: string; |
| 62 | + plugins: Record<string, CachedPluginEntry>; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Compute plugin fingerprint |
| 67 | + * @param plugin - The plugin to compute the fingerprint for |
| 68 | + * @returns The plugin fingerprint |
| 69 | + */ |
| 70 | +export function computeFingerprint(plugin: DiscoveredPlugin): PluginFingerprint { |
| 71 | + const commandsDir = join(plugin.root, plugin.bailianCli.commands ?? ""); |
| 72 | + let commandsMtimeMs = 0; |
| 73 | + if (existsSync(commandsDir)) { |
| 74 | + try { |
| 75 | + commandsMtimeMs = statSync(commandsDir).mtimeMs; |
| 76 | + } catch { |
| 77 | + commandsMtimeMs = 0; |
| 78 | + } |
| 79 | + } |
| 80 | + return { |
| 81 | + name: plugin.name, |
| 82 | + version: plugin.version, |
| 83 | + source: plugin.source, |
| 84 | + root: plugin.root, |
| 85 | + commandsMtimeMs, |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Check if the fingerprints match |
| 91 | + * @param cached - The cached fingerprint |
| 92 | + * @param current - The current fingerprint |
| 93 | + * @returns true if the fingerprints match, false otherwise |
| 94 | + */ |
| 95 | +export function fingerprintMatches(cached: PluginFingerprint, current: PluginFingerprint): boolean { |
| 96 | + return ( |
| 97 | + cached.name === current.name && |
| 98 | + cached.version === current.version && |
| 99 | + cached.source === current.source && |
| 100 | + cached.root === current.root && |
| 101 | + cached.commandsMtimeMs === current.commandsMtimeMs |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Remove cached entries for uninstalled plugins |
| 107 | + * @param plugins - The plugins to prune |
| 108 | + * @param activeNames - The names of the active plugins |
| 109 | + * @returns The pruned plugins |
| 110 | + */ |
| 111 | +export function pruneCache( |
| 112 | + plugins: Record<string, CachedPluginEntry>, |
| 113 | + activeNames: string[], |
| 114 | +): Record<string, CachedPluginEntry> { |
| 115 | + const active = new Set(activeNames); |
| 116 | + const pruned: Record<string, CachedPluginEntry> = {}; |
| 117 | + for (const [name, entry] of Object.entries(plugins)) { |
| 118 | + if (active.has(name)) pruned[name] = entry; |
| 119 | + } |
| 120 | + return pruned; |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Read the disk cache (returns undefined on failure) |
| 125 | + * @returns The commands cache or undefined if the cache is not found or invalid |
| 126 | + */ |
| 127 | +export async function readCommandsCache(): Promise<CommandsCache | undefined> { |
| 128 | + const path = getPluginsCachePath(); |
| 129 | + if (!existsSync(path)) return undefined; |
| 130 | + try { |
| 131 | + const raw = JSON.parse(await readFile(path, "utf8")) as CommandsCache; |
| 132 | + if (raw.schema !== COMMANDS_CACHE_SCHEMA) return undefined; |
| 133 | + if (!raw.cliVersion || typeof raw.plugins !== "object") return undefined; |
| 134 | + return raw; |
| 135 | + } catch { |
| 136 | + return undefined; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Write the disk cache (best-effort, silent on failure) |
| 142 | + * @param cache - The commands cache to write |
| 143 | + */ |
| 144 | +export async function writeCommandsCache(cache: CommandsCache): Promise<void> { |
| 145 | + const path = getPluginsCachePath(); |
| 146 | + try { |
| 147 | + await mkdir(dirname(path), { recursive: true, mode: 0o700 }); |
| 148 | + await writeFile(path, `${JSON.stringify(cache, null, 2)}\n`, { mode: 0o600 }); |
| 149 | + } catch { |
| 150 | + /* best-effort */ |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Delete the disk cache (invalidated after install/remove/link) |
| 156 | + * @returns The commands cache or undefined if the cache is not found or invalid |
| 157 | + */ |
| 158 | +export async function clearCommandsCache(): Promise<void> { |
| 159 | + const path = getPluginsCachePath(); |
| 160 | + try { |
| 161 | + await unlink(path); |
| 162 | + } catch { |
| 163 | + /* file may not exist */ |
| 164 | + } |
| 165 | +} |
0 commit comments