Skip to content

Commit

Permalink
fix: mso
Browse files Browse the repository at this point in the history
  • Loading branch information
soridalac committed Dec 17, 2024
1 parent 1732d41 commit 428b378
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 52 deletions.
76 changes: 38 additions & 38 deletions src/commands/org/create/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import { Duration } from '@salesforce/kit';
import { MultiStageOutput } from '@oclif/multi-stage-output';
import { Flags } from '@salesforce/sf-plugins-core';
import { Lifecycle, Messages, SandboxEvents, SandboxRequest, SfError } from '@salesforce/core';
import { Interfaces } from '@oclif/core';
Expand Down Expand Up @@ -192,73 +191,74 @@ export default class CreateSandbox extends SandboxCommandBase<SandboxCommandResp

this.prodOrg = this.flags['target-org'];

this.registerLifecycleListeners(lifecycle, {
isAsync: this.flags.async,
setDefault: this.flags['set-default'],
alias: this.flags.alias,
prodOrg: this.prodOrg,
tracksSource: this.flags['no-track-source'] === true ? false : undefined,
});
const sandboxReq = await this.createSandboxRequest();
await this.confirmSandboxReq({
...sandboxReq,
});
this.initSandboxProcessData(sandboxReq);

const mso = new MultiStageOutput<{ status: string; id: string }>({
stages: this.flags.async
? ['Prepare request', 'Send request', 'Done']
: ['Prepare request', 'Send request', 'Waiting for org to respond', 'Done'],
title: this.flags.async ? 'Create Sandbox (async)' : 'Create Sandbox',
jsonEnabled: false,
postStagesBlock: [
{
label: 'Sandbox ID',
get: (data) => data?.id,
type: 'dynamic-key-value',
bold: true,
},
{
label: 'Status',
get: (data) => data?.status,
type: 'dynamic-key-value',
bold: true,
},
],
this.registerLifecycleListeners(lifecycle, {
isAsync: this.flags.async,
setDefault: this.flags['set-default'],
alias: this.flags.alias,
prodOrg: this.prodOrg,
tracksSource: this.flags['no-track-source'] === true ? false : undefined,
});

mso.goto('Prepare request', { status: 'Pending' });
// const mso = new MultiStageOutput<{ status: string; id: string }>({
// stages: this.flags.async
// ? ['Prepare request', 'Send request', 'Done']
// : ['Prepare request', 'Send request', 'Waiting for org to respond', 'Done'],
// title: this.flags.async ? 'Create Sandbox (async)' : 'Create Sandbox',
// jsonEnabled: false,
// postStagesBlock: [
// {
// label: 'Sandbox ID',
// get: (data) => data?.id,
// type: 'dynamic-key-value',
// bold: true,
// },
// {
// label: 'Status',
// get: (data) => data?.status,
// type: 'dynamic-key-value',
// bold: true,
// },
// ],
// });

// this.mso.goto('Prepare request', { status: 'Pending' });

if (!this.flags.async) {
mso.skipTo('Sandbox Create', { status: 'Pending' });
this.spinner.start('Sandbox Create');
// this.mso.skipTo('Sandbox Create', { status: 'Pending' });
}

this.debug('Calling create with SandboxRequest: %s ', sandboxReq);

try {
mso.goto('Send request', { status: 'In Progress' });
// this.mso.goto('Send request', { status: 'In Progress' });
const sandboxProcessObject = await this.prodOrg.createSandbox(sandboxReq, {
wait: this.flags.wait,
interval: this.flags['poll-interval'],
async: this.flags.async,
});
// console.log('Sandbox Process Object', sandboxProcessObject);

mso.updateData({ status: sandboxProcessObject.Status, id: sandboxProcessObject.Id });
// this.mso.updateData({ status: sandboxProcessObject.Status, id: sandboxProcessObject.Id });

this.latestSandboxProgressObj = sandboxProcessObject;
this.saveSandboxProgressConfig();

mso.goto('Done', { status: sandboxProcessObject.Status, id: sandboxProcessObject.Id });
// this.mso.goto('Done', { status: sandboxProcessObject.Status, id: sandboxProcessObject.Id });
if (this.flags.async) {
mso.goto('Waiting for org to respond', { status: 'Pending', id: sandboxProcessObject.Id });
mso.skipTo('Done');
mso.stop();
// this.mso.goto('Waiting for org to respond', { status: 'Pending', id: sandboxProcessObject.Id });
// this.mso.skipTo('Done');
// this.mso.stop();
process.exitCode = 68;
}
return this.getSandboxCommandResponse();
} catch (err) {
mso.error();
// this.mso.error();
if (this.pollingTimeOut && this.latestSandboxProgressObj) {
void lifecycle.emit(SandboxEvents.EVENT_ASYNC_RESULT, undefined);
process.exitCode = 68;
Expand Down
73 changes: 59 additions & 14 deletions src/shared/sandboxCommandBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import os from 'node:os';

import { MultiStageOutput } from '@oclif/multi-stage-output';
import { SfCommand } from '@salesforce/sf-plugins-core';
import { Config } from '@oclif/core';
import {
Expand All @@ -22,7 +22,13 @@ import {
StatusEvent,
} from '@salesforce/core';
import { SandboxProgress } from './sandboxProgress.js';
import { State } from './stagedProgress.js';
// import { State } from './stagedProgress.js';

type SandboxData = {
sandboxStatus: StatusEvent;
status: string;
id: string;
};

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-org', 'sandboxbase');
Expand Down Expand Up @@ -50,6 +56,7 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
: this.constructor.name === 'CreateSandbox'
? 'Create'
: 'Create/Refresh';

this.sandboxProgress = new SandboxProgress({ action: this.action });
}
protected async getSandboxRequestConfig(): Promise<SandboxRequestCache> {
Expand Down Expand Up @@ -89,6 +96,34 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
lifecycle: Lifecycle,
options: { isAsync: boolean; alias?: string; setDefault?: boolean; prodOrg?: Org; tracksSource?: boolean }
): void {
const mso = new MultiStageOutput<SandboxData>({
stages: ['Creating new sandbox', 'Authenticating'],
title: 'Create Sandbox',
jsonEnabled: false,
postStagesBlock: [
{
label: 'Sandbox ID',
get: (data): string | undefined => data?.id,
type: 'dynamic-key-value',
bold: true,
},
{
label: 'Status',
get: (data): string | undefined => data?.status,
type: 'dynamic-key-value',
bold: true,
},
{
label: 'Copy Progress',
get: (data): string | undefined => {
if (data?.sandboxStatus.sandboxProcessObj.CopyProgress)
return `${data?.sandboxStatus.sandboxProcessObj.CopyProgress}`;
},
type: 'dynamic-key-value',
bold: true,
},
],
});
lifecycle.on('POLLING_TIME_OUT', async () => {
this.pollingTimeOut = true;
this.warn('Sandbox creation process time out.');
Expand All @@ -97,6 +132,7 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
const sandboxId = this.latestSandboxProgressObj.Id;
if (sandboxId) {
this.info(`Sandbox ID: ${sandboxId}`);
mso.updateData({ status: this.latestSandboxProgressObj.Status });
}
}
return Promise.resolve(this.updateSandboxRequestData());
Expand All @@ -114,7 +150,7 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
this.latestSandboxProgressObj = results ?? this.latestSandboxProgressObj;
this.updateSandboxRequestData();
if (!options.isAsync) {
this.spinner.stop();
// this.spinner.stop();
}
// things that require data on latestSandboxProgressObj
if (this.latestSandboxProgressObj) {
Expand All @@ -124,7 +160,7 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
});
const currentStage = progress.status;
this.sandboxProgress.markPreviousStagesAsCompleted(currentStage);
this.updateStage(currentStage, 'inProgress');
// this.updateStage(currentStage, 'inProgress');
this.updateProgress(
{ sandboxProcessObj: this.latestSandboxProgressObj, sandboxRes: undefined },
options.isAsync
Expand All @@ -140,15 +176,15 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
lifecycle.on(SandboxEvents.EVENT_STATUS, async (results: StatusEvent) => {
this.latestSandboxProgressObj = results.sandboxProcessObj;
const sandboxId = this.latestSandboxProgressObj?.Id;

// results.sandboxProcessObj.Status;
if (sandboxId) {
this.info(`Sandbox ID: ${sandboxId}`);
}

this.updateSandboxRequestData();
const progress = this.sandboxProgress.getSandboxProgress(results);
const currentStage = progress.status;
this.updateStage(currentStage, 'inProgress');
// const progress = this.sandboxProgress.getSandboxProgress(results);
// const currentStage = progress.status;
// this.updateStage(currentStage, 'inProgress');
return Promise.resolve(this.updateProgress(results, options.isAsync));
});

Expand Down Expand Up @@ -231,16 +267,25 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
sandboxProcessObj: event.sandboxProcessObj,
};
if (!isAsync) {
this.spinner.status = this.sandboxProgress.formatProgressStatus();
// this.spinner.status = this.sandboxProgress.formatProgressStatus();
}
}

protected updateStage(stage: string | undefined, state: State): void {
if (stage) {
this.sandboxProgress.transitionStages(stage, state);
}
// Dynamically update MultiStageOutput status
// const currentStage = this.sandboxProgress.getSandboxProgress(event)?.status;
// this.mso.updateStage(currentStage, 'inProgress');

// if (event.sandboxProcessObj) {
// this.mso.updateProgress(event.sandboxProcessObj, true);
// }
}

// protected updateStage(stage: string | undefined, state: State): void {
// if (stage) {
// this.sandboxProgress.transitionStages(stage, state);
// this.mso.updateData(stage, state);
// }
// }

protected updateSandboxRequestData(): void {
if (this.sandboxRequestData && this.latestSandboxProgressObj) {
this.sandboxRequestData.sandboxProcessObject = this.latestSandboxProgressObj;
Expand Down

0 comments on commit 428b378

Please sign in to comment.