/
json.js
53 lines (41 loc) · 1.29 KB
/
json.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const core = require('@actions/core')
const objectPath = require('object-path')
const BaseVersioning = require('./base')
const bumpVersion = require('../helpers/bumpVersion')
module.exports = class Json extends BaseVersioning {
/**
* Bumps the version in the package.json
*
* @param {!string} releaseType - The type of release
* @return {*}
*/
bump = async (releaseType) => {
// Read the file
const fileContent = this.read()
// Parse the file
let jsonContent
try {
jsonContent = JSON.parse(fileContent)
} catch (error) {
core.startGroup(`Error when parsing the file '${this.fileLocation}'`)
core.info(`File-Content: ${fileContent}`)
core.info(error) // should be 'warning' ?
core.endGroup()
jsonContent = {}
}
// Get the old version
const oldVersion = objectPath.get(jsonContent, this.versionPath, null)
// Get the new version
this.newVersion = await bumpVersion(
releaseType,
oldVersion,
)
core.info(`Bumped file "${this.fileLocation}" from "${oldVersion}" to "${this.newVersion}"`)
// Update the content with the new version
objectPath.set(jsonContent, this.versionPath, this.newVersion)
// Update the file
this.update(
JSON.stringify(jsonContent, null, 2),
)
}
}