forked from HarmvZ/set-package-json-version-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
24 lines (23 loc) · 843 Bytes
/
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
const core = require('@actions/core');
const { resolve } = require('path');
const fs = require('fs');
const semver = require('semver');
try {
const version = core.getInput('version');
if (semver.valid(version) === null) {
throw new Error("Invalid SEMVER version");
}
if (!version) {
throw new Error('No version input defined');
}
const inputPath = core.getInput('path');
const packageJsonPath = resolve(inputPath, './package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
const oldVersion = packageJson.version;
packageJson.version = version;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
console.log(`Replaced old package.json version (${oldVersion}) with ${version}.`);
} catch (error) {
console.error(error);
core.setFailed(error.message);
}