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

feat:Allow specifying environment using flag for redeploying existing project #28

Merged
merged 5 commits into from
Mar 20, 2025
Merged
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
50 changes: 44 additions & 6 deletions src/adapters/base-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
fileFrameworkQuery,
createDeploymentMutation,
cmsEnvironmentVariablesQuery,
environmentsQuery
} from '../graphql';
import {
LogFn,
Expand All @@ -36,6 +37,7 @@ import {
EmitMessage,
DeploymentLogResp,
ServerLogResp,
Environment,
} from '../types';

export default class BaseClass {
Expand Down Expand Up @@ -85,14 +87,14 @@ export default class BaseClass {
* @return {*} {Promise<void>}
* @memberof GitHub
*/
async createNewDeployment(skipGitData = false, uploadUid?: string): Promise<void> {
async createNewDeployment(skipGitData = false, environmentUid:string, uploadUid?: string): Promise<void> {
const deployment: Record<string, any> = {
environment: (first(this.config.currentConfig.environments) as Record<string, any>)?.uid,
environment: environmentUid
};

if (uploadUid) {
deployment.uploadUid = uploadUid;
}
}

await this.apolloClient
.mutate({
Expand Down Expand Up @@ -394,7 +396,7 @@ export default class BaseClass {
data.project = this.config.currentConfig;
}

writeFileSync(this.config.config, JSON.stringify(data), {
writeFileSync(this.config.config, JSON.stringify(data, null, 2), {
encoding: 'utf8',
flag: 'w',
});
Expand Down Expand Up @@ -675,7 +677,7 @@ export default class BaseClass {
},
baseUrl: this.config.manageApiBaseUrl,
}).apolloClient;
this.config.environment = (last(this.config.currentConfig.environments) as Record<string, any>)?.uid;
this.config.environment = (await this.getEnvironment()).uid;
this.config.deployment = (last(this.config.currentConfig.deployments) as Record<string, any>)?.uid;
const logs = new LogPolling({
config: this.config,
Expand Down Expand Up @@ -744,6 +746,42 @@ export default class BaseClass {
this.exit(1);
}

async getEnvironment(): Promise<Environment> | never {
const environmentFlagInput = this.config['environment'];

if (!environmentFlagInput) {
const defaultEnvironment = (first(this.config.currentConfig.environments) as Environment);
this.setEnvironmentOnConfig(defaultEnvironment as Environment);
return defaultEnvironment;
}
const environmentList = await this.fetchEnvironments();
let environment = environmentList.find((env: Environment) => env.name === environmentFlagInput || env.uid === environmentFlagInput);

if (!environment) {
this.log(`Environment "${environmentFlagInput}" not found in this project. Please provide a valid environment name or UID.`, 'error');
this.exit(1);
}

environment = environment as Environment;
this.setEnvironmentOnConfig(environment);
return environment;
}

async setEnvironmentOnConfig(environment: Environment): Promise<void> {
this.config.environment = environment.uid;
}

async fetchEnvironments(): Promise<Environment[]> | never {
try {
const { data } = await this.apolloClient.query({ query: environmentsQuery });
const environments = map(data.Environments.edges, 'node');
return environments;
} catch (error: unknown) {
this.log(error instanceof Error ? error.message : String(error), 'error');
process.exit(1);
}
}

/**
* @method showDeploymentUrl - show deployment URL and open it on browser
*
Expand Down Expand Up @@ -791,4 +829,4 @@ export default class BaseClass {
});
}
}
}
}
12 changes: 8 additions & 4 deletions src/adapters/file-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { print } from '../util';
import BaseClass from './base-class';
import { getFileList } from '../util/fs';
import { createSignedUploadUrlMutation, importProjectMutation } from '../graphql';
import { SignedUploadUrlData, FileUploadMethod } from '../types/launch';
import { SignedUploadUrlData, FileUploadMethod, DeploymentStatus } from '../types/launch';
import config from '../config';

export default class FileUpload extends BaseClass {
Expand All @@ -26,18 +26,22 @@ export default class FileUpload extends BaseClass {
*/
async run(): Promise<void> {
if (this.config.isExistingProject) {
await this.handleExistingProject();
const environment = await this.getEnvironment();
await this.handleExistingProject(environment.uid);
} else {
await this.handleNewProject();
}

this.prepareLaunchConfig();
await this.showLogs();
if(this.config.currentDeploymentStatus === DeploymentStatus.FAILED) {
this.exit(1);
}
this.showDeploymentUrl();
this.showSuggestion();
}

private async handleExistingProject(): Promise<void> {
private async handleExistingProject(environment: string): Promise<void> {
await this.initApolloClient();

let redeployLatest = this.config['redeploy-latest'];
Expand All @@ -63,7 +67,7 @@ export default class FileUpload extends BaseClass {
await this.uploadFile(zipName, zipPath, signedUploadUrlData);
}

await this.createNewDeployment(true, uploadUid);
await this.createNewDeployment(true, environment, uploadUid);
}

private async confirmRedeployment(): Promise<void> {
Expand Down
11 changes: 8 additions & 3 deletions src/adapters/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { print } from '../util';
import BaseClass from './base-class';
import { getRemoteUrls } from '../util/create-git-meta';
import { repositoriesQuery, userConnectionsQuery, importProjectMutation } from '../graphql';
import { DeploymentStatus } from '../types';

export default class GitHub extends BaseClass {
/**
Expand All @@ -22,18 +23,22 @@ export default class GitHub extends BaseClass {
*/
async run(): Promise<void> {
if (this.config.isExistingProject) {
await this.handleExistingProject();
const environment = await this.getEnvironment();
await this.handleExistingProject(environment.uid);
} else {
await this.handleNewProject();
}

this.prepareLaunchConfig();
await this.showLogs();
if(this.config.currentDeploymentStatus === DeploymentStatus.FAILED) {
this.exit(1);
}
this.showDeploymentUrl();
this.showSuggestion();
}

private async handleExistingProject(): Promise<void> {
private async handleExistingProject(environmentUid:string): Promise<void> {
await this.initApolloClient();

const redeployLastUpload = this.config['redeploy-last-upload'];
Expand All @@ -48,7 +53,7 @@ export default class GitHub extends BaseClass {
await this.confirmLatestRedeployment();
}

await this.createNewDeployment();
await this.createNewDeployment(false, environmentUid);
}

private async confirmLatestRedeployment(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/pre-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class PreCheck extends BaseClass {
this.log('Current Project details:', { bold: true, color: 'green' });
this.log(''); // Empty line
const { name, projectType, repository, environments } = this.config.currentConfig;
const [environment] = environments;
const environment = await this.getEnvironment();

const detail: Record<string, any> = {
'Project Name': name,
Expand Down
3 changes: 2 additions & 1 deletion src/commands/launch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export default class Launch extends BaseCommand<typeof Launch> {
'<%= config.bin %> <%= command.id %> --type <options: GitHub|FileUpload>',
'<%= config.bin %> <%= command.id %> --data-dir <path/of/current/working/dir> --type <options: GitHub|FileUpload>',
'<%= config.bin %> <%= command.id %> --data-dir <path/of/current/working/dir> --redeploy-latest',
'<%= config.bin %> <%= command.id %> --data-dir <path/of/current/working/dir> --redeploy-latest --redeploy-last-upload',
'<%= config.bin %> <%= command.id %> --data-dir <path/of/current/working/dir> --redeploy-last-upload',
'<%= config.bin %> <%= command.id %> --config <path/to/launch/config/file> --type <options: GitHub|FileUpload>',
'<%= config.bin %> <%= command.id %> --environment=<value> --redeploy-latest',
'<%= config.bin %> <%= command.id %> --config <path/to/launch/config/file> --type <options: GitHub|FileUpload> --name=<value> --environment=<value> --branch=<value> --build-command=<value> --framework=<option> --org=<value> --out-dir=<value>',
'<%= config.bin %> <%= command.id %> --config <path/to/launch/config/file> --type <options: GitHub|FileUpload> --name=<value> --environment=<value> --branch=<value> --build-command=<value> --framework=<option> --org=<value> --out-dir=<value> --server-command=<value>',
'<%= config.bin %> <%= command.id %> --config <path/to/launch/config/file> --type <options: GitHub|FileUpload> --name=<value> --environment=<value> --branch=<value> --build-command=<value> --framework=<option> --org=<value> --out-dir=<value> --variable-type="Import variables from a stack" --alias=<value>',
Expand Down
17 changes: 17 additions & 0 deletions src/types/launch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ type SignedUploadUrlData = {
uploadUid: string;
};

export type Environment = {
uid: string;
name: string;
frameworkPreset: string;
};

export enum DeploymentStatus {
QUEUED = 'QUEUED',
LIVE = 'LIVE',
DEPLOYED = 'DEPLOYED',
ARCHIVED = 'ARCHIVED',
DEPLOYING = 'DEPLOYING',
SKIPPED = 'SKIPPED',
FAILED = 'FAILED',
CANCELLED = 'CANCELLED',
}

export {
LogFn,
ExitFn,
Expand Down
5 changes: 4 additions & 1 deletion src/util/logs-polling-utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ApolloClient, ObservableQuery } from '@apollo/client/core';

import { LogPollingInput, ConfigType } from '../types';
import { deploymentQuery, deploymentLogsQuery, serverlessLogsQuery } from '../graphql';
import { setTimeout as sleep } from "timers/promises";

export default class LogPolling {
private config: ConfigType;
Expand Down Expand Up @@ -79,6 +80,7 @@ export default class LogPolling {
}
this.deploymentStatus = data?.Deployment?.status;
if (this.config.deploymentStatus.includes(this.deploymentStatus)) {
this.config.currentDeploymentStatus = this.deploymentStatus;
statusWatchQuery.stopPolling();
}
});
Expand Down Expand Up @@ -109,7 +111,7 @@ export default class LogPolling {
>,
): void {
let timestamp: number = 0;
logsWatchQuery.subscribe(({ data, errors, error }) => {
logsWatchQuery.subscribe(async({ data, errors, error }) => {
ux.action.start('Loading deployment logs...');

if (error) {
Expand Down Expand Up @@ -152,6 +154,7 @@ export default class LogPolling {
}

if (this.config.deploymentStatus.includes(this.deploymentStatus)) {
await sleep(1_000);
logsWatchQuery.stopPolling();
this.$event.emit('deployment-logs', {
message: 'DONE',
Expand Down
Loading