Skip to content

Commit

Permalink
Rewrite build script to js
Browse files Browse the repository at this point in the history
ZipFile committed Aug 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a4f0be7 commit 86dde75
Showing 5 changed files with 50 additions and 54 deletions.
42 changes: 42 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env node

import { argv, env } from "node:process";
import { cp, mkdir, rm } from "fs/promises";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { chromeifyManifestFile } from "./chromeifyManifest.impl.js";
import webExt from "web-ext";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const __main = argv[1] === __filename;
const distDir = join(__dirname, "dist");
const distManifest = join(distDir, "manifest.json");
const srcDir = join(__dirname, "src");
const chrome = argv.includes("--chrome") || /yes|true|1/i.test(env.CHROME || "");

async function main() {
await rm(distDir, {recursive: true, force: true});
await mkdir(distDir);
await cp(srcDir, distDir, {recursive: true});

let filename;

if (chrome) {
await chromeifyManifestFile(distManifest);
filename = "{name}-{version}-chrome.zip";
} else {
filename = "{name}-{version}.zip";
}

await webExt.cmd.build({
artifactsDir: "web-ext-artifacts",
sourceDir: distDir,
overwriteDest: true,
filename: filename,
});
}

if (__main) {
main().catch(console.error);
}
19 changes: 0 additions & 19 deletions build.sh

This file was deleted.

27 changes: 6 additions & 21 deletions chromeifyManifest.impl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { readFile, writeFile } from "fs/promises";

export function chromeifyManifest(manifest) {
manifest["manifest_version"] = 3;
manifest["minimum_chrome_version"] = "97";
@@ -16,26 +18,9 @@ export function chromeifyManifest(manifest) {
return manifest;
}

export function parseArgs(args) {
switch (args.length) {
case 0:
return ["-", "-"];
case 1:
return [args[0], "-"];
default:
return [args[0], args[1]];
}
}

export function transformStream(instream, outstream) {
const chunks = [];

instream.on("data", (data) => chunks.push(data));
instream.on("end", function() {
const manifest = chromeifyManifest(JSON.parse(chunks.join("")));
export async function chromeifyManifestFile(path) {
const options = {"encoding": "utf-8"};
const manifest = chromeifyManifest(JSON.parse(await readFile(path, options)));

outstream.write(JSON.stringify(manifest, null, 4));
outstream.end("\n");
});
instream.resume();
await writeFile(path, JSON.stringify(manifest, null, 4), options);
}
12 changes: 0 additions & 12 deletions chromeifyManifest.js

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@
"scripts": {
"test": "mocha",
"coverage": "c8 mocha",
"build": "./build.sh",
"buildChrome": "CHROME=yes ./build.sh"
"build": "./build.js",
"buildChrome": "./build.js --chrome"
},
"exports": {
"./*": "./src/*"

0 comments on commit 86dde75

Please sign in to comment.