|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
|
9 |
| -import { Messages } from '@salesforce/core'; |
| 9 | +import { Messages, SfProject } from '@salesforce/core'; |
| 10 | +import { Package, PackagingSObjects, PackageInstallOptions } from '@salesforce/packaging'; |
| 11 | +import PackageInstallRequest = PackagingSObjects.PackageInstallRequest; |
| 12 | +import PackageInstallCreateRequest = PackagingSObjects.PackageInstallCreateRequest; |
10 | 13 |
|
11 | 14 | Messages.importMessagesDirectory(__dirname);
|
12 | 15 | const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_install');
|
@@ -42,35 +45,70 @@ export class PackageInstallCommand extends SfdxCommand {
|
42 | 45 | char: 'p',
|
43 | 46 | description: messages.getMessage('package'),
|
44 | 47 | longDescription: messages.getMessage('packageLong'),
|
| 48 | + required: true, |
45 | 49 | }),
|
46 | 50 | apexcompile: flags.enum({
|
47 | 51 | char: 'a',
|
48 | 52 | description: messages.getMessage('apexCompile'),
|
49 | 53 | longDescription: messages.getMessage('apexCompileLong'),
|
50 |
| - |
51 | 54 | default: 'all',
|
52 | 55 | options: ['all', 'package'],
|
53 | 56 | }),
|
54 | 57 | securitytype: flags.enum({
|
55 | 58 | char: 's',
|
56 | 59 | description: messages.getMessage('securityType'),
|
57 | 60 | longDescription: messages.getMessage('securityTypeLong'),
|
58 |
| - |
59 | 61 | default: 'AdminsOnly',
|
60 | 62 | options: ['AllUsers', 'AdminsOnly'],
|
61 | 63 | }),
|
62 | 64 | upgradetype: flags.enum({
|
63 | 65 | char: 't',
|
64 | 66 | description: messages.getMessage('upgradeType'),
|
65 | 67 | longDescription: messages.getMessage('upgradeTypeLong'),
|
66 |
| - |
67 | 68 | default: 'Mixed',
|
68 | 69 | options: ['DeprecateOnly', 'Mixed', 'Delete'],
|
69 | 70 | }),
|
70 | 71 | };
|
71 | 72 |
|
72 |
| - public async run(): Promise<unknown> { |
73 |
| - process.exitCode = 1; |
74 |
| - return Promise.resolve('Not yet implemented'); |
| 73 | + public async run(): Promise<PackageInstallRequest> { |
| 74 | + const connection = this.org.getConnection(); |
| 75 | + const pkg = new Package({ connection }); |
| 76 | + |
| 77 | + const request: PackageInstallCreateRequest = { |
| 78 | + SubscriberPackageVersionKey: await this.resolveSubscriberPackageVersionKey(this.flags.package) |
| 79 | + }; |
| 80 | + |
| 81 | + const installOptions: PackageInstallOptions = { |
| 82 | + pollingTimeout: this.flags.wait, |
| 83 | + }; |
| 84 | + |
| 85 | + return pkg.install(request, installOptions); |
| 86 | + } |
| 87 | + |
| 88 | + // Given a package version ID (04t) or an alias for the package, |
| 89 | + // return the package version ID (aka SubscriberPackageVersionKey). |
| 90 | + private async resolveSubscriberPackageVersionKey(idOrAlias: string): Promise<string> { |
| 91 | + if (idOrAlias.startsWith('04t')) { |
| 92 | + return idOrAlias; |
| 93 | + } else { |
| 94 | + let packageAliases: { [k: string ]: string }; |
| 95 | + try { |
| 96 | + const projectJson = SfProject.getInstance().getSfProjectJson(); |
| 97 | + packageAliases = projectJson.getContents().packageAliases ?? {}; |
| 98 | + } catch (e) { |
| 99 | + // If not within a package, provide that error |
| 100 | + } |
| 101 | + const id = packageAliases[idOrAlias]; |
| 102 | + if (!id) { |
| 103 | + // throw NotFound error |
| 104 | + } |
| 105 | + if (!id.startsWith('04t')) { |
| 106 | + // throw InvalidSubscriberPackageVersion - doesn't start with 04t |
| 107 | + } |
| 108 | + if ([15,18].includes(id.length)) { |
| 109 | + // throw InvalidSubscriberPackageVersion - incorrect length |
| 110 | + } |
| 111 | + return id; |
| 112 | + } |
75 | 113 | }
|
76 | 114 | }
|
0 commit comments