Skip to content

Commit bc1614d

Browse files
committed
fix: several fixes for pkg commands
1 parent ea53915 commit bc1614d

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

messages/package_uninstall.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ The ID (starts with 04t) or alias of the package version to uninstall.
6060

6161
Include either a %s value or a %s value.
6262

63-
# invalidIdOrPackage
64-
65-
Invalid alias or ID: %s. Either your alias is invalid or undefined, or the ID provided is invalid (must start with 04t).
66-
6763
# InProgress
6864

6965
PackageUninstallRequest is currently InProgress.

src/commands/force/package/beta/installed/list.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class PackageInstalledListCommand extends SfdxCommand {
2929
public static readonly longDescription = messages.getMessage('cliLongDescription');
3030
public static readonly examples = messages.getMessage('examples').split(os.EOL);
3131
public static readonly requiresUsername = true;
32+
public static readonly requiresProject = true;
3233

3334
public async run(): Promise<PackageInstalledListResult[]> {
3435
const result = await packageInstalledList(this.org.getConnection());

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
import * as os from 'os';
99
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
10-
import { Lifecycle, Messages } from '@salesforce/core';
11-
import { getPackageIdFromAlias, PackagingSObjects, uninstallPackage } from '@salesforce/packaging';
10+
import { Lifecycle, Messages, SfProject } from '@salesforce/core';
11+
import { Package, PackagingSObjects, uninstallPackage } from '@salesforce/packaging';
1212
import { Duration } from '@salesforce/kit';
1313

1414
type UninstallResult = PackagingSObjects.SubscriberPackageVersionUninstallRequest;
1515

1616
Messages.importMessagesDirectory(__dirname);
1717
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_uninstall');
18+
const installMsgs = Messages.loadMessages('@salesforce/plugin-packaging', 'package_install');
1819

1920
export class PackageUninstallCommand extends SfdxCommand {
2021
public static readonly description = messages.getMessage('cliDescription');
@@ -44,15 +45,38 @@ export class PackageUninstallCommand extends SfdxCommand {
4445
this.ux.log(`Waiting for the package uninstall request to get processed. Status = ${data.Status}`);
4546
});
4647

47-
const packageId = getPackageIdFromAlias(this.flags.package, this.project);
48-
if (!packageId.startsWith('04t')) {
49-
throw messages.createError('invalidIdOrPackage', [packageId]);
50-
}
48+
const packageId = this.resolveSubscriberPackageVersionKey(this.flags.package);
5149

5250
const result: UninstallResult = await uninstallPackage(packageId, this.org.getConnection(), this.flags.wait);
5351
const arg = result.Status === 'Success' ? [result.SubscriberPackageVersionId] : [result.Id, this.org.getUsername()];
5452
this.ux.log(messages.getMessage(result.Status, arg));
5553

5654
return result;
5755
}
56+
57+
// Given a package version ID (04t) or an alias for the package, validate and
58+
// return the package version ID (aka SubscriberPackageVersionKey).
59+
private resolveSubscriberPackageVersionKey(idOrAlias: string): string {
60+
let resolvedId: string;
61+
62+
if (idOrAlias.startsWith('04t')) {
63+
Package.validateId(idOrAlias, 'SubscriberPackageVersionId');
64+
resolvedId = idOrAlias;
65+
} else {
66+
let packageAliases: { [k: string]: string };
67+
try {
68+
const projectJson = SfProject.getInstance().getSfProjectJson();
69+
packageAliases = projectJson.getContents().packageAliases ?? {};
70+
} catch (e) {
71+
throw installMsgs.createError('projectNotFound', [idOrAlias]);
72+
}
73+
resolvedId = packageAliases[idOrAlias];
74+
if (!resolvedId) {
75+
throw installMsgs.createError('packageAliasNotFound', [idOrAlias]);
76+
}
77+
Package.validateId(resolvedId, 'SubscriberPackageVersionId');
78+
}
79+
80+
return resolvedId;
81+
}
5882
}

0 commit comments

Comments
 (0)