style: run prettier

This commit is contained in:
Waldemar Reusch 2023-04-02 22:49:12 +02:00
parent dcfefad17f
commit 5abbc7f9a7
2 changed files with 68 additions and 61 deletions

View File

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

View File

@ -5,74 +5,82 @@ import { Readable } from "stream";
type AsyncIterable<T> = { type AsyncIterable<T> = {
[Symbol.asyncIterator](): AsyncIterableIterator<T>; [Symbol.asyncIterator](): AsyncIterableIterator<T>;
} };
async function arrayFromAsync<T>(asyncIterable: AsyncIterable<T>) { async function arrayFromAsync<T>(asyncIterable: AsyncIterable<T>) {
const chunks: T[] = [] const chunks: T[] = [];
for await (const chunk of asyncIterable) chunks.push(chunk) for await (const chunk of asyncIterable) chunks.push(chunk);
return chunks return chunks;
} }
async function readToBuffer(stream: NodeJS.ReadableStream) { async function readToBuffer(stream: NodeJS.ReadableStream) {
return Buffer.concat(await arrayFromAsync(stream as AsyncIterable<Buffer>)) return Buffer.concat(await arrayFromAsync(stream as AsyncIterable<Buffer>));
} }
function unzip(buffer: Buffer) { function unzip(buffer: Buffer) {
return new Promise<ZipFile>((resolve, reject) => return new Promise<ZipFile>((resolve, reject) =>
fromBuffer(buffer, { lazyEntries: true }, (err, zipFile) => { fromBuffer(buffer, { lazyEntries: true }, (err, zipFile) => {
if (err !== null) { reject(err) } else { resolve(zipFile) } if (err !== null) {
})) reject(err);
} else {
resolve(zipFile);
}
})
);
} }
function readEntry(zipFile: ZipFile, entry: Entry): Promise<Readable> { function readEntry(zipFile: ZipFile, entry: Entry): Promise<Readable> {
return new Promise<Readable>((resolve, reject) => { return new Promise<Readable>((resolve, reject) => {
zipFile.openReadStream(entry, (err, stream) => { zipFile.openReadStream(entry, (err, stream) => {
if (err !== null) { reject(err) } else { resolve(stream) } if (err !== null) {
}) reject(err);
}) } else {
resolve(stream);
}
});
});
} }
function readAll(zipFile: ZipFile): Promise<Map<string, Buffer>> { function readAll(zipFile: ZipFile): Promise<Map<string, Buffer>> {
return new Promise<Map<string, Buffer>>((resolve, reject) => { return new Promise<Map<string, Buffer>>((resolve, reject) => {
const entries1: Map<string, Buffer> = new Map() const entries1: Map<string, Buffer> = new Map();
zipFile.on("entry", async (entry: Entry) => { zipFile.on("entry", async (entry: Entry) => {
const stream = await readEntry(zipFile, entry) const stream = await readEntry(zipFile, entry);
const buffer = await readToBuffer(stream) const buffer = await readToBuffer(stream);
entries1.set(entry.fileName, buffer) entries1.set(entry.fileName, buffer);
zipFile.readEntry() zipFile.readEntry();
}) });
zipFile.on("end", () => resolve(entries1)) zipFile.on("end", () => resolve(entries1));
zipFile.on("error", e => reject(e)) zipFile.on("error", e => reject(e));
zipFile.readEntry() zipFile.readEntry();
}) });
} }
describe("jar", () => { describe("jar", () => {
it("creates jar artifacts without error", async () => { it("creates jar artifacts without error", async () => {
async function* mockFiles(): ZipEntryGenerator { async function* mockFiles(): ZipEntryGenerator {
yield { zipPath: "foo", buffer: Buffer.from("foo") } yield { zipPath: "foo", buffer: Buffer.from("foo") };
} }
const opts = { artifactId: "someArtifactId", groupId: "someGroupId", version: "1.2.3", asyncPathGeneratorFn: mockFiles } const opts = { artifactId: "someArtifactId", groupId: "someGroupId", version: "1.2.3", asyncPathGeneratorFn: mockFiles };
const zipped = await jarStream(opts); const zipped = await jarStream(opts);
const buffered = await readToBuffer(zipped.outputStream) const buffered = await readToBuffer(zipped.outputStream);
const unzipped = await unzip(buffered) const unzipped = await unzip(buffered);
const entries = await readAll(unzipped) const entries = await readAll(unzipped);
assert.equal(entries.size, 3) assert.equal(entries.size, 3);
assert.isOk(entries.has("foo")) assert.isOk(entries.has("foo"));
assert.isOk(entries.has("META-INF/MANIFEST.MF")) assert.isOk(entries.has("META-INF/MANIFEST.MF"));
assert.isOk(entries.has("META-INF/maven/someGroupId/someArtifactId/pom.properties")) assert.isOk(entries.has("META-INF/maven/someGroupId/someArtifactId/pom.properties"));
assert.equal("foo", entries.get("foo")?.toString("utf-8")) assert.equal("foo", entries.get("foo")?.toString("utf-8"));
const manifest = entries.get("META-INF/MANIFEST.MF")?.toString("utf-8") const manifest = entries.get("META-INF/MANIFEST.MF")?.toString("utf-8");
const pomProperties = entries.get("META-INF/maven/someGroupId/someArtifactId/pom.properties")?.toString("utf-8") const pomProperties = entries.get("META-INF/maven/someGroupId/someArtifactId/pom.properties")?.toString("utf-8");
assert.isOk(manifest?.includes("Created-By: Keycloakify"))
assert.isOk(pomProperties?.includes("1.2.3"))
assert.isOk(pomProperties?.includes("someGroupId"))
assert.isOk(pomProperties?.includes("someArtifactId"))
assert.isOk(manifest?.includes("Created-By: Keycloakify"));
assert.isOk(pomProperties?.includes("1.2.3"));
assert.isOk(pomProperties?.includes("someGroupId"));
assert.isOk(pomProperties?.includes("someArtifactId"));
}); });
}); });