fix: 收口发布包完整性与模板契约迁移
This commit is contained in:
1 parent
38e9d59864
commit
16fd827634
7 files changed
+812
-26
No files matched your search
@@ -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