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:
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(
apiKey,
@ -8,8 +11,6 @@ async function sign(
version,
destination
) {
const { signAddon } = await import("sign-addon");
const tempDir = await import("temp-dir");
const result = await signAddon({
xpiPath: unsignedPath,
version,
@ -26,8 +27,8 @@ async function sign(
if (result.downloadedFiles.length === 1) {
// Copy the downloaded file to the destination.
// (Avoid `rename` because it fails if the destination is on a different drive.)
fs.copyFileSync(result.downloadedFiles[0], destination);
fs.unlinkSync(result.downloadedFiles[0]);
await fs.copyFile(result.downloadedFiles[0], destination);
await fs.unlink(result.downloadedFiles[0]);
} else {
console.warn(
"Unexpected downloads for signed Firefox extension, expected 1."
@ -36,12 +37,14 @@ async function sign(
}
}
(async () => {
try {
if (
process.env.MOZILLA_API_KEY &&
process.env.MOZILLA_API_SECRET &&
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");
await sign(
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"
);
}
})().catch((error) => {
} catch (error) {
console.error("Error while signing Firefox extension:");
console.error(error);
process.exit(-1);
});
}

View File

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