This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.js
88 lines (76 loc) · 2.78 KB
/
api.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
'use strict';
const semver = require('semver');
const git = require('./git-helper');
const defaultOptions = require('./options');
const releaseTypes = ['major', 'minor', 'patch', 'premajor', 'preminor', 'prepatch', 'prerelease'];
// Checks to see if a valid release type is passed
const validBumper = releaseType => {
return !!releaseType && releaseTypes.indexOf(releaseType) !== -1;
};
// Cleans up the options object
const checkOptions = options => {
options = Object.assign({}, defaultOptions, options);
options.tag = options.tag || options.publish;
return Promise.resolve(options);
};
// version - returns the current version number, if there is one.
const latestVersionTag = options => checkOptions(options)
.then(options => git.latestVersionTag(options));
// bump - bumps up the version number.
const bump = (version, options) => checkOptions(options).then(options => {
options.includePrerelease = options.includePrerelease || (/^pre/).test(version);
return latestVersionTag(options).then(currentVersion => {
// x.x.x
if (semver.valid(version)) {
if (!currentVersion || semver.gt(version, currentVersion)) {
return version;
}
else {
return Promise.reject('Version number must be greater than the current version!');
}
}
// major, minor, patch, preminor, patch, prepatch, or prerelease
else if (validBumper(version)) {
if (!currentVersion && !semver.valid(options.base)) {
return Promise.reject('No version can be found and therefore it cannot be bumped!');
}
return semver.inc(currentVersion || options.base, version, options.preid);
}
// error
else {
return Promise.reject('Invalid version or release type');
}
});
})
// Handle any git commands
.then(version => {
if (options.tag) {
return git.checkStatus(options).then(ok => {
if (ok) {
return git.tag(`v${version}`, `${options.message.replace(/\%s/g, version) || version}`, options.publish, options);
}
else {
return git.branch(options).then(branch => Promise.reject(`Cannot tag as your local repository is not in sync with its remote '${branch}' branch.`));
}
})
.then(() => {
return version;
});
}
return Promise.resolve(version);
});
/** DEPRECATED */
module.exports = { version: latestVersionTag, bump };
/**
* API
* @param {String} [version] If defined bumps up the version number, otherwise return the current version
* @param {Object} [options] Options to pass to check/bump the version
* @returns {Promise} promise
*/
module.exports = function (version, options) {
if (typeof version !== 'string' && arguments.length === 1) {
options = version;
version = null;
}
return typeof version === 'string' ? bump(version, options) : latestVersionTag(options);
};