generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-version.js
29 lines (24 loc) · 934 Bytes
/
set-version.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
let setVersion = function (fileContent, version, buildNumberOnly) {
const versionStringRx = /\d+(\.\d+){2,}/ig;
if (!buildNumberOnly && (!version || !version.match(versionStringRx))) {
throw new Error('Invalid version string');
}
if (buildNumberOnly && (!version || !version.match(/^[0-9]+$/ig))) {
throw new Error('Invalid version string');
}
const lines = fileContent.split('\n');
const versionLine = lines.find(l => l.match(/^version\s*=\s*/ig));
let currentVersion = versionLine.match(versionStringRx);
currentVersion = currentVersion && currentVersion[0];
const newVersion = !buildNumberOnly ? version : currentVersion.replace(/[0-9]+$/, version);
return {
version: newVersion,
content: lines.map(l => {
if (l !== versionLine) {
return l;
}
return versionLine.replace(currentVersion, newVersion);
}).join('\n')
};
};
module.exports = setVersion;