extension: Convert tools to ESM

This commit is contained in:
relrelb 2022-11-04 18:11:33 +02:00 committed by relrelb
parent 8cbd527e4c
commit b52ae03759
4 changed files with 32 additions and 23 deletions

View File

@ -1,2 +1,5 @@
env: env:
node: true node: true
parserOptions:
ecmaVersion: 2022 # Needed for top-level-await.
sourceType: module

View File

@ -0,0 +1,3 @@
{
"type": "module"
}

View File

@ -1,4 +1,7 @@
const fs = require("fs"); import fs from "fs/promises";
import { createRequire } from "module";
import tempDir from "temp-dir";
import { signAddon } from "sign-addon";
async function sign( async function sign(
apiKey, apiKey,
@ -8,8 +11,6 @@ async function sign(
version, version,
destination destination
) { ) {
const { signAddon } = await import("sign-addon");
const tempDir = await import("temp-dir");
const result = await signAddon({ const result = await signAddon({
xpiPath: unsignedPath, xpiPath: unsignedPath,
version, version,
@ -26,8 +27,8 @@ async function sign(
if (result.downloadedFiles.length === 1) { if (result.downloadedFiles.length === 1) {
// Copy the downloaded file to the destination. // Copy the downloaded file to the destination.
// (Avoid `rename` because it fails if the destination is on a different drive.) // (Avoid `rename` because it fails if the destination is on a different drive.)
fs.copyFileSync(result.downloadedFiles[0], destination); await fs.copyFile(result.downloadedFiles[0], destination);
fs.unlinkSync(result.downloadedFiles[0]); await fs.unlink(result.downloadedFiles[0]);
} else { } else {
console.warn( console.warn(
"Unexpected downloads for signed Firefox extension, expected 1." "Unexpected downloads for signed Firefox extension, expected 1."
@ -36,12 +37,14 @@ async function sign(
} }
} }
(async () => { try {
if ( if (
process.env.MOZILLA_API_KEY && process.env.MOZILLA_API_KEY &&
process.env.MOZILLA_API_SECRET && process.env.MOZILLA_API_SECRET &&
process.env.FIREFOX_EXTENSION_ID process.env.FIREFOX_EXTENSION_ID
) { ) {
// TODO: Import as a JSON module once it becomes stable.
const require = createRequire(import.meta.url);
const { version } = require("../assets/manifest.json"); const { version } = require("../assets/manifest.json");
await sign( await sign(
process.env.MOZILLA_API_KEY, process.env.MOZILLA_API_KEY,
@ -56,8 +59,8 @@ async function sign(
"Skipping signing of Firefox extension. To enable this, please provide MOZILLA_API_KEY, MOZILLA_API_SECRET and FIREFOX_EXTENSION_ID environment variables" "Skipping signing of Firefox extension. To enable this, please provide MOZILLA_API_KEY, MOZILLA_API_SECRET and FIREFOX_EXTENSION_ID environment variables"
); );
} }
})().catch((error) => { } catch (error) {
console.error("Error while signing Firefox extension:"); console.error("Error while signing Firefox extension:");
console.error(error); console.error(error);
process.exit(-1); process.exit(-1);
}); }

View File

@ -1,11 +1,12 @@
const fs = require("fs"); import fs from "fs/promises";
const path = require("path"); import path from "path";
const archiver = require("archiver"); import url from "url";
import archiver from "archiver";
async function zip(source, destination) { async function zip(source, destination) {
fs.mkdirSync(path.dirname(destination), { recursive: true }); await fs.mkdir(path.dirname(destination), { recursive: true });
const output = fs.createWriteStream(destination); const output = (await fs.open(destination, "w")).createWriteStream();
const archive = archiver("zip", {}); const archive = archiver("zip");
output.on("close", () => { output.on("close", () => {
console.log( console.log(
@ -13,15 +14,15 @@ async function zip(source, destination) {
); );
}); });
archive.on("error", (err) => { archive.on("error", (error) => {
throw err; throw error;
}); });
archive.on("warning", (err) => { archive.on("warning", (error) => {
if (err.code === "ENOENT") { if (error.code === "ENOENT") {
console.warn(`Warning whilst zipping extension: ${err}`); console.warn(`Warning whilst zipping extension: ${error}`);
} else { } else {
throw err; throw error;
} }
}); });
@ -32,6 +33,5 @@ async function zip(source, destination) {
await archive.finalize(); await archive.finalize();
} }
(async () => { const assets = url.fileURLToPath(new URL("../assets/", import.meta.url));
await zip(path.resolve(__dirname, "../assets/"), process.argv[2]); await zip(assets, process.argv[2]);
})();