feat: 增加模板确定性打包命令
This commit is contained in:
1 parent
58370224b5
commit
cddf836f4f
4 files changed
+231
-2
No files matched your search
@@ -360,7 +360,7 @@ Phase 0 执行记录(2026-10-16):
|
|||||||
### Phase 1:包契约和确定性导出
|
### Phase 1:包契约和确定性导出
|
||||||
|
|
||||||
- [ ] 扩展 packages/template-contract,定义 package schema、rendererRef、preview/bootstrap、资源和兼容矩阵;
|
- [ ] 扩展 packages/template-contract,定义 package schema、rendererRef、preview/bootstrap、资源和兼容矩阵;
|
||||||
- [ ] 实现 template:pack,生成稳定清单和 checksum;
|
- [x] 实现 `template:pack`,生成稳定清单和 checksum;当前覆盖 3 个仓库模板,测试重复导出目录和 archive checksum 一致;
|
||||||
- [ ] 实现 package-discovered template:conformance;
|
- [ ] 实现 package-discovered template:conformance;
|
||||||
- [ ] 为每个当前模板补齐真实 manifest、可见字段、编辑绑定、空态和 fixture;
|
- [ ] 为每个当前模板补齐真实 manifest、可见字段、编辑绑定、空态和 fixture;
|
||||||
- [ ] 把版本漂移、缺资源、未知 binding、未消费字段和不兼容 renderer 变成阻断 issue;
|
- [ ] 把版本漂移、缺资源、未知 binding、未消费字段和不兼容 renderer 变成阻断 issue;
|
||||||
|
|||||||
+2
-1
@@ -20,8 +20,9 @@
|
|||||||
"validate:templates": "node scripts/validate-template-packages.cjs && pnpm --filter @personal-brand/template-contract test",
|
"validate:templates": "node scripts/validate-template-packages.cjs && pnpm --filter @personal-brand/template-contract test",
|
||||||
"generate:template-catalog": "node scripts/generate-template-catalog.cjs --write",
|
"generate:template-catalog": "node scripts/generate-template-catalog.cjs --write",
|
||||||
"template:dev": "node packages/template-compiler/src/cli.mjs --template",
|
"template:dev": "node packages/template-compiler/src/cli.mjs --template",
|
||||||
"template:check": "pnpm validate:templates && pnpm test:template-compiler && pnpm --filter @personal-brand/miniapp test:visual-fixture",
|
"template:check": "pnpm validate:templates && node --test scripts/template-pack.test.mjs && pnpm test:template-compiler && pnpm --filter @personal-brand/miniapp test:visual-fixture",
|
||||||
"validate:template-release": "node scripts/validate-template-release.cjs",
|
"validate:template-release": "node scripts/validate-template-release.cjs",
|
||||||
|
"template:pack": "node scripts/template-pack.cjs",
|
||||||
"test:template-compiler": "pnpm --filter @personal-brand/template-compiler test",
|
"test:template-compiler": "pnpm --filter @personal-brand/template-compiler test",
|
||||||
"clean": "turbo run clean && rimraf node_modules .turbo",
|
"clean": "turbo run clean && rimraf node_modules .turbo",
|
||||||
"preview:food": "pnpm --filter @personal-brand/taro dev:h5",
|
"preview:food": "pnpm --filter @personal-brand/taro dev:h5",
|
||||||
|
|||||||
@@ -0,0 +1,184 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const crypto = require("node:crypto");
|
||||||
|
const fs = require("node:fs");
|
||||||
|
const path = require("node:path");
|
||||||
|
const { spawnSync } = require("node:child_process");
|
||||||
|
|
||||||
|
const root = path.resolve(__dirname, "..");
|
||||||
|
const packagesRoot = path.join(root, "packages");
|
||||||
|
const ignoredNames = new Set(["node_modules", ".turbo", "dist", ".git"]);
|
||||||
|
|
||||||
|
function fail(message) {
|
||||||
|
console.error(message);
|
||||||
|
process.exitCode = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function usage() {
|
||||||
|
console.error("Usage: node scripts/template-pack.cjs --template <packageKey> --out <directory>");
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseArgs(argv) {
|
||||||
|
const values = {};
|
||||||
|
const argumentsWithoutSeparator = argv[0] === "--" ? argv.slice(1) : argv;
|
||||||
|
for (let index = 0; index < argumentsWithoutSeparator.length; index += 1) {
|
||||||
|
const argument = argumentsWithoutSeparator[index];
|
||||||
|
if (!argument.startsWith("--")) throw new Error(`Unknown argument: ${argument}`);
|
||||||
|
const key = argument.slice(2);
|
||||||
|
const value = argumentsWithoutSeparator[index + 1];
|
||||||
|
if (!value || value.startsWith("--")) throw new Error(`Missing value for --${key}`);
|
||||||
|
values[key] = value;
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
function canonical(value) {
|
||||||
|
if (Array.isArray(value)) return value.map(canonical);
|
||||||
|
if (!value || typeof value !== "object") return value;
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.keys(value)
|
||||||
|
.sort()
|
||||||
|
.map((key) => [key, canonical(value[key])]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function sha256(value) {
|
||||||
|
return crypto.createHash("sha256").update(value).digest("hex");
|
||||||
|
}
|
||||||
|
|
||||||
|
function toPosix(value) {
|
||||||
|
return value.split(path.sep).join("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
function listFiles(directory, relative = "") {
|
||||||
|
const files = [];
|
||||||
|
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
||||||
|
if (ignoredNames.has(entry.name)) continue;
|
||||||
|
const entryRelative = relative ? path.join(relative, entry.name) : entry.name;
|
||||||
|
const entryPath = path.join(directory, entry.name);
|
||||||
|
if (entry.isSymbolicLink())
|
||||||
|
throw new Error(`Template package cannot contain symlink: ${entryRelative}`);
|
||||||
|
if (entry.isDirectory()) files.push(...listFiles(entryPath, entryRelative));
|
||||||
|
else if (entry.isFile()) files.push(toPosix(entryRelative));
|
||||||
|
else throw new Error(`Unsupported template package entry: ${entryRelative}`);
|
||||||
|
}
|
||||||
|
return files.sort();
|
||||||
|
}
|
||||||
|
|
||||||
|
function copyFiles(source, target, files) {
|
||||||
|
for (const relative of files) {
|
||||||
|
const destination = path.join(target, relative);
|
||||||
|
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
||||||
|
fs.copyFileSync(path.join(source, relative), destination);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function runTemplateValidation() {
|
||||||
|
const result = spawnSync(
|
||||||
|
process.execPath,
|
||||||
|
[path.join(root, "scripts/validate-template-packages.cjs")],
|
||||||
|
{
|
||||||
|
cwd: root,
|
||||||
|
encoding: "utf8",
|
||||||
|
stdio: "inherit",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (result.status !== 0) throw new Error("Template package validation failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
function createArchive(directory, archive) {
|
||||||
|
const parent = path.dirname(directory);
|
||||||
|
const name = path.basename(directory);
|
||||||
|
const result = spawnSync(
|
||||||
|
"tar",
|
||||||
|
[
|
||||||
|
"--create",
|
||||||
|
"--gzip",
|
||||||
|
"--file",
|
||||||
|
archive,
|
||||||
|
"--sort=name",
|
||||||
|
"--mtime=UTC 1970-01-01",
|
||||||
|
"--owner=0",
|
||||||
|
"--group=0",
|
||||||
|
"--numeric-owner",
|
||||||
|
"--directory",
|
||||||
|
parent,
|
||||||
|
name,
|
||||||
|
],
|
||||||
|
{ cwd: root, stdio: "inherit" },
|
||||||
|
);
|
||||||
|
if (result.status !== 0) throw new Error("Could not create deterministic template archive");
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
const values = parseArgs(process.argv.slice(2));
|
||||||
|
const packageKey = values.template;
|
||||||
|
const outputRoot = values.out;
|
||||||
|
if (!packageKey || !outputRoot) {
|
||||||
|
usage();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(packageKey))
|
||||||
|
throw new Error(`Invalid template package key: ${packageKey}`);
|
||||||
|
|
||||||
|
runTemplateValidation();
|
||||||
|
const source = path.join(packagesRoot, `template-${packageKey}`);
|
||||||
|
const manifestPath = path.join(source, "template.json");
|
||||||
|
const packageJsonPath = path.join(source, "package.json");
|
||||||
|
if (!fs.existsSync(manifestPath) || !fs.existsSync(packageJsonPath))
|
||||||
|
throw new Error(`Template package not found: ${packageKey}`);
|
||||||
|
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
||||||
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
||||||
|
if (manifest.packageKey !== packageKey)
|
||||||
|
throw new Error("template.json packageKey does not match request");
|
||||||
|
if (packageJson.version !== manifest.version)
|
||||||
|
throw new Error("package.json version does not match template.json version");
|
||||||
|
|
||||||
|
const destinationRoot = path.resolve(root, outputRoot);
|
||||||
|
const packageName = `${packageKey}-${manifest.version}`;
|
||||||
|
const destination = path.join(destinationRoot, packageName);
|
||||||
|
const archive = path.join(destinationRoot, `${packageName}.tar.gz`);
|
||||||
|
const sourceRelative = path.relative(source, destination);
|
||||||
|
if (!sourceRelative.startsWith(".." + path.sep) && sourceRelative !== "..")
|
||||||
|
throw new Error("Output directory cannot be inside the source template package");
|
||||||
|
|
||||||
|
fs.mkdirSync(destinationRoot, { recursive: true });
|
||||||
|
fs.rmSync(destination, { recursive: true, force: true });
|
||||||
|
fs.rmSync(archive, { force: true });
|
||||||
|
fs.mkdirSync(destination, { recursive: true });
|
||||||
|
|
||||||
|
const files = listFiles(source);
|
||||||
|
copyFiles(source, destination, files);
|
||||||
|
const entries = files.map((relative) => {
|
||||||
|
const content = fs.readFileSync(path.join(destination, relative));
|
||||||
|
return { path: relative, bytes: content.byteLength, sha256: sha256(content) };
|
||||||
|
});
|
||||||
|
const contentChecksum = sha256(JSON.stringify(canonical(entries)));
|
||||||
|
const checksums = {
|
||||||
|
formatVersion: "1",
|
||||||
|
packageKey,
|
||||||
|
version: manifest.version,
|
||||||
|
contentChecksum,
|
||||||
|
files: entries,
|
||||||
|
};
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(destination, "checksums.json"),
|
||||||
|
JSON.stringify(checksums, null, 2) + "\n",
|
||||||
|
"utf8",
|
||||||
|
);
|
||||||
|
createArchive(destination, archive);
|
||||||
|
const archiveChecksum = sha256(fs.readFileSync(archive));
|
||||||
|
console.log(`template package: ${packageKey}@${manifest.version}`);
|
||||||
|
console.log(`content checksum: ${contentChecksum}`);
|
||||||
|
console.log(`archive checksum: ${archiveChecksum}`);
|
||||||
|
console.log(`directory: ${path.relative(root, destination)}`);
|
||||||
|
console.log(`archive: ${path.relative(root, archive)}`);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
process.exitCode = main();
|
||||||
|
} catch (error) {
|
||||||
|
fail(error instanceof Error ? error.message : String(error));
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { execFileSync } from "node:child_process";
|
||||||
|
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
import test from "node:test";
|
||||||
|
|
||||||
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||||||
|
const script = path.join(root, "scripts/template-pack.cjs");
|
||||||
|
const templatePackages = [
|
||||||
|
["acheng-campus-food", "1.0.0"],
|
||||||
|
["atelier-solopreneur", "1.1.0"],
|
||||||
|
["linjing-editorial", "1.0.0"],
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const [packageKey, version] of templatePackages) {
|
||||||
|
test(`${packageKey} produces a stable package directory and archive`, () => {
|
||||||
|
const output = mkdtempSync(path.join("/tmp", "personal-brand-template-pack-"));
|
||||||
|
try {
|
||||||
|
const args = [script, "--template", packageKey, "--out", output];
|
||||||
|
const first = execFileSync(process.execPath, args, { cwd: root, encoding: "utf8" });
|
||||||
|
const firstChecksums = readFileSync(
|
||||||
|
path.join(output, `${packageKey}-${version}`, "checksums.json"),
|
||||||
|
"utf8",
|
||||||
|
);
|
||||||
|
const firstArchive = readFileSync(path.join(output, `${packageKey}-${version}.tar.gz`));
|
||||||
|
const second = execFileSync(process.execPath, args, { cwd: root, encoding: "utf8" });
|
||||||
|
const secondChecksums = readFileSync(
|
||||||
|
path.join(output, `${packageKey}-${version}`, "checksums.json"),
|
||||||
|
"utf8",
|
||||||
|
);
|
||||||
|
const secondArchive = readFileSync(path.join(output, `${packageKey}-${version}.tar.gz`));
|
||||||
|
assert.match(first, /content checksum: [a-f0-9]{64}/);
|
||||||
|
assert.match(second, /content checksum: [a-f0-9]{64}/);
|
||||||
|
assert.deepEqual(JSON.parse(secondChecksums), JSON.parse(firstChecksums));
|
||||||
|
assert.deepEqual(secondArchive, firstArchive);
|
||||||
|
assert.ok(
|
||||||
|
JSON.parse(firstChecksums).files.every((file) => !file.path.includes("node_modules")),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
rmSync(output, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in new issue
Block a user