/
install.ts
74 lines (57 loc) · 2.82 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
import { flags } from '@oclif/command';
import { SfdxCommand } from '@salesforce/command';
import child_process = require('child_process');
import request = require('request-promise-native');
import util = require('util');
const exec = util.promisify(child_process.exec);
export default class GithubPackageInstall extends SfdxCommand {
public static description = 'installs a package from github using the sfdx-project.json file (v43+) OR the latestVersion.json file convention';
public static examples = [
`sfdx shane:github:package:install -g someUser -r someRepo -u someOrg
// installs packageVersion (04t) from https://github.com/someUser/someRepo/sfdx-project.json or https://github.com/someUser/someRepo/latestVersion.json
`
];
protected static requiresUsername = true;
protected static flagsConfig = {
githubuser: {type: 'string', required: true, char: 'g', description: 'github username where the package lives' },
repo: {type: 'string', required: true, char: 'r', description: 'repo where the packages lives' }
// branch: { type: 'string', char: 'b', description: 'optional branch' })
};
protected static requiresProject = true;
public async run(): Promise<any> { // tslint:disable-line:no-any
let packageVersionId;
// first, look in the sfdx-project.json file, using the packaging output from v43
let url = `https://raw.githubusercontent.com/${this.flags.githubuser}/${this.flags.repo}/master/sfdx-project.json`;
const primaryResult = await request.get({
url,
json: true
});
// this.ux.log(`file at ${url} says:`);
if (primaryResult.packageAliases) {
// grab the last package version unless a version is specified
const packages = Object.values(primaryResult.packageAliases);
// this.ux.log(`packages are ${packages}`);
packageVersionId = packages[packages.length - 1 ];
if ( packageVersionId.startsWith('04t')) {
this.ux.log(`found packageVersionId ${packageVersionId} in the sfdx-project.json file`);
} else {
throw new Error('no package version id found');
}
} else {
// try the fallback option
// get the SubscriberPackageVersionId from github using Shane's original format
url = `https://raw.githubusercontent.com/${this.flags.githubuser}/${this.flags.repo}/master/latestVersion.json`;
// this.ux.log(`file at ${url} says:`);
const result = await request.get({
url,
json: true
});
// this.ux.log(result);
packageVersionId = result.SubscriberPackageVersionId;
// install in the org
}
const installResult = await exec(`sfdx force:package:install --package ${packageVersionId} -r -u ${this.org.getUsername()} -w 20 --publishwait 20 --json`);
// this.ux.logJson(JSON.parse(installResult.stdout));
return JSON.parse(installResult.stdout);
}
}