Skip to content

Commit

Permalink
feat: --tag parameter #196 (#282)
Browse files Browse the repository at this point in the history
* --tag implementation

* revert utils

* use app-specific flag map

* remove unused import

Co-authored-by: Louis <[email protected]>
  • Loading branch information
imbdb and louisgv authored Nov 5, 2022
1 parent 6a9d52e commit f4ed50f
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 49 deletions.
2 changes: 1 addition & 1 deletion cli/plasmo/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function dev() {
}

if (event.type === "buildSuccess") {
iLog(`✨ Extension reloaded in ${event.buildTime}ms!`)
aLog(`✨ Extension re-packaged in ${event.buildTime}ms!`)
await plasmoManifest.postBuild()
} else if (event.type === "buildFailure") {
event.diagnostics.forEach((diagnostic) => {
Expand Down
28 changes: 8 additions & 20 deletions cli/plasmo/src/features/extension-devtools/common-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,28 @@ import { existsSync } from "fs"
import { basename, resolve } from "path"
import { cwd } from "process"

import { getFlag } from "@plasmo/utils"
import { flagMap } from "~features/helpers/flag"

import { getBundleConfig } from "./get-bundle-config"

export const getCommonPath = (
projectDirectory = cwd(),
{ target } = getBundleConfig()
) => {
export const getCommonPath = (projectDirectory = cwd()) => {
process.env.PLASMO_PROJECT_DIR = projectDirectory

const packageName = basename(projectDirectory)

process.env.PLASMO_SRC_PATH =
getFlag("--src-path") || process.env.PLASMO_SRC_PATH || "src"
process.env.PLASMO_SRC_PATH = flagMap.srcPath

const srcDirectory = resolve(projectDirectory, process.env.PLASMO_SRC_PATH)
const srcDirectory = resolve(projectDirectory, flagMap.srcPath)

process.env.PLASMO_SRC_DIR = existsSync(srcDirectory)
? srcDirectory
: projectDirectory

process.env.PLASMO_BUILD_PATH =
getFlag("--build-path") || process.env.PLASMO_BUILD_PATH || "build"
process.env.PLASMO_BUILD_PATH = flagMap.buildPath

const buildDirectory = resolve(
projectDirectory,
process.env.PLASMO_BUILD_PATH
)
const buildDirectory = resolve(projectDirectory, flagMap.buildPath)

process.env.PLASMO_BUILD_DIR = buildDirectory

const distDirectoryName = `${target}-${
process.env.NODE_ENV === "production" ? "prod" : "dev"
}`
const distDirectoryName = `${flagMap.target}-${flagMap.tag}`

const distDirectory = resolve(buildDirectory, distDirectoryName)

Expand Down Expand Up @@ -65,7 +53,7 @@ export const getCommonPath = (
genAssetsDirectory: resolve(dotPlasmoDirectory, "gen-assets"),
entryManifestPath: resolve(
dotPlasmoDirectory,
`${target}.plasmo.manifest.json`
`${flagMap.target}.plasmo.manifest.json`
)
}
}
Expand Down
18 changes: 15 additions & 3 deletions cli/plasmo/src/features/extension-devtools/generate-icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import sharp from "sharp"

import { vLog, wLog } from "@plasmo/utils"

import { flagMap } from "~features/helpers/flag"

import type { CommonPath } from "./common-path"

const getIconNameVariants = (size = 512 as string | number, name = "icon") => [
Expand All @@ -19,10 +21,21 @@ const baseIconNames = [
...getIconNameVariants(1024)
]

// We pick env based icon first, then plain icon
/**
* We pick icon in this order
* 1. tag based icon
* 2. env and tag based icon
* 3. plain icon
*
* */
const getPrioritizedIconPaths = (iconNames = baseIconNames) =>
iconNames
.map((name) => [`${name}.${process.env.NODE_ENV}.png`, `${name}.png`])
.map((name) => [
`${name}.${flagMap.tag}.${process.env.NODE_ENV}.png`,
`${name}.${process.env.NODE_ENV}.png`,
`${name}.${flagMap.tag}.png`,
`${name}.png`
])
.flat()

// Use this to cache the path resolving result
Expand Down Expand Up @@ -73,7 +86,6 @@ export async function generateIcons({
}

const devProvidedIcon = iconState.devProvidedIcons[width].find(existsSync)

const generatedIconPath = resolve(
genAssetsDirectory,
`icon${width}.plasmo.png`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { paramCase } from "change-case"

import { getFlag } from "@plasmo/utils"
import { flagMap } from "~features/helpers/flag"

export const getBundleConfig = () => {
const target = paramCase(getFlag("--target") || "chrome-mv3")
const target = flagMap.target

const [browser, manifestVersion] = target.split("-")

const tag = flagMap.tag

return {
tag,
target,
browser,
manifestVersion
Expand Down
4 changes: 4 additions & 0 deletions cli/plasmo/src/features/extension-devtools/load-env-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { join } from "path"

import { eLog, iLog } from "@plasmo/utils"

import { flagMap } from "~features/helpers/flag"

export type Env = Record<string, string | undefined>
export type LoadedEnvFiles = Array<{
path: string
Expand Down Expand Up @@ -73,11 +75,13 @@ function processEnv(loadedEnvFiles: LoadedEnvFiles, dir?: string) {
export async function loadEnvConfig(dir: string) {
const mode = process.env.NODE_ENV
const dotenvFilePaths = [
`.env.${flagMap.tag}.local`,
`.env.${mode}.local`,
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
mode !== "test" ? `.env.local` : "",
`.env.${flagMap.tag}`,
`.env.${mode}`,
".env"
]
Expand Down
45 changes: 45 additions & 0 deletions cli/plasmo/src/features/helpers/flag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { paramCase } from "change-case"

import { flagsHelp as baseFlagHelp, getFlag } from "@plasmo/utils"

const srcPath = getFlag("--src-path") || process.env.PLASMO_SRC_PATH || "src"

const buildPath =
getFlag("--build-path") || process.env.PLASMO_BUILD_PATH || "build"

const tag =

This comment has been minimized.

Copy link
@yehuachun

yehuachun Nov 8, 2022

I think someone got operator precedence wrong here. The value of tag can only be "prod" or "dev". getFlag("--tag") and process.env.PLASMO_TAG does not work as expected. Or maybe it should be like this
const tag = getFlag("--tag") || process.env.PLASMO_TAG || (process.env.NODE_ENV === "production" ? "prod" : "dev")

This comment has been minimized.

Copy link
@louisgv

louisgv Nov 8, 2022

Author Contributor

You're absolutely right! Pushing a fix soon :D

getFlag("--tag") ||
process.env.PLASMO_TAG ||
process.env.NODE_ENV === "production"
? "prod"
: "dev"

const target = paramCase(
getFlag("--target") || process.env.PLASMO_TARGET || "chrome-mv3"
)

const entry = getFlag("--entry") || "popup"

export const flagMap = {
tag,
srcPath,
buildPath,
target,
entry
}

export const flagHelp = `
init
--entry entry files (default: popup)
--with-<name> use an example template
dev/build
--target [string] set the target (default: chrome-mv3)
--tag [string] set the build tag (default: dev or prod depending on NODE_ENV)
--src-path [path] set the source path relative to project root (default: src)
--build-path [path] set the build path relative to project root (default: build)
--entry entry point name (default: popup)
`
9 changes: 7 additions & 2 deletions cli/plasmo/src/features/helpers/print.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { cLog } from "@plasmo/utils"

import { validCommandList } from "~commands"
import { flagHelp } from "~features/helpers/flag"

export const printHeader = () => {
console.log(`🟣 Plasmo v${process.env.APP_VERSION}`)

console.log("🟠 The browser extension development framework.")
console.log("🔴 The Browser Extension Framework")
}

export const printHelp = () => cLog("🟡 MODES", validCommandList.join(" | "))
export const printHelp = () => {
cLog("🟠 CMDS", validCommandList.join(" | "))

cLog("🟡 OPTS", flagHelp)
}
28 changes: 9 additions & 19 deletions cli/plasmo/src/features/manifest-factory/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,21 @@ export const iconMap = {
export const autoPermissionList: ManifestPermission[] = ["storage"]

export abstract class BaseFactory<T extends ExtensionManifest = any> {
#bundleConfig: PlasmoBundleConfig
get browser() {
return this.#bundleConfig.browser
return this.bundleConfig.browser
}

#commonPath?: CommonPath
public get commonPath(): CommonPath {
public get commonPath() {
return assertTruthy(this.#commonPath)
}

#projectPath?: ProjectPath
public get projectPath(): ProjectPath {
public get projectPath() {
return assertTruthy(this.#projectPath)
}

#templatePath: TemplatePath
public get templatePath(): TemplatePath {
return this.#templatePath
}
readonly templatePath = getTemplatePath()

#envConfig?: EnvConfig

Expand Down Expand Up @@ -135,10 +131,7 @@ export abstract class BaseFactory<T extends ExtensionManifest = any> {

protected copyQueue: Array<[string, string]> = []

#scaffolder: Scaffolder
get scaffolder() {
return this.#scaffolder
}
readonly scaffolder = new Scaffolder(this)

get changed() {
return this.#hash !== this.#prevHash
Expand All @@ -164,11 +157,8 @@ export abstract class BaseFactory<T extends ExtensionManifest = any> {
return resolve(this.templatePath.staticTemplatePath, this.uiLibrary.path)
}

protected constructor(bundleConfig: PlasmoBundleConfig) {
protected constructor(private bundleConfig: PlasmoBundleConfig) {
this.data.icons = iconMap
this.#bundleConfig = bundleConfig
this.#templatePath = getTemplatePath()
this.#scaffolder = new Scaffolder(this)
}

async startup() {
Expand All @@ -188,7 +178,7 @@ export abstract class BaseFactory<T extends ExtensionManifest = any> {

async updateEnv(envRootDirectory = this.commonPath.projectDirectory) {
this.#envConfig = await loadEnvConfig(envRootDirectory)
this.#commonPath = getCommonPath(envRootDirectory, this.#bundleConfig)
this.#commonPath = getCommonPath(envRootDirectory)
}

// https://github.com/PlasmoHQ/plasmo/issues/195
Expand Down Expand Up @@ -328,7 +318,7 @@ export abstract class BaseFactory<T extends ExtensionManifest = any> {
const parsedModulePath = parse(modulePath)
scriptPath = relative(
this.commonPath.dotPlasmoDirectory,
await this.#scaffolder.createContentScriptMount(parsedModulePath)
await this.scaffolder.createContentScriptMount(parsedModulePath)
)
}

Expand Down Expand Up @@ -391,7 +381,7 @@ export abstract class BaseFactory<T extends ExtensionManifest = any> {

const parsedModulePath = parse(modulePath)

await this.#scaffolder.createPageMount(parsedModulePath)
await this.scaffolder.createPageMount(parsedModulePath)
}

this.#hash = ""
Expand Down

0 comments on commit f4ed50f

Please sign in to comment.