Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
25 changes: 25 additions & 0 deletions src/lib/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
12 changes: 10 additions & 2 deletions src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@ export function parseEnvContent(content: string): Record<string, string> {
/**
* Converts JSON object to .env format string.
*/
export function jsonToEnv(data: Record<string, string>): string {
export function jsonToEnv(data: Record<string, unknown>): 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");
Expand Down
Loading