diff --git a/src/index.ts b/src/index.ts index 208f3ab..43a3286 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,7 @@ const command = define({ console.error("Error: Please specify a subcommand (init, set, get, pull, or delete)"); console.error(""); console.error("Usage:"); - console.error(" e2sm init - Initialize .e2smrc.json configuration file"); + console.error(" e2sm init - Initialize .e2smrc.jsonc configuration file"); console.error(" e2sm set - Upload .env file to AWS Secrets Manager"); console.error(" e2sm get - Display secret from AWS Secrets Manager"); console.error(" e2sm pull - Pull secret and generate .env file"); diff --git a/src/lib/env.test.ts b/src/lib/env.test.ts index e9aaba0..9711510 100644 --- a/src/lib/env.test.ts +++ b/src/lib/env.test.ts @@ -117,6 +117,31 @@ describe("jsonToEnv", () => { const result = jsonToEnv({ FOO: "bar\nbaz" }); expect(result).toBe('FOO="bar\nbaz"'); }); + + test("converts number values to string", () => { + const result = jsonToEnv({ PORT: 3000, DEBUG: 1 }); + expect(result).toBe('PORT="3000"\nDEBUG="1"'); + }); + + test("converts boolean values to string", () => { + const result = jsonToEnv({ ENABLED: true, DISABLED: false }); + expect(result).toBe('ENABLED="true"\nDISABLED="false"'); + }); + + test("converts null and undefined to string", () => { + const result = jsonToEnv({ NULL_VAL: null, UNDEF_VAL: undefined }); + expect(result).toBe('NULL_VAL="null"\nUNDEF_VAL="undefined"'); + }); + + test("converts object values to JSON string", () => { + const result = jsonToEnv({ OBJ: { nested: "value" } }); + expect(result).toBe('OBJ="{\\"nested\\":\\"value\\"}"'); + }); + + test("converts array values to JSON string", () => { + const result = jsonToEnv({ ITEMS: [1, 2, 3] }); + expect(result).toBe('ITEMS="[1,2,3]"'); + }); }); describe("generateEnvHeader", () => { diff --git a/src/lib/env.ts b/src/lib/env.ts index dbafbe8..0ce5977 100644 --- a/src/lib/env.ts +++ b/src/lib/env.ts @@ -46,10 +46,18 @@ export function parseEnvContent(content: string): Record { /** * Converts JSON object to .env format string. */ -export function jsonToEnv(data: Record): string { +export function jsonToEnv(data: Record): string { return Object.entries(data) .map(([key, value]) => { - const escaped = value.replace(/"/g, '\\"'); + let stringValue: string; + if (typeof value === "string") { + stringValue = value; + } else if (value !== null && typeof value === "object") { + stringValue = JSON.stringify(value); + } else { + stringValue = String(value); + } + const escaped = stringValue.replace(/"/g, '\\"'); return `${key}="${escaped}"`; }) .join("\n");