diff --git a/src/version/base.js b/src/version/base.js index 9089e29..c9e142a 100644 --- a/src/version/base.js +++ b/src/version/base.js @@ -25,9 +25,7 @@ module.exports = class BaseVersioning { * @return {string} */ read = () => { - if (fs.existsSync(this.fileLocation)) { - return fs.readFileSync(this.fileLocation, 'utf8') - } + return fs.existsSync(this.fileLocation) ? fs.readFileSync(this.fileLocation, 'utf8') : '' } /** diff --git a/src/version/json.js b/src/version/json.js index 662101f..8125079 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -12,15 +12,10 @@ module.exports = new (class Json extends BaseVersioning { * @return {*} */ bump = (releaseType) => { - let jsonContent = {} - let oldVersion - // Read the file const fileContent = this.read() - if (fileContent) { - jsonContent = JSON.parse(fileContent) - oldVersion = objectPath.get(jsonContent, this.versionPath) - } + const jsonContent = JSON.parse(fileContent) + const oldVersion = objectPath.get(jsonContent, this.versionPath, null) // Get the new version this.newVersion = bumpVersion( diff --git a/src/version/toml.js b/src/version/toml.js index 734ff8e..ff889ee 100644 --- a/src/version/toml.js +++ b/src/version/toml.js @@ -13,15 +13,10 @@ module.exports = new (class Toml extends BaseVersioning{ * @return {*} */ bump = (releaseType) => { - let tomlContent = {} - let oldVersion - // Read the file const fileContent = this.read() - if (fileContent) { - tomlContent = toml.parse(fileContent) - oldVersion = objectPath.get(tomlContent, this.versionPath) - } + const tomlContent = toml.parse(fileContent) + const oldVersion = objectPath.get(tomlContent, this.versionPath, null) // Get the new version this.newVersion = bumpVersion( @@ -29,11 +24,11 @@ module.exports = new (class Toml extends BaseVersioning{ oldVersion, ) - // Get the name of where the version is in - const versionName = this.versionPath.split('.').pop() - // Update the file - if (fileContent) { + if (oldVersion) { + // Get the name of where the version is in + const versionName = this.versionPath.split('.').pop() + this.update( // We use replace instead of yaml.stringify so we can preserve white spaces and comments fileContent.replace( diff --git a/src/version/yaml.js b/src/version/yaml.js index 133d9c1..c29b9b2 100644 --- a/src/version/yaml.js +++ b/src/version/yaml.js @@ -13,15 +13,10 @@ module.exports = new (class Yaml extends BaseVersioning{ * @return {*} */ bump = (releaseType) => { - let yamlContent = {} - let oldVersion - // Read the file const fileContent = this.read() - if (fileContent) { - yamlContent = yaml.parse(fileContent) - oldVersion = objectPath.get(yamlContent, this.versionPath) - } + const yamlContent = yaml.parse(fileContent) || {} + const oldVersion = objectPath.get(yamlContent, this.versionPath, null) // Get the new version this.newVersion = bumpVersion( @@ -29,11 +24,11 @@ module.exports = new (class Yaml extends BaseVersioning{ oldVersion, ) - // Get the name of where the version is in - const versionName = this.versionPath.split('.').pop() - // Update the file - if (fileContent) { + if (oldVersion) { + // Get the name of where the version is in + const versionName = this.versionPath.split('.').pop() + this.update( // We use replace instead of yaml.stringify so we can preserve white spaces and comments fileContent.replace(