Skip to content

Commit

Permalink
feature(eas-cli): add worker --json support for 3rd parties and cus…
Browse files Browse the repository at this point in the history
…tom tooling (#2561)

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

* docs(eas-cli): add changelog entry

* refactor(eas-cli): rename `expoDashboardUrl` -> `dashboardUrl`

* chore(eas-cli): resolve linting issues

* docs(eas-cli): fix changelog entry
  • Loading branch information
byCedric authored Sep 13, 2024
1 parent 66ce64e commit fa32629
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This is the log of notable changes to EAS CLI and related packages.
- Add `worker --id` flag to use a custom deployment identifier. ([#2552](https://github.com/expo/eas-cli/pull/2552) by [@byCedric](https://github.com/byCedric)))
- 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)))

### 🐛 Bug fixes

Expand Down
71 changes: 55 additions & 16 deletions packages/eas-cli/src/commands/worker/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { EnvironmentVariableEnvironment } from '../../graphql/generated';
import Log from '../../log';
import { ora } from '../../ora';
import formatFields, { FormatFieldsItem } from '../../utils/formatFields';
import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json';
import { createProgressTracker } from '../../utils/progress';
import * as WorkerAssets from '../../worker/assets';
import {
Expand Down Expand Up @@ -84,11 +85,19 @@ export default class WorkerDeploy extends EasCommand {
};

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(WorkerDeploy);
const flags = this.sanitizeFlags(rawFlags);

if (flags.json) {
enableJsonOutput();
}

const {
getDynamicPrivateProjectConfigAsync,
loggedIn: { graphqlClient },
Expand Down Expand Up @@ -259,17 +268,16 @@ export default class WorkerDeploy extends EasCommand {

await uploadAssetsAsync(assetMap, deployResult.uploads);

let deploymentAliasUrl: string | null = null;
let deploymentAlias: null | Awaited<ReturnType<typeof assignWorkerDeploymentAliasAsync>> = null;
if (flags.aliasName) {
progress = ora(chalk`Assigning alias {bold ${flags.aliasName}} to deployment`).start();
try {
const workerAlias = await assignWorkerDeploymentAliasAsync({
deploymentAlias = await assignWorkerDeploymentAliasAsync({
graphqlClient,
appId: projectId,
deploymentId: deployResult.id,
aliasName: flags.aliasName,
});
deploymentAliasUrl = workerAlias.url;

// Only stop the spinner when not promoting to production
if (!flags.isProduction) {
Expand All @@ -281,7 +289,9 @@ export default class WorkerDeploy extends EasCommand {
}
}

let deploymentProductionUrl: string | null = null;
let deploymentProdAlias: null | Awaited<
ReturnType<typeof assignWorkerDeploymentProductionAsync>
> = null;
if (flags.isProduction) {
try {
if (!flags.aliasName) {
Expand All @@ -290,12 +300,11 @@ export default class WorkerDeploy extends EasCommand {
progress.text = chalk`Promoting deployment to {bold production}`;
}

const workerProdAlias = await assignWorkerDeploymentProductionAsync({
deploymentProdAlias = await assignWorkerDeploymentProductionAsync({
graphqlClient,
appId: projectId,
deploymentId: deployResult.id,
});
deploymentProductionUrl = workerProdAlias.url;

progress.succeed(
!flags.aliasName
Expand All @@ -311,10 +320,12 @@ export default class WorkerDeploy extends EasCommand {
const expoBaseDomain = process.env.EXPO_STAGING ? 'staging.expo' : 'expo';

logDeployment({
json: flags.json,
expoDashboardUrl: `https://${expoBaseDomain}.dev/projects/${projectId}/serverless/deployments`,
deploymentId: deployResult.id,
deploymentUrl: `https://${deployResult.fullName}.${expoBaseDomain}.app`,
aliasedUrl: deploymentAliasUrl,
productionUrl: deploymentProductionUrl,
deploymentAlias,
deploymentProdAlias,
});
}

Expand All @@ -331,13 +342,41 @@ export default class WorkerDeploy extends EasCommand {
}

type LogDeploymentOptions = {
json: boolean;
expoDashboardUrl: string;
deploymentId: string;
deploymentUrl: string;
aliasedUrl?: string | null;
productionUrl?: string | null;
deploymentAlias?: null | Awaited<ReturnType<typeof assignWorkerDeploymentAliasAsync>>;
deploymentProdAlias?: null | Awaited<ReturnType<typeof assignWorkerDeploymentProductionAsync>>;
};

function logDeployment(options: LogDeploymentOptions): void {
if (options.json) {
printJsonOnlyOutput({
dashboardUrl: options.expoDashboardUrl,
deployment: {
id: options.deploymentId,
url: options.deploymentUrl,
aliases: !options.deploymentAlias
? undefined
: [
{
id: options.deploymentAlias.id,
name: options.deploymentAlias.aliasName,
url: options.deploymentAlias.url,
},
],
production: !options.deploymentProdAlias
? undefined
: {
id: options.deploymentProdAlias.id,
url: options.deploymentProdAlias.url,
},
},
});
return;
}

Log.addNewLineIfNone();
Log.log(`🎉 Your deployment is ready`);
Log.addNewLineIfNone();
Expand All @@ -347,19 +386,19 @@ function logDeployment(options: LogDeploymentOptions): void {
{ label: 'Deployment URL', value: options.deploymentUrl },
];

if (options.aliasedUrl) {
fields.push({ label: 'Alias URL', value: options.aliasedUrl });
if (options.deploymentAlias) {
fields.push({ label: 'Alias URL', value: options.deploymentAlias.url });
}
if (options.productionUrl) {
fields.push({ label: 'Production URL', value: options.productionUrl });
if (options.deploymentProdAlias) {
fields.push({ label: 'Production URL', value: options.deploymentProdAlias.url });
}

const lastUrlField = fields[fields.length - 1];
lastUrlField.value = chalk.cyan(lastUrlField.value);

Log.log(formatFields(fields));

if (!options.productionUrl) {
if (!options.deploymentProdAlias) {
Log.addNewLineIfNone();
Log.log('🚀 When you are ready to deploy to production:');
Log.log(chalk` $ eas deploy {bold --prod}`);
Expand Down

0 comments on commit fa32629

Please sign in to comment.