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
5 changes: 5 additions & 0 deletions .changeset/late-sides-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'dotenv-diff': patch
---

fixed interactive prompt may run in non-TTY environments
7 changes: 7 additions & 0 deletions packages/cli/src/commands/prompts/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export async function confirmYesNo(
): Promise<boolean> {
if (isYesMode) return true;
if (isCiMode) return false;

// Avoid hanging in non-interactive environments (e.g. hooks/scripts without TTY)
const hasInteractiveTty = Boolean(
process.stdin?.isTTY && process.stdout?.isTTY,
);
if (!hasInteractiveTty) return false;

const res = await prompts({
type: 'select',
name: 'ok',
Expand Down
10 changes: 4 additions & 6 deletions packages/cli/test/e2e/cli.compare.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('added .env to gitignore with --compare and --fix', () => {
fs.mkdirSync(path.join(cwd, 'src'), { recursive: true });
fs.writeFileSync(
path.join(cwd, 'src', 'index.ts'),
`const apiKey = process.env.API_KEY;`.trimStart(),
'const apiKey = process.env.API_KEY;'.trimStart(),
);

const res = runCli(cwd, ['--compare', '--fix']);
Expand All @@ -78,7 +78,7 @@ describe('added .env to gitignore with --compare and --fix', () => {
fs.mkdirSync(path.join(cwd, 'src'), { recursive: true });
fs.writeFileSync(
path.join(cwd, 'src', 'index.ts'),
`const apiKey = process.env.API_KEY;`.trimStart(),
'const apiKey = process.env.API_KEY;'.trimStart(),
);

const res = runCli(cwd, ['--compare', '--json']);
Expand All @@ -89,17 +89,15 @@ describe('added .env to gitignore with --compare and --fix', () => {
expect(json[0].gitignoreIssue?.reason).toBe('not-ignored');
});

it('Will prompt .env.example file not found. and prompt if .env.local exists and .env.example is set to --example', async () => {
it('Will skip interactive prompt in non-TTY when .env.example is missing and --example is set', async () => {
const cwd = tmpDir();
fs.writeFileSync(path.join(cwd, '.env.local'), 'FOO=bar');

const res = runCli(cwd, ['--compare', '--example', '.env.example']);

expect(res.status).toBe(0);
expect(res.stdout).toContain('▸ File not found');
expect(res.stdout).toContain(
'Do you want to create a .env.example file from .env.local?',
);
expect(res.stdout).toContain('▸ Skipping .env.example creation');
});

describe('Values mismatch checks', () => {
Expand Down
26 changes: 26 additions & 0 deletions packages/cli/test/unit/commands/prompts/prompts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ vi.mock('prompts');

const mockPrompts = prompts as unknown as ReturnType<typeof vi.fn>;

function setTTY(stdinTTY: boolean, stdoutTTY: boolean): void {
Object.defineProperty(process.stdin, 'isTTY', {
value: stdinTTY,
configurable: true,
});
Object.defineProperty(process.stdout, 'isTTY', {
value: stdoutTTY,
configurable: true,
});
}

describe('confirmYesNo', () => {
beforeEach(() => {
vi.clearAllMocks();
Expand All @@ -32,6 +43,7 @@ describe('confirmYesNo', () => {
});

it('returns true when user selects Yes', async () => {
setTTY(true, true);
mockPrompts.mockResolvedValue({ ok: true });

const result = await confirmYesNo('Are you sure?', {
Expand All @@ -44,6 +56,7 @@ describe('confirmYesNo', () => {
});

it('returns false when user selects No', async () => {
setTTY(true, true);
mockPrompts.mockResolvedValue({ ok: false });

const result = await confirmYesNo('Are you sure?', {
Expand All @@ -56,6 +69,7 @@ describe('confirmYesNo', () => {
});

it('returns false when prompt returns undefined', async () => {
setTTY(true, true);
mockPrompts.mockResolvedValue({});

const result = await confirmYesNo('Are you sure?', {
Expand All @@ -65,4 +79,16 @@ describe('confirmYesNo', () => {

expect(result).toBe(false);
});

it('returns false when no TTY is available and does not prompt', async () => {
setTTY(false, false);

const result = await confirmYesNo('Are you sure?', {
isCiMode: false,
isYesMode: false,
});

expect(result).toBe(false);
expect(mockPrompts).not.toHaveBeenCalled();
});
});
Loading