From bdf8ec04e6f0d493ef859df06ffbeecb1f47a970 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Fri, 3 Jul 2020 21:55:24 +0200 Subject: [PATCH] feat: Added support for yaml files --- .github/workflows/test.yml | 47 ++++++++++++++++++++++++++++++---- README.md | 14 ++++++++-- package.json | 4 ++- src/test.js | 9 ------- src/version/base.js | 52 ++++++++++++++++++++++++++++++++++++++ src/version/git.js | 9 +++---- src/version/index.js | 12 ++++----- src/version/yaml.js | 41 ++++++++++++++++++++++++++++++ test-file.yaml | 8 ++++++ yarn.lock | 10 ++++++++ 10 files changed, 176 insertions(+), 30 deletions(-) delete mode 100644 src/test.js create mode 100644 src/version/base.js create mode 100644 src/version/yaml.js create mode 100644 test-file.yaml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 902ee8b..c0276fe 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,15 +1,17 @@ name: 'Action to test the action locally with act' +on: + push: + branches: + - branch-that-does-not-exist jobs: - test: + test-json: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 with: - path: "./conventional-changelog-action" - - - run: cd conventional-changelog-action && yarn --production + path: "./" - name: Generate changelog id: changelog @@ -19,7 +21,6 @@ jobs: with: github-token: ${{ secrets.github_token }} - test-git: runs-on: ubuntu-latest steps: @@ -36,3 +37,39 @@ jobs: with: github-token: ${{ secrets.github_token }} skip-commit: 'true' + + test-yaml: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + path: "./" + + - name: Generate changelog + id: changelog + uses: ./ + env: + ENV: 'test' + with: + github-token: ${{ secrets.github_token }} + version-file: 'test-file.yaml' + version-path: 'package.version' + + test-toml: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + path: "./" + + - name: Generate changelog + id: changelog + uses: ./ + env: + ENV: 'test' + with: + github-token: ${{ secrets.github_token }} + version-file: 'test-file.toml' + version-path: 'package.version' diff --git a/README.md b/README.md index a921725..d63a188 100644 --- a/README.md +++ b/README.md @@ -114,11 +114,21 @@ Github releases ## Development If you'd like to contribute to this project, all you need to do is clone and install [act](https://github.com/nektos/act) this project and run: > Make sure that `main: 'src/index.js'` is updated to `main: '../src/index.js'` inside the `action.yml` +> Note: The image used is 18 gb! ```shell $ yarn install -# We need the full 18 gb image because we use GIT -$ act -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token= +# To run / test json versioning +$ act -j test-json -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token= + +# To run / test git versioning +$ act -j test-git -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token= + +# To run / test yaml versioning +$ act -j test-yaml -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token= + +# To run / toml git versioning +$ act -j test-toml -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token= ``` ## [License](./LICENSE) diff --git a/package.json b/package.json index 53b21d1..60f4c6a 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,8 @@ "conventional-changelog": "3.1.21", "conventional-recommended-bump": "6.0.9", "git-semver-tags": "4.0.0", - "object-path": "^0.11.4" + "object-path": "^0.11.4", + "toml": "^3.0.0", + "yaml": "^1.10.0" } } diff --git a/src/test.js b/src/test.js deleted file mode 100644 index 702a3b9..0000000 --- a/src/test.js +++ /dev/null @@ -1,9 +0,0 @@ -const conventionalRecommendedBump = require('conventional-recommended-bump') - -async function run() { - conventionalRecommendedBump({ preset: 'angular', tagPrefix: 'v' }, async(error, recommendation, tag) => { - console.log(recommendation, tag) - }) -} - -run() diff --git a/src/version/base.js b/src/version/base.js new file mode 100644 index 0000000..04a172d --- /dev/null +++ b/src/version/base.js @@ -0,0 +1,52 @@ +const fs = require('fs') + +module.exports = class BaseVersioning { + + fileLocation = null + + versionPath = null + + newVersion = null + + /** + * Set some basic configurations + * + * @param {!string} fileLocation - Full location of the file + * @param {!string} versionPath - Path inside the file where the version is located + */ + init = (fileLocation, versionPath) => { + this.fileLocation = fileLocation + this.versionPath = versionPath + } + + /** + * Get the file's content + * + * @return {string} + */ + read = () => { + return fs.readFileSync(this.fileLocation, 'utf8') + } + + /** + * Logic for bumping the version + * + * @param {!string} releaseType - The type of release + * @return {*} + */ + bump = (releaseType) => { + throw new Error('Implement bump logic in class!') + } + + /** + * Update the file + * + * @param {!string} newContent - New content for the file + * @return {*} + */ + update = (newContent) => ( + fs.writeFileSync(this.fileLocation, newContent) + ) + +} + diff --git a/src/version/git.js b/src/version/git.js index f1ba820..b73cc38 100644 --- a/src/version/git.js +++ b/src/version/git.js @@ -1,13 +1,10 @@ const core = require('@actions/core') const gitSemverTags = require('git-semver-tags') +const BaseVersioning = require('./base') const bumpVersion = require('../helpers/bumpVersion') -module.exports = new (class Git { - - newVersion = null - - init = (fileLocation, versionPath) => {} +module.exports = new (class Git extends BaseVersioning { bump = (releaseType) => { return new Promise((resolve) => { @@ -21,7 +18,7 @@ module.exports = new (class Git { // Get the new version this.newVersion = bumpVersion( releaseType, - currentVersion + currentVersion, ) // We are done diff --git a/src/version/index.js b/src/version/index.js index c5e5e93..edaa5b1 100644 --- a/src/version/index.js +++ b/src/version/index.js @@ -1,18 +1,16 @@ const JSON = require('./json') const Git = require('./git') +const Yaml = require('./yaml') module.exports = (fileExtension) => { switch (fileExtension.toLowerCase()) { case 'json': return JSON + case 'yaml': + case 'yml': + return Yaml + - // case 'yaml': - // case 'yml': - // return Yaml - // - // case 'toml': - // return Toml - // case 'git': return Git diff --git a/src/version/yaml.js b/src/version/yaml.js new file mode 100644 index 0000000..da7db77 --- /dev/null +++ b/src/version/yaml.js @@ -0,0 +1,41 @@ +const objectPath = require('object-path') +const yaml = require('yaml') + +const BaseVersioning = require('./base') +const bumpVersion = require('../helpers/bumpVersion') + +module.exports = new (class Yaml extends BaseVersioning{ + + /** + * Bumps the version in the package.json + * + * @param {!string} releaseType - The type of release + * @return {*} + */ + bump = (releaseType) => { + // Read the file + const fileContent = this.read() + const yamlContent = yaml.parse(fileContent) + const oldVersion = objectPath.get(yamlContent, this.versionPath) + + // Get the new version + this.newVersion = bumpVersion( + releaseType, + oldVersion, + ) + + // Get the name of where the version is in + 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}'`, + ), + ) + } + +}) + diff --git a/test-file.yaml b/test-file.yaml new file mode 100644 index 0000000..2b255eb --- /dev/null +++ b/test-file.yaml @@ -0,0 +1,8 @@ +package: + version: '0.1.0' + +# Comment +different: + props: + - one + - two diff --git a/yarn.lock b/yarn.lock index c35c774..f39ff30 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1307,6 +1307,11 @@ through@2, "through@>=2.2.7 <3": resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU= +toml@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/toml/-/toml-3.0.0.tgz#342160f1af1904ec9d204d03a5d61222d762c5ee" + integrity sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w== + trim-newlines@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613" @@ -1383,6 +1388,11 @@ xtend@~4.0.1: resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54" integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ== +yaml@^1.10.0: + version "1.10.0" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e" + integrity sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg== + yargs-parser@^18.1.3: version "18.1.3" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0"