-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor how to load .env file (#34)
- Loading branch information
1 parent
a5ec2ee
commit 7405dab
Showing
16 changed files
with
164 additions
and
131 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,8 @@ | ||
--- | ||
"@dandori/core": patch | ||
"@dandori/libs": patch | ||
"@dandori/cli": patch | ||
"@dandori/ui": patch | ||
--- | ||
|
||
refactor how to load .env file |
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
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
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,88 @@ | ||
import { loadEnvFile, logger } from "../index"; | ||
import { | ||
describe, | ||
it, | ||
expect, | ||
beforeAll, | ||
afterAll, | ||
beforeEach, | ||
vi, | ||
afterEach, | ||
} from "vitest"; | ||
import { mkdir, rm, rmdir, writeFile } from "fs/promises"; | ||
|
||
describe("loadEnvFile", () => { | ||
describe("with valid .env file", () => { | ||
const openApiKeyPropName = "OPENAI_API_KEY"; | ||
|
||
afterEach(() => { | ||
delete process.env[openApiKeyPropName]; | ||
}); | ||
|
||
describe("no filePath argument", () => { | ||
const apiKey = "123"; | ||
const envFileName = ".env"; | ||
|
||
beforeAll(async () => { | ||
await writeFile(envFileName, `${openApiKeyPropName}=${apiKey}`); | ||
}); | ||
|
||
afterAll(async () => { | ||
await rm(envFileName); | ||
}); | ||
|
||
beforeEach(() => { | ||
loadEnvFile(); | ||
}); | ||
|
||
it(`loaded ${openApiKeyPropName}`, () => { | ||
expect(process.env[openApiKeyPropName]).toBe(apiKey); | ||
}); | ||
}); | ||
|
||
describe("filePath argument", () => { | ||
const apiKey = "456"; | ||
const envFileDir = "./dir"; | ||
const envFilePath = `./${envFileDir}/.env`; | ||
|
||
beforeAll(async () => { | ||
await mkdir(envFileDir); | ||
await writeFile(envFilePath, `${openApiKeyPropName}=${apiKey}`); | ||
}); | ||
|
||
afterAll(async () => { | ||
await rm(envFilePath); | ||
await rmdir(envFileDir); | ||
}); | ||
|
||
beforeEach(() => { | ||
loadEnvFile(envFilePath); | ||
}); | ||
|
||
it(`loaded ${openApiKeyPropName}`, () => { | ||
expect(process.env[openApiKeyPropName]).toBe(apiKey); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("without valid .env file", () => { | ||
const mockErrorLog = vi.spyOn(logger, "error").mockImplementation(() => {}); | ||
const runErrorLoadEnvFile = () => loadEnvFile("./nodir/.env"); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("throw Error", async () => { | ||
expect(runErrorLoadEnvFile).toThrowError(); | ||
}); | ||
|
||
it("called error log", async () => { | ||
try { | ||
runErrorLoadEnvFile(); | ||
} catch (e) { | ||
expect(mockErrorLog).toBeCalled(); | ||
} | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./generateDandoriFilePath"; | ||
export * from "./loadFile"; | ||
export * from "./loadEnvFile"; | ||
export * from "./logger"; | ||
export * from "./runPromisesSequentially"; |
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,13 @@ | ||
import { configDotenv } from "dotenv"; | ||
import { loadFile } from "./loadFile"; | ||
import { logger } from "./logger"; | ||
|
||
export function loadEnvFile(filePath: string = ".env"): void { | ||
const loadEnvResult = configDotenv({ | ||
path: loadFile(filePath), | ||
}); | ||
if (loadEnvResult.error) { | ||
logger.error(loadEnvResult.error); | ||
throw loadEnvResult.error; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/libs/src/generateDandoriFilePath.ts → packages/libs/src/loadFile.ts
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
Oops, something went wrong.