-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
getDistTag.js
32 lines (29 loc) · 1.04 KB
/
getDistTag.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
/* Get npm dist-tag by comparing local version and latest version.
*
* Npm server set dist-tag to "latest" for the last published package. And prevent
* package publish if local version is less than or equal to latest version. To
* publish a package at lower version (a patch or mis-ordered publish), a dist-tag is
* required.
*/
const compareVersions = require("compare-versions");
// Get dist tag, if localVer >= latestVer return latest, otherwise patch@localVer.
const getDistTag = function(localVer, latestVer) {
try {
let ret = compareVersions(localVer, latestVer);
if (ret == 0 || ret == 1) return "latest";
else return `patch@${localVer}`;
} catch (err) {
// Not valid semver, always return latest.
return "latest";
}
};
if (require.main === module) {
if (process.argv.length < 4) {
console.log("Usage: node getDistTag.js <local_version> <latest_version>");
process.exit(1);
} else {
console.log(getDistTag(process.argv[2], process.argv[3]));
process.exit(0);
}
}
module.exports = { getDistTag };