Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ export async function runWizard(

if (!writeResult.success) {
s.stop("Failed to write .env file.");
if (writeResult.error) {
console.error(`Error details: ${writeResult.error.message}`);
}
cancel("Could not write to .env file.");
return;
}
Expand Down
9 changes: 5 additions & 4 deletions src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface WriteResult {
success: boolean;
path: string;
keysWritten: number;
error?: Error;
}

/**
Expand Down Expand Up @@ -43,7 +44,7 @@ export function appendToEnv(
fs.appendFileSync(absolutePath, content);
return { success: true, path: absolutePath, keysWritten: entries.length };
} catch (error) {
return { success: false, path: absolutePath, keysWritten: 0 };
return { success: false, path: absolutePath, keysWritten: 0, error: error as Error };
}
}

Expand Down Expand Up @@ -93,8 +94,8 @@ export function syncExampleFile(
try {
fs.appendFileSync(absolutePath, content);
return { success: true, path: absolutePath, keysWritten: newKeys.length };
} catch {
return { success: false, path: absolutePath, keysWritten: 0 };
} catch (error) {
return { success: false, path: absolutePath, keysWritten: 0, error: error as Error };
}
}

Expand Down Expand Up @@ -137,6 +138,6 @@ export function generateTypeDefinitions(
fs.writeFileSync(absolutePath, content);
return { success: true, path: absolutePath, keysWritten: sortedKeys.length };
} catch (error) {
return { success: false, path: absolutePath, keysWritten: 0 };
return { success: false, path: absolutePath, keysWritten: 0, error: error as Error };
}
}
11 changes: 11 additions & 0 deletions tests/writer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ describe("Writer", () => {
expect(result.success).toBe(true);
expect(result.keysWritten).toBe(0);
});

it("should return error when writing fails", () => {
// Create a directory where the file should be to force a write error
// (EISDIR: illegal operation on a directory, open ...)
fs.mkdirSync(path.join(tempDir, ".env"));

const result = appendToEnv(".env", { KEY: "value" });

expect(result.success).toBe(false);
expect(result.error).toBeDefined();
});
});

describe("syncExampleFile", () => {
Expand Down