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 trimIndent from "./trimIndent";
export type ZipEntry = { zipPath: string } & ({ fsPath: string } | { buffer: Buffer })
export type ZipEntryGenerator = AsyncGenerator<ZipEntry, void, unknown>
export type ZipEntry = { zipPath: string } & ({ fsPath: string } | { buffer: Buffer });
export type ZipEntryGenerator = AsyncGenerator<ZipEntry, void, unknown>;
type CommonJarArgs = {
groupId: string;
artifactId: string;
version: string;
}
};
export type JarStreamArgs = CommonJarArgs & {
asyncPathGeneratorFn(): ZipEntryGenerator
}
asyncPathGeneratorFn(): ZipEntryGenerator;
};
export type JarArgs = CommonJarArgs & {
targetPath: string;
rootPath: string;
};
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`
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Keycloakify
Built-By: unknown
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`
# Generated by keycloakify
# ${new Date()}
artifactId=${artifactId}
groupId=${groupId}
version=${version}
`)
`);
const zipFile = new ZipFile()
const zipFile = new ZipFile();
for await (const entry of asyncPathGeneratorFn()) {
if ("buffer" in entry) {
zipFile.addBuffer(entry.buffer, entry.zipPath)
zipFile.addBuffer(entry.buffer, entry.zipPath);
} else if ("fsPath" in entry) {
zipFile.addFile(entry.fsPath, entry.zipPath)
zipFile.addFile(entry.fsPath, entry.zipPath);
}
}
zipFile.addBuffer(manifestData, manifestPath)
zipFile.addBuffer(pomPropsData, pomPropsPath)
zipFile.addBuffer(manifestData, manifestPath);
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
* 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) {
await mkdir(dirname(targetPath), { recursive: true });
const asyncPathGeneratorFn = (async function* (): ZipEntryGenerator {
const asyncPathGeneratorFn = async function* (): ZipEntryGenerator {
for await (const fsPath of walk(rootPath)) {
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) => {
zipFile.outputStream.pipe(createWriteStream(targetPath, { encoding: "binary" }))
zipFile.outputStream
.pipe(createWriteStream(targetPath, { encoding: "binary" }))
.on("close", () => resolve())
.on("error", e => reject(e))
.on("error", e => reject(e));
});
}