Skip to content

Commit

Permalink
feat: adding package cmd (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
louisgv authored Nov 21, 2022
1 parent 7ba2f2a commit 05c1fe8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
22 changes: 3 additions & 19 deletions cli/plasmo/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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",
Expand All @@ -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)
}
}

Expand Down
22 changes: 22 additions & 0 deletions cli/plasmo/src/commands/package.ts
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions cli/plasmo/src/features/manifest-factory/zip.ts
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 05c1fe8

Please sign in to comment.