Skip to content

Commit

Permalink
feature(eas-cli): add worker:alias --json support for 3rd parties a…
Browse files Browse the repository at this point in the history
…nd custom tooling (#2562)

* feature(eas-cli): add `worker:alias --json` flag to integrate with 3rd parties

* docs(eas-cli): add changelog entry

* docs(eas-cli): fix changelog entry

* fix(eas-cli): lowercase deployment identifier

* fix(eas-cli): resolve linting issues
  • Loading branch information
byCedric authored Sep 13, 2024
1 parent fa32629 commit 47e1989
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
94 changes: 77 additions & 17 deletions packages/eas-cli/src/commands/worker/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -31,6 +47,7 @@ export default class WorkerAlias extends EasCommand {
helpValue: 'xyz123',
required: false,
}),
...EasNonInteractiveAndJsonFlags,
};

static override contextDefinition = {
Expand All @@ -40,9 +57,19 @@ export default class WorkerAlias extends EasCommand {
};

override async runAsync(): Promise<void> {
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 },
Expand All @@ -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(
Expand All @@ -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([
Expand All @@ -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<string> {
if (flagAlias?.trim()) {
return flagAlias.trim().toLowerCase();
async function resolveDeploymentAliasAsync(flags: DeployAliasFlags): Promise<string> {
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({
Expand All @@ -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<string> {
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`,
});

Expand Down

0 comments on commit 47e1989

Please sign in to comment.