|
7 | 7 |
|
8 | 8 | import * as os from 'os';
|
9 | 9 | 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'; |
12 | 12 | import { Duration } from '@salesforce/kit';
|
13 | 13 |
|
14 | 14 | type UninstallResult = PackagingSObjects.SubscriberPackageVersionUninstallRequest;
|
15 | 15 |
|
16 | 16 | Messages.importMessagesDirectory(__dirname);
|
17 | 17 | const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_uninstall');
|
| 18 | +const installMsgs = Messages.loadMessages('@salesforce/plugin-packaging', 'package_install'); |
18 | 19 |
|
19 | 20 | export class PackageUninstallCommand extends SfdxCommand {
|
20 | 21 | public static readonly description = messages.getMessage('cliDescription');
|
@@ -44,15 +45,38 @@ export class PackageUninstallCommand extends SfdxCommand {
|
44 | 45 | this.ux.log(`Waiting for the package uninstall request to get processed. Status = ${data.Status}`);
|
45 | 46 | });
|
46 | 47 |
|
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); |
51 | 49 |
|
52 | 50 | const result: UninstallResult = await uninstallPackage(packageId, this.org.getConnection(), this.flags.wait);
|
53 | 51 | const arg = result.Status === 'Success' ? [result.SubscriberPackageVersionId] : [result.Id, this.org.getUsername()];
|
54 | 52 | this.ux.log(messages.getMessage(result.Status, arg));
|
55 | 53 |
|
56 | 54 | return result;
|
57 | 55 | }
|
| 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 | + } |
58 | 82 | }
|
0 commit comments