forked from codaline-io/dev-version-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (26 loc) · 1.01 KB
/
index.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
const core = require('@actions/core')
const fs = require('fs')
const path = require('path')
try {
const filePath = core.getInput('filePath') != '' ? core.getInput('filePath') : 'package.json'
const jsonFile = JSON.parse(fs.readFileSync(path.resolve(process.env.GITHUB_WORKSPACE, filePath)))
const branch = core.getInput('branch').replace('/', '-')
const packageVersion = jsonFile.version
jsonFile.version = getVersion(branch, packageVersion)
core.setOutput('version', jsonFile.version);
} catch (error) {
core.setFailed(error.message);
}
function getVersion(branch, packageVersion) {
const separatorIndex = packageVersion.indexOf('-');
if (separatorIndex > -1) {
const baseVersion = packageVersion.substring(0, separatorIndex).replace(/\./g, "-");
const isMainBranch = (branch === 'main' || branch === '');
if (isMainBranch) {
return `${baseVersion}`;
}
return `${baseVersion}-${branch}`;
}
const stringVersion = packageVersion.replace(/\./g, "-");
return `${stringVersion}`;
}