diff --git a/src/version/index.js b/src/version/index.js index 2d3a1a8..6f9a80b 100644 --- a/src/version/index.js +++ b/src/version/index.js @@ -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 diff --git a/src/version/json.js b/src/version/json.js index 6e3bc17..150e9a3 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -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)) - ) - })