-
Notifications
You must be signed in to change notification settings - Fork 41
/
bump.ts
130 lines (107 loc) · 5.67 KB
/
bump.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
import { flags, SfdxCommand } from '@salesforce/command';
import chalk from 'chalk';
import cli from 'cli-ux';
import { exec, exec2JSON } from '@mshanemc/plugin-helpers';
import fs = require('fs-extra');
export default class Bump extends SfdxCommand {
public static description = 'bump the major/minor version number in the packageDirectory';
public static aliases = ['shane:package:version:bump'];
public static examples = [
`sfdx shane:package2:version:bump -m
// bump the minor version up by one (and set patch to 0)
`,
`sfdx shane:package2:version:bump -M
// bump the major version up by one (and set minor/patch to 0)
`,
`sfdx shane:package2:version:bump -p
// bump the patch version up by one
`,
`sfdx shane:package2:version:bump -M -t myDir
// bump the major version up by one for a particular directory that's not the default
`,
`sfdx shane:package2:version:bump --minor --create
// bump the minor version up by one and create a new package2version
`,
`sfdx shane:package2:version:bump --minor --release
// bump the minor version up by one and create a new package2version, then set that as released
`
];
protected static flagsConfig = {
major: flags.boolean({
char: 'M',
description: 'Bump the major version by 1, sets minor,build to 0',
exclusive: ['minor', 'patch']
}),
minor: flags.boolean({ char: 'm', description: 'Bump the minor version by 1', exclusive: ['major', 'patch'] }),
patch: flags.boolean({ char: 'p', description: 'Bump the patch version by 1', exclusive: ['major', 'minor'] }),
create: flags.boolean({ char: 'c', description: 'create a new packageVersion from the new versionNumber' }),
release: flags.boolean({
char: 'r',
description: 'set the newly version as released (out of Beta). Implies create whether you flag it or not :)'
}),
target: flags.string({
char: 't',
default: 'force-app',
description: 'name of your package directory (defaults to force-app)'
})
};
protected static requiresProject = true;
protected static requiresDevhubUsername = true;
public async run(): Promise<any> {
if ((this.flags.major && this.flags.minor) || (!this.flags.major && !this.flags.minor)) {
throw new Error('You have to specify either --major or --minor or --patch but not both');
}
const projectFile = await this.project.retrieveSfdxProjectJson(false);
const project = await fs.readJson(projectFile.getPath());
const targetDirIndex = project.packageDirectories.findIndex(i => i.path === this.flags.target);
if (targetDirIndex < 0) {
this.ux.error(`found nothing in packageDirectories matching path ${this.flags.path}`);
}
const versionNumber = project.packageDirectories[targetDirIndex].versionNumber.split('.');
if (this.flags.major) {
versionNumber[0] = parseInt(versionNumber[0], 10) + 1;
versionNumber[1] = 0;
versionNumber[2] = 0;
} else if (this.flags.minor) {
versionNumber[1] = parseInt(versionNumber[1], 10) + 1;
versionNumber[2] = 0;
} else if (this.flags.patch) {
versionNumber[2] = parseInt(versionNumber[2], 10) + 1;
}
project.packageDirectories[targetDirIndex].versionNumber = versionNumber.join('.');
// await fs.writeFile(projectFile.getPath(), JSON.stringify(project, null, 2));
await fs.writeJSON(projectFile.getPath(), project, { spaces: 2 });
this.ux.log(chalk.green(`Updated sfdx-project.json for ${this.flags.target} to ${project.packageDirectories[targetDirIndex].versionNumber}`));
if (!this.flags.create && !this.flags.release) {
return project.packageDirectories[targetDirIndex].versionNumber;
}
// do we need to generate the new version?
cli.action.start('Creating package version (this may take a while)');
// createResult = <CreateResultI>await exec(`sfdx force:package2:version:create -d ${this.flags.target} -w 20 -v ${this.hubOrg.getUsername()} --json`);
const packageCreationStart = new Date();
const createResult = await exec2JSON(
`sfdx force:package:version:create -x -d ${this.flags.target} -w 20 -v ${this.hubOrg.getUsername()} --json`
);
const packageCreationEnd = new Date();
cli.action.stop(`done in (${Math.round((packageCreationEnd.getTime() - packageCreationStart.getTime()) / 1000)} seconds)`);
const actualResult = createResult.result;
if (!this.flags.json) {
this.ux.logJson(actualResult);
}
// await fs.writeFile(`${this.project.getPath()}/${pkgVersionFileName}`, JSON.stringify(actualResult, null, 2));
this.ux.log(chalk.green(`Version Created with Id: ${actualResult.Package2VersionId}`));
// now, are we publishing
if (this.flags.release) {
cli.action.start(
`Releasing: sfdx force:package:version:promote -n -p ${actualResult.Package2VersionId} -v ${this.hubOrg.getUsername()} --json`
);
await exec(`sfdx force:package:version:promote -n -p ${actualResult.Package2VersionId} -v ${this.hubOrg.getUsername()} --json`);
cli.action.stop();
this.ux.log(`${chalk.green('Version released')}. May take several minutes to become available to destination org.`);
this.ux.log(
`Install with sfdx force:package:install -r -b 20 -w 20 -p ${actualResult.SubscriberPackageVersionId} -u destinationOrgAlias`
);
}
return createResult.result;
}
}