Skip to content

Commit

Permalink
Add support for snapshots (--snapshot=feat → v0.0.0-feat.0), alpha/un…
Browse files Browse the repository at this point in the history
…documented
  • Loading branch information
webpro committed Sep 23, 2023
1 parent 07f373e commit 6d027fd
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
19 changes: 15 additions & 4 deletions lib/config.js
Expand Up @@ -49,12 +49,23 @@ class Config {
}

expandPreReleaseShorthand(options) {
const { increment, preRelease, preReleaseId } = options;
const { increment, preRelease, preReleaseId, snapshot } = options;
const isPreRelease = Boolean(preRelease) || Boolean(snapshot);
const inc = snapshot ? 'prerelease' : increment;
const preId = typeof preRelease === 'string' ? preRelease : typeof snapshot === 'string' ? snapshot : preReleaseId;
options.version = {
increment,
isPreRelease: Boolean(preRelease),
preReleaseId: typeof preRelease === 'string' ? preRelease : preReleaseId
increment: inc,
isPreRelease,
preReleaseId: preId
};
if (typeof snapshot === 'string' && options.git) {
// Pre set and hard code some options
options.git.tagMatch = `0.0.0-${snapshot}.[0-9]*`;
options.git.getLatestTagFromAllRefs = true;
options.git.requireBranch = '!main';
options.git.requireUpstream = false;
options.npm.ignoreVersion = true;
}
return options;
}

Expand Down
6 changes: 6 additions & 0 deletions lib/index.js
Expand Up @@ -69,6 +69,12 @@ const runTasks = async (opts, di) => {

const incrementBase = { latestVersion, increment, isPreRelease, preReleaseId };

const { snapshot } = config.options;
if (snapshot && !incrementBase.latestVersion.startsWith('0.0.0')) {
// Reading the latest version first allows to increment the final counter, fake it if it's not a snapshot:
incrementBase.latestVersion = `0.0.0-${snapshot}.-1`;
}

let version;
if (config.isIncrement) {
incrementBase.increment = await reduceUntil(plugins, plugin => plugin.getIncrement(incrementBase));
Expand Down
4 changes: 4 additions & 0 deletions lib/plugin/GitBase.js
Expand Up @@ -32,6 +32,7 @@ class GitBase extends Plugin {
}

async getChangelog() {
const { snapshot } = this.config.getContext();
const { latestTag, secondLatestTag } = this.config.getContext();
const context = { latestTag, from: latestTag, to: 'HEAD' };
const { changelog } = this.options;
Expand All @@ -42,6 +43,9 @@ class GitBase extends Plugin {
context.to = `${latestTag}^1`;
}

// For now, snapshots do not get a changelog, as it often goes haywire (easy to add to release manually)
if (snapshot) return '';

if (!context.from && changelog.includes('${from}')) {
return this.exec(changelogFallback);
}
Expand Down
9 changes: 8 additions & 1 deletion lib/plugin/git/Git.js
Expand Up @@ -95,7 +95,14 @@ class Git extends GitBase {
async isRequiredBranch() {
const branch = await this.getBranchName();
const requiredBranches = _.castArray(this.options.requireBranch);
return matcher(requiredBranches)(branch);
const [branches, negated] = requiredBranches.reduce(
([p, n], b) => (b.startsWith('!') ? [p, [...n, b.slice(1)]] : [[...p, b], n]),
[[], []]
);
return (
(branches.length > 0 ? matcher(branches)(branch) : true) &&
(negated.length > 0 ? !matcher(negated)(branch) : true)
);
}

async hasUpstreamBranch() {
Expand Down
11 changes: 11 additions & 0 deletions test/config.js
Expand Up @@ -131,3 +131,14 @@ test('should expand pre-release shortcut (including increment and npm.tag)', t =
preReleaseId: 'rc'
});
});

test('should expand pre-release shortcut (snapshot)', t => {
const config = new Config({ snapshot: 'feat' });
t.deepEqual(config.options.version, {
increment: 'prerelease',
isPreRelease: true,
preReleaseId: 'feat'
});
t.is(config.options.git.tagMatch, '0.0.0-feat.[0-9]*');
t.true(config.options.git.getLatestTagFromAllRefs);
});
7 changes: 7 additions & 0 deletions test/git.init.js
Expand Up @@ -26,6 +26,13 @@ test.serial('should throw if on wrong branch', async t => {
await t.throwsAsync(gitClient.init(), { message: /^Must be on branch dev/ });
});

test.serial('should throw if on negated branch', async t => {
const options = { git: { requireBranch: '!main' } };
const gitClient = factory(Git, { options });
sh.exec('git checkout -b main');
await t.throwsAsync(gitClient.init(), { message: /^Must be on branch !main/ });
});

test.serial('should not throw if required branch matches', async t => {
const options = { git: { requireBranch: 'ma?*' } };
const gitClient = factory(Git, { options });
Expand Down

0 comments on commit 6d027fd

Please sign in to comment.