-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathinstall.ts
266 lines (205 loc) · 10.4 KB
/
install.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { flags, SfdxCommand } from '@salesforce/command';
import { JsonArray, JsonMap } from '@salesforce/ts-types';
import { Messages, SfdxProjectJson, SfdxError } from '@salesforce/core';
const spawn = require('child-process-promise').spawn;
const packageIdPrefix = '0Ho';
const packageVersionIdPrefix = '04t';
const packageAliasesMap = [];
const defaultWait = 10;
// Initialize Messages with the current plugin directory
Messages.importMessagesDirectory(__dirname);
// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core,
// or any library that is using the messages framework can also be loaded this way.
const messages = Messages.loadMessages('texei-sfdx-plugin', 'install');
export default class Install extends SfdxCommand {
public static description = messages.getMessage('commandDescription');
public static examples = [
'$ texei:package:dependencies:install -u MyScratchOrg -v MyDevHub -k "1:MyPackage1Key 2: 3:MyPackage3Key" -b "DEV"'
];
protected static flagsConfig = {
installationkeys: flags.string({ char: 'k', required: false, description: 'installation key for key-protected packages (format is 1:MyPackage1Key 2: 3:MyPackage3Key... to allow some packages without installation key)' }),
branch: flags.string({ char: 'b', required: false, description: 'the package version’s branch' }),
packages: flags.string({ char: 'p', required: false, description: "comma-separated list of the packages to install related dependencies" }),
namespaces: flags.string({ char: 'n', required: false, description: 'filter package installation by namespace' }),
wait: flags.number({ char: 'w', required: false, description: 'number of minutes to wait for installation status (also used for publishwait). Default is 10' }),
noprompt: flags.boolean({ char: 'r', required: false, description: 'allow Remote Site Settings and Content Security Policy websites to send or receive data without confirmation' })
};
// Comment this out if your command does not require an org username
protected static requiresUsername = true;
// Comment this out if your command does not require a hub org username
protected static requiresDevhubUsername = true;
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
protected static requiresProject = true;
public async run(): Promise<any> {
const result = { installedPackages: {} };
const username = this.org.getUsername();
const options = SfdxProjectJson.getDefaultOptions();
const project = await SfdxProjectJson.create(options);
if (this.flags.packages != null) {
this.ux.log('Filtering by packages: ' + this.flags.packages);
}
if (this.flags.namespaces != null) {
this.ux.log('Filtering by namespaces: ' + this.flags.namespaces);
}
const packageAliases = project.get('packageAliases') || {};
if (typeof packageAliases !== undefined ) {
Object.entries(packageAliases).forEach(([key, value]) => {
packageAliasesMap[key] = value;
});
}
// Getting Package
const packagesToInstall = [];
const packageDirectories = project.get('packageDirectories') as JsonArray || [];
const packages = new Set();
if (this.flags.packages) {
for (let pkg of this.flags.packages.split(',')) {
packages.add(pkg.trim());
}
}
//see if no filter is true
const packagesNoFilter = (this.flags.packages == null);;
this.ux.startSpinner('Resolving dependencies');
for (let packageDirectory of packageDirectories) {
packageDirectory = packageDirectory as JsonMap;
const packageName = (packageDirectory.package && packageDirectory.package.toString()) ? packageDirectory.package.toString() : '';
// If the package is found, or if there isn't any package filtering
if (packages.has(packageName) || packagesNoFilter) {
const dependencies = packageDirectory.dependencies || [];
// TODO: Move all labels to message
if (dependencies && dependencies[0] !== undefined) {
this.ux.log(`Package dependencies found for package directory ${packageDirectory.path}`);
for (const dependency of (dependencies as JsonArray)) {
const packageInfo = { } as JsonMap;
const dependencyInfo = dependency as JsonMap;
const dependentPackage: string = ((dependencyInfo.packageId != null) ? dependencyInfo.packageId : dependencyInfo.package) as string;
const versionNumber: string = (dependencyInfo.versionNumber) as string;
const namespaces: string[] = this.flags.namespaces !== undefined ? this.flags.namespaces.split(',') : null;
if (dependentPackage == null) {
throw Error('Dependent package version unknow error.');
}
packageInfo.dependentPackage = dependentPackage;
packageInfo.versionNumber = versionNumber;
const packageVersionId = await this.getPackageVersionId(dependentPackage, versionNumber, namespaces);
if (packageVersionId != null) {
packageInfo.packageVersionId = packageVersionId;
packagesToInstall.push( packageInfo );
this.ux.log(` ${packageInfo.packageVersionId} : ${packageInfo.dependentPackage}${ packageInfo.versionNumber === undefined ? '' : ' ' + packageInfo.versionNumber }`);
}
}
} else {
this.ux.log(`No dependencies found for package directory ${packageDirectory.path}`);
}
// Removing package from packages flag list --> Used later to log if one of them wasn't found
if (packages && packages.has(packageName)) {
packages.delete(packageName);
}
}
}
// In case one package wasn't found when filtering by packages
if (packages && packages.size > 0) {
this.ux.log(`Following packages were used in the --packages flag but were not found in the packageDirectories:`);
for (let packageName of packages) {
this.ux.log(` ${packageName}`);
}
}
this.ux.stopSpinner('Done.');
if (packagesToInstall.length > 0) { // Installing Packages
// Getting Installation Key(s)
let installationKeys = this.flags.installationkeys;
if (installationKeys) {
installationKeys = installationKeys.trim();
installationKeys = installationKeys.split(' ');
// Format is 1: 2: 3: ... need to remove these
for (let keyIndex = 0; keyIndex < installationKeys.length; keyIndex++) {
const key = installationKeys[keyIndex].trim();
if (key.startsWith(`${keyIndex + 1}:`)) {
installationKeys[keyIndex] = key.substring(2);
} else {
// Format is not correct, throw an error
throw new SfdxError('Installation Key should have this format: 1:MyPackage1Key 2: 3:MyPackage3Key');
}
}
}
this.ux.log('\n');
let i = 0;
for (let packageInfo of packagesToInstall) {
packageInfo = packageInfo as JsonMap;
if (result.installedPackages.hasOwnProperty(packageInfo.packageVersionId)) {
this.ux.log(`PackageVersionId ${packageInfo.packageVersionId} already installed. Skipping...`);
continue;
}
// Split arguments to use spawn
const args = [];
args.push('force:package:install');
// USERNAME
args.push('--targetusername');
args.push(`${username}`);
// PACKAGE ID
args.push('--package');
args.push(`${packageInfo.packageVersionId}`);
// INSTALLATION KEY
if (installationKeys && installationKeys[i]) {
args.push('--installationkey');
args.push(`${installationKeys[i]}`);
}
// WAIT
const wait = this.flags.wait ? this.flags.wait.trim() : defaultWait;
args.push('--wait');
args.push(`${wait}`);
args.push('--publishwait');
args.push(`${wait}`);
// NOPROMPT
if (this.flags.noprompt) {
args.push('--noprompt');
}
// INSTALL PACKAGE
// TODO: Fix waiting messages that should not be visibile with --json
this.ux.log(`Installing package ${packageInfo.packageVersionId} : ${packageInfo.dependentPackage}${ packageInfo.versionNumber === undefined ? '' : ' ' + packageInfo.versionNumber }`);
await spawn('sfdx', args, { stdio: 'inherit' });
this.ux.log('\n');
result.installedPackages[packageInfo.packageVersionId] = packageInfo;
i++;
}
}
return { message: result };
}
private async getPackageVersionId(name: string, version: string, namespaces: string[]) {
let packageId = null;
// Keeping original name so that it can be used in error message if needed
let packageName = name;
// TODO: Some stuff are duplicated here, some code don't need to be executed for every package
// First look if it's an alias
if (typeof packageAliasesMap[packageName] !== 'undefined') {
packageName = packageAliasesMap[packageName];
}
if (packageName.startsWith(packageVersionIdPrefix)) {
// Package2VersionId is set directly
packageId = packageName;
} else if (packageName.startsWith(packageIdPrefix)) {
// Get Package version id from package + versionNumber
const vers = version.split('.');
let query = 'Select SubscriberPackageVersionId, IsPasswordProtected, IsReleased, Package2.NamespacePrefix ';
query += 'from Package2Version ';
query += `where Package2Id='${packageName}' and MajorVersion=${vers[0]} and MinorVersion=${vers[1]} and PatchVersion=${vers[2]} `;
if (namespaces != null) {
query += ` and Package2.NamespacePrefix IN ('${namespaces.join('\',\'')}')`;
}
// If Build Number isn't set to LATEST, look for the exact Package Version
if (vers[3] !== 'LATEST') {
query += `and BuildNumber=${vers[3]} `;
}
// If Branch is specified, use it to filter
if (this.flags.branch) {
query += `and Branch='${this.flags.branch.trim()}' `;
}
query += ' ORDER BY BuildNumber DESC Limit 1';
// Query DevHub to get the expected Package2Version
const conn = this.hubOrg.getConnection();
const resultPackageId = await conn.tooling.query(query) as any;
if (resultPackageId.size > 0) {
packageId = resultPackageId.records[0].SubscriberPackageVersionId;
}
}
return packageId;
}
}