Skip to content

Commit

Permalink
Add build option to package using system utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mstieranka committed Oct 22, 2023
1 parent 11009a3 commit cedfddf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
25 changes: 16 additions & 9 deletions scripts/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ const MANIFEST_TARGET = `${BUILD_DIR}/manifest.json` as const;
const FIREFOX_DIST_DIR = `${OUT_DIR}/firefox` as const;
const CHROME_DIST_DIR = `${OUT_DIR}/chrome` as const;

async function pack(distDir: string) {
async function pack(distDir: string, useSystemUtilities: boolean) {
await rm(distDir, { recursive: true });
await mkdir(distDir);
await zipDirectory(BUILD_DIR, `${distDir}/progtest_themes.zip`);
await zipDirectory(BUILD_DIR, `${distDir}/progtest_themes.zip`, {
useSystemUtilities,
});
}

async function signFirefox() {
Expand Down Expand Up @@ -44,39 +46,43 @@ async function signFirefox() {
);
}

async function packChrome() {
async function packChrome(useSystemUtilities: boolean) {
cp(CHROME_MANIFEST, MANIFEST_TARGET);
pack(CHROME_DIST_DIR);
pack(CHROME_DIST_DIR, useSystemUtilities);
}

async function packFirefox() {
async function packFirefox(useSystemUtilities: boolean) {
cp(FIREFOX_MANIFEST, MANIFEST_TARGET);
pack(FIREFOX_DIST_DIR);
pack(FIREFOX_DIST_DIR, useSystemUtilities);
}

async function main() {
const args = minimist(process.argv.slice(2), {
boolean: ["chrome", "firefox", "sign"],
boolean: ["chrome", "firefox", "sign", "system"],
default: {
chrome: false,
firefox: false,
sign: false,
system: false,
},
});

if (args.chrome) {
if (args.sign) {
throw new Error("Cannot sign Chrome extension");
}
await packChrome();
await packChrome(args.system);
return;
}

if (args.firefox) {
if (args.sign) {
if (args.system) {
throw new Error("System utilities cannot be used with signing");
}
await signFirefox();
} else {
await packFirefox();
await packFirefox(args.system);
}
return;
}
Expand All @@ -85,6 +91,7 @@ async function main() {
"Use '--chrome' or '--firefox' to build a specific browser extension",
);
console.log("Use '--firefox --sign' to create a signed Firefox extension");
console.log("Use '--system' to use system utilities for zipping");
}

main();
48 changes: 32 additions & 16 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import archiver from "archiver";
import { cp, exists, mkdir, readdir, stat } from "fs/promises";
import { cp, rm, exists, mkdir, readdir, stat } from "fs/promises";
import { createWriteStream } from "fs";
import { BUILD_DIR } from "./constants";

export async function* walkDir(dir: string): AsyncGenerator<string> {
const files = await readdir(dir);
Expand Down Expand Up @@ -47,29 +48,44 @@ export async function copyDirectory(
}
}

export function zipDirectory(
export async function zipDirectory(
sourceDir: string,
outPath: string,
options?: {
zipDestPath?: string;
useSystemUtilities?: boolean;
},
) {
console.log(`Zipping ${sourceDir} to ${outPath}`);
const archive = archiver("zip", { zlib: { level: 9 } });
if (options?.useSystemUtilities) {
console.info("Zipping with system utilities");
await rm(outPath, { force: true });
// . + ./ => ../
const proc = Bun.spawn(["zip", "-r", `.${outPath}`, "."], {
cwd: BUILD_DIR,
});
await proc.exited;
const output = await new Response(proc.stdout).text();
const error = await new Response(proc.stderr).text();
console.log(output);
console.error(error);
} else {
console.log(`Zipping ${sourceDir} to ${outPath}`);
const archive = archiver("zip", { zlib: { level: 9 } });

const stream = createWriteStream(outPath);
stream.on("error", (err) => {
console.error(err);
throw err;
});

archive
.on("error", (err) => {
const stream = createWriteStream(outPath);
stream.on("error", (err) => {
console.error(err);
throw err;
})
.directory(sourceDir, options?.zipDestPath ?? false)
.pipe(stream);
});

archive
.on("error", (err) => {
console.error(err);
throw err;
})
.directory(sourceDir, options?.zipDestPath ?? false)
.pipe(stream);

return archive.finalize();
await archive.finalize();
}
}

0 comments on commit cedfddf

Please sign in to comment.