88 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-02-05 12:59:05 +01:00
import { dirname, relative, sep } from "path";
import { createWriteStream } from "fs";
import walk from "./walk";
import { ZipFile } from "yazl";
2023-02-05 12:59:05 +01:00
import { mkdir } from "fs/promises";
import trimIndent from "./trimIndent";
2023-04-02 22:49:12 +02:00
export type ZipEntry = { zipPath: string } & ({ fsPath: string } | { buffer: Buffer });
export type ZipEntryGenerator = AsyncGenerator<ZipEntry, void, unknown>;
type CommonJarArgs = {
groupId: string;
artifactId: string;
version: string;
2023-04-02 22:49:12 +02:00
};
export type JarStreamArgs = CommonJarArgs & {
2023-04-02 22:49:12 +02:00
asyncPathGeneratorFn(): ZipEntryGenerator;
};
export type JarArgs = CommonJarArgs & {
targetPath: string;
rootPath: string;
};
export async function jarStream({ groupId, artifactId, version, asyncPathGeneratorFn }: JarStreamArgs) {
2023-04-02 22:49:12 +02:00
const manifestPath = "META-INF/MANIFEST.MF";
const manifestData = Buffer.from(trimIndent`
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Keycloakify
Built-By: unknown
Build-Jdk: 19.0.0
2023-04-02 22:49:12 +02:00
`);
2023-04-02 22:49:12 +02:00
const pomPropsPath = `META-INF/maven/${groupId}/${artifactId}/pom.properties`;
const pomPropsData = Buffer.from(trimIndent`
# Generated by keycloakify
# ${new Date()}
artifactId=${artifactId}
groupId=${groupId}
version=${version}
2023-04-02 22:49:12 +02:00
`);
2023-04-02 22:49:12 +02:00
const zipFile = new ZipFile();
for await (const entry of asyncPathGeneratorFn()) {
if ("buffer" in entry) {
2023-04-02 22:49:12 +02:00
zipFile.addBuffer(entry.buffer, entry.zipPath);
} else if ("fsPath" in entry) {
2023-04-02 22:49:12 +02:00
zipFile.addFile(entry.fsPath, entry.zipPath);
}
}
2023-04-02 22:49:12 +02:00
zipFile.addBuffer(manifestData, manifestPath);
zipFile.addBuffer(pomPropsData, pomPropsPath);
2023-04-02 22:49:12 +02:00
zipFile.end();
2023-04-02 22:49:12 +02:00
return zipFile;
}
/**
* Create a jar archive, using the resources found at `rootPath` (a directory) and write the
* archive to `targetPath` (a file). Use `groupId`, `artifactId` and `version` to define
* the contents of the pom.properties file which is going to be added to the archive.
*/
export default async function jar({ groupId, artifactId, version, rootPath, targetPath }: JarArgs) {
await mkdir(dirname(targetPath), { recursive: true });
2023-04-02 22:49:12 +02:00
const asyncPathGeneratorFn = async function* (): ZipEntryGenerator {
for await (const fsPath of walk(rootPath)) {
const zipPath = relative(rootPath, fsPath).split(sep).join("/");
2023-04-02 22:49:12 +02:00
yield { fsPath, zipPath };
}
2023-04-02 22:49:12 +02:00
};
2023-04-02 22:49:12 +02:00
const zipFile = await jarStream({ groupId, artifactId, version, asyncPathGeneratorFn });
await new Promise<void>(async (resolve, reject) => {
2023-04-02 22:49:12 +02:00
zipFile.outputStream
.pipe(createWriteStream(targetPath, { encoding: "binary" }))
.on("close", () => resolve())
2023-04-02 22:49:12 +02:00
.on("error", e => reject(e));
});
}