diff --git a/CHANGELOG.md b/CHANGELOG.md index 93ac7984c0..a92c5c42f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This is the log of notable changes to EAS CLI and related packages. - Add `worker --environment` flag to deploy with EAS environment variables. ([#2557](https://github.com/expo/eas-cli/pull/2557) by [@kitten](https://github.com/kitten))) - Add `worker --export-dir` flag to select exported directory. ([#2560](https://github.com/expo/eas-cli/pull/2560) by [@byCedric](https://github.com/byCedric))) - Add `worker --json` flag to allow integrating with 3rd parties and custom tooling. ([#2561](https://github.com/expo/eas-cli/pull/2561) by [@byCedric](https://github.com/byCedric))) +- Add `worker:alias --json` flag to allow integrating with 3rd parties and custom tooling. ([#2562](https://github.com/expo/eas-cli/pull/2562) by [@byCedric](https://github.com/byCedric))) ### 🐛 Bug fixes diff --git a/packages/eas-cli/src/commands/worker/alias.ts b/packages/eas-cli/src/commands/worker/alias.ts index 66937c8a11..d3ea577cbc 100644 --- a/packages/eas-cli/src/commands/worker/alias.ts +++ b/packages/eas-cli/src/commands/worker/alias.ts @@ -3,15 +3,31 @@ import chalk from 'chalk'; import EasCommand from '../../commandUtils/EasCommand'; import { ExpoGraphqlClient } from '../../commandUtils/context/contextUtils/createGraphqlClient'; +import { EasNonInteractiveAndJsonFlags } from '../../commandUtils/flags'; import Log from '../../log'; import { ora } from '../../ora'; import { promptAsync } from '../../prompts'; import formatFields from '../../utils/formatFields'; +import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json'; import { assignWorkerDeploymentAliasAsync, selectWorkerDeploymentOnAppAsync, } from '../../worker/deployment'; +interface DeployAliasFlags { + nonInteractive: boolean; + json: boolean; + aliasName?: string; + deploymentIdentifier?: string; +} + +interface RawDeployAliasFlags { + 'non-interactive': boolean; + json: boolean; + alias?: string; + id?: string; +} + export default class WorkerAlias extends EasCommand { static override description = 'Assign deployment aliases'; static override aliases = ['deploy:alias']; @@ -31,6 +47,7 @@ export default class WorkerAlias extends EasCommand { helpValue: 'xyz123', required: false, }), + ...EasNonInteractiveAndJsonFlags, }; static override contextDefinition = { @@ -40,9 +57,19 @@ export default class WorkerAlias extends EasCommand { }; override async runAsync(): Promise { - Log.warn('EAS Worker Deployments are in beta and subject to breaking changes.'); + // NOTE(cedric): `Log.warn` uses `console.log`, which is incorrect when running with `--json` + // eslint-disable-next-line no-console + console.warn( + chalk.yellow('EAS Worker Deployments are in beta and subject to breaking changes.') + ); + + const { flags: rawFlags } = await this.parse(WorkerAlias); + const flags = this.sanitizeFlags(rawFlags); + + if (flags.json) { + enableJsonOutput(); + } - const { flags } = await this.parse(WorkerAlias); const { getDynamicPrivateProjectConfigAsync, loggedIn: { graphqlClient }, @@ -51,12 +78,12 @@ export default class WorkerAlias extends EasCommand { }); const { projectId } = await getDynamicPrivateProjectConfigAsync(); - const aliasName = await resolveDeploymentAliasAsync({ flagAlias: flags.alias }); + const aliasName = await resolveDeploymentAliasAsync(flags); const deploymentId = await resolveDeploymentIdAsync({ + ...flags, graphqlClient, + projectId, aliasName, - appId: projectId, - flagId: flags.id, }); const progress = ora( @@ -81,6 +108,23 @@ export default class WorkerAlias extends EasCommand { const expoBaseDomain = process.env.EXPO_STAGING ? 'staging.expo' : 'expo'; const expoDashboardUrl = `https://${expoBaseDomain}.dev/projects/${projectId}/serverless/deployments`; + if (flags.json) { + printJsonOnlyOutput({ + dashboardUrl: expoDashboardUrl, + deployment: { + id: deploymentId, + aliases: [ + { + id: workerAlias.id, + name: workerAlias.aliasName, + url: workerAlias.url, + }, + ], + }, + }); + return; + } + Log.addNewLineIfNone(); Log.log( formatFields([ @@ -89,11 +133,24 @@ export default class WorkerAlias extends EasCommand { ]) ); } + + private sanitizeFlags(flags: RawDeployAliasFlags): DeployAliasFlags { + return { + nonInteractive: flags['non-interactive'], + json: flags['json'], + aliasName: flags.alias?.trim().toLowerCase(), + deploymentIdentifier: flags.id?.trim().toLowerCase(), + }; + } } -async function resolveDeploymentAliasAsync({ flagAlias }: { flagAlias?: string }): Promise { - if (flagAlias?.trim()) { - return flagAlias.trim().toLowerCase(); +async function resolveDeploymentAliasAsync(flags: DeployAliasFlags): Promise { + if (flags.aliasName?.trim()) { + return flags.aliasName.trim().toLowerCase(); + } + + if (flags.nonInteractive) { + throw new Error('The `--alias` flag must be set when running in `--non-interactive` mode.'); } const { alias: aliasName } = await promptAsync({ @@ -109,22 +166,25 @@ async function resolveDeploymentAliasAsync({ flagAlias }: { flagAlias?: string } async function resolveDeploymentIdAsync({ graphqlClient, + deploymentIdentifier, aliasName, - appId, - flagId, -}: { + projectId, + nonInteractive, +}: DeployAliasFlags & { graphqlClient: ExpoGraphqlClient; - aliasName: string; - appId: string; - flagId?: string; + projectId: string; }): Promise { - if (flagId) { - return flagId; + if (deploymentIdentifier) { + return deploymentIdentifier; + } + + if (nonInteractive) { + throw new Error('The `--id` flag must be set when running in `--non-interactive` mode.'); } const deployment = await selectWorkerDeploymentOnAppAsync({ graphqlClient, - appId, + appId: projectId, selectTitle: chalk`deployment to assign the {underline ${aliasName}} alias`, });