star-chart-search-enhancer/scripts/package-release-archive.mjs
admin123 f683b1db4f
All checks were successful
continuous-integration/drone/tag Build is passing
fix: keep release archive wrapped in a folder
2026-05-25 14:22:59 +08:00

38 lines
1.1 KiB
JavaScript

import { createWriteStream } from "node:fs";
import { readdir } from "node:fs/promises";
import path from "node:path";
import { pipeline } from "node:stream/promises";
import yazl from "yazl";
export async function createReleaseArchive({
archivePath,
rootDirName = "star-chart-search-enhancer-internal",
sourceDir
}) {
const zip = new yazl.ZipFile();
const output = createWriteStream(archivePath);
await addDirectory(zip, sourceDir, sourceDir, rootDirName);
zip.end();
await pipeline(zip.outputStream, output);
}
async function addDirectory(zip, rootDir, currentDir, rootDirName) {
const entries = await readdir(currentDir, { withFileTypes: true });
entries.sort((left, right) => left.name.localeCompare(right.name));
for (const entry of entries) {
const absolutePath = path.join(currentDir, entry.name);
const relativePath = path.relative(rootDir, absolutePath);
if (entry.isDirectory()) {
await addDirectory(zip, rootDir, absolutePath, rootDirName);
continue;
}
if (entry.isFile()) {
zip.addFile(absolutePath, path.join(rootDirName, relativePath));
}
}
}