diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aec3180..0d59bde 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,6 +21,47 @@ jobs: with: github-token: ${{ secrets.github_token }} + test-json-new: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + path: "./" + + - name: Generate changelog + id: changelog + uses: ./ + env: + ENV: 'dont-use-git' + with: + github-token: ${{ secrets.github_token }} + version-file: 'test-file-new.json' + + - name: Show file + id: show + run: | + echo "$( { - let [major, minor, patch] = version.split('.') + let major, minor, patch - switch (releaseType) { - case 'major': - major = parseInt(major, 10) + 1 - minor = 0 + if (version) { + [major, minor, patch] = version.split('.') + + switch (releaseType) { + case 'major': + major = parseInt(major, 10) + 1 + minor = 0 + patch = 0 + break + + case 'minor': + minor = parseInt(minor, 10) + 1 + patch = 0 + break + + default: + patch = parseInt(patch, 10) + 1 + } + } else { + let version = semverValid(core.getInput('fallback-version')) + + if (version) { + [major, minor, patch] = version.split('.') + } else { + // default + major = 0 + minor = 1 patch = 0 - break - - case 'minor': - minor = parseInt(minor, 10) + 1 - patch = 0 - break - - default: - patch = parseInt(patch, 10) + 1 + } } return `${major}.${minor}.${patch}` diff --git a/src/version/base.js b/src/version/base.js index 04a172d..9089e29 100644 --- a/src/version/base.js +++ b/src/version/base.js @@ -25,7 +25,9 @@ module.exports = class BaseVersioning { * @return {string} */ read = () => { - return fs.readFileSync(this.fileLocation, 'utf8') + if (fs.existsSync(this.fileLocation)) { + return fs.readFileSync(this.fileLocation, 'utf8') + } } /** diff --git a/src/version/git.js b/src/version/git.js index b73cc38..4077c93 100644 --- a/src/version/git.js +++ b/src/version/git.js @@ -13,7 +13,7 @@ module.exports = new (class Git extends BaseVersioning { gitSemverTags({ tagPrefix, }, (err, tags) => { - const currentVersion = tags.shift().replace(tagPrefix, '') + const currentVersion = tags.length > 0 ? tags.shift().replace(tagPrefix, '') : null // Get the new version this.newVersion = bumpVersion( diff --git a/src/version/json.js b/src/version/json.js index 150e9a3..662101f 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -12,10 +12,15 @@ module.exports = new (class Json extends BaseVersioning { * @return {*} */ bump = (releaseType) => { + let jsonContent = {} + let oldVersion + // Read the file const fileContent = this.read() - const jsonContent = JSON.parse(fileContent) - const oldVersion = objectPath.get(jsonContent, this.versionPath) + if (fileContent) { + jsonContent = JSON.parse(fileContent) + oldVersion = objectPath.get(jsonContent, this.versionPath) + } // Get the new version this.newVersion = bumpVersion( diff --git a/src/version/toml.js b/src/version/toml.js index e164e38..734ff8e 100644 --- a/src/version/toml.js +++ b/src/version/toml.js @@ -1,5 +1,5 @@ const objectPath = require('object-path') -const toml = require('toml') +const toml = require('@iarna/toml') const BaseVersioning = require('./base') const bumpVersion = require('../helpers/bumpVersion') @@ -13,10 +13,15 @@ module.exports = new (class Toml extends BaseVersioning{ * @return {*} */ bump = (releaseType) => { + let tomlContent = {} + let oldVersion + // Read the file const fileContent = this.read() - const tomlContent = toml.parse(fileContent) - const oldVersion = objectPath.get(tomlContent, this.versionPath) + if (fileContent) { + tomlContent = toml.parse(fileContent) + oldVersion = objectPath.get(tomlContent, this.versionPath) + } // Get the new version this.newVersion = bumpVersion( @@ -28,13 +33,19 @@ module.exports = new (class Toml extends BaseVersioning{ const versionName = this.versionPath.split('.').pop() // Update the file - this.update( - // We use replace so we can preserve white spaces and comments - fileContent.replace( - `${versionName} = "${oldVersion}"`, - `${versionName} = "${this.newVersion}"`, - ), - ) + if (fileContent) { + this.update( + // We use replace instead of yaml.stringify so we can preserve white spaces and comments + fileContent.replace( + `${versionName} = "${oldVersion}"`, + `${versionName} = "${this.newVersion}"`, + ), + ) + } else { + // Update the content with the new version + objectPath.set(tomlContent, this.versionPath, this.newVersion) + this.update(toml.stringify(tomlContent)) + } } }) diff --git a/src/version/yaml.js b/src/version/yaml.js index da7db77..133d9c1 100644 --- a/src/version/yaml.js +++ b/src/version/yaml.js @@ -13,10 +13,15 @@ module.exports = new (class Yaml extends BaseVersioning{ * @return {*} */ bump = (releaseType) => { + let yamlContent = {} + let oldVersion + // Read the file const fileContent = this.read() - const yamlContent = yaml.parse(fileContent) - const oldVersion = objectPath.get(yamlContent, this.versionPath) + if (fileContent) { + yamlContent = yaml.parse(fileContent) + oldVersion = objectPath.get(yamlContent, this.versionPath) + } // Get the new version this.newVersion = bumpVersion( @@ -28,13 +33,19 @@ module.exports = new (class Yaml extends BaseVersioning{ const versionName = this.versionPath.split('.').pop() // Update the file - this.update( - // We use replace instead of yaml.stringify so we can preserve white spaces and comments - fileContent.replace( - `${versionName}: '${oldVersion}'`, - `${versionName}: '${this.newVersion}'`, - ), - ) + if (fileContent) { + this.update( + // We use replace instead of yaml.stringify so we can preserve white spaces and comments + fileContent.replace( + `${versionName}: '${oldVersion}'`, + `${versionName}: '${this.newVersion}'`, + ), + ) + } else { + // Update the content with the new version + objectPath.set(yamlContent, this.versionPath, this.newVersion) + this.update(yaml.stringify(yamlContent)) + } } }) diff --git a/test/test-file-empty.json b/test/test-file-empty.json new file mode 100644 index 0000000..e69de29 diff --git a/test/test-file-empty.toml b/test/test-file-empty.toml new file mode 100644 index 0000000..e69de29 diff --git a/test/test-file-empty.yaml b/test/test-file-empty.yaml new file mode 100644 index 0000000..e69de29