From ba07cd2f92628c02ec5e039f3bed05825913e1e9 Mon Sep 17 00:00:00 2001 From: Craig Slusher Date: Thu, 7 Sep 2023 11:27:25 -0400 Subject: [PATCH 1/3] Introduces JSON type to non-public env --- cli/plasmo/package.json | 1 - cli/plasmo/src/features/env/env-config.ts | 39 +++++++++++++---------- pnpm-lock.yaml | 8 ----- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/cli/plasmo/package.json b/cli/plasmo/package.json index 4bb388652..d88430877 100644 --- a/cli/plasmo/package.json +++ b/cli/plasmo/package.json @@ -49,7 +49,6 @@ "chalk": "5.3.0", "change-case": "4.1.2", "dotenv": "16.3.1", - "dotenv-expand": "10.0.0", "events": "3.3.0", "fast-glob": "3.3.1", "fflate": "0.8.0", diff --git a/cli/plasmo/src/features/env/env-config.ts b/cli/plasmo/src/features/env/env-config.ts index 59e165763..cb608889f 100644 --- a/cli/plasmo/src/features/env/env-config.ts +++ b/cli/plasmo/src/features/env/env-config.ts @@ -3,7 +3,6 @@ import { readFile } from "fs/promises" import { resolve } from "path" import { constantCase } from "change-case" import dotenv from "dotenv" -import { expand as dotenvExpand } from "dotenv-expand" import { isFile, isReadable } from "@plasmo/utils/fs" import { eLog, iLog, vLog } from "@plasmo/utils/logging" @@ -16,6 +15,7 @@ type LoadedEnvFiles = Array<{ contents: string }> +const JSON_DIRECTIVE_REGEX = /^\s*~/ export const INTERNAL_ENV_PREFIX = "PLASMO_" export const PUBLIC_ENV_PREFIX = "PLASMO_PUBLIC_" @@ -50,22 +50,17 @@ function cascadeEnv(loadedEnvFiles: LoadedEnvFiles) { for (const { contents, name } of loadedEnvFiles) { try { envFileSet.add(name) - const result = dotenvExpand({ - parsed: dotenv.parse(contents) - }) - - if (!!result.parsed) { - vLog(`Loaded env from ${name}`) - const resultData = result.parsed || {} - - for (const envKey of Object.keys(resultData)) { - if (typeof parsed[envKey] === "undefined") { - parsed[envKey] = resultData[envKey] - - // Pass through internal env variables - if (envKey.startsWith(INTERNAL_ENV_PREFIX)) { - process.env[envKey] = resultData[envKey] - } + + vLog(`Loaded env from ${name}`) + const resultData: dotenv.DotenvParseOutput = dotenv.parse(contents) || {} + + for (const [envKey, envValue] of Object.entries(resultData)) { + if (typeof parsed[envKey] === "undefined") { + parsed[envKey] = maybeParseJSON(envValue) + + // Pass through internal env variables + if (envKey.startsWith(INTERNAL_ENV_PREFIX)) { + process.env[envKey] = envValue } } } @@ -77,6 +72,16 @@ function cascadeEnv(loadedEnvFiles: LoadedEnvFiles) { return parsed } +function isJSONDirective(value: string): boolean { + return JSON_DIRECTIVE_REGEX.test(value); +} + +function maybeParseJSON(value: string): any { + return isJSONDirective(value) + ? JSON.parse(value.replace(JSON_DIRECTIVE_REGEX, "")) + : value; +} + export const getEnvFileNames = () => { const nodeEnv = process.env.NODE_ENV const flagMap = getFlagMap() diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ebd5ba9bd..4f2b654c9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -327,9 +327,6 @@ importers: dotenv: specifier: 16.3.1 version: 16.3.1 - dotenv-expand: - specifier: 10.0.0 - version: 10.0.0 events: specifier: 3.3.0 version: 3.3.0 @@ -11197,11 +11194,6 @@ packages: tslib: 2.5.2 dev: false - /dotenv-expand@10.0.0: - resolution: {integrity: sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==} - engines: {node: '>=12'} - dev: false - /dotenv-expand@5.1.0: resolution: {integrity: sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==} From 9ca5babf672545f1c6fbcc96594589b06200e1ba Mon Sep 17 00:00:00 2001 From: Craig Slusher Date: Thu, 7 Sep 2023 21:55:48 -0400 Subject: [PATCH 2/3] Uses json(...) as the directive --- cli/plasmo/src/features/env/env-config.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cli/plasmo/src/features/env/env-config.ts b/cli/plasmo/src/features/env/env-config.ts index cb608889f..a26b708f3 100644 --- a/cli/plasmo/src/features/env/env-config.ts +++ b/cli/plasmo/src/features/env/env-config.ts @@ -15,7 +15,6 @@ type LoadedEnvFiles = Array<{ contents: string }> -const JSON_DIRECTIVE_REGEX = /^\s*~/ export const INTERNAL_ENV_PREFIX = "PLASMO_" export const PUBLIC_ENV_PREFIX = "PLASMO_PUBLIC_" @@ -56,7 +55,11 @@ function cascadeEnv(loadedEnvFiles: LoadedEnvFiles) { for (const [envKey, envValue] of Object.entries(resultData)) { if (typeof parsed[envKey] === "undefined") { - parsed[envKey] = maybeParseJSON(envValue) + try { + parsed[envKey] = maybeParseJSON(envValue) + } catch (ex) { + eLog(`Failed to parse JSON directive ${envKey} in ${name}:`, ex.message) + } // Pass through internal env variables if (envKey.startsWith(INTERNAL_ENV_PREFIX)) { @@ -72,14 +75,11 @@ function cascadeEnv(loadedEnvFiles: LoadedEnvFiles) { return parsed } -function isJSONDirective(value: string): boolean { - return JSON_DIRECTIVE_REGEX.test(value); -} +const JSON_DIRECTIVE_RE = /^\s*json\((.+)\)\s*$/si function maybeParseJSON(value: string): any { - return isJSONDirective(value) - ? JSON.parse(value.replace(JSON_DIRECTIVE_REGEX, "")) - : value; + const match = value.match(JSON_DIRECTIVE_RE) + return match ? JSON.parse(match[1]) : value } export const getEnvFileNames = () => { From 47059a18cfb247726126d389b1367654ba01ae70 Mon Sep 17 00:00:00 2001 From: Craig Slusher Date: Thu, 7 Sep 2023 22:15:18 -0400 Subject: [PATCH 3/3] Uses dotenv-expand for var interpolation --- cli/plasmo/package.json | 1 + cli/plasmo/src/features/env/env-config.ts | 37 ++++++++++++++--------- pnpm-lock.yaml | 8 +++++ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/cli/plasmo/package.json b/cli/plasmo/package.json index d88430877..4bb388652 100644 --- a/cli/plasmo/package.json +++ b/cli/plasmo/package.json @@ -49,6 +49,7 @@ "chalk": "5.3.0", "change-case": "4.1.2", "dotenv": "16.3.1", + "dotenv-expand": "10.0.0", "events": "3.3.0", "fast-glob": "3.3.1", "fflate": "0.8.0", diff --git a/cli/plasmo/src/features/env/env-config.ts b/cli/plasmo/src/features/env/env-config.ts index a26b708f3..7b1296c95 100644 --- a/cli/plasmo/src/features/env/env-config.ts +++ b/cli/plasmo/src/features/env/env-config.ts @@ -3,6 +3,7 @@ import { readFile } from "fs/promises" import { resolve } from "path" import { constantCase } from "change-case" import dotenv from "dotenv" +import { expand as dotenvExpand } from "dotenv-expand" import { isFile, isReadable } from "@plasmo/utils/fs" import { eLog, iLog, vLog } from "@plasmo/utils/logging" @@ -49,21 +50,27 @@ function cascadeEnv(loadedEnvFiles: LoadedEnvFiles) { for (const { contents, name } of loadedEnvFiles) { try { envFileSet.add(name) - - vLog(`Loaded env from ${name}`) - const resultData: dotenv.DotenvParseOutput = dotenv.parse(contents) || {} - - for (const [envKey, envValue] of Object.entries(resultData)) { - if (typeof parsed[envKey] === "undefined") { - try { - parsed[envKey] = maybeParseJSON(envValue) - } catch (ex) { - eLog(`Failed to parse JSON directive ${envKey} in ${name}:`, ex.message) - } - - // Pass through internal env variables - if (envKey.startsWith(INTERNAL_ENV_PREFIX)) { - process.env[envKey] = envValue + const result = dotenvExpand({ + ignoreProcessEnv: true, + parsed: dotenv.parse(contents) + }) + + if (!!result.parsed) { + vLog(`Loaded env from ${name}`) + const resultData = result.parsed || {} + + for (const [envKey, envValue] of Object.entries(resultData)) { + if (typeof parsed[envKey] === "undefined") { + try { + parsed[envKey] = maybeParseJSON(envValue) + } catch (ex) { + eLog(`Failed to parse JSON directive ${envKey} in ${name}:`, ex.message) + } + + // Pass through internal env variables + if (envKey.startsWith(INTERNAL_ENV_PREFIX)) { + process.env[envKey] = envValue + } } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4f2b654c9..ebd5ba9bd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -327,6 +327,9 @@ importers: dotenv: specifier: 16.3.1 version: 16.3.1 + dotenv-expand: + specifier: 10.0.0 + version: 10.0.0 events: specifier: 3.3.0 version: 3.3.0 @@ -11194,6 +11197,11 @@ packages: tslib: 2.5.2 dev: false + /dotenv-expand@10.0.0: + resolution: {integrity: sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==} + engines: {node: '>=12'} + dev: false + /dotenv-expand@5.1.0: resolution: {integrity: sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==}