Files
NODEDC_PLATFORM/infra/deploy-runner/build-module-foundry-map-sector-workspace-artifact.mjs
T

185 lines
7.3 KiB
JavaScript

#!/usr/bin/env node
import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { copyFile, lstat, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const scriptDir = dirname(fileURLToPath(import.meta.url));
const workspaceRoot = resolve(scriptDir, "../../..");
const foundryRoot = resolve(workspaceRoot, "NODEDC_DESIGN_GUIDELINE");
const artifactDir = resolve(
process.env.NODEDC_DEPLOY_ARTIFACT_DIR || resolve(scriptDir, "../deploy-artifacts"),
);
const [patchId = "module-foundry-map-sector-workspace-20260808-001", ...extra] =
process.argv.slice(2);
if (extra.length || !/^module-foundry-map-sector-workspace-\d{8}-\d{3}$/.test(patchId)) {
throw new Error(
"usage: build-module-foundry-map-sector-workspace-artifact.mjs "
+ "[module-foundry-map-sector-workspace-YYYYMMDD-NNN]",
);
}
const files = Object.freeze([
"apps/catalog/src/MapFixturePreview.tsx",
"apps/catalog/src/mapPresentationProfile.ts",
"apps/catalog/src/mapSectorGrid.d.mts",
"apps/catalog/src/mapSectorGrid.mjs",
"apps/catalog/src/styles.css",
"scripts/map-object-layers.test.mjs",
"scripts/map-presentation-filters.test.mjs",
"scripts/map-sector-grid.test.mjs",
]);
const artifact = join(artifactDir, `nodedc-${patchId}.tgz`);
const checksum = `${artifact}.sha256`;
const stage = await mkdtemp(join(tmpdir(), "nodedc-foundry-map-sector-workspace-"));
await assertFresh(artifact);
await assertMapSectorWorkspaceBoundary();
try {
for (const relativePath of files) {
const source = join(foundryRoot, relativePath);
const info = await lstat(source);
if (!info.isFile() || info.isSymbolicLink()) {
throw new Error(`source_file_rejected:${relativePath}`);
}
const destination = join(stage, "payload", relativePath);
await mkdir(dirname(destination), { recursive: true });
await copyFile(source, destination);
}
await writeFile(
join(stage, "manifest.env"),
`id=${patchId}\ncomponent=module-foundry\ntype=app-overlay\n`,
"utf8",
);
await writeFile(join(stage, "files.txt"), `${files.join("\n")}\n`, "utf8");
await mkdir(artifactDir, { recursive: true });
run("python3", ["-c", canonicalTarScript(), artifact, stage]);
const sha256 = digest(await readFile(artifact));
await writeFile(checksum, `${sha256} ${artifact.split("/").at(-1)}\n`, "utf8");
console.log(JSON.stringify({
ok: true,
patchId,
artifact,
checksum,
sha256,
sourceCommit: gitHead(foundryRoot),
services: ["nodedc-module-foundry"],
transition: "activate-sector-scoped-map-workspace",
files,
}, null, 2));
} finally {
await rm(stage, { recursive: true, force: true });
}
async function assertMapSectorWorkspaceBoundary() {
const preview = await readFile(join(foundryRoot, "apps/catalog/src/MapFixturePreview.tsx"), "utf8");
const profile = await readFile(join(foundryRoot, "apps/catalog/src/mapPresentationProfile.ts"), "utf8");
const sectorGrid = await readFile(join(foundryRoot, "apps/catalog/src/mapSectorGrid.mjs"), "utf8");
const sectorGridTypes = await readFile(join(foundryRoot, "apps/catalog/src/mapSectorGrid.d.mts"), "utf8");
const styles = await readFile(join(foundryRoot, "apps/catalog/src/styles.css"), "utf8");
const objectTests = await readFile(join(foundryRoot, "scripts/map-object-layers.test.mjs"), "utf8");
const filterTests = await readFile(join(foundryRoot, "scripts/map-presentation-filters.test.mjs"), "utf8");
const sectorTests = await readFile(join(foundryRoot, "scripts/map-sector-grid.test.mjs"), "utf8");
for (const marker of [
"type MapWorkspaceWindowId = \"settings\" | \"layers\" | \"sector\" | \"subject-card\"",
"const sectorSpatialEntities = useMemo",
"const sectorScopedPrimaryRuntimeBindings = useMemo",
"runtimeBindings={[...sectorScopedPrimaryRuntimeBindings, ...referenceRuntimeBindings]}",
"label=\"Скрыть объекты за сектором\"",
"onClose={deactivateGridSector}",
"Деактивировать сектор",
"Домены данных",
"Провайдеры",
"Типы объектов",
"Объекты сектора",
]) {
if (!preview.includes(marker)) throw new Error(`sector_workspace_marker_missing:${marker}`);
}
for (const marker of [
"Values inside one facet form a union",
"return selectedFacets.every",
]) {
if (!profile.includes(marker)) throw new Error(`presentation_filter_marker_missing:${marker}`);
}
for (const marker of [
"export function geodeticToLocalGridPlane",
"export function localSectorAtGeodetic",
"normalScale",
]) {
if (!sectorGrid.includes(marker)) throw new Error(`sector_projection_marker_missing:${marker}`);
if (marker.startsWith("export function") && !sectorGridTypes.includes(marker)) {
throw new Error(`sector_projection_type_marker_missing:${marker}`);
}
}
for (const marker of [
".catalog-map-fixture__sector-window",
".catalog-map-sector-window__object-list",
]) {
if (!styles.includes(marker)) throw new Error(`sector_styles_marker_missing:${marker}`);
}
for (const marker of [
"an active sector scopes point facts, exposes data facets and deactivates on close",
"map windows share one bounded activation and z-index stack",
]) {
if (!objectTests.includes(marker)) throw new Error(`sector_object_regression_missing:${marker}`);
}
if (!filterTests.includes("chips are OR within one facet and AND between facets")) {
throw new Error("sector_filter_regression_missing");
}
if (!sectorTests.includes("WGS84 positions use the same tangent-plane address as Cesium sector picking")) {
throw new Error("sector_projection_regression_missing");
}
}
async function assertFresh(path) {
try {
await lstat(path);
} catch (error) {
if (error?.code === "ENOENT") return;
throw error;
}
throw new Error("artifact_already_exists");
}
function canonicalTarScript() {
return [
"import gzip,io,pathlib,sys,tarfile",
"root=pathlib.Path(sys.argv[2])",
"with open(sys.argv[1],'wb') as out:",
" with gzip.GzipFile(filename='',mode='wb',fileobj=out,compresslevel=9,mtime=0) as gz:",
" with tarfile.open(fileobj=gz,mode='w',format=tarfile.PAX_FORMAT) as tar:",
" for top in ('manifest.env','files.txt','payload'):",
" p=root/top; paths=[p]+(sorted(p.rglob('*')) if p.is_dir() else [])",
" for x in paths:",
" info=tar.gettarinfo(str(x),arcname=x.relative_to(root).as_posix())",
" info.uid=info.gid=0; info.uname=info.gname='root'; info.mtime=0; info.mode=0o755 if info.isdir() else 0o644",
" with (open(x,'rb') if info.isfile() else io.BytesIO()) as src: tar.addfile(info,src if info.isfile() else None)",
].join("\n");
}
function digest(value) {
return createHash("sha256").update(value).digest("hex");
}
function gitHead(path) {
const result = spawnSync("git", ["-C", path, "rev-parse", "HEAD"], { encoding: "utf8" });
if (result.status !== 0) throw new Error(`git_head_failed:${result.stderr || result.stdout}`);
return result.stdout.trim();
}
function run(command, args) {
const result = spawnSync(command, args, {
encoding: "utf8",
maxBuffer: 64 * 1024 * 1024,
});
if (result.status !== 0) {
throw new Error(`${command}_failed:${result.stderr || result.stdout}`);
}
}