Skip to content

Commit 2d8ddcc

Browse files
authored
fix(@inquirer/confirm): Fix when using with unix yes command. (#1815)
Fixes #365
1 parent f5c39a8 commit 2d8ddcc

3 files changed

Lines changed: 46 additions & 17 deletions

File tree

.github/workflows/main.yml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- run: corepack enable
2525
- uses: actions/setup-node@v4
2626
with:
27-
node-version: 20
27+
node-version: latest
2828
cache: 'yarn'
2929
- run: yarn install --immutable --immutable-cache
3030
- name: Typescript - packages
@@ -46,7 +46,7 @@ jobs:
4646
- run: corepack enable
4747
- uses: actions/setup-node@v4
4848
with:
49-
node-version: 20
49+
node-version: latest
5050
cache: 'yarn'
5151
- run: yarn install --immutable --immutable-cache
5252
- run: yarn tsc
@@ -65,9 +65,7 @@ jobs:
6565
strategy:
6666
fail-fast: false
6767
matrix:
68-
node-version:
69-
- 20
70-
- 18
68+
node-version: [18, 20, 22]
7169
steps:
7270
- uses: actions/checkout@v4
7371
- run: corepack enable
@@ -83,12 +81,6 @@ jobs:
8381
name: Integration Tests
8482
runs-on: ubuntu-latest
8583
needs: Compile
86-
strategy:
87-
fail-fast: false
88-
matrix:
89-
node-version:
90-
- 20
91-
- 18
9284
steps:
9385
- uses: actions/checkout@v4
9486
- name: Cache turbo build setup
@@ -100,7 +92,7 @@ jobs:
10092
- run: corepack enable
10193
- uses: actions/setup-node@v4
10294
with:
103-
node-version: ${{ matrix.node-version }}
95+
node-version: latest
10496
cache: 'yarn'
10597
- run: yarn install --immutable --immutable-cache
10698
- run: yarn tsc
@@ -124,7 +116,7 @@ jobs:
124116
- run: corepack enable
125117
- uses: actions/setup-node@v4
126118
with:
127-
node-version: 20
119+
node-version: latest
128120
cache: 'yarn'
129121
- run: yarn install --immutable --immutable-cache
130122
- run: yarn tsc
@@ -168,7 +160,7 @@ jobs:
168160
- run: corepack enable
169161
- uses: actions/setup-node@v4
170162
with:
171-
node-version: 20
163+
node-version: latest
172164
cache: 'yarn'
173165
- run: yarn install --immutable --immutable-cache
174166
- run: yarn turbo attw

integration/esm/integration.test.mjs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { createRequire } from 'node:module';
21
import { describe, it } from 'node:test';
32
import assert from 'node:assert/strict';
3+
import child_process from 'node:child_process';
4+
import { writeFileSync, unlinkSync } from 'node:fs';
5+
import path from 'node:path';
6+
import { promisify } from 'node:util';
47
import { input } from '@inquirer/prompts';
58
import defaultInput from '@inquirer/input';
69
import { createPrompt } from '@inquirer/core';
710
import inquirer, { createPromptModule } from 'inquirer';
811
import fixturePrompt from './fixturePrompt.mjs';
912

10-
const require = createRequire(import.meta.url);
13+
const exec = promisify(child_process.exec);
1114

1215
describe('ESM Integration', () => {
1316
it('@inquirer/prompts should be exported', () => {
@@ -24,7 +27,7 @@ describe('ESM Integration', () => {
2427

2528
it('works when prompt throws an error', async () => {
2629
await assert.rejects(() => fixturePrompt({}), {
27-
message: `Prompt functions must return a string.\n at file://${require.resolve('./fixturePrompt.mjs')}`,
30+
message: `Prompt functions must return a string.\n at file://${path.join(import.meta.dirname, './fixturePrompt.mjs')}`,
2831
});
2932
});
3033

@@ -33,4 +36,36 @@ describe('ESM Integration', () => {
3336
assert.ok(typeof inquirer.createPromptModule === 'function');
3437
assert.ok(typeof createPromptModule === 'function');
3538
});
39+
40+
it('works with Unix yes command piped input', async () => {
41+
try {
42+
await exec('which yes');
43+
} catch {
44+
assert.ok(true);
45+
console.warn('WARN: Skipping test due to absence of `yes` in the PATH');
46+
}
47+
48+
const testScript = path.join(import.meta.dirname, 'test-yes-pipe.js');
49+
writeFileSync(
50+
testScript,
51+
`
52+
import { confirm } from '@inquirer/prompts';
53+
54+
const answer = await confirm({
55+
message: 'Do you want to proceed?'
56+
});
57+
58+
process.exit(answer ? 0 : 1);
59+
`,
60+
);
61+
62+
try {
63+
await exec(`yes | node ${testScript} > /dev/null`);
64+
assert.ok(true);
65+
} catch {
66+
assert.fail('Command thew');
67+
} finally {
68+
unlinkSync(testScript);
69+
}
70+
}, 10000);
3671
});

packages/confirm/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export default createPrompt<boolean, ConfirmConfig>((config, done) => {
3636
const prefix = usePrefix({ status, theme });
3737

3838
useKeypress((key, rl) => {
39+
if (status !== 'idle') return;
40+
3941
if (isEnterKey(key)) {
4042
const answer = getBooleanValue(value, config.default);
4143
setValue(transformer(answer));

0 commit comments

Comments
 (0)