Skip to content

Commit 1044e0e

Browse files
committed
fix: add force:package:beta:install:report command
1 parent 0bb9fed commit 1044e0e

File tree

3 files changed

+57
-45
lines changed

3 files changed

+57
-45
lines changed

messages/package_install.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,6 @@ The subscriber package version alias: [%s] isn't defined in the sfdx-project.jso
138138

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

141-
# invalidPackageId
142-
143-
The subscriber package version ID: [%s] is invalid. It must start with "04t".
144-
145-
# invalidIdLength
146-
147-
The subscriber package version ID: [%s] is invalid. It must be either 15 or 18 characters.
148-
149141
# promptEnableRss
150142

151143
This package might send or receive data from these third-party websites:

src/commands/force/package/beta/install.ts

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ export class Install extends SfdxCommand {
8787

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

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

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

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

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

139-
const pkgInstallRequest = await pkg.install(request, installOptions);
137+
const pkgInstallRequest = await this.pkg.install(request, installOptions);
140138
const { Status } = pkgInstallRequest;
141139
if (Status === 'SUCCESS') {
142-
this.display(messages.getMessage('packageInstallSuccess', [this.flags.package]));
140+
this.ux.log(messages.getMessage('packageInstallSuccess', [this.flags.package]));
143141
} else if (['IN_PROGRESS', 'UNKNOWN'].includes(Status)) {
144-
this.display(messages.getMessage('packageInstallInProgress', [pkgInstallRequest.Id, this.org.getUsername()]));
142+
this.ux.log(messages.getMessage('packageInstallInProgress', [pkgInstallRequest.Id, this.org.getUsername()]));
145143
} else {
146144
throw messages.createError('packageInstallError', [this.parseInstallErrors(pkgInstallRequest)]);
147145
}
148146

149147
return pkgInstallRequest;
150148
}
151149

152-
private display(message: string): void {
153-
if (!this.flags.json) {
154-
this.ux.log(message);
155-
}
156-
}
157-
158150
private async confirmUpgradeType(request: PackageInstallCreateRequest, noPrompt: boolean): Promise<void> {
159151
const pkgType = await getPackageTypeBy04t(request.SubscriberPackageVersionKey, this.connection, request.Password);
160152
if (pkgType === 'Unlocked' && !noPrompt) {
@@ -180,13 +172,11 @@ export class Install extends SfdxCommand {
180172
}
181173

182174
private async waitForPublish(request: PackageInstallCreateRequest): Promise<void> {
183-
if (!this.flags.json) {
184-
// eslint-disable-next-line @typescript-eslint/require-await
185-
Lifecycle.getInstance().on('SubscriberPackageVersion:status', async (status: string) => {
186-
const tokens = status ? [` Status = ${status}`] : [];
187-
this.ux.log(messages.getMessage('publishWaitProgress', tokens));
188-
});
189-
}
175+
// eslint-disable-next-line @typescript-eslint/require-await
176+
Lifecycle.getInstance().on('SubscriberPackageVersion:status', async (status: string) => {
177+
const tokens = status ? [` Status = ${status}`] : [];
178+
this.ux.log(messages.getMessage('publishWaitProgress', tokens));
179+
});
190180

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

227217
if (idOrAlias.startsWith('04t')) {
218+
Package.validateId(idOrAlias, 'SubscriberPackageVersionId');
228219
resolvedId = idOrAlias;
229220
} else {
230221
let packageAliases: { [k: string]: string };
@@ -238,13 +229,7 @@ export class Install extends SfdxCommand {
238229
if (!resolvedId) {
239230
throw messages.createError('packageAliasNotFound', [idOrAlias]);
240231
}
241-
if (!resolvedId.startsWith('04t')) {
242-
throw messages.createError('invalidPackageId', [resolvedId]);
243-
}
244-
}
245-
246-
if (![15, 18].includes(resolvedId.length)) {
247-
throw messages.createError('invalidIdLength', [resolvedId]);
232+
Package.validateId(resolvedId, 'SubscriberPackageVersionId');
248233
}
249234

250235
return resolvedId;

src/commands/force/package/beta/install/report.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
99
import { Messages } from '@salesforce/core';
10+
import { Package, PackagingSObjects } from '@salesforce/packaging';
11+
12+
type PackageInstallRequest = PackagingSObjects.PackageInstallRequest;
1013

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

14-
export class PackageInstallReportCommand extends SfdxCommand {
18+
export class Report extends SfdxCommand {
1519
public static readonly description = messages.getMessage('cliDescription');
1620
public static readonly longDescription = messages.getMessage('cliDescriptionLong');
1721
public static readonly help = messages.getMessage('help');
@@ -26,8 +30,39 @@ export class PackageInstallReportCommand extends SfdxCommand {
2630
}),
2731
};
2832

29-
public async run(): Promise<unknown> {
30-
process.exitCode = 1;
31-
return Promise.resolve('Not yet implemented');
33+
public async run(): Promise<PackageInstallRequest> {
34+
const connection = this.org.getConnection();
35+
const pkg = new Package({ connection });
36+
const installRequestId = this.flags.requestid as string;
37+
Package.validateId(installRequestId, 'PackageInstallRequestId');
38+
const pkgInstallRequest = await pkg.getInstallStatus(installRequestId);
39+
this.parseStatus(pkgInstallRequest);
40+
41+
return pkgInstallRequest;
42+
}
43+
44+
// @fixme: refactor with install code and any others
45+
private parseStatus(request: PackageInstallRequest): void {
46+
const { Status } = request;
47+
if (Status === 'SUCCESS') {
48+
this.ux.log(installMsgs.getMessage('packageInstallSuccess', [request.Id]));
49+
} else if (['IN_PROGRESS', 'UNKNOWN'].includes(Status)) {
50+
this.ux.log(installMsgs.getMessage('packageInstallInProgress', [request.Id, this.org.getUsername()]));
51+
} else {
52+
throw installMsgs.createError('packageInstallError', [this.parseInstallErrors(request)]);
53+
}
54+
}
55+
56+
// @fixme: refactor with install code and any others
57+
private parseInstallErrors(request: PackageInstallRequest): string {
58+
const errors = request?.Errors?.errors;
59+
if (errors?.length) {
60+
let errorMessage = 'Installation errors: ';
61+
for (let i = 0; i < errors.length; i++) {
62+
errorMessage += `\n${i + 1}) ${errors[i].message}`;
63+
}
64+
return errorMessage;
65+
}
66+
return '<empty>';
3267
}
3368
}

0 commit comments

Comments
 (0)