fix: repertoire add のパイプ stdin で複数 confirm が失敗する問題を修正
gh api の stdio を inherit→pipe に変更し stdin の消費を防止。 readline の内部バッファ消失を防ぐためシングルトン pipe line queue を導入。
This commit is contained in:
parent
69f13283a2
commit
3970b6bcf9
@ -70,7 +70,7 @@ export async function repertoireAddCommand(spec: string): Promise<void> {
|
||||
'api',
|
||||
`/repos/${owner}/${repo}/tarball/${ref}`,
|
||||
],
|
||||
{ stdio: ['inherit', 'pipe', 'pipe'] },
|
||||
{ stdio: ['pipe', 'pipe', 'pipe'] },
|
||||
);
|
||||
writeFileSync(tmpTarPath, tarballBuffer);
|
||||
|
||||
@ -160,8 +160,9 @@ export async function repertoireAddCommand(spec: string): Promise<void> {
|
||||
const packageDir = getRepertoirePackageDir(owner, repo);
|
||||
|
||||
if (existsSync(packageDir)) {
|
||||
info(`⚠ パッケージ @${owner}/${repo} は既にインストールされています`);
|
||||
const overwrite = await confirm(
|
||||
`${owner}/${repo} は既にインストールされています。上書きしますか?`,
|
||||
'上書きしますか?',
|
||||
false,
|
||||
);
|
||||
if (!overwrite) {
|
||||
|
||||
@ -98,7 +98,8 @@ export async function confirm(message: string, defaultYes = true): Promise<boole
|
||||
assertTtyIfForced(forceTouchTty);
|
||||
if (!useTty) {
|
||||
// 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 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 });
|
||||
|
||||
return new Promise((resolve) => {
|
||||
let resolved = false;
|
||||
rl.on('line', (line) => {
|
||||
lines.push(line);
|
||||
});
|
||||
|
||||
rl.once('line', (line) => {
|
||||
resolved = true;
|
||||
rl.close();
|
||||
pauseStdinSafely();
|
||||
rl.on('close', () => {
|
||||
pipeLineQueue = lines;
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
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();
|
||||
if (!trimmed) {
|
||||
resolve(defaultYes);
|
||||
return;
|
||||
return defaultYes;
|
||||
}
|
||||
resolve(trimmed === 'y' || trimmed === 'yes');
|
||||
});
|
||||
|
||||
rl.once('close', () => {
|
||||
if (!resolved) {
|
||||
resolve(defaultYes);
|
||||
}
|
||||
});
|
||||
});
|
||||
return trimmed === 'y' || trimmed === 'yes';
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user