UNPKG

version-for-publishing

Version:

A github action to retrieve package version for CDN deploys, etc.

35 lines (26 loc) 1.07 kB
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}`; }