From deb666116ad18273c36c46bf0400072f332bf891 Mon Sep 17 00:00:00 2001 From: Matt Gaunt-Seo Date: Wed, 21 Feb 2024 21:17:53 -0800 Subject: [PATCH] Correct build for zip --- .github/workflows/publish.yml | 2 +- vite.config.ts | 217 +++++++++++++++++----------------- 2 files changed, 110 insertions(+), 109 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 6f3458a..a365638 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -70,7 +70,7 @@ jobs: run: | pnpm run chrome-webstore-upload upload \ --auto-publish \ - --source ./dist \ + --source gauntface-pin-it-extension.zip \ --extension-id $EXTENSION_ID \ --client-id $CLIENT_ID \ --client-secret $CLIENT_SECRET \ diff --git a/vite.config.ts b/vite.config.ts index eb63d71..be3b485 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -7,6 +7,111 @@ import { createWriteStream } from "fs"; import { execSync } from "node:child_process"; import archiver from "archiver"; +async function writeManifest() { + console.log("Setting up extension manifest"); + const manifestBuffer = await readFile( + resolve(__dirname, "src/manifest.json"), + ); + const manifest = JSON.parse(manifestBuffer.toString()); + + if (process.env.NODE_ENV !== "production") { + manifest.key = "developmentkalohmonpfgdhimepifhl"; + manifest.name = `Dev: ${manifest.name}`; + for (const k of Object.keys(manifest.icons)) { + const parts = manifest.icons[k].split("."); + manifest.icons[k] = `${parts[0]}-dev.${parts[1]}`; + } + for (const k of Object.keys(manifest.action.default_icon)) { + const parts = manifest.action.default_icon[k].split("."); + manifest.action.default_icon[k] = `${parts[0]}-dev.${parts[1]}`; + } + } else { + const newVersion = semver.parse(process.env.EXTENSION_VERSION); + if (!newVersion) { + throw new Error( + `Version could not be parsed by semver: '${process.env.EXTENSION_VERSION}'`, + ); + } + manifest.version = newVersion.version; + } + + await writeFile( + resolve(__dirname, "dist/manifest.json"), + JSON.stringify(manifest, null, 2), + ); + console.log(`written manifest`); +} + +function injectSentryDebugIDs() { + console.log("Injecting sentry sourcemaps"); + const sentryCli = resolve(__dirname, "node_modules/.bin/sentry-cli"); + execSync(`${sentryCli} sourcemaps inject ${resolve(__dirname, "dist")}`); +} + +async function bundleExtension() { + console.log("Bundling zip"); + const zipPath = join(resolve(__dirname), "gauntface-pin-it-extension.zip"); + try { + await rm(zipPath); + } catch (e) { + // NOOP + } + + const output = createWriteStream(zipPath); + // eslint-disable-next-line new-cap + const archive = new archiver("zip", { + zlib: { + // Sets the compression level + level: 9, + }, + }); + + const distDir = resolve(__dirname, "dist"); + + try { + await new Promise((resolve, reject) => { + output.on("close", () => { + resolve(null); + }); + + output.on("end", () => { + console.log("Output End event"); + }); + + // good practice to catch warnings (ie stat failures and other + // non-blocking errors) + archive.on("warning", (err) => { + if (err.code === "ENOENT") { + console.warn(`Archiver warning: ${err.message}`); + } else { + reject(err); + } + }); + + // good practice to catch this error explicitly + archive.on("error", (err) => { + reject(err); + }); + + // pipe archive data to the file + archive.pipe(output); + + // append files from a sub-directory and naming it `gauntface-pin-it-extension` + // within the archive + archive.directory(distDir, false); + + // finalize the archive (ie we are done appending files but streams have to + // finish yet) + // 'close', 'end' or 'finish' may be fired right after calling this method + // so register to them beforehand + archive.finalize(); + }); + } catch (err) { + console.error(`Error while zipping: ${err}`); + throw err; + } +} + // https://vitejs.dev/config/ export default defineConfig({ build: { @@ -28,115 +133,11 @@ export default defineConfig({ plugins: [ svelte(), { - name: "chrome-extension-manifest", + name: "bundle-extension", closeBundle: async () => { - const manifestBuffer = await readFile( - resolve(__dirname, "src/manifest.json"), - ); - const manifest = JSON.parse(manifestBuffer.toString()); - - if (process.env.NODE_ENV !== "production") { - manifest.key = "developmentkalohmonpfgdhimepifhl"; - manifest.name = `Dev: ${manifest.name}`; - for (const k of Object.keys(manifest.icons)) { - const parts = manifest.icons[k].split("."); - manifest.icons[k] = `${parts[0]}-dev.${parts[1]}`; - } - for (const k of Object.keys(manifest.action.default_icon)) { - const parts = manifest.action.default_icon[k].split("."); - manifest.action.default_icon[k] = `${parts[0]}-dev.${parts[1]}`; - } - } else { - const newVersion = semver.parse(process.env.EXTENSION_VERSION); - if (!newVersion) { - throw new Error( - `Version could not be parsed by semver: '${process.env.EXTENSION_VERSION}'`, - ); - } - manifest.version = newVersion.version; - } - - await writeFile( - resolve(__dirname, "dist/manifest.json"), - JSON.stringify(manifest, null, 2), - ); - }, - }, - { - name: "sentry-sourcemaps-inject", - closeBundle: async () => { - const sentryCli = resolve(__dirname, "node_modules/.bin/sentry-cli"); - execSync( - `${sentryCli} sourcemaps inject ${resolve(__dirname, "dist")}`, - ); - }, - }, - { - name: "bundle-zip", - closeBundle: async () => { - const zipPath = join( - resolve(__dirname), - "gauntface-pin-it-extension.zip", - ); - try { - await rm(zipPath); - } catch (e) { - // NOOP - } - - const output = createWriteStream(zipPath); - // eslint-disable-next-line new-cap - const archive = new archiver("zip", { - zlib: { - // Sets the compression level - level: 9, - }, - }); - - const distDir = resolve(__dirname, "dist"); - - try { - await new Promise((resolve, reject) => { - output.on("close", () => { - resolve(null); - }); - - output.on("end", () => { - console.log("Output End event"); - }); - - // good practice to catch warnings (ie stat failures and other - // non-blocking errors) - archive.on("warning", (err) => { - if (err.code === "ENOENT") { - console.warn(`Archiver warning: ${err.message}`); - } else { - reject(err); - } - }); - - // good practice to catch this error explicitly - archive.on("error", (err) => { - reject(err); - }); - - // pipe archive data to the file - archive.pipe(output); - - // append files from a sub-directory and naming it `gauntface-pin-it-extension` - // within the archive - archive.directory(distDir, "gauntface-pin-it-extension"); - - // finalize the archive (ie we are done appending files but streams have to - // finish yet) - // 'close', 'end' or 'finish' may be fired right after calling this method - // so register to them beforehand - archive.finalize(); - }); - } catch (err) { - console.error(`Error while zipping: ${err}`); - throw err; - } + await writeManifest(); + await injectSentryDebugIDs(); + await bundleExtension(); }, }, ],