Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"handlebars": "~4.7.8",
"ignore": "^7.0.0",
"indent-string": "^5.0.0",
"intl-messageformat": "^11.2.1",
"is-ci": "~4.1.0",
"istextorbinary": "~9.5.0",
"jju": "~1.4.0",
Expand Down
9 changes: 4 additions & 5 deletions src/commands/actor/calculate-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { Flags } from '../../lib/command-framework/flags.js';
import { CommandExitCodes } from '../../lib/consts.js';
import { useActorConfig } from '../../lib/hooks/useActorConfig.js';
import { error, info, success } from '../../lib/outputs.js';
import { getJsonFileContent, getLocalKeyValueStorePath } from '../../lib/utils.js';

const DEFAULT_INPUT_PATH = join(getLocalKeyValueStorePath('default'), 'INPUT.json');
Expand Down Expand Up @@ -77,7 +76,7 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul
const inputPath = resolve(process.cwd(), input);
const inputJson = getJsonFileContent(inputPath) ?? {};

info({ message: `Evaluating memory expression: ${memoryExpression}` });
this.logger.stderr.info(`Evaluating memory expression: ${memoryExpression}`);

try {
const result = await calculateRunDynamicMemory(memoryExpression, {
Expand All @@ -86,9 +85,9 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul
});
const clampedResult = Math.min(Math.max(result, minMemory), maxMemory);

success({ message: `Calculated memory: ${clampedResult} MB`, stdout: true });
this.logger.stdout.success(`Calculated memory: ${clampedResult} MB`);
} catch (err) {
error({ message: `Memory calculation failed: ${(err as Error).message}` });
this.logger.stderr.error(`Memory calculation failed: ${(err as Error).message}`);
}
}

Expand Down Expand Up @@ -132,7 +131,7 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul
if (localConfigResult.isErr()) {
const { message, cause } = localConfigResult.unwrapErr();

error({ message: `${message}${cause ? `\n ${cause.message}` : ''}` });
this.logger.stderr.error(`${message}${cause ? `\n ${cause.message}` : ''}`);
process.exitCode = CommandExitCodes.InvalidActorJson;
return {};
}
Expand Down
22 changes: 9 additions & 13 deletions src/commands/actor/charge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { getApifyTokenFromEnvOrAuthFile } from '../../lib/actor.js';
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { Args } from '../../lib/command-framework/args.js';
import { Flags } from '../../lib/command-framework/flags.js';
import { info } from '../../lib/outputs.js';
import { getLoggedClient } from '../../lib/utils.js';

/**
Expand Down Expand Up @@ -52,18 +51,16 @@ export class ActorChargeCommand extends ApifyCommand<typeof ActorChargeCommand>
const isAtHome = Boolean(process.env.APIFY_IS_AT_HOME);

if (!isAtHome) {
info({
message: `No platform detected: would charge ${count} events of type "${eventName}" with idempotency key "${idempotencyKey ?? 'not-provided'}".`,
stdout: true,
});
this.logger.stdout.info(
`No platform detected: would charge ${count} events of type "${eventName}" with idempotency key "${idempotencyKey ?? 'not-provided'}".`,
);
return;
}

if (testPayPerEvent) {
info({
message: `PPE test mode: would charge ${count} events of type "${eventName}" with idempotency key "${idempotencyKey ?? 'not-provided'}".`,
stdout: true,
});
this.logger.stdout.info(
`PPE test mode: would charge ${count} events of type "${eventName}" with idempotency key "${idempotencyKey ?? 'not-provided'}".`,
);
return;
}

Expand All @@ -83,10 +80,9 @@ export class ActorChargeCommand extends ApifyCommand<typeof ActorChargeCommand>
throw new Error('Charge command can only be used with pay-per-event pricing model.');
}

info({
message: `Charging ${count} events of type "${eventName}" with idempotency key "${idempotencyKey ?? 'not-provided'}" (runId: ${runId}).`,
stdout: true,
});
this.logger.stdout.info(
`Charging ${count} events of type "${eventName}" with idempotency key "${idempotencyKey ?? 'not-provided'}" (runId: ${runId}).`,
);
await apifyClient.run(runId).charge({
eventName,
count,
Expand Down
45 changes: 24 additions & 21 deletions src/commands/actor/generate-schema-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
readOutputSchema,
readStorageSchema,
} from '../../lib/input_schema.js';
import { error, info, success, warning } from '../../lib/outputs.js';
import {
clearAllRequired,
makePropertiesRequired,
Expand Down Expand Up @@ -118,7 +117,7 @@ Optionally specify custom schema path to use.`;
const outputFile = path.join(outputDir, `${name}.ts`);
await writeFile(outputFile, result, 'utf-8');

success({ message: `Generated types written to ${outputFile}` });
this.logger.stderr.success(`Generated types written to ${outputFile}`);

// When no custom path is provided, also generate types from additional schemas
if (!this.args.path) {
Expand All @@ -134,9 +133,9 @@ Optionally specify custom schema path to use.`;
for (const [i, schemaResult] of schemaResults.entries()) {
if (schemaResult.status === 'rejected') {
anyFailed = true;
error({
message: `Failed to generate types for ${schemaLabels[i]} schema: ${schemaResult.reason instanceof Error ? schemaResult.reason.message : String(schemaResult.reason)}`,
});
this.logger.stderr.error(
`Failed to generate types for ${schemaLabels[i]} schema: ${schemaResult.reason instanceof Error ? schemaResult.reason.message : String(schemaResult.reason)}`,
);
}
}

Expand Down Expand Up @@ -164,15 +163,17 @@ Optionally specify custom schema path to use.`;
const { datasetSchema, datasetSchemaPath } = datasetResult;

if (datasetSchemaPath) {
info({ message: `[experimental] Generating types from Dataset schema at ${datasetSchemaPath}` });
this.logger.stderr.info(`[experimental] Generating types from Dataset schema at ${datasetSchemaPath}`);
} else {
info({ message: `[experimental] Generating types from Dataset schema embedded in '${LOCAL_CONFIG_PATH}'` });
this.logger.stderr.info(
`[experimental] Generating types from Dataset schema embedded in '${LOCAL_CONFIG_PATH}'`,
);
}

const prepared = prepareFieldsSchemaForCompilation(datasetSchema);

if (!prepared) {
warning({ message: 'Dataset schema has no fields defined, skipping type generation.' });
this.logger.stderr.warning('Dataset schema has no fields defined, skipping type generation.');
return;
}

Expand All @@ -185,7 +186,7 @@ Optionally specify custom schema path to use.`;
const outputFile = path.join(outputDir, `${datasetName}.ts`);
await writeFile(outputFile, result, 'utf-8');

success({ message: `Generated types written to ${outputFile}` });
this.logger.stderr.success(`Generated types written to ${outputFile}`);
}

private async generateOutputTypes({
Expand All @@ -206,15 +207,17 @@ Optionally specify custom schema path to use.`;
const { outputSchema, outputSchemaPath } = outputResult;

if (outputSchemaPath) {
info({ message: `[experimental] Generating types from Output schema at ${outputSchemaPath}` });
this.logger.stderr.info(`[experimental] Generating types from Output schema at ${outputSchemaPath}`);
} else {
info({ message: `[experimental] Generating types from Output schema embedded in '${LOCAL_CONFIG_PATH}'` });
this.logger.stderr.info(
`[experimental] Generating types from Output schema embedded in '${LOCAL_CONFIG_PATH}'`,
);
}

const prepared = prepareOutputSchemaForCompilation(outputSchema);

if (!prepared) {
warning({ message: 'Output schema has no properties defined, skipping type generation.' });
this.logger.stderr.warning('Output schema has no properties defined, skipping type generation.');
return;
}

Expand All @@ -227,7 +230,7 @@ Optionally specify custom schema path to use.`;
const outputFile = path.join(outputDir, `${outputName}.ts`);
await writeFile(outputFile, result, 'utf-8');

success({ message: `Generated types written to ${outputFile}` });
this.logger.stderr.success(`Generated types written to ${outputFile}`);
}

private async generateKvsTypes({
Expand All @@ -248,19 +251,19 @@ Optionally specify custom schema path to use.`;
const { schema: kvsSchema, schemaPath: kvsSchemaPath } = kvsResult;

if (kvsSchemaPath) {
info({ message: `[experimental] Generating types from Key-Value Store schema at ${kvsSchemaPath}` });
this.logger.stderr.info(`[experimental] Generating types from Key-Value Store schema at ${kvsSchemaPath}`);
} else {
info({
message: `[experimental] Generating types from Key-Value Store schema embedded in '${LOCAL_CONFIG_PATH}'`,
});
this.logger.stderr.info(
`[experimental] Generating types from Key-Value Store schema embedded in '${LOCAL_CONFIG_PATH}'`,
);
}

const collections = prepareKvsCollectionsForCompilation(kvsSchema);

if (collections.length === 0) {
warning({
message: 'Key-Value Store schema has no collections with JSON schemas, skipping type generation.',
});
this.logger.stderr.warning(
'Key-Value Store schema has no collections with JSON schemas, skipping type generation.',
);
return;
}

Expand All @@ -281,6 +284,6 @@ Optionally specify custom schema path to use.`;
const outputFile = path.join(outputDir, 'key-value-store.ts');
await writeFile(outputFile, parts.join('\n'), 'utf-8');

success({ message: `Generated types written to ${outputFile}` });
this.logger.stderr.success(`Generated types written to ${outputFile}`);
}
}
15 changes: 7 additions & 8 deletions src/commands/actor/get-public-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { getApifyStorageClient } from '../../lib/actor.js';
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { Args } from '../../lib/command-framework/args.js';
import { CommandExitCodes } from '../../lib/consts.js';
import { error } from '../../lib/outputs.js';

export class ActorGetPublicUrlCommand extends ApifyCommand<typeof ActorGetPublicUrlCommand> {
static override name = 'get-public-url' as const;
Expand All @@ -24,17 +23,17 @@ export class ActorGetPublicUrlCommand extends ApifyCommand<typeof ActorGetPublic
const { key } = this.args;

if ([undefined, 'false', ''].includes(process.env[APIFY_ENV_VARS.IS_AT_HOME])) {
error({ message: 'get-public-url is not yet implemented for local development' });
this.logger.stderr.error('get-public-url is not yet implemented for local development');
process.exitCode = CommandExitCodes.NotImplemented;
return;
}
const storeId = process.env[ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID];

// This should never happen, but handle it gracefully to prevent crashes.
if (!storeId) {
error({
message: `Missing environment variable: ${ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID}. Please set it before running the command.`,
});
this.logger.stderr.error(
`Missing environment variable: ${ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID}. Please set it before running the command.`,
);
process.exitCode = CommandExitCodes.InvalidInput;
return;
}
Expand All @@ -43,9 +42,9 @@ export class ActorGetPublicUrlCommand extends ApifyCommand<typeof ActorGetPublic
const store = await apifyClient.keyValueStore(storeId).get();

if (!store) {
error({
message: `Key-Value store with ID '${storeId}' was not found. Ensure the store exists and that the correct ID is set in ${ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID}.`,
});
this.logger.stderr.error(
`Key-Value store with ID '${storeId}' was not found. Ensure the store exists and that the correct ID is set in ${ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID}.`,
);
process.exitCode = CommandExitCodes.NotFound;
return;
}
Expand Down
3 changes: 1 addition & 2 deletions src/commands/actor/push-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { cachedStdinInput } from '../../entrypoints/_shared.js';
import { APIFY_STORAGE_TYPES, getApifyStorageClient, getDefaultStorageId } from '../../lib/actor.js';
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { Args } from '../../lib/command-framework/args.js';
import { error } from '../../lib/outputs.js';

export class ActorPushDataCommand extends ApifyCommand<typeof ActorPushDataCommand> {
static override name = 'push-data' as const;
Expand All @@ -28,7 +27,7 @@ export class ActorPushDataCommand extends ApifyCommand<typeof ActorPushDataComma
const item = _item || cachedStdinInput;

if (!item) {
error({ message: 'No item was provided.' });
this.logger.stderr.error('No item was provided.');
return;
}

Expand Down
24 changes: 9 additions & 15 deletions src/commands/actors/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ import { Flags } from '../../lib/command-framework/flags.js';
import { getInputOverride } from '../../lib/commands/resolve-input.js';
import { runActorOrTaskOnCloud, SharedRunOnCloudFlags } from '../../lib/commands/run-on-cloud.js';
import { CommandExitCodes, LOCAL_CONFIG_PATH } from '../../lib/consts.js';
import { error, simpleLog } from '../../lib/outputs.js';
import {
getLocalConfig,
getLocalUserInfo,
getLoggedClientOrThrow,
printJsonToStdout,
TimestampFormatter,
} from '../../lib/utils.js';
import { getLocalConfig, getLocalUserInfo, getLoggedClientOrThrow, TimestampFormatter } from '../../lib/utils.js';

export class ActorsCallCommand extends ApifyCommand<typeof ActorsCallCommand> {
static override name = 'call' as const;
Expand Down Expand Up @@ -80,7 +73,9 @@ export class ActorsCallCommand extends ApifyCommand<typeof ActorsCallCommand> {
const usernameOrId = userInfo.username || (userInfo.id as string);

if (this.flags.json && this.flags.outputDataset) {
error({ message: 'You cannot use both the --json and --output-dataset flags when running this command.' });
this.logger.stderr.error(
'You cannot use both the --json and --output-dataset flags when running this command.',
);
process.exitCode = CommandExitCodes.InvalidInput;

return;
Expand Down Expand Up @@ -196,25 +191,24 @@ export class ActorsCallCommand extends ApifyCommand<typeof ActorsCallCommand> {
// url
message.push(`${chalk.blue('View on Apify Console')}: ${url}`, '');

simpleLog({ message: message.join('\n'), stdout: !this.flags.json });
(!this.flags.json ? this.logger.stdout : this.logger.stderr).log(message.join('\n'));
}
}
}

if (this.flags.json) {
printJsonToStdout(run!);
this.logger.stdout.json(run!);
return;
}

if (!this.flags.silent) {
simpleLog({
message: [
this.logger.stdout.log(
[
'',
`${chalk.blue('Export results')}: ${datasetUrl!}`,
`${chalk.blue('View on Apify Console')}: ${url!}`,
].join('\n'),
stdout: true,
});
);
}

if (this.flags.outputDataset) {
Expand Down
Loading