Skip to content

Commit

Permalink
chore: remove unused constructor prop, break inheritance chain
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jun 6, 2024
1 parent 650e132 commit a3f7a67
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 79 deletions.
5 changes: 2 additions & 3 deletions src/commands/force/mdapi/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from '../../../deployCommand.js';
import { DeployCommandAsyncResult } from '../../../formatters/source/deployAsyncResultFormatter.js';
import { MdDeployResult, MdDeployResultFormatter } from '../../../formatters/mdapi/mdDeployResultFormatter.js';
import { ProgressFormatter } from '../../../formatters/progressFormatter.js';
import { DeployProgressBarFormatter } from '../../../formatters/deployProgressBarFormatter.js';
import { DeployProgressStatusFormatter } from '../../../formatters/deployProgressStatusFormatter.js';
import { MdDeployAsyncResultFormatter } from '../../../formatters/mdapi/mdDeployAsyncResultFormatter.js';
Expand Down Expand Up @@ -205,8 +204,8 @@ export class Deploy extends DeployCommand {

if (!this.isAsync) {
if (!this.jsonEnabled()) {
const progressFormatter: ProgressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }))
const progressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter()
: new DeployProgressStatusFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }));
progressFormatter.progress(deploy);
}
Expand Down
5 changes: 2 additions & 3 deletions src/commands/force/mdapi/deploy/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import { Interfaces } from '@oclif/core';
import { MdDeployResult, MdDeployResultFormatter } from '../../../../formatters/mdapi/mdDeployResultFormatter.js';
import { DeployCommand, getCoverageFormattersOptions, reportsFormatters } from '../../../../deployCommand.js';
import { ProgressFormatter } from '../../../../formatters/progressFormatter.js';
import { DeployProgressBarFormatter } from '../../../../formatters/deployProgressBarFormatter.js';
import { DeployProgressStatusFormatter } from '../../../../formatters/deployProgressStatusFormatter.js';

Expand Down Expand Up @@ -112,8 +111,8 @@ export class Report extends DeployCommand {

const deploy = this.createDeploy(this.org.getConnection(), deployId);
if (!this.jsonEnabled()) {
const progressFormatter: ProgressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }))
const progressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter()
: new DeployProgressStatusFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }));
progressFormatter.progress(deploy);
}
Expand Down
5 changes: 2 additions & 3 deletions src/commands/force/source/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
DeployAsyncResultFormatter,
DeployCommandAsyncResult,
} from '../../../formatters/source/deployAsyncResultFormatter.js';
import { ProgressFormatter } from '../../../formatters/progressFormatter.js';
import { DeployProgressBarFormatter } from '../../../formatters/deployProgressBarFormatter.js';
import { DeployProgressStatusFormatter } from '../../../formatters/deployProgressStatusFormatter.js';
import { filterConflictsByComponentSet, trackingSetup, updateTracking } from '../../../trackingFunctions.js';
Expand Down Expand Up @@ -283,8 +282,8 @@ export class Deploy extends DeployCommand {
if (!this.isAsync) {
// we're not print JSON output
if (!this.jsonEnabled()) {
const progressFormatter: ProgressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }))
const progressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter()
: new DeployProgressStatusFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }), {
verbose: this.flags.verbose,
});
Expand Down
5 changes: 2 additions & 3 deletions src/commands/force/source/deploy/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
DeployReportCommandResult,
DeployReportResultFormatter,
} from '../../../../formatters/deployReportResultFormatter.js';
import { ProgressFormatter } from '../../../../formatters/progressFormatter.js';
import { DeployProgressBarFormatter } from '../../../../formatters/deployProgressBarFormatter.js';
import { DeployProgressStatusFormatter } from '../../../../formatters/deployProgressStatusFormatter.js';
import { ResultFormatterOptions } from '../../../../formatters/resultFormatter.js';
Expand Down Expand Up @@ -112,8 +111,8 @@ export class Report extends DeployCommand {
const waitDuration = this.flags.wait;
const deploy = this.createDeploy(this.flags['target-org'].getConnection(), deployId);
if (!this.jsonEnabled()) {
const progressFormatter: ProgressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }))
const progressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter()
: new DeployProgressStatusFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }));
progressFormatter.progress(deploy);
}
Expand Down
5 changes: 2 additions & 3 deletions src/commands/force/source/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
import { Interfaces } from '@oclif/core';
import { DeployCommand } from '../../../deployCommand.js';
import { PushResponse, PushResultFormatter } from '../../../formatters/source/pushResultFormatter.js';
import { ProgressFormatter } from '../../../formatters/progressFormatter.js';
import { DeployProgressBarFormatter } from '../../../formatters/deployProgressBarFormatter.js';
import { DeployProgressStatusFormatter } from '../../../formatters/deployProgressStatusFormatter.js';
import { trackingSetup, updateTracking } from '../../../trackingFunctions.js';
Expand Down Expand Up @@ -149,8 +148,8 @@ export default class Push extends DeployCommand {

// we're not print JSON output
if (!this.jsonEnabled()) {
const progressFormatter: ProgressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }))
const progressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter()
: new DeployProgressStatusFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }));
progressFormatter.progress(deploy);
}
Expand Down
44 changes: 20 additions & 24 deletions src/formatters/deployProgressBarFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,38 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { MetadataApiDeploy } from '@salesforce/source-deploy-retrieve';
import { once } from '@salesforce/kit';
import { MetadataApiDeploy, MetadataApiDeployStatus } from '@salesforce/source-deploy-retrieve';
import { SingleBar } from 'cli-progress';
import { Ux } from '@salesforce/sf-plugins-core';
import { ProgressFormatter } from './progressFormatter.js';

export class DeployProgressBarFormatter extends ProgressFormatter {
export class DeployProgressBarFormatter {
protected progressBar = new SingleBar({
format: 'DEPLOY PROGRESS | {bar} | {value}/{total} Components',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
linewrap: true,
noTTYOutput: Boolean(process.env.TERM === 'dumb' || !process.stdin.isTTY),
});
public constructor(ux: Ux) {
super(ux);
}

public constructor() {}

// displays the progress of the Deployment
public progress(deploy: MetadataApiDeploy): void {
const startProgressBar = once((componentTotal: number) => {
this.progressBar.start(componentTotal, 0);
});

deploy.onUpdate((data) => {
// the numCompTot. isn't computed right away, wait to start until we know how many we have
const total = data.numberComponentsTotal + data.numberTestsTotal;
if (data.numberComponentsTotal) {
startProgressBar(total);
this.progressBar.update(data.numberComponentsDeployed + data.numberTestsCompleted);
this.progressBar.start(0, 0);
deploy.onUpdate(
({
numberComponentsTotal,
numberTestsTotal,
numberComponentsDeployed,
numberTestsCompleted,
}: MetadataApiDeployStatus) => {
// the numberComponentsTotal isn't computed right away, wait to start until we know how many we have
const total = numberComponentsTotal + numberTestsTotal;
if (this.progressBar.getTotal() !== total) {
this.progressBar.setTotal(total);
}
this.progressBar.update(numberComponentsDeployed + numberTestsCompleted);
}

// the numTestsTot. isn't computed until validated as tests by the server, update the PB once we know
if (data.numberTestsTotal && data.numberComponentsTotal) {
this.progressBar.setTotal(total);
}
});
);

// any thing else should stop the progress bar
deploy.onFinish((data) => {
Expand Down
6 changes: 3 additions & 3 deletions src/sourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import { ComponentSet } from '@salesforce/source-deploy-retrieve';
import { getString, Optional } from '@salesforce/ts-types';
import { SingleBar } from 'cli-progress';
import { SfCommand } from '@salesforce/sf-plugins-core';
import { EnsureFsFlagOptions, FsError, ProgressBar } from './types.js';
import { EnsureFsFlagOptions, FsError } from './types.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-source', 'flags.validation');

export abstract class SourceCommand extends SfCommand<unknown> {
public static readonly DEFAULT_WAIT_MINUTES = 33;

protected progressBar?: ProgressBar;
protected progressBar?: SingleBar;
protected componentSet?: ComponentSet;

protected initProgressBar(): void {
Expand All @@ -31,7 +31,7 @@ export abstract class SourceCommand extends SfCommand<unknown> {
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
linewrap: true,
}) as ProgressBar;
});
}

/**
Expand Down
11 changes: 1 addition & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@

export type FsError = {
code: string;
} & Error
} & Error;

export type EnsureFsFlagOptions = {
flagName: string;
path?: string;
type: 'dir' | 'file' | 'any';
throwOnENOENT?: boolean;
}

export type ProgressBar = {
total?: number;
value?: number;
start: (total: number, startValue: number, payload?: object) => void;
update: (num: number, payload?: object) => void;
setTotal: (num: number) => void;
stop: () => void;
};
52 changes: 25 additions & 27 deletions test/commands/source/progressBarFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,33 @@ import { EventEmitter } from 'node:events';
import { MetadataApiDeploy } from '@salesforce/source-deploy-retrieve';
import { spyMethod } from '@salesforce/ts-sinon';
import { assert, expect } from 'chai';
import { Ux } from '@salesforce/sf-plugins-core';
import { TestContext } from '@salesforce/core/testSetup';
import { SingleBar } from 'cli-progress';
import { DeployProgressBarFormatter } from '../../../src/formatters/deployProgressBarFormatter.js';
import { ProgressBar } from '../../../src/types.js';

describe('Progress Bar Events', () => {
const sandbox = new TestContext().SANDBOX;
const username = '[email protected]';
const deploy = new MetadataApiDeploy({ usernameOrConnection: username, id: '123' });
const progressBarFormatter = new DeployProgressBarFormatter(Ux.prototype);
let bar: ProgressBar;
let progressBarFormatter: DeployProgressBarFormatter;
let bar: SingleBar;
let events: EventEmitter;

const overrideEvent = (event: EventEmitter) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore the deploy must be listening to the same EventEmitter emitting events
// @ts-expect-error the deploy must be listening to the same EventEmitter emitting events
deploy.event = event;
};

const getProgressbar = () =>
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore protected member access
progressBarFormatter.progressBar;
// @ts-expect-error protected member access
const getProgressbar = () => progressBarFormatter.progressBar;

afterEach(() => {
sandbox.restore();
});

beforeEach(() => {
events = new EventEmitter();
progressBarFormatter = new DeployProgressBarFormatter();
overrideEvent(events);
progressBarFormatter.progress(deploy);
bar = getProgressbar();
Expand All @@ -49,8 +47,8 @@ describe('Progress Bar Events', () => {
numberTestsCompleted: 0,
numberTestsTotal: 0,
});
expect(bar.value).to.equal(3);
expect(bar.total).to.equal(5);
expect(bar.getTotal()).to.equal(5);
expect(bar.getProgress()).to.equal(3 / 5);
events.emit('finish', {
response: {
numberComponentsTotal: 5,
Expand All @@ -68,17 +66,17 @@ describe('Progress Bar Events', () => {
numberTestsCompleted: 0,
numberTestsTotal: 0,
});
expect(bar.total).to.equal(10);
expect(bar.value).to.equal(3);
expect(bar.getTotal()).to.equal(10);
expect(bar.getProgress()).to.equal(3 / 10);
// deploy done, tests running
events.emit('update', {
numberComponentsTotal: 10,
numberComponentsDeployed: 10,
numberTestsCompleted: 5,
numberTestsTotal: 10,
});
expect(bar.total).to.equal(20);
expect(bar.value).to.equal(15);
expect(bar.getTotal()).to.equal(20);
expect(bar.getProgress()).to.equal(15 / 20);
// all done
events.emit('finish', {
response: {
Expand All @@ -88,7 +86,7 @@ describe('Progress Bar Events', () => {
numberTestsTotal: 10,
},
});
expect(bar.value).to.equal(20);
expect(bar.getProgress()).to.equal(20 / 20);
});

it('should update progress bar when server returns different calculated value', () => {
Expand All @@ -98,17 +96,17 @@ describe('Progress Bar Events', () => {
numberTestsCompleted: 0,
numberTestsTotal: 0,
});
expect(bar.total).to.equal(20);
expect(bar.value).to.equal(3);
expect(bar.getTotal()).to.equal(20);
expect(bar.getProgress()).to.equal(3 / 20);
// deploy done, tests running
events.emit('update', {
numberComponentsTotal: 20,
numberComponentsDeployed: 10,
numberTestsCompleted: 5,
numberTestsTotal: 10,
});
expect(bar.total).to.equal(30);
expect(bar.value).to.equal(15);
expect(bar.getTotal()).to.equal(30);
expect(bar.getProgress()).to.equal(15 / 30);
// all done - notice 19 comps. deployed
events.emit('finish', {
response: {
Expand All @@ -118,8 +116,8 @@ describe('Progress Bar Events', () => {
numberTestsTotal: 10,
},
});
expect(bar.value).to.equal(29);
expect(bar.total).to.equal(29);
expect(bar.getProgress()).to.equal(29 / 29);
expect(bar.getTotal()).to.equal(29);
});

it('should stop progress bar onCancel', () => {
Expand All @@ -131,8 +129,8 @@ describe('Progress Bar Events', () => {
numberTestsCompleted: 0,
numberTestsTotal: 0,
});
expect(bar.total).to.equal(10);
expect(bar.value).to.equal(3);
expect(bar.getTotal()).to.equal(10);
expect(bar.getProgress()).to.equal(3 / 10);

events.emit('cancel');
expect(stopSpy.calledOnce).to.be.true;
Expand All @@ -148,8 +146,8 @@ describe('Progress Bar Events', () => {
numberTestsTotal: 0,
});

expect(bar.total).to.equal(10);
expect(bar.value).to.equal(3);
expect(bar.getTotal()).to.equal(10);
expect(bar.getProgress()).to.equal(3 / 10);

try {
events.emit('error', new Error('error on deploy'));
Expand Down

0 comments on commit a3f7a67

Please sign in to comment.