Skip to content

Commit

Permalink
fix: add force:package:beta:install:report command
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Aug 5, 2022
1 parent 0bb9fed commit 1044e0e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
8 changes: 0 additions & 8 deletions messages/package_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,6 @@ The subscriber package version alias: [%s] isn't defined in the sfdx-project.jso

Add it to the packageDirectories section and add the alias to packageAliases with its 04t ID.

# invalidPackageId

The subscriber package version ID: [%s] is invalid. It must start with "04t".

# invalidIdLength

The subscriber package version ID: [%s] is invalid. It must be either 15 or 18 characters.

# promptEnableRss

This package might send or receive data from these third-party websites:
Expand Down
51 changes: 18 additions & 33 deletions src/commands/force/package/beta/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ export class Install extends SfdxCommand {

public async run(): Promise<PackageInstallRequest> {
const noPrompt = this.flags.noprompt as boolean;
const connection = (this.connection = this.org.getConnection());
const pkg = (this.pkg = new Package({ connection }));
this.connection = this.org.getConnection();
this.pkg = new Package({ connection: this.connection });

const apiVersion = parseInt(connection.getApiVersion(), 10);
const apiVersion = parseInt(this.connection.getApiVersion(), 10);
if (apiVersion < 36) {
throw messages.createError('apiVersionTooLow');
}
Expand All @@ -105,7 +105,7 @@ export class Install extends SfdxCommand {

// eslint-disable-next-line @typescript-eslint/require-await
Lifecycle.getInstance().on('PackageInstallRequest:warning', async (warningMsg: string) => {
this.display(warningMsg);
this.ux.log(warningMsg);
});

// If the user has specified --upgradetype Delete, then prompt for confirmation
Expand All @@ -128,33 +128,25 @@ export class Install extends SfdxCommand {
pollingTimeout: this.flags.wait as Duration,
};

if (!this.flags.json) {
// eslint-disable-next-line @typescript-eslint/require-await
Lifecycle.getInstance().on('PackageInstallRequest:status', async (piRequest: PackageInstallRequest) => {
this.ux.log(messages.getMessage('packageInstallPolling', [piRequest?.Status]));
});
}
// eslint-disable-next-line @typescript-eslint/require-await
Lifecycle.getInstance().on('PackageInstallRequest:status', async (piRequest: PackageInstallRequest) => {
this.ux.log(messages.getMessage('packageInstallPolling', [piRequest?.Status]));
});
}

const pkgInstallRequest = await pkg.install(request, installOptions);
const pkgInstallRequest = await this.pkg.install(request, installOptions);
const { Status } = pkgInstallRequest;
if (Status === 'SUCCESS') {
this.display(messages.getMessage('packageInstallSuccess', [this.flags.package]));
this.ux.log(messages.getMessage('packageInstallSuccess', [this.flags.package]));
} else if (['IN_PROGRESS', 'UNKNOWN'].includes(Status)) {
this.display(messages.getMessage('packageInstallInProgress', [pkgInstallRequest.Id, this.org.getUsername()]));
this.ux.log(messages.getMessage('packageInstallInProgress', [pkgInstallRequest.Id, this.org.getUsername()]));
} else {
throw messages.createError('packageInstallError', [this.parseInstallErrors(pkgInstallRequest)]);
}

return pkgInstallRequest;
}

private display(message: string): void {
if (!this.flags.json) {
this.ux.log(message);
}
}

private async confirmUpgradeType(request: PackageInstallCreateRequest, noPrompt: boolean): Promise<void> {
const pkgType = await getPackageTypeBy04t(request.SubscriberPackageVersionKey, this.connection, request.Password);
if (pkgType === 'Unlocked' && !noPrompt) {
Expand All @@ -180,13 +172,11 @@ export class Install extends SfdxCommand {
}

private async waitForPublish(request: PackageInstallCreateRequest): Promise<void> {
if (!this.flags.json) {
// eslint-disable-next-line @typescript-eslint/require-await
Lifecycle.getInstance().on('SubscriberPackageVersion:status', async (status: string) => {
const tokens = status ? [` Status = ${status}`] : [];
this.ux.log(messages.getMessage('publishWaitProgress', tokens));
});
}
// eslint-disable-next-line @typescript-eslint/require-await
Lifecycle.getInstance().on('SubscriberPackageVersion:status', async (status: string) => {
const tokens = status ? [` Status = ${status}`] : [];
this.ux.log(messages.getMessage('publishWaitProgress', tokens));
});

// wait for the Subscriber Package Version ID to become available in the target org
try {
Expand Down Expand Up @@ -225,6 +215,7 @@ export class Install extends SfdxCommand {
let resolvedId: string;

if (idOrAlias.startsWith('04t')) {
Package.validateId(idOrAlias, 'SubscriberPackageVersionId');
resolvedId = idOrAlias;
} else {
let packageAliases: { [k: string]: string };
Expand All @@ -238,13 +229,7 @@ export class Install extends SfdxCommand {
if (!resolvedId) {
throw messages.createError('packageAliasNotFound', [idOrAlias]);
}
if (!resolvedId.startsWith('04t')) {
throw messages.createError('invalidPackageId', [resolvedId]);
}
}

if (![15, 18].includes(resolvedId.length)) {
throw messages.createError('invalidIdLength', [resolvedId]);
Package.validateId(resolvedId, 'SubscriberPackageVersionId');
}

return resolvedId;
Expand Down
43 changes: 39 additions & 4 deletions src/commands/force/package/beta/install/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@

import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import { Package, PackagingSObjects } from '@salesforce/packaging';

type PackageInstallRequest = PackagingSObjects.PackageInstallRequest;

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_install_report');
const installMsgs = Messages.loadMessages('@salesforce/plugin-packaging', 'package_install');

export class PackageInstallReportCommand extends SfdxCommand {
export class Report extends SfdxCommand {
public static readonly description = messages.getMessage('cliDescription');
public static readonly longDescription = messages.getMessage('cliDescriptionLong');
public static readonly help = messages.getMessage('help');
Expand All @@ -26,8 +30,39 @@ export class PackageInstallReportCommand extends SfdxCommand {
}),
};

public async run(): Promise<unknown> {
process.exitCode = 1;
return Promise.resolve('Not yet implemented');
public async run(): Promise<PackageInstallRequest> {
const connection = this.org.getConnection();
const pkg = new Package({ connection });
const installRequestId = this.flags.requestid as string;
Package.validateId(installRequestId, 'PackageInstallRequestId');
const pkgInstallRequest = await pkg.getInstallStatus(installRequestId);
this.parseStatus(pkgInstallRequest);

return pkgInstallRequest;
}

// @fixme: refactor with install code and any others
private parseStatus(request: PackageInstallRequest): void {
const { Status } = request;
if (Status === 'SUCCESS') {
this.ux.log(installMsgs.getMessage('packageInstallSuccess', [request.Id]));
} else if (['IN_PROGRESS', 'UNKNOWN'].includes(Status)) {
this.ux.log(installMsgs.getMessage('packageInstallInProgress', [request.Id, this.org.getUsername()]));
} else {
throw installMsgs.createError('packageInstallError', [this.parseInstallErrors(request)]);
}
}

// @fixme: refactor with install code and any others
private parseInstallErrors(request: PackageInstallRequest): string {
const errors = request?.Errors?.errors;
if (errors?.length) {
let errorMessage = 'Installation errors: ';
for (let i = 0; i < errors.length; i++) {
errorMessage += `\n${i + 1}) ${errors[i].message}`;
}
return errorMessage;
}
return '<empty>';
}
}

0 comments on commit 1044e0e

Please sign in to comment.