forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-release-task.ts
More file actions
81 lines (69 loc) · 3.31 KB
/
Copy pathbase-release-task.ts
File metadata and controls
81 lines (69 loc) · 3.31 KB
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
import {green, italic, red, yellow} from 'chalk';
import {prompt} from 'inquirer';
import {GitClient} from './git/git-client';
import {Version} from './version-name/parse-version';
import {getAllowedPublishBranches} from './version-name/publish-branches';
/**
* Base release task class that contains shared methods that are commonly used across
* the staging and publish script.
*/
export class BaseReleaseTask {
constructor(public git: GitClient) {}
/** Checks if the user is on an allowed publish branch for the specified version. */
protected switchToPublishBranch(newVersion: Version): string {
const allowedBranches = getAllowedPublishBranches(newVersion);
const currentBranchName = this.git.getCurrentBranch();
// If current branch already matches one of the allowed publish branches, just continue
// by exiting this function and returning the currently used publish branch.
if (allowedBranches.includes(currentBranchName)) {
console.log(green(` ✓ Using the "${italic(currentBranchName)}" branch.`));
return currentBranchName;
}
// In case there are multiple allowed publish branches for this version, we just
// exit and let the user decide which branch they want to release from.
if (allowedBranches.length !== 1) {
console.warn(yellow(' ✘ You are not on an allowed publish branch.'));
console.warn(yellow(` Please switch to one of the following branches: ` +
`${allowedBranches.join(', ')}`));
process.exit(0);
}
// For this version there is only *one* allowed publish branch, so we could
// automatically switch to that branch in case the user isn't on it yet.
const defaultPublishBranch = allowedBranches[0];
if (!this.git.checkoutBranch(defaultPublishBranch)) {
console.error(red(` ✘ Could not switch to the "${italic(defaultPublishBranch)}" ` +
`branch.`));
console.error(red(` Please ensure that the branch exists or manually switch to the ` +
`branch.`));
process.exit(1);
}
console.log(green(` ✓ Switched to the "${italic(defaultPublishBranch)}" branch.`));
}
/** Verifies that the local branch is up to date with the given publish branch. */
protected verifyLocalCommitsMatchUpstream(publishBranch: string) {
const upstreamCommitSha = this.git.getRemoteCommitSha(publishBranch);
const localCommitSha = this.git.getLocalCommitSha('HEAD');
// Check if the current branch is in sync with the remote branch.
if (upstreamCommitSha !== localCommitSha) {
console.error(red(` ✘ The current branch is not in sync with the remote branch. Please ` +
`make sure your local branch "${italic(publishBranch)}" is up to date.`));
process.exit(1);
}
}
/** Verifies that there are no uncommitted changes in the project. */
protected verifyNoUncommittedChanges() {
if (this.git.hasUncommittedChanges()) {
console.error(red(` ✘ There are changes which are not committed and should be ` +
`discarded.`));
process.exit(1);
}
}
/** Prompts the user with a confirmation question and a specified message. */
protected async promptConfirm(message: string): Promise<boolean> {
return (await prompt<{result: boolean}>({
type: 'confirm',
name: 'result',
message: message,
})).result;
}
}