refactor: Updated JSON versioning
parent
5aff23f514
commit
c7be6541a8
|
@ -1,4 +1,4 @@
|
|||
const JSON = require('./json')
|
||||
const Json = require('./json')
|
||||
const Git = require('./git')
|
||||
const Yaml = require('./yaml')
|
||||
const Toml = require('./toml')
|
||||
|
@ -6,7 +6,8 @@ const Toml = require('./toml')
|
|||
module.exports = (fileExtension) => {
|
||||
switch (fileExtension.toLowerCase()) {
|
||||
case 'json':
|
||||
return JSON
|
||||
return Json
|
||||
|
||||
case 'yaml':
|
||||
case 'yml':
|
||||
return Yaml
|
||||
|
|
|
@ -1,29 +1,9 @@
|
|||
const fs = require('fs')
|
||||
const objectPath = require('object-path')
|
||||
|
||||
const BaseVersioning = require('./base')
|
||||
const bumpVersion = require('../helpers/bumpVersion')
|
||||
|
||||
module.exports = new (class JSON {
|
||||
|
||||
fileLocation = null
|
||||
|
||||
versionPath = null
|
||||
|
||||
newVersion = null
|
||||
|
||||
init = (fileLocation, versionPath) => {
|
||||
this.fileLocation = fileLocation
|
||||
this.versionPath = versionPath
|
||||
}
|
||||
|
||||
/**
|
||||
* Get's the JSON file
|
||||
*
|
||||
* @return {string}
|
||||
*/
|
||||
read = () => {
|
||||
return JSON.parse(fs.readFileSync(this.fileLocation))
|
||||
}
|
||||
module.exports = new (class Json extends BaseVersioning {
|
||||
|
||||
/**
|
||||
* Bumps the version in the package.json
|
||||
|
@ -32,31 +12,25 @@ module.exports = new (class JSON {
|
|||
* @return {*}
|
||||
*/
|
||||
bump = (releaseType) => {
|
||||
// Read the JSON file
|
||||
const jsonFile = this.read()
|
||||
// Read the file
|
||||
const fileContent = this.read()
|
||||
const jsonContent = JSON.parse(fileContent)
|
||||
const oldVersion = objectPath.get(jsonContent, this.versionPath)
|
||||
|
||||
// Get the new version
|
||||
this.newVersion = bumpVersion(
|
||||
releaseType,
|
||||
objectPath.get(jsonFile, this.versionPath),
|
||||
oldVersion,
|
||||
)
|
||||
|
||||
// Update the json file with the new version
|
||||
objectPath.set(jsonFile, this.versionPath, this.newVersion)
|
||||
// Update the content with the new version
|
||||
objectPath.set(jsonContent, this.versionPath, this.newVersion)
|
||||
|
||||
// Update the JSON file
|
||||
this.update(jsonFile)
|
||||
// Update the file
|
||||
this.update(
|
||||
JSON.stringify(jsonContent, null, 2),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update JSON file
|
||||
*
|
||||
* @param {!string} newJSONContent - New content for the JSON file
|
||||
* @return {*}
|
||||
*/
|
||||
update = (newJSONContent) => (
|
||||
fs.writeFileSync(this.fileLocation, JSON.stringify(newJSONContent, null, 2))
|
||||
)
|
||||
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in New Issue