fix(#224): return byte counts for --pick stdout capture (#226)

This commit is contained in:
Tom Boucher
2026-05-24 16:34:02 -04:00
committed by GitHub
parent 5f3eb42864
commit cf7e65e18c
3 changed files with 101 additions and 58 deletions

View File

@@ -514,38 +514,19 @@ async function main() {
cwd = findProjectRoot(cwd);
}
// When --pick is active, intercept stdout to extract the requested field.
// When --pick is active, capture stdout and extract the requested field.
if (pickField) {
const origWriteSync = fs.writeSync;
let captured = '';
fs.writeSync = function (fd, data, ...rest) {
if (fd === 1) {
captured += String(data);
return;
}
return origWriteSync.call(fs, fd, data, ...rest);
};
const cleanup = () => {
fs.writeSync = origWriteSync;
let jsonStr = captured;
if (jsonStr.startsWith('@file:')) {
jsonStr = fs.readFileSync(jsonStr.slice(6), 'utf-8');
}
try {
const obj = JSON.parse(jsonStr);
const value = extractField(obj, pickField);
const result = value === null || value === undefined ? '' : String(value);
origWriteSync.call(fs, 1, result);
} catch {
origWriteSync.call(fs, 1, captured);
}
};
try {
const captured = await captureStdoutSyncWrites(async () => {
await runCommand(command, args, cwd, raw, defaultValue, originalCommand);
cleanup();
} catch (e) {
fs.writeSync = origWriteSync;
throw e;
});
const resolved = resolveAtFileOutput(captured);
try {
const obj = JSON.parse(resolved);
const value = extractField(obj, pickField);
const result = value === null || value === undefined ? '' : String(value);
fs.writeSync(1, result);
} catch {
fs.writeSync(1, captured);
}
return;
}
@@ -555,24 +536,49 @@ async function main() {
// already resolves this, but the normal path wrote @file: to stdout, forcing
// every workflow to have a bash-specific `if [[ "$INIT" == @file:* ]]` check
// that breaks on PowerShell and other non-bash shells.
const origWriteSync2 = fs.writeSync;
let captured = '';
fs.writeSync = function (fd, data, ...rest) {
if (fd === 1) {
captured += String(data);
return;
}
return origWriteSync2.call(fs, fd, data, ...rest);
};
try {
const captured = await captureStdoutSyncWrites(async () => {
await runCommand(command, args, cwd, raw, defaultValue, originalCommand);
} finally {
fs.writeSync = origWriteSync2;
}
if (captured.startsWith('@file:')) {
captured = fs.readFileSync(captured.slice(6), 'utf-8');
}
origWriteSync2.call(fs, 1, captured);
});
fs.writeSync(1, resolveAtFileOutput(captured));
}
function captureStdoutSyncWrites(run) {
const originalWriteSync = fs.writeSync;
let captured = '';
fs.writeSync = function patchedWriteSync(fd, data, ...rest) {
if (fd === 1) {
if (Buffer.isBuffer(data)) {
captured += data.toString('utf-8');
return data.length;
}
const text = String(data);
captured += text;
let encoding = 'utf-8';
if (typeof rest[1] === 'string') encoding = rest[1];
return Buffer.byteLength(text, encoding);
}
return originalWriteSync.call(fs, fd, data, ...rest);
};
const restore = () => {
fs.writeSync = originalWriteSync;
};
return Promise.resolve()
.then(() => run())
.then(() => {
restore();
return captured;
}, (err) => {
restore();
throw err;
});
}
function resolveAtFileOutput(captured) {
if (!captured.startsWith('@file:')) return captured;
return fs.readFileSync(captured.slice(6), 'utf-8');
}
/**