Skip to content

Commit

Permalink
update parse env and docker image (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
gladwindos committed Apr 7, 2024
1 parent 7d971c9 commit 4816c3e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["docs"]
"ignore": ["docs", "e2e"]
}
5 changes: 5 additions & 0 deletions .changeset/itchy-geckos-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gateweaver/server": patch
---

Update parse env and docker image
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ FROM base
COPY --from=build /prod/server /app
WORKDIR /app
EXPOSE 8080
CMD [ "pnpm", "start" ]
CMD [ "node", "./dist/main.js" ]
17 changes: 12 additions & 5 deletions packages/server/src/config/parse-config.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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}");
Expand All @@ -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);
});
});
12 changes: 10 additions & 2 deletions packages/server/src/config/parse-config.ts
Original file line number Diff line number Diff line change
@@ -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"];

Expand All @@ -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);

Expand Down

0 comments on commit 4816c3e

Please sign in to comment.