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
78 lines (67 loc) · 3.23 KB
/
Copy pathbase-release-task.ts
File metadata and controls
78 lines (67 loc) · 3.23 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
import chalk 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 async assertValidPublishBranch(newVersion: Version): Promise<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(chalk.green(` ✓ Using the "${chalk.italic(currentBranchName)}" branch.`));
return currentBranchName;
}
console.error(chalk.red(' ✘ You are not on an allowed publish branch.'));
console.info(chalk.yellow(
` Allowed branches are: ${chalk.bold(allowedBranches.join(', '))}`));
console.info();
// Prompt the user if they wants to forcibly use the current branch. We support this
// because in some cases, releases do not use the common publish branches. e.g. a major
// release is delayed, and new features for the next minor version are collected.
if (await this.promptConfirm(
`Do you want to forcibly use the current branch? (${chalk.italic(currentBranchName)})`)) {
console.log();
console.log(chalk.green(` ✓ Using the "${chalk.italic(currentBranchName)}" branch.`));
return currentBranchName;
}
console.warn();
console.warn(chalk.yellow(' Please switch to one of the allowed publish branches.'));
process.exit(0);
}
/** 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(chalk.red(` ✘ The current branch is not in sync with the remote branch. ` +
`Please make sure your local branch "${chalk.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(chalk.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, defaultValue = false): Promise<boolean> {
return (await prompt<{result: boolean}>({
type: 'confirm',
name: 'result',
message: message,
default: defaultValue,
})).result;
}
}