refactor: Updated JSON versioning

releases/v3
Tycho Bokdam 2020-07-03 21:56:09 +02:00
parent 5aff23f514
commit c7be6541a8
No known key found for this signature in database
GPG Key ID: A0FAE77C8CDF33C7
2 changed files with 16 additions and 41 deletions

View File

@ -1,4 +1,4 @@
const JSON = require('./json') const Json = require('./json')
const Git = require('./git') const Git = require('./git')
const Yaml = require('./yaml') const Yaml = require('./yaml')
const Toml = require('./toml') const Toml = require('./toml')
@ -6,7 +6,8 @@ const Toml = require('./toml')
module.exports = (fileExtension) => { module.exports = (fileExtension) => {
switch (fileExtension.toLowerCase()) { switch (fileExtension.toLowerCase()) {
case 'json': case 'json':
return JSON return Json
case 'yaml': case 'yaml':
case 'yml': case 'yml':
return Yaml return Yaml

View File

@ -1,29 +1,9 @@
const fs = require('fs')
const objectPath = require('object-path') const objectPath = require('object-path')
const BaseVersioning = require('./base')
const bumpVersion = require('../helpers/bumpVersion') const bumpVersion = require('../helpers/bumpVersion')
module.exports = new (class JSON { module.exports = new (class Json extends BaseVersioning {
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))
}
/** /**
* Bumps the version in the package.json * Bumps the version in the package.json
@ -32,31 +12,25 @@ module.exports = new (class JSON {
* @return {*} * @return {*}
*/ */
bump = (releaseType) => { bump = (releaseType) => {
// Read the JSON file // Read the file
const jsonFile = this.read() const fileContent = this.read()
const jsonContent = JSON.parse(fileContent)
const oldVersion = objectPath.get(jsonContent, this.versionPath)
// Get the new version // Get the new version
this.newVersion = bumpVersion( this.newVersion = bumpVersion(
releaseType, releaseType,
objectPath.get(jsonFile, this.versionPath), oldVersion,
) )
// Update the json file with the new version // Update the content with the new version
objectPath.set(jsonFile, this.versionPath, this.newVersion) objectPath.set(jsonContent, this.versionPath, this.newVersion)
// Update the JSON file // Update the file
this.update(jsonFile) 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))
)
}) })