diff --git a/.changeset/config.json b/.changeset/config.json index a363828..370760e 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -7,5 +7,5 @@ "access": "public", "baseBranch": "main", "updateInternalDependencies": "patch", - "ignore": ["docs"] + "ignore": ["docs", "e2e"] } diff --git a/.changeset/itchy-geckos-pay.md b/.changeset/itchy-geckos-pay.md new file mode 100644 index 0000000..5276974 --- /dev/null +++ b/.changeset/itchy-geckos-pay.md @@ -0,0 +1,5 @@ +--- +"@gateweaver/server": patch +--- + +Update parse env and docker image diff --git a/Dockerfile b/Dockerfile index 2484b39..d0c3f7b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,4 +14,4 @@ FROM base COPY --from=build /prod/server /app WORKDIR /app EXPOSE 8080 -CMD [ "pnpm", "start" ] \ No newline at end of file +CMD [ "node", "./dist/main.js" ] \ No newline at end of file diff --git a/packages/server/src/config/parse-config.test.ts b/packages/server/src/config/parse-config.test.ts index d909998..b0ae830 100644 --- a/packages/server/src/config/parse-config.test.ts +++ b/packages/server/src/config/parse-config.test.ts @@ -1,16 +1,14 @@ import fs from "fs"; import { parseConfig } from "./parse-config"; -import { validateConfig } from "./validate-config"; +import { InvalidConfigError, validateConfig } from "./validate-config"; jest.mock("fs"); jest.mock("./validate-config"); describe("parseConfig", () => { - const mockEnv = { TEST_VAR: "value" }; - beforeEach(() => { jest.resetAllMocks(); - process.env = { ...mockEnv }; // Reset environment variables for each test + process.env.TEST_VAR = "test value"; }); it("should correctly identify and parse a file with .yaml extension", () => { @@ -41,7 +39,7 @@ describe("parseConfig", () => { it("should replace environment variables in the file content before validating", () => { const configPath = "config"; const fullPath = `${configPath}.yml`; - const mockConfig = { endpoints: "value" }; + const mockConfig = { endpoints: "test value" }; (fs.existsSync as jest.Mock).mockReturnValue(true); (fs.readFileSync as jest.Mock).mockReturnValue("endpoints: ${TEST_VAR}"); @@ -51,4 +49,13 @@ describe("parseConfig", () => { expect(fs.readFileSync).toHaveBeenCalledWith(fullPath, "utf8"); expect(validateConfig).toHaveBeenCalledWith(mockConfig); }); + + it("should throw an InvalidConfigError if an environment variable is not found", () => { + const configPath = "config"; + + (fs.existsSync as jest.Mock).mockReturnValue(true); + (fs.readFileSync as jest.Mock).mockReturnValue("endpoints: ${MISSING_VAR}"); + + expect(() => parseConfig(configPath)).toThrow(InvalidConfigError); + }); }); diff --git a/packages/server/src/config/parse-config.ts b/packages/server/src/config/parse-config.ts index b599ec0..9620d92 100644 --- a/packages/server/src/config/parse-config.ts +++ b/packages/server/src/config/parse-config.ts @@ -1,8 +1,16 @@ import fs from "fs"; import YAML from "yaml"; -import { validateConfig } from "./validate-config"; +import { InvalidConfigError, validateConfig } from "./validate-config"; import { Config } from "./config.types"; +export const parseEnv = (value: string): string => { + if (!process.env[value]) { + throw new InvalidConfigError(`Environment variable "${value}" not found.`); + } + + return process.env[value]!; +}; + export const parseConfig = (filePath: string): Config => { const extensions = [".yml", ".yaml"]; @@ -24,7 +32,7 @@ export const parseConfig = (filePath: string): Config => { const file = fs .readFileSync(finalPath, "utf8") - .replace(/\$\{(.+?)\}/g, (match, name) => process.env[name] || match); + .replace(/\$\{(.+?)\}/g, (_, value) => parseEnv(value)); const config = YAML.parse(file);