Skip to content

Commit 6cf28f9

Browse files
committed
fix(cli): buffer stdin for cip query --stdin
1 parent 91667ad commit 6cf28f9

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

packages/b2c-cli/bin/dev.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,17 @@ if (userWantsTelemetryEnabled) {
2727

2828
import {execute} from '@oclif/core';
2929

30+
function shouldBufferCipQueryStdin(argv) {
31+
return argv.includes('cip') && argv.includes('query') && argv.includes('--stdin');
32+
}
33+
34+
if (shouldBufferCipQueryStdin(process.argv) && !process.stdin.isTTY) {
35+
let bufferedInput = '';
36+
for await (const chunk of process.stdin) {
37+
bufferedInput += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
38+
}
39+
40+
process.env.SFCC_CIP_QUERY_STDIN = bufferedInput;
41+
}
42+
3043
await execute({development: true, dir: import.meta.url});

packages/b2c-cli/bin/run.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,17 @@ try {
1414

1515
import {execute} from '@oclif/core';
1616

17+
function shouldBufferCipQueryStdin(argv) {
18+
return argv.includes('cip') && argv.includes('query') && argv.includes('--stdin');
19+
}
20+
21+
if (shouldBufferCipQueryStdin(process.argv) && !process.stdin.isTTY) {
22+
let bufferedInput = '';
23+
for await (const chunk of process.stdin) {
24+
bufferedInput += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
25+
}
26+
27+
process.env.SFCC_CIP_QUERY_STDIN = bufferedInput;
28+
}
29+
1730
await execute({dir: import.meta.url});

packages/b2c-cli/src/commands/cip/query.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class CipQuery extends CipCommand<typeof CipQuery> {
4444
};
4545

4646
async run(): Promise<CipQueryCommandResult> {
47-
const sql = this.resolveSql();
47+
const sql = await this.resolveSql();
4848

4949
this.requireCipCredentials();
5050
const client = this.getCipClient();
@@ -69,6 +69,21 @@ export default class CipQuery extends CipCommand<typeof CipQuery> {
6969
return output;
7070
}
7171

72+
private async readSqlFromStdin(): Promise<string> {
73+
const preBufferedInput = process.env.SFCC_CIP_QUERY_STDIN;
74+
if (preBufferedInput !== undefined) {
75+
return preBufferedInput;
76+
}
77+
78+
let data = '';
79+
80+
for await (const chunk of process.stdin) {
81+
data += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
82+
}
83+
84+
return data;
85+
}
86+
7287
private renderRows(columns: string[], rows: Array<Record<string, unknown>>, format: CipOutputFormat): void {
7388
if (format === 'csv') {
7489
process.stdout.write(`${toCsv(columns, rows)}\n`);
@@ -78,7 +93,7 @@ export default class CipQuery extends CipCommand<typeof CipQuery> {
7893
renderTable(columns, rows);
7994
}
8095

81-
private resolveSql(): string {
96+
private async resolveSql(): Promise<string> {
8297
const positionalSql = this.args.sql;
8398
const hasPositional = Boolean(positionalSql);
8499
const hasFile = Boolean(this.flags.file);
@@ -97,7 +112,7 @@ export default class CipQuery extends CipCommand<typeof CipQuery> {
97112
}
98113

99114
const rawSql = hasStdin
100-
? fs.readFileSync(0, 'utf8')
115+
? await this.readSqlFromStdin()
101116
: hasFile
102117
? fs.readFileSync(this.flags.file as string, 'utf8')
103118
: positionalSql;

packages/b2c-cli/test/commands/cip/query.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,15 @@ describe('cip query', () => {
3737
expect(error).to.not.be.undefined;
3838
expect(error?.message).to.include('SQL input is empty');
3939
});
40+
41+
it('uses pre-buffered stdin fallback when stream is unavailable', async () => {
42+
process.env.SFCC_CIP_QUERY_STDIN = 'SELECT 1';
43+
44+
const {error} = await runCommand(
45+
'cip query --stdin --tenant-id zzxy_prd --client-id test --client-secret test --cip-host 127.0.0.1:9',
46+
);
47+
48+
expect(error).to.not.be.undefined;
49+
expect(error?.message).to.not.include('SQL input is empty');
50+
});
4051
});

0 commit comments

Comments
 (0)