fix: 收口发布包完整性与模板契约迁移
This commit is contained in:
1 parent
38e9d59864
commit
16fd827634
7 files changed
+812
-26
No files matched your search
@@ -54,6 +54,66 @@ export async function checkPackage(directory) {
|
||||
if (content.byteLength !== file.bytes || digest !== file.sha256)
|
||||
throw new Error("Release checksum mismatch: " + file.path);
|
||||
}
|
||||
|
||||
const workspacePackages = new Map();
|
||||
for (const relative of actual.filter(
|
||||
(file) => file === "package.json" || file.endsWith("/package.json"),
|
||||
)) {
|
||||
const workspacePackage = JSON.parse(await readFile(path.join(directory, relative), "utf8"));
|
||||
if (typeof workspacePackage.name !== "string" || !workspacePackage.name)
|
||||
throw new Error("Workspace package has no name: " + relative);
|
||||
workspacePackages.set(workspacePackage.name, {
|
||||
directory: path.posix.dirname(relative),
|
||||
manifest: workspacePackage,
|
||||
});
|
||||
}
|
||||
for (const [name, workspacePackage] of workspacePackages) {
|
||||
for (const section of ["dependencies", "devDependencies", "optionalDependencies"]) {
|
||||
for (const [dependency, range] of Object.entries(workspacePackage.manifest[section] ?? {})) {
|
||||
if (typeof range !== "string" || !range.startsWith("workspace:")) continue;
|
||||
const target = workspacePackages.get(dependency);
|
||||
if (!target) {
|
||||
throw new Error(
|
||||
`Release is missing workspace dependency ${dependency} required by ${name}`,
|
||||
);
|
||||
}
|
||||
if (target.directory === ".") {
|
||||
throw new Error(`Workspace dependency ${dependency} cannot be the release root package`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (actual.includes("template-catalog.json")) {
|
||||
const catalog = JSON.parse(
|
||||
await readFile(path.join(directory, "template-catalog.json"), "utf8"),
|
||||
);
|
||||
if (catalog.formatVersion !== "1" || !Array.isArray(catalog.packages))
|
||||
throw new Error("Invalid template catalog");
|
||||
const keys = new Set();
|
||||
for (const entry of catalog.packages) {
|
||||
if (
|
||||
typeof entry.packageKey !== "string" ||
|
||||
typeof entry.version !== "string" ||
|
||||
typeof entry.archiveChecksum !== "string" ||
|
||||
typeof entry.archivePath !== "string"
|
||||
)
|
||||
throw new Error("Invalid template catalog entry");
|
||||
if (keys.has(entry.packageKey))
|
||||
throw new Error("Duplicate template package in catalog: " + entry.packageKey);
|
||||
keys.add(entry.packageKey);
|
||||
if (
|
||||
path.isAbsolute(entry.archivePath) ||
|
||||
entry.archivePath.split("/").includes("..") ||
|
||||
!actual.includes(entry.archivePath)
|
||||
)
|
||||
throw new Error("Template archive is missing from release: " + entry.packageKey);
|
||||
const digest = createHash("sha256")
|
||||
.update(await readFile(path.join(directory, entry.archivePath)))
|
||||
.digest("hex");
|
||||
if (digest !== entry.archiveChecksum)
|
||||
throw new Error("Template archive checksum mismatch: " + entry.packageKey);
|
||||
}
|
||||
}
|
||||
return manifest;
|
||||
}
|
||||
|
||||
|
||||
@@ -65,6 +65,7 @@ test("release package checker rejects files missing from the manifest", async ()
|
||||
test("release package checker rejects patched dependencies missing from the package", async () => {
|
||||
const directory = await createPackage({
|
||||
"package.json": JSON.stringify({
|
||||
name: "fixture-release",
|
||||
pnpm: {
|
||||
patchedDependencies: {
|
||||
"example-package@1.0.0": "patches/example-package.patch",
|
||||
@@ -78,3 +79,87 @@ test("release package checker rejects patched dependencies missing from the pack
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("release package checker accepts a complete workspace dependency closure", async () => {
|
||||
const directory = await createPackage({
|
||||
"package.json": JSON.stringify({ name: "fixture-release" }),
|
||||
"packages/api/package.json": JSON.stringify({
|
||||
name: "@fixture/api",
|
||||
dependencies: { "@fixture/contract": "workspace:^" },
|
||||
}),
|
||||
"packages/contract/package.json": JSON.stringify({
|
||||
name: "@fixture/contract",
|
||||
}),
|
||||
});
|
||||
try {
|
||||
await checkPackage(directory);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("release package checker rejects a missing workspace dependency", async () => {
|
||||
const directory = await createPackage({
|
||||
"package.json": JSON.stringify({ name: "fixture-release" }),
|
||||
"packages/api/package.json": JSON.stringify({
|
||||
name: "@fixture/api",
|
||||
dependencies: { "@fixture/contract": "workspace:^" },
|
||||
}),
|
||||
});
|
||||
try {
|
||||
await assert.rejects(
|
||||
checkPackage(directory),
|
||||
/missing workspace dependency @fixture\/contract/,
|
||||
);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("release package checker accepts a valid template catalog", async () => {
|
||||
const archive = Buffer.from("template archive");
|
||||
const archiveChecksum = createHash("sha256").update(archive).digest("hex");
|
||||
const archivePath = `template-artifacts/example/1.0.0/${archiveChecksum}.tar.gz`;
|
||||
const directory = await createPackage({
|
||||
[archivePath]: archive,
|
||||
"template-catalog.json": JSON.stringify({
|
||||
formatVersion: "1",
|
||||
packages: [
|
||||
{
|
||||
packageKey: "example",
|
||||
version: "1.0.0",
|
||||
archiveChecksum,
|
||||
archivePath,
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
try {
|
||||
await checkPackage(directory);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("release package checker rejects a template archive with a mismatched checksum", async () => {
|
||||
const archivePath = "template-artifacts/example/1.0.0/archive.tar.gz";
|
||||
const directory = await createPackage({
|
||||
[archivePath]: "template archive",
|
||||
"template-catalog.json": JSON.stringify({
|
||||
formatVersion: "1",
|
||||
packages: [
|
||||
{
|
||||
packageKey: "example",
|
||||
version: "1.0.0",
|
||||
archiveChecksum: "0".repeat(64),
|
||||
archivePath,
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
try {
|
||||
await assert.rejects(checkPackage(directory), /template archive checksum mismatch/i);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync } from "node:fs";
|
||||
import { cp, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
||||
import { cp, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
@@ -71,9 +71,13 @@ async function copyWorkspace(workspace) {
|
||||
const target = path.join(output, workspace);
|
||||
await mkdir(target, { recursive: true });
|
||||
await cp(path.join(source, "package.json"), path.join(target, "package.json"));
|
||||
await cp(path.join(source, "src"), path.join(target, "src"), { recursive: true });
|
||||
await cp(path.join(source, "dist"), path.join(target, "dist"), { recursive: true });
|
||||
await cp(path.join(source, "tsconfig.json"), path.join(target, "tsconfig.json"));
|
||||
for (const directory of ["src", "dist"]) {
|
||||
if (!existsSync(path.join(source, directory))) continue;
|
||||
await cp(path.join(source, directory), path.join(target, directory), { recursive: true });
|
||||
}
|
||||
if (existsSync(path.join(source, "tsconfig.json"))) {
|
||||
await cp(path.join(source, "tsconfig.json"), path.join(target, "tsconfig.json"));
|
||||
}
|
||||
if (workspace === "apps/api") {
|
||||
await cp(path.join(source, "prisma/schema.prisma"), path.join(target, "prisma/schema.prisma"));
|
||||
await cp(path.join(source, "prisma/migrations"), path.join(target, "prisma/migrations"), {
|
||||
@@ -92,15 +96,80 @@ async function copyTaroWorkspace() {
|
||||
await cp(path.join(source, "tsconfig.json"), path.join(target, "tsconfig.json"));
|
||||
}
|
||||
|
||||
async function copyTemplatePackage(directoryName) {
|
||||
const source = path.join(root, "packages", directoryName);
|
||||
const target = path.join(output, "packages", directoryName);
|
||||
await mkdir(target, { recursive: true });
|
||||
await cp(path.join(source, "package.json"), path.join(target, "package.json"));
|
||||
await cp(path.join(source, "template.json"), path.join(target, "template.json"));
|
||||
await cp(path.join(source, "miniapp"), path.join(target, "miniapp"), { recursive: true });
|
||||
await cp(path.join(source, "taro"), path.join(target, "taro"), { recursive: true });
|
||||
await cp(path.join(source, "tsconfig.json"), path.join(target, "tsconfig.json"));
|
||||
function sha256File(file) {
|
||||
return readFile(file).then((content) => createHash("sha256").update(content).digest("hex"));
|
||||
}
|
||||
|
||||
async function discoverTemplatePackages() {
|
||||
const packages = [];
|
||||
for (const entry of await readdir(path.join(root, "packages"), { withFileTypes: true })) {
|
||||
const templateDirectory = path.join(root, "packages", entry.name);
|
||||
if (
|
||||
entry.isDirectory() &&
|
||||
entry.name.startsWith("template-") &&
|
||||
existsSync(path.join(templateDirectory, "template.json")) &&
|
||||
existsSync(path.join(templateDirectory, "taro/index.tsx"))
|
||||
) {
|
||||
packages.push({
|
||||
directoryName: entry.name,
|
||||
manifest: JSON.parse(await readFile(path.join(templateDirectory, "template.json"), "utf8")),
|
||||
});
|
||||
}
|
||||
}
|
||||
return packages.sort((left, right) =>
|
||||
left.manifest.packageKey.localeCompare(right.manifest.packageKey),
|
||||
);
|
||||
}
|
||||
|
||||
async function buildTemplateCatalog() {
|
||||
const packedRoot = path.join(root, "dist/template-packages");
|
||||
await rm(packedRoot, { recursive: true, force: true });
|
||||
const packages = [];
|
||||
for (const template of await discoverTemplatePackages()) {
|
||||
const { packageKey, version } = template.manifest;
|
||||
if (typeof packageKey !== "string" || typeof version !== "string")
|
||||
throw new Error("Template manifest must declare packageKey and version");
|
||||
run("node", ["scripts/template-pack.cjs", "--template", packageKey, "--out", packedRoot]);
|
||||
const packedDirectory = path.join(packedRoot, `${packageKey}-${version}`);
|
||||
const checksums = JSON.parse(
|
||||
await readFile(path.join(packedDirectory, "checksums.json"), "utf8"),
|
||||
);
|
||||
const archiveSource = packedDirectory + ".tar.gz";
|
||||
const archiveChecksum = await sha256File(archiveSource);
|
||||
const archivePath = path.posix.join(
|
||||
"template-artifacts",
|
||||
packageKey,
|
||||
version,
|
||||
archiveChecksum + ".tar.gz",
|
||||
);
|
||||
const archiveTarget = path.join(output, archivePath);
|
||||
await mkdir(path.dirname(archiveTarget), { recursive: true });
|
||||
await cp(archiveSource, archiveTarget);
|
||||
const target = path.join(output, "packages", template.directoryName);
|
||||
await cp(packedDirectory, target, { recursive: true });
|
||||
packages.push({
|
||||
packageKey,
|
||||
version,
|
||||
displayName: template.manifest.displayName,
|
||||
supportedIndustryTemplates: template.manifest.supportedIndustryTemplates,
|
||||
manifest: template.manifest,
|
||||
validationReport: {
|
||||
formatVersion: "1",
|
||||
errors: [],
|
||||
fileCount: checksums.files.length,
|
||||
extractedBytes: checksums.files.reduce((total, file) => total + file.bytes, 0),
|
||||
files: checksums.files,
|
||||
},
|
||||
contentChecksum: checksums.contentChecksum,
|
||||
archiveChecksum,
|
||||
archivePath,
|
||||
});
|
||||
}
|
||||
await writeFile(
|
||||
path.join(output, "template-catalog.json"),
|
||||
JSON.stringify({ formatVersion: "1", packages }, null, 2) + "\n",
|
||||
);
|
||||
return packages;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
@@ -123,6 +192,7 @@ async function main() {
|
||||
for (const workspace of [
|
||||
"apps/api",
|
||||
"packages/config",
|
||||
"packages/template-contract",
|
||||
"packages/types",
|
||||
"packages/validation",
|
||||
]) {
|
||||
@@ -130,17 +200,8 @@ async function main() {
|
||||
}
|
||||
await copyTaroWorkspace();
|
||||
await cp(adminDist, path.join(output, "apps/admin/dist"), { recursive: true });
|
||||
for (const entry of await readdir(path.join(root, "packages"), { withFileTypes: true })) {
|
||||
const templateDirectory = path.join(root, "packages", entry.name);
|
||||
if (
|
||||
entry.isDirectory() &&
|
||||
entry.name.startsWith("template-") &&
|
||||
existsSync(path.join(templateDirectory, "template.json")) &&
|
||||
existsSync(path.join(templateDirectory, "taro/index.tsx"))
|
||||
) {
|
||||
await copyTemplatePackage(entry.name);
|
||||
}
|
||||
}
|
||||
const templateCatalog = await buildTemplateCatalog();
|
||||
if (!templateCatalog.length) throw new Error("Release contains no template package");
|
||||
await cp(path.join(root, "apps/api/prisma/assets"), path.join(output, "seed-media"), {
|
||||
recursive: true,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user