Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(eas-cli): add worker --json support for 3rd parties and custom tooling #2561

Merged
merged 6 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This is the log of notable changes to EAS CLI and related packages.
- Add `worker --alias` flag to assign custom aliases when deploying. ([#2551](https://github.com/expo/eas-cli/pull/2551) by [@byCedric](https://github.com/byCedric)))
- 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 --json` flag to allow integrating with 3rd parties and custom tooling.

### 🐛 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 @@ -77,11 +78,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 @@ -252,17 +261,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 @@ -274,7 +282,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 @@ -283,12 +293,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 @@ -304,10 +313,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 @@ -323,13 +334,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 @@ -339,19 +378,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
Loading