fix: repertoire add のパイプ stdin で複数 confirm が失敗する問題を修正

gh api の stdio を inherit→pipe に変更し stdin の消費を防止。
readline の内部バッファ消失を防ぐためシングルトン pipe line queue を導入。
This commit is contained in:
nrslib 2026-02-23 15:18:52 +09:00
parent 69f13283a2
commit 3970b6bcf9
2 changed files with 41 additions and 21 deletions

View File

@ -70,7 +70,7 @@ export async function repertoireAddCommand(spec: string): Promise<void> {
'api', 'api',
`/repos/${owner}/${repo}/tarball/${ref}`, `/repos/${owner}/${repo}/tarball/${ref}`,
], ],
{ stdio: ['inherit', 'pipe', 'pipe'] }, { stdio: ['pipe', 'pipe', 'pipe'] },
); );
writeFileSync(tmpTarPath, tarballBuffer); writeFileSync(tmpTarPath, tarballBuffer);
@ -160,8 +160,9 @@ export async function repertoireAddCommand(spec: string): Promise<void> {
const packageDir = getRepertoirePackageDir(owner, repo); const packageDir = getRepertoirePackageDir(owner, repo);
if (existsSync(packageDir)) { if (existsSync(packageDir)) {
info(`⚠ パッケージ @${owner}/${repo} は既にインストールされています`);
const overwrite = await confirm( const overwrite = await confirm(
`${owner}/${repo} は既にインストールされています。上書きしますか?`, '上書きしますか?',
false, false,
); );
if (!overwrite) { if (!overwrite) {

View File

@ -98,7 +98,8 @@ export async function confirm(message: string, defaultYes = true): Promise<boole
assertTtyIfForced(forceTouchTty); assertTtyIfForced(forceTouchTty);
if (!useTty) { if (!useTty) {
// Support piped stdin (e.g. echo "y" | takt repertoire add ...) // Support piped stdin (e.g. echo "y" | takt repertoire add ...)
if (!process.stdin.isTTY && process.stdin.readable && !process.stdin.destroyed) { // Once the pipe queue is initialized, stdin may be destroyed but queued lines remain.
if (pipeLineQueue !== null || (!process.stdin.isTTY && process.stdin.readable && !process.stdin.destroyed)) {
return readConfirmFromPipe(defaultYes); return readConfirmFromPipe(defaultYes);
} }
return defaultYes; return defaultYes;
@ -127,28 +128,46 @@ export async function confirm(message: string, defaultYes = true): Promise<boole
}); });
} }
function readConfirmFromPipe(defaultYes: boolean): Promise<boolean> { /**
* Shared pipe reader singleton.
*
* readline.createInterface buffers data from stdin internally.
* Creating and closing multiple interfaces loses buffered lines.
* This singleton reads all lines once and serves them as a queue.
*/
let pipeLineQueue: string[] | null = null;
let pipeQueueReady: Promise<void> | null = null;
function ensurePipeQueue(): Promise<void> {
if (pipeQueueReady) return pipeQueueReady;
pipeQueueReady = new Promise((resolve) => {
const lines: string[] = [];
const rl = readline.createInterface({ input: process.stdin }); const rl = readline.createInterface({ input: process.stdin });
return new Promise((resolve) => { rl.on('line', (line) => {
let resolved = false; lines.push(line);
});
rl.once('line', (line) => { rl.on('close', () => {
resolved = true; pipeLineQueue = lines;
rl.close(); resolve();
pauseStdinSafely(); });
});
return pipeQueueReady;
}
async function readConfirmFromPipe(defaultYes: boolean): Promise<boolean> {
await ensurePipeQueue();
const line = pipeLineQueue!.shift();
if (line === undefined) {
return defaultYes;
}
const trimmed = line.trim().toLowerCase(); const trimmed = line.trim().toLowerCase();
if (!trimmed) { if (!trimmed) {
resolve(defaultYes); return defaultYes;
return;
} }
resolve(trimmed === 'y' || trimmed === 'yes'); return trimmed === 'y' || trimmed === 'yes';
});
rl.once('close', () => {
if (!resolved) {
resolve(defaultYes);
}
});
});
} }