From cf26b773855bf713be484809246419b9cb971b22 Mon Sep 17 00:00:00 2001 From: Louis <6723574+louisgv@users.noreply.github.com> Date: Mon, 21 Nov 2022 00:10:09 -0400 Subject: [PATCH] Adding package cmd --- cli/plasmo/src/commands/build.ts | 22 ++------------ cli/plasmo/src/commands/package.ts | 22 ++++++++++++++ .../src/features/manifest-factory/zip.ts | 30 +++++++++++++++++++ 3 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 cli/plasmo/src/commands/package.ts create mode 100644 cli/plasmo/src/features/manifest-factory/zip.ts diff --git a/cli/plasmo/src/commands/build.ts b/cli/plasmo/src/commands/build.ts index df0b1d452..713dfd78a 100644 --- a/cli/plasmo/src/commands/build.ts +++ b/cli/plasmo/src/commands/build.ts @@ -10,6 +10,7 @@ import { nextNewTab } from "~features/extra/next-new-tab" import { createParcelBuilder } from "~features/helpers/create-parcel-bundler" import { printHeader } from "~features/helpers/print" import { createManifest } from "~features/manifest-factory/create-manifest" +import { zipBundle } from "~features/manifest-factory/zip" async function build() { printHeader() @@ -29,8 +30,7 @@ async function build() { const plasmoManifest = await createManifest(bundleConfig) - const { distDirectory, buildDirectory, distDirectoryName } = - plasmoManifest.commonPath + const { distDirectory } = plasmoManifest.commonPath const bundler = await createParcelBuilder(plasmoManifest.commonPath, { mode: "production", @@ -54,23 +54,7 @@ async function build() { await plasmoManifest.postBuild() if (hasFlag("--zip")) { - const { default: archiver } = await import("archiver") - const zip = archiver("zip", { - zlib: { level: 9 } - }) - - const zipFilePath = resolve(buildDirectory, `${distDirectoryName}.zip`) - - const output = createWriteStream(zipFilePath) - output.on("close", () => { - iLog(`Zip Package size: ${zip.pointer()} bytes`) - }) - - zip.pipe(output) - - zip.directory(distDirectory, "") - - await zip.finalize() + await zipBundle(plasmoManifest.commonPath) } } diff --git a/cli/plasmo/src/commands/package.ts b/cli/plasmo/src/commands/package.ts new file mode 100644 index 000000000..8504c9bc2 --- /dev/null +++ b/cli/plasmo/src/commands/package.ts @@ -0,0 +1,22 @@ +import { iLog } from "@plasmo/utils/logging" + +import { getBundleConfig } from "~features/extension-devtools/get-bundle-config" +import { printHeader } from "~features/helpers/print" +import { createManifest } from "~features/manifest-factory/create-manifest" +import { zipBundle } from "~features/manifest-factory/zip" + +async function packageCmd() { + printHeader() + + process.env.NODE_ENV = "production" + + iLog("Prepare to package the extension bundle...") + + const bundleConfig = getBundleConfig() + + const plasmoManifest = await createManifest(bundleConfig) + + await zipBundle(plasmoManifest.commonPath) +} + +export default packageCmd diff --git a/cli/plasmo/src/features/manifest-factory/zip.ts b/cli/plasmo/src/features/manifest-factory/zip.ts new file mode 100644 index 000000000..fecd939b9 --- /dev/null +++ b/cli/plasmo/src/features/manifest-factory/zip.ts @@ -0,0 +1,30 @@ +import { createWriteStream } from "fs" +import { resolve } from "path" + +import { iLog } from "@plasmo/utils/logging" + +import type { CommonPath } from "~features/extension-devtools/common-path" + +export const zipBundle = async ({ + distDirectory, + buildDirectory, + distDirectoryName +}: CommonPath) => { + const { default: archiver } = await import("archiver") + const zipClient = archiver("zip", { + zlib: { level: 9 } + }) + + const zipFilePath = resolve(buildDirectory, `${distDirectoryName}.zip`) + + const output = createWriteStream(zipFilePath) + output.on("close", () => { + iLog(`Zip Package size: ${zipClient.pointer()} bytes`) + }) + + zipClient.pipe(output) + + zipClient.directory(distDirectory, "") + + await zipClient.finalize() +}