From e55c11915c3900a40819efb6250386976217601c Mon Sep 17 00:00:00 2001 From: Cristian Dominguez <6853656+cristiand391@users.noreply.github.com> Date: Thu, 25 Jan 2024 21:02:18 -0300 Subject: [PATCH] fix: manually read from stdin If you do `flag.allowStdin: true` then oclif will read from stdin for you if the value is `-`, so there's no way to know if the flag value came from stdin or the user specified a file path (here we need to read the file). I don't like having another specific flag just to read from stdin so I made this a string flag and handle reading from stdin/file in `flag.parse`. --- src/commands/org/api.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/commands/org/api.ts b/src/commands/org/api.ts index 0a98ed8..29b66bd 100644 --- a/src/commands/org/api.ts +++ b/src/commands/org/api.ts @@ -6,7 +6,7 @@ import { ProxyAgent } from 'proxy-agent'; import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; import { SfError, Org } from '@salesforce/core'; import { Args, ux } from '@oclif/core'; -import { fileExists } from '@oclif/core/lib/util/fs.js'; +import { readStdin } from '@oclif/core/lib/parser/parse.js'; export class OrgApi extends SfCommand { public static readonly summary = @@ -64,10 +64,19 @@ export class OrgApi extends SfCommand { summary: 'The file to use as the body for the request (use "-" to read from standard input).', parse: async (input) => { - if (input === '-') return input; - await fileExists(input); + if (input === '-') { + const body = await readStdin(); + if (body) { + return body.trim(); + } else { + throw new Error( + 'Unable to read body: `-` was provided but STDIN is empty.', + ); + } + } else { + return readFile(input, 'utf8'); + } }, - allowStdin: true, helpValue: 'file', }), }; @@ -117,12 +126,7 @@ export class OrgApi extends SfCommand { }`, ...(flags.header ? OrgApi.getHeaders(flags.header) : {}), }, - body: - flags.method === 'GET' - ? undefined - : flags.body - ? await readFile(flags.body) - : undefined, + body: flags.method === 'GET' ? undefined : flags.body, throwHttpErrors: false, followRedirect: false, });