Skip to content

Commit 9ff960d

Browse files
committed
chore: stdin actually working pause
1 parent efd2270 commit 9ff960d

File tree

12 files changed

+90
-29
lines changed

12 files changed

+90
-29
lines changed

scripts/generate-reference.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import { execa } from 'execa';
88
import type { Code, Link, Paragraph, PhrasingContentMap } from 'mdast';
99
import { fromMarkdown } from 'mdast-util-from-markdown';
1010

11+
console.error('!!! REMAKE THIS !!!');
12+
console.error('!!! REMAKE THIS !!!');
13+
console.error('!!! REMAKE THIS !!!');
14+
console.error('!!! REMAKE THIS !!!');
15+
16+
process.exit(0);
17+
1118
const finalOutputFile = new URL('../docs/reference.md', import.meta.url);
1219
const temporaryReferenceFile = fileURLToPath(new URL('./temporary-reference.md', import.meta.url));
1320
const referenceTemplateFile = new URL('./reference-template.md', import.meta.url);

src/commands/actor/push-data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { cachedStdinInput } from '../../entrypoints/_shared.js';
12
import { APIFY_STORAGE_TYPES, getApifyStorageClient, getDefaultStorageId } from '../../lib/actor.js';
23
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
34
import { Args } from '../../lib/command-framework/args.js';
4-
import { readStdin } from '../../lib/commands/read-stdin.js';
55
import { error } from '../../lib/outputs.js';
66

77
export class ActorPushDataCommand extends ApifyCommand<typeof ActorPushDataCommand> {
@@ -25,7 +25,7 @@ export class ActorPushDataCommand extends ApifyCommand<typeof ActorPushDataComma
2525
async run() {
2626
const { item: _item } = this.args;
2727

28-
const item = _item || (await readStdin());
28+
const item = _item || cachedStdinInput;
2929

3030
if (!item) {
3131
error({ message: 'No item was provided.' });

src/commands/actors/call.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from 'apify-client';
1111
import chalk from 'chalk';
1212

13-
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
13+
import { ApifyCommand, StdinMode } from '../../lib/command-framework/apify-command.js';
1414
import { Args } from '../../lib/command-framework/args.js';
1515
import { Flags } from '../../lib/command-framework/flags.js';
1616
import { getInputOverride } from '../../lib/commands/resolve-input.js';
@@ -38,15 +38,15 @@ export class ActorsCallCommand extends ApifyCommand<typeof ActorsCallCommand> {
3838
char: 'i',
3939
description: 'Optional JSON input to be given to the Actor.',
4040
required: false,
41-
allowStdin: true,
41+
stdin: StdinMode.Stringified,
4242
exclusive: ['input-file'],
4343
}),
4444
'input-file': Flags.string({
4545
aliases: ['if'],
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,
49-
allowStdin: true,
49+
stdin: StdinMode.Stringified,
5050
exclusive: ['input'],
5151
}),
5252
silent: Flags.boolean({

src/commands/actors/start.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ActorRun, ActorStartOptions, ActorTaggedBuild } from 'apify-client
22
import chalk from 'chalk';
33

44
import { ActorsCallCommand } from './call.js';
5-
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
5+
import { ApifyCommand, StdinMode } from '../../lib/command-framework/apify-command.js';
66
import { Args } from '../../lib/command-framework/args.js';
77
import { Flags } from '../../lib/command-framework/flags.js';
88
import { getInputOverride } from '../../lib/commands/resolve-input.js';
@@ -30,15 +30,15 @@ export class ActorsStartCommand extends ApifyCommand<typeof ActorsStartCommand>
3030
char: 'i',
3131
description: 'Optional JSON input to be given to the Actor.',
3232
required: false,
33-
allowStdin: true,
33+
stdin: StdinMode.Stringified,
3434
exclusive: ['input-file'],
3535
}),
3636
'input-file': Flags.string({
3737
aliases: ['if'],
3838
description:
3939
'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.',
4040
required: false,
41-
allowStdin: true,
41+
stdin: StdinMode.Stringified,
4242
exclusive: ['input'],
4343
}),
4444
};

src/commands/datasets/push-items.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { ApifyApiError } from 'apify-client';
22
import chalk from 'chalk';
33

4+
import { cachedStdinInput } from '../../entrypoints/_shared.js';
45
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
56
import { Args } from '../../lib/command-framework/args.js';
6-
import { readStdin } from '../../lib/commands/read-stdin.js';
77
import { tryToGetDataset } from '../../lib/commands/storages.js';
88
import { error, success } from '../../lib/outputs.js';
99
import { getLoggedClientOrThrow } from '../../lib/utils.js';
@@ -42,7 +42,7 @@ export class DatasetsPushDataCommand extends ApifyCommand<typeof DatasetsPushDat
4242

4343
let parsedData: Record<string, unknown> | Array<Record<string, unknown>>;
4444

45-
const item = _item || (await readStdin());
45+
const item = _item || cachedStdinInput;
4646

4747
if (!item) {
4848
error({ message: 'No items were provided.' });

src/commands/run.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { loadJsonFile } from 'load-json-file';
1010
import mime from 'mime';
1111
import { minVersion } from 'semver';
1212

13-
import { ApifyCommand } from '../lib/command-framework/apify-command.js';
13+
import { ApifyCommand, StdinMode } from '../lib/command-framework/apify-command.js';
1414
import { Flags } from '../lib/command-framework/flags.js';
1515
import { getInputOverride } from '../lib/commands/resolve-input.js';
1616
import {
@@ -89,15 +89,15 @@ export class RunCommand extends ApifyCommand<typeof RunCommand> {
8989
char: 'i',
9090
description: 'Optional JSON input to be given to the Actor.',
9191
required: false,
92-
allowStdin: true,
92+
stdin: StdinMode.Stringified,
9393
exclusive: ['input-file'],
9494
}),
9595
'input-file': Flags.string({
9696
aliases: ['if'],
9797
description:
9898
'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.',
9999
required: false,
100-
allowStdin: true,
100+
stdin: StdinMode.Stringified,
101101
exclusive: ['input'],
102102
}),
103103
};

src/entrypoints/_shared.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import process from 'node:process';
33
import yargonaut from 'yargonaut';
44
import yargs from 'yargs/yargs';
55

6+
import { readStdin } from '../lib/commands/read-stdin.js';
67
import { version } from '../lib/consts.js';
78

89
yargonaut //
@@ -33,3 +34,5 @@ export function printCLIVersionAndExit(): never {
3334
console.log(version);
3435
process.exit(0);
3536
}
37+
38+
export const cachedStdinInput = await readStdin();

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ArgumentsCamelCase, Argv, CommandBuilder, CommandModule } from 'ya
55

66
import type { ArgTag, TaggedArgBuilder } from './args.js';
77
import type { FlagTag, TaggedFlagBuilder } from './flags.js';
8+
import { cachedStdinInput } from '../../entrypoints/_shared.js';
89
import { error } from '../outputs.js';
910

1011
interface ArgTagToTSType {
@@ -185,6 +186,11 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
185186
case 'string':
186187
default:
187188
this.args[camelCasedName] = String(rawArgs[yargsArgName]);
189+
190+
if (rawArgs[yargsArgName] === '-' && builderData.stdin) {
191+
this.args[camelCasedName] = this._handleStdin(builderData.stdin);
192+
}
193+
188194
break;
189195
}
190196
}
@@ -224,6 +230,11 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
224230
case 'string':
225231
default: {
226232
this.flags[camelCasedName] = rawArgs[yargsFlagName];
233+
234+
if (rawArgs[yargsFlagName] === '-' && builderData.stdin) {
235+
this.flags[camelCasedName] = this._handleStdin(builderData.stdin);
236+
}
237+
227238
break;
228239
}
229240
}
@@ -243,6 +254,15 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
243254
}
244255
}
245256

257+
private _handleStdin(mode: StdinMode) {
258+
switch (mode) {
259+
case StdinMode.Stringified:
260+
return cachedStdinInput?.toString('utf8') ?? '';
261+
default:
262+
return cachedStdinInput;
263+
}
264+
}
265+
246266
private _buildCommandStrings(nameOverride?: string): readonly string[] {
247267
let baseDefinition = `${nameOverride || this.ctor.name}`;
248268

@@ -448,3 +468,8 @@ export async function runCommand<Cmd extends typeof BuiltApifyCommand>(
448468
export declare class BuiltApifyCommand extends ApifyCommand {
449469
override run(): Awaitable<void>;
450470
}
471+
472+
export enum StdinMode {
473+
Raw = 1,
474+
Stringified = 2,
475+
}

src/lib/command-framework/args.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import type { Argv } from 'yargs';
22

3+
import { StdinMode } from './apify-command.js';
4+
35
export type ArgTag = 'string';
46

57
export interface BaseArgOptions {
68
required?: boolean;
79
description?: string;
810
aliases?: readonly string[];
11+
/**
12+
* Whether the argument should be prefilled from stdin
13+
* @default false
14+
*/
15+
stdin?: StdinMode;
916
}
1017

1118
export type StringArgOptions = BaseArgOptions;
@@ -14,6 +21,7 @@ export type TaggedArgBuilder<Tag extends ArgTag, Required = boolean> = {
1421
argTag: Tag;
1522
builder: (args: Argv, objectName: string) => Argv;
1623
required: Required;
24+
stdin: StdinMode;
1725
};
1826

1927
function stringArg<const T extends StringArgOptions>(option: T): TaggedArgBuilder<'string', T['required']> {
@@ -28,6 +36,7 @@ function stringArg<const T extends StringArgOptions>(option: T): TaggedArgBuilde
2836
return args;
2937
},
3038
required: option.required ?? false,
39+
stdin: option.stdin ?? StdinMode.Raw,
3140
};
3241
}
3342

src/lib/command-framework/flags.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Argv } from 'yargs';
22

3+
import { StdinMode } from './apify-command.js';
4+
35
export type FlagTag = 'string' | 'boolean' | 'integer';
46

57
export interface BaseFlagOptions {
@@ -12,6 +14,11 @@ export interface BaseFlagOptions {
1214
* Maps to the `conflicts` option in yargs, ensures that if this flag is set, others in the array cannot be
1315
*/
1416
exclusive?: readonly string[];
17+
/**
18+
* Whether the argument should be prefilled from stdin
19+
* @default false
20+
*/
21+
stdin?: StdinMode;
1522
}
1623

1724
export interface StringFlagOptions<Choices extends readonly string[] = readonly string[]> extends BaseFlagOptions {
@@ -34,6 +41,7 @@ export type TaggedFlagBuilder<Tag extends FlagTag, ChoicesType = unknown, Requir
3441
choicesType: ChoicesType;
3542
required: Required;
3643
hasDefault: HasDefault;
44+
stdin: StdinMode;
3745
};
3846

3947
export const Flags = {
@@ -65,13 +73,15 @@ function stringFlag<const Choices, const T extends StringFlagOptions<readonly st
6573
default: options.default,
6674
choices: options.choices,
6775
string: true,
76+
// we only require something be passed in if we don't have a default or read from stdin
6877
nargs: 1,
69-
requiresArg: true,
78+
requiresArg: !(options.default ?? options.stdin),
7079
});
7180
},
7281
choicesType: options.choices as Choices,
7382
required: (options.required ?? false) as never,
7483
hasDefault: options.default,
84+
stdin: options.stdin ?? StdinMode.Raw,
7585
};
7686
}
7787

@@ -102,6 +112,7 @@ function booleanFlag<const T extends BooleanFlagOptions>(
102112
choicesType: null as never,
103113
required: (options.required ?? false) as never,
104114
hasDefault: options.default,
115+
stdin: options.stdin ?? StdinMode.Raw,
105116
};
106117
}
107118

@@ -134,5 +145,6 @@ function integerFlag<const T extends IntegerFlagOptions>(
134145
choicesType: null as never,
135146
required: (options.required ?? false) as never,
136147
hasDefault: options.default,
148+
stdin: options.stdin ?? StdinMode.Raw,
137149
};
138150
}

0 commit comments

Comments
 (0)