-
-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow CLI dev to use injected environment variables (#532)
* feat: allow CLI dev to use injected environment variables * Remove console.log --------- Co-authored-by: Eric Allam <[email protected]>
- Loading branch information
Showing
4 changed files
with
125 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@trigger.dev/cli": patch | ||
--- | ||
|
||
allow CLI dev to use injected environment variable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import pathModule from "path"; | ||
import { pathExists, readFile } from "./fileSystem"; | ||
import dotenv from "dotenv"; | ||
|
||
const ENV_FILES_FALLBACK = [".env", ".env.local", ".env.development.local"]; | ||
|
||
export type EnvVarSourceRuntime = { | ||
type: "runtime"; | ||
}; | ||
|
||
export type EnvVarSourceFile = { | ||
type: "file"; | ||
name: string; | ||
}; | ||
|
||
export type EnvVarSource = EnvVarSourceRuntime | EnvVarSourceFile; | ||
|
||
export type EnvironmentVariable = { | ||
value: string; | ||
source: EnvVarSource; | ||
}; | ||
|
||
export type EnvironmentVariables = { | ||
[name: string]: EnvironmentVariable | undefined; | ||
}; | ||
|
||
// Reads `varsToRead` from `process.env` and `envFile` (with fallbacks). | ||
// `process.env` takes precedence over the `envFile`. | ||
export async function readEnvVariables( | ||
path: string, | ||
envFile: string, | ||
varsToRead: string[] | ||
): Promise<EnvironmentVariables> { | ||
const resolvedEnvFile = await readEnvFilesWithBackups(path, envFile); | ||
const parsedEnvFile = resolvedEnvFile | ||
? { output: dotenv.parse(resolvedEnvFile.content), filename: resolvedEnvFile.fileName } | ||
: {}; | ||
|
||
return Object.fromEntries( | ||
varsToRead.map((envVar) => [ | ||
envVar, | ||
readFromRuntime(envVar) ?? readFromFile(envVar, parsedEnvFile), | ||
]) | ||
); | ||
} | ||
|
||
async function readEnvFilesWithBackups( | ||
path: string, | ||
envFile: string | ||
): Promise<{ content: string; fileName: string } | undefined> { | ||
const envFilePath = pathModule.join(path, envFile); | ||
const envFileExists = await pathExists(envFilePath); | ||
|
||
if (envFileExists) { | ||
const content = await readFile(envFilePath); | ||
|
||
return { content, fileName: envFile }; | ||
} | ||
|
||
for (const fallBack of ENV_FILES_FALLBACK) { | ||
const fallbackPath = pathModule.join(path, fallBack); | ||
const fallbackExists = await pathExists(fallbackPath); | ||
|
||
if (fallbackExists) { | ||
const content = await readFile(fallbackPath); | ||
|
||
return { content, fileName: fallBack }; | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
function readFromRuntime(envVar: string): EnvironmentVariable | undefined { | ||
const val = process.env[envVar]; | ||
if (!val) { | ||
return; | ||
} | ||
return { | ||
value: val, | ||
source: { | ||
type: "runtime", | ||
} as EnvVarSourceRuntime, | ||
}; | ||
} | ||
|
||
function readFromFile( | ||
envVar: string, | ||
parsedEnvFile: { output?: dotenv.DotenvParseOutput; filename?: string } | ||
): EnvironmentVariable | undefined { | ||
const val = parsedEnvFile.output ? parsedEnvFile.output[envVar] : undefined; | ||
if (!val) { | ||
return; | ||
} | ||
return { | ||
value: val, | ||
source: { | ||
type: "file", | ||
name: parsedEnvFile.filename, | ||
} as EnvVarSourceFile, | ||
}; | ||
} |