-
Notifications
You must be signed in to change notification settings - Fork 4k
/
common.js
143 lines (130 loc) · 3.9 KB
/
common.js
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
/**
* External dependencies
*/
const fs = require( 'fs' );
const rimraf = require( 'rimraf' );
const semver = require( 'semver' );
/**
* Internal dependencies
*/
const { log, formats } = require( '../lib/logger' );
const { runStep, readJSONFile } = require( '../lib/utils' );
const git = require( '../lib/git' );
const config = require( '../config' );
/**
* Clone the repository and returns the working directory.
*
* @param {string} abortMessage Abort message.
*
* @return {Promise<string>} Repository local path.
*/
async function runGitRepositoryCloneStep( abortMessage ) {
// Cloning the repository
let gitWorkingDirectoryPath;
await runStep( 'Cloning the Git repository', abortMessage, async () => {
log( '>> Cloning the Git repository' );
gitWorkingDirectoryPath = await git.clone( config.gitRepositoryURL );
log(
'>> The Git repository has been successfully cloned in the following temporary folder: ' +
formats.success( gitWorkingDirectoryPath )
);
} );
return gitWorkingDirectoryPath;
}
/**
* Clean the working directories.
*
* @param {string[]} folders Folders to clean.
* @param {string} abortMessage Abort message.
*/
async function runCleanLocalFoldersStep( folders, abortMessage ) {
await runStep( 'Cleaning the temporary folders', abortMessage, async () => {
await Promise.all(
folders.map( async ( directoryPath ) => {
if ( fs.existsSync( directoryPath ) ) {
await rimraf( directoryPath, ( err ) => {
if ( err ) {
throw err;
}
} );
}
} )
);
} );
}
/**
* Finds the name of the current release branch based on the version in
* the package.json file.
*
* @param {string} packageJsonPath Path to the package.json file.
*
* @return {string} Name of the release branch.
*/
function findReleaseBranchName( packageJsonPath ) {
const mainPackageJson = readJSONFile( packageJsonPath );
const mainParsedVersion = semver.parse( mainPackageJson.version );
return 'release/' + mainParsedVersion.major + '.' + mainParsedVersion.minor;
}
/**
* Calculates version bump for the packages based on the content
* from the provided CHANGELOG file split into individual lines.
*
* @param {string[]} lines Changelog content split into lines.
* @param {('patch'|'minor'|'major')} [minimumVersionBump] Minimum version bump for the package.
* Defaults to `patch`.
*
* @return {string|null} Version bump when applicable, or null otherwise.
*/
function calculateVersionBumpFromChangelog(
lines,
minimumVersionBump = 'patch'
) {
let changesDetected = false;
let versionBump = null;
for ( const line of lines ) {
const lineNormalized = line.toLowerCase().trimLeft();
// Detect unpublished changes first.
if ( lineNormalized.startsWith( '## unreleased' ) ) {
changesDetected = true;
continue;
}
// Skip all lines until unpublished changes found.
if ( ! changesDetected ) {
continue;
}
// A previous published version detected. Stop processing.
if ( lineNormalized.startsWith( '## ' ) ) {
break;
}
// A major version bump required. Stop processing.
if ( lineNormalized.startsWith( '### breaking change' ) ) {
versionBump = 'major';
break;
}
// A minor version bump required. Proceed to the next line.
if (
lineNormalized.startsWith( '### deprecation' ) ||
lineNormalized.startsWith( '### enhancement' ) ||
lineNormalized.startsWith( '### new api' ) ||
lineNormalized.startsWith( '### new feature' )
) {
versionBump = 'minor';
continue;
}
// A version bump required. Found new changelog section.
if (
versionBump !== 'minor' &&
( lineNormalized.startsWith( '### ' ) ||
lineNormalized.includes( '- ' ) )
) {
versionBump = minimumVersionBump;
}
}
return versionBump;
}
module.exports = {
calculateVersionBumpFromChangelog,
findReleaseBranchName,
runGitRepositoryCloneStep,
runCleanLocalFoldersStep,
};