-
Notifications
You must be signed in to change notification settings - Fork 84
inline project env files in worker #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
181910c
fcd1156
c8ac7d8
3ffdc83
df61c41
d3163b3
ae2b8ee
9ff653c
e740d18
ea4bb69
c43a925
64e51a5
8e5b382
bf62788
a107abb
a9f7ec4
a5268a7
dbb57bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
|
|
||
| import { BuildOptions } from "@opennextjs/aws/build/helper.js"; | ||
|
|
||
| import { extractProjectEnvVars } from "../../utils"; | ||
|
|
||
| /** | ||
| * Copies the values extracted from the project's env files to the output directory for use in the worker. | ||
| */ | ||
| export function copyEnvFiles(options: BuildOptions) { | ||
| ["production", "development", "test"].forEach((mode) => | ||
james-elicx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fs.appendFileSync( | ||
| path.join(options.outputDir, `.env.mjs`), | ||
| `export const ${mode} = ${JSON.stringify(extractProjectEnvVars(mode, options))};\n` | ||
| ) | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { appendFileSync, writeFileSync } from "node:fs"; | ||
|
|
||
| import { BuildOptions } from "@opennextjs/aws/build/helper.js"; | ||
| import mockFs from "mock-fs"; | ||
| import { afterEach, beforeEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { extractProjectEnvVars } from "./extract-project-env-vars"; | ||
|
|
||
| const options = { monorepoRoot: "", appPath: "" } as BuildOptions; | ||
|
|
||
| describe("extractProjectEnvVars", () => { | ||
| beforeEach(() => { | ||
| mockFs({ | ||
| ".env": "ENV_VAR=value", | ||
| ".env.local": "ENV_LOCAL_VAR=value", | ||
| ".env.development": "ENV_DEV_VAR=value", | ||
| ".env.development.local": "ENV_DEV_LOCAL_VAR=value", | ||
| ".env.production": "ENV_PROD_VAR=value", | ||
| ".env.production.local": "ENV_PROD_LOCAL_VAR=value", | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => mockFs.restore()); | ||
|
|
||
| it("should extract production env vars", () => { | ||
| const result = extractProjectEnvVars("production", options); | ||
| expect(result).toEqual({ | ||
| ENV_LOCAL_VAR: "value", | ||
| ENV_PROD_LOCAL_VAR: "value", | ||
| ENV_PROD_VAR: "value", | ||
| ENV_VAR: "value", | ||
| }); | ||
| }); | ||
|
|
||
| it("should extract development env vars", () => { | ||
| writeFileSync(".dev.vars", 'NEXTJS_ENV = "development"'); | ||
|
|
||
| const result = extractProjectEnvVars("development", options); | ||
| expect(result).toEqual({ | ||
| ENV_LOCAL_VAR: "value", | ||
| ENV_DEV_LOCAL_VAR: "value", | ||
| ENV_DEV_VAR: "value", | ||
| ENV_VAR: "value", | ||
| }); | ||
| }); | ||
|
|
||
| it("should override env vars with those in a local file", () => { | ||
| writeFileSync(".env.production.local", "ENV_PROD_VAR=overridden"); | ||
|
|
||
| const result = extractProjectEnvVars("production", options); | ||
| expect(result).toEqual({ | ||
| ENV_LOCAL_VAR: "value", | ||
| ENV_PROD_VAR: "overridden", | ||
| ENV_VAR: "value", | ||
| }); | ||
| }); | ||
|
|
||
| it("should support referencing variables", () => { | ||
| appendFileSync(".env.production.local", "\nENV_PROD_LOCAL_VAR_REF=$ENV_PROD_LOCAL_VAR"); | ||
|
|
||
| const result = extractProjectEnvVars("production", options); | ||
| expect(result).toEqual({ | ||
| ENV_LOCAL_VAR: "value", | ||
| ENV_PROD_LOCAL_VAR: "value", | ||
| ENV_PROD_LOCAL_VAR_REF: "value", | ||
| ENV_PROD_VAR: "value", | ||
| ENV_VAR: "value", | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import * as fs from "node:fs"; | ||
| import * as path from "node:path"; | ||
|
|
||
| import { parse } from "@dotenvx/dotenvx"; | ||
| import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; | ||
|
|
||
| function readEnvFiles(fileNames: string[], { monorepoRoot, appPath }: BuildOptions) { | ||
james-elicx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return fileNames | ||
| .flatMap((fileName) => [ | ||
| ...(monorepoRoot !== appPath ? [path.join(monorepoRoot, fileName)] : []), | ||
| path.join(appPath, fileName), | ||
| ]) | ||
| .filter((filePath) => fs.existsSync(filePath) && fs.statSync(filePath).isFile()) | ||
| .map((filePath) => parse(fs.readFileSync(filePath).toString())); | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the environment variables defined in various .env files for a project. | ||
| * | ||
| * The `NEXTJS_ENV` environment variable in `.dev.vars` determines the mode. | ||
| * | ||
| * Merged variables respect the following priority order. | ||
james-elicx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * 1. `.env.{mode}.local` | ||
| * 2. `.env.local` | ||
| * 3. `.env.{mode}` | ||
| * 4. `.env` | ||
| * | ||
james-elicx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * In a monorepo, the env files in an app's directory will take precedence over | ||
| * the env files at the root of the monorepo. | ||
| */ | ||
| export function extractProjectEnvVars(mode: string, options: BuildOptions) { | ||
|
||
| const envVars = readEnvFiles([".env", `.env.${mode}`, ".env.local", `.env.${mode}.local`], options); | ||
|
|
||
| return envVars.reduce<Record<string, string>>((acc, overrides) => ({ ...acc, ...overrides }), {}); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from "./copy-prerendered-routes"; | ||
| export * from "./extract-project-env-vars"; | ||
| export * from "./normalize-path"; | ||
| export * from "./ts-parse-file"; |
Uh oh!
There was an error while loading. Please reload this page.