refactor(plugins): compact package json index metadata

This commit is contained in:
Vincent Koc
2026-04-25 02:08:12 -07:00
parent c959c18fc7
commit c52c161f5a
3 changed files with 48 additions and 20 deletions

View File

@@ -56,8 +56,12 @@ const InstalledPluginIndexRecordSchema = z
packageInstall: z.unknown().optional(),
manifestPath: z.string(),
manifestHash: z.string(),
packageJsonPath: z.string().optional(),
packageJsonHash: z.string().optional(),
packageJson: z
.object({
path: z.string(),
hash: z.string(),
})
.optional(),
rootDir: z.string(),
origin: z.string(),
enabled: z.boolean(),

View File

@@ -196,8 +196,10 @@ describe("installed plugin index", () => {
],
});
expect(index.plugins[0]?.manifestHash).toMatch(/^[a-f0-9]{64}$/u);
expect(index.plugins[0]?.packageJsonHash).toMatch(/^[a-f0-9]{64}$/u);
expect(index.plugins[0]?.packageJsonPath).toBe(path.join(fixture.rootDir, "package.json"));
expect(index.plugins[0]?.packageJson).toMatchObject({
path: "package.json",
hash: expect.stringMatching(/^[a-f0-9]{64}$/u),
});
expect(index.plugins[0]?.installRecord).toBeUndefined();
expect(index.plugins[0]?.installRecordHash).toBeUndefined();

View File

@@ -87,8 +87,10 @@ export type InstalledPluginIndexRecord = {
packageInstall?: PluginInstallSourceInfo;
manifestPath: string;
manifestHash: string;
packageJsonPath?: string;
packageJsonHash?: string;
packageJson?: {
path: string;
hash: string;
};
rootDir: string;
origin: PluginManifestRecord["origin"];
enabled: boolean;
@@ -244,6 +246,30 @@ function resolvePackageJsonPath(candidate: PluginCandidate | undefined): string
return fs.existsSync(packageJsonPath) ? packageJsonPath : undefined;
}
function resolvePackageJsonRecord(params: {
candidate: PluginCandidate | undefined;
packageJsonPath: string | undefined;
diagnostics: PluginDiagnostic[];
pluginId: string;
}): InstalledPluginIndexRecord["packageJson"] | undefined {
if (!params.candidate?.packageDir || !params.packageJsonPath) {
return undefined;
}
const hash = safeHashFile({
filePath: params.packageJsonPath,
pluginId: params.pluginId,
diagnostics: params.diagnostics,
required: false,
});
if (!hash) {
return undefined;
}
return {
path: path.relative(params.candidate.rootDir, params.packageJsonPath) || "package.json",
hash,
};
}
function describePackageInstallSource(
candidate: PluginCandidate | undefined,
): PluginInstallSourceInfo | undefined {
@@ -416,14 +442,12 @@ function buildInstalledPluginIndex(
diagnostics,
required: true,
}) ?? "";
const packageJsonHash = packageJsonPath
? safeHashFile({
filePath: packageJsonPath,
pluginId: record.id,
diagnostics,
required: false,
})
: undefined;
const packageJson = resolvePackageJsonRecord({
candidate,
packageJsonPath,
diagnostics,
pluginId: record.id,
});
const enabled = resolveEffectiveEnableState({
id: record.id,
origin: record.origin,
@@ -457,11 +481,8 @@ function buildInstalledPluginIndex(
if (packageInstall) {
indexRecord.packageInstall = packageInstall;
}
if (packageJsonPath) {
indexRecord.packageJsonPath = packageJsonPath;
}
if (packageJsonHash) {
indexRecord.packageJsonHash = packageJsonHash;
if (packageJson) {
indexRecord.packageJson = packageJson;
}
return indexRecord;
});
@@ -698,7 +719,8 @@ export function diffInstalledPluginIndexInvalidationReasons(
}
if (
previousPlugin.packageVersion !== currentPlugin.packageVersion ||
previousPlugin.packageJsonHash !== currentPlugin.packageJsonHash
previousPlugin.packageJson?.path !== currentPlugin.packageJson?.path ||
previousPlugin.packageJson?.hash !== currentPlugin.packageJson?.hash
) {
reasons.add("stale-package");
}