Skip to content

Commit a034cb3

Browse files
authored
feat(prerelease): added logic to bump local dependencies on prereleases (#49)
1 parent 5432def commit a034cb3

File tree

10 files changed

+476
-5
lines changed

10 files changed

+476
-5
lines changed

lib/createInlinePluginCreator.js

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ function createInlinePluginCreator(packages, multiContext, synchronizer, flags)
8686
*/
8787
const analyzeCommits = async (pluginOptions, context) => {
8888
const firstParentBranch = flags.firstParent ? context.branch.name : undefined;
89+
pkg._preRelease = context.branch.prerelease || null;
8990

9091
// Filter commits by directory.
9192
commits = await getCommitsFiltered(cwd, dir, context.lastRelease.gitHead, firstParentBranch);

lib/updateDeps.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ const getNextVersion = (pkg) => {
1717
: lastVersion || "1.0.0";
1818
};
1919

20+
/**
21+
* Resolve next package version on prereleases.
22+
*
23+
* @param {Package} pkg Package object.
24+
* @returns {string|undefined} Next pkg version.
25+
* @internal
26+
*/
27+
const getNextPreVersion = (pkg) => {
28+
const lastVersion = pkg._lastRelease && pkg._lastRelease.version;
29+
30+
return lastVersion ? semver.inc(lastVersion, "prerelease", pkg._preRelease) : `1.0.0-${pkg._preRelease}.1`;
31+
};
32+
2033
/**
2134
* Resolve package release type taking into account the cascading dependency update.
2235
*
@@ -90,7 +103,12 @@ const getDependentRelease = (pkg, bumpStrategy, releaseStrategy, ignore) => {
90103
// 1. Any local dep package itself has changed
91104
// 2. Any local dep package has local deps that have changed.
92105
const nextType = resolveReleaseType(p, bumpStrategy, releaseStrategy,[...ignore, ...localDeps]);
93-
const nextVersion = getNextVersion(p);
106+
107+
// Set the nextVersion fallback to the last local dependency package last version
108+
let nextVersion = p._lastRelease && p._lastRelease.version;
109+
110+
// Update the nextVersion only if there is a next type to be bumped
111+
if (nextType) nextVersion = p._preRelease ? getNextPreVersion(p) : getNextVersion(p);
94112
const lastVersion = pkg._lastRelease && pkg._lastRelease.version;
95113

96114
// 3. And this change should correspond to manifest updating rule.
@@ -169,6 +187,7 @@ const updateManifestDeps = (pkg) => {
169187

170188
module.exports = {
171189
getNextVersion,
190+
getNextPreVersion,
172191
updateManifestDeps,
173192
resolveReleaseType,
174193
resolveNextVersion,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "msr-test-yarn",
3+
"author": "Dave Houlbrooke <[email protected]",
4+
"version": "0.0.0-semantically-released",
5+
"private": true,
6+
"license": "0BSD",
7+
"engines": {
8+
"node": ">=8.3"
9+
},
10+
"workspaces": [
11+
"packages/*"
12+
],
13+
"release": {
14+
"plugins": [
15+
"@semantic-release/commit-analyzer",
16+
"@semantic-release/release-notes-generator"
17+
],
18+
"noCi": true
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"tagFormat": "multi-semantic-release-test-c@v${version}"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "msr-test-c",
3+
"version": "0.0.0",
4+
"dependencies": {
5+
"msr-test-d": "*"
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "msr-test-d",
3+
"version": "0.0.0"
4+
}

test/helpers/file.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { basename, join } = require("path");
2-
const { copyFileSync, existsSync, mkdirSync, lstatSync, readdirSync, readFileSync } = require("fs");
2+
const { copyFileSync, existsSync, mkdirSync, lstatSync, readdirSync, readFileSync, writeFileSync } = require("fs");
33

44
// Deep copy a directory.
55
function copyDirectory(source, target) {
@@ -37,8 +37,16 @@ function isDirectory(path) {
3737
return typeof path === "string" && existsSync(path) && lstatSync(path).isDirectory();
3838
}
3939

40+
// Creates testing files on all specified folders.
41+
function createNewTestingFiles(folders, cwd) {
42+
folders.forEach((fld) => {
43+
writeFileSync(`${cwd}/${fld}test.txt`, fld);
44+
});
45+
}
46+
4047
// Exports.
4148
module.exports = {
4249
copyDirectory,
4350
isDirectory,
51+
createNewTestingFiles,
4452
};

test/helpers/git.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ function gitInitRemote() {
6565
* _Created in a temp folder._
6666
*
6767
* @param {string} cwd The cwd to create and set the origin for.
68+
* @param {string} releaseBranch="null" Optional branch to be added in case of prerelease is activated for a branch.
6869
* @return {Promise<string>} Promise that resolves to string URL of the of the remote origin.
6970
*/
70-
function gitInitOrigin(cwd) {
71+
function gitInitOrigin(cwd, releaseBranch = null) {
7172
// Check params.
7273
check(cwd, "cwd: absolute");
7374

@@ -76,6 +77,13 @@ function gitInitOrigin(cwd) {
7677

7778
// Set origin on local repo.
7879
execa.sync("git", ["remote", "add", "origin", url], { cwd });
80+
81+
// Set up a release branch. Return to master afterwards.
82+
if (releaseBranch) {
83+
execa.sync("git", ["checkout", "-b", releaseBranch], { cwd });
84+
execa.sync("git", ["checkout", "master"], { cwd });
85+
}
86+
7987
execa.sync("git", ["push", "--all", "origin"], { cwd });
8088

8189
// Return URL for remote.

0 commit comments

Comments
 (0)