Skip to content

Commit 162b157

Browse files
committed
chore: final touches
1 parent 63881c2 commit 162b157

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/commands/actors/call.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class ActorsCallCommand extends ApifyCommand<typeof ActorsCallCommand> {
4242
exclusive: ['input-file'],
4343
}),
4444
'input-file': Flags.string({
45-
aliases: ['if'],
45+
char: 'f',
4646
description:
4747
'Optional path to a file with JSON input to be given to the Actor. The file must be a valid JSON file. You can also specify `-` to read from standard input.',
4848
required: false,

src/lib/command-framework/CommandError.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export enum CommandErrorCode {
2929
* Used when a flag is provided, but it is exclusive with another flag
3030
*/
3131
APIFY_FLAG_IS_EXCLUSIVE_WITH_ANOTHER_FLAG,
32+
/**
33+
* Used when two flag options want to receive their value from stdin
34+
*/
35+
APIFY_TOO_MANY_REQUESTERS_OF_STDIN,
3236
APIFY_UNKNOWN_ERROR,
3337
}
3438

@@ -62,7 +66,7 @@ export class CommandError extends Error {
6266
switch (this.code) {
6367
case CommandErrorCode.NODEJS_ERR_PARSE_ARGS_INVALID_OPTION_VALUE: {
6468
const match =
65-
/'(?:-[a-z], )?--(?<flagName>[a-zA-Z]+)(?: <value>)?' (?<noArg>does not take)?(?<missingArg>argument missing)?(?<ambiguous>argument is ambiguous\.(?<ambiguousMessage>\s*.*\s*.*)?)?/gi.exec(
69+
/'(?:-[a-z], )?-?-(?<flagName>[a-zA-Z-]+)(?: <value>)?' (?<noArg>does not take)?(?<missingArg>argument missing)?(?<ambiguous>argument is ambiguous\.(?<ambiguousMessage>\s*.*\s*.*)?)?/gi.exec(
6670
this.message,
6771
);
6872

@@ -192,6 +196,14 @@ export class CommandError extends Error {
192196
return messageParts.join('\n');
193197
}
194198

199+
case CommandErrorCode.APIFY_TOO_MANY_REQUESTERS_OF_STDIN: {
200+
const { firstUse, secondUse } = this.metadata as { firstUse: string; secondUse: string };
201+
202+
return chalk.gray(
203+
`Flag ${chalk.white.bold(`--${firstUse}`)} and ${chalk.white.bold(`--${secondUse}`)} cannot both request that their value comes from standard input at the same time.`,
204+
);
205+
}
206+
195207
default: {
196208
const cliMetadata = useCLIMetadata();
197209

@@ -222,7 +234,12 @@ export class CommandError extends Error {
222234
base.push(`is ambiguous (meaning the provided value could be interpreted as a flag too).`);
223235

224236
if (flagData.ambiguousMessage) {
225-
base.push(`\n${flagData.ambiguousMessage}`);
237+
base.push(
238+
`\n${flagData.ambiguousMessage
239+
.split('\n')
240+
.map((line) => ` ${line}`)
241+
.join('\n')}`,
242+
);
226243
} else {
227244
base.push(`To solve this, provide the flag like this: --${flagData.name}=<value>`);
228245
}

src/lib/command-framework/apify-command.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
333333

334334
const exclusiveFlagMap = new Map<string, Set<string>>();
335335

336+
let flagThatUsedStdin: string | undefined;
337+
336338
for (const [commandFlagKey, builderData] of Object.entries(this.ctor.flags)) {
337339
if (typeof builderData === 'string') {
338340
throw new RangeError('Do not provide the string for the json arg! It is a type level assertion!');
@@ -437,6 +439,16 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
437439
this.flags[camelCasedName] = rawFlag;
438440

439441
if (rawFlag === '-' && builderData.stdin) {
442+
if (flagThatUsedStdin) {
443+
throw new CommandError({
444+
code: CommandErrorCode.APIFY_TOO_MANY_REQUESTERS_OF_STDIN,
445+
command: this.ctor,
446+
metadata: { firstUse: flagThatUsedStdin, secondUse: baseFlagName },
447+
});
448+
}
449+
450+
flagThatUsedStdin = baseFlagName;
451+
440452
this.flags[camelCasedName] = this._handleStdin(builderData.stdin);
441453
}
442454

@@ -555,7 +567,7 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
555567
private _handleStdin(mode: StdinMode) {
556568
switch (mode) {
557569
case StdinMode.Stringified:
558-
return cachedStdinInput?.toString('utf8') ?? '';
570+
return (cachedStdinInput?.toString('utf8') ?? '').trim();
559571
default:
560572
return cachedStdinInput;
561573
}

0 commit comments

Comments
 (0)