From d71558cec69d773fb6846734b57ff55a6d7b3648 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Fri, 18 Oct 2019 10:35:25 +0200 Subject: [PATCH] feat: Test release --- action.yml | 5 ++++ src/helpers/packageJson.js | 58 ++++++++++++++++++++++++++++++++++++++ src/index.js | 21 ++++++++++---- 3 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 src/helpers/packageJson.js diff --git a/action.yml b/action.yml index 2023a46a..7ee09519 100644 --- a/action.yml +++ b/action.yml @@ -15,3 +15,8 @@ inputs: description: 'Commit message to use' default: 'chore(release): {version} :tada:' required: false + + preset: + description: 'The preset from Conventional Changelog to use' + default: 'preset' + required: false diff --git a/src/helpers/packageJson.js b/src/helpers/packageJson.js new file mode 100644 index 00000000..1b03dd2b --- /dev/null +++ b/src/helpers/packageJson.js @@ -0,0 +1,58 @@ +const path = require('path') +const fs = require('fs') + +module.exports = { + + /** + * Get's the project package.json + * @return {any} + */ + get: () => { + const packageJsonLoc = path.resolve('./', 'package.json') + + return JSON.parse(fs.readFileSync(packageJsonLoc)) + }, + + /** + * Bumps the version in the package.json + * + * @param packageJson + * @param releaseType + * @return {*} + */ + bump: (packageJson, releaseType) => { + let [major, minor, patch] = packageJson.version.split('.') + + switch (releaseType) { + case 'major': + major = major + 1 + minor = 0 + patch = 0 + break + + case 'minor': + minor = minor + 1 + patch = 0 + break + + default: + patch = patch + 1 + } + + // Update the package.json with the new version + packageJson.version = `${major}.${minor}.${patch}` + + return packageJson + }, + + /** + * Update package.json + * + * @param packageJson + * @return {*} + */ + update: (packageJson) => ( + fs.writeFileSync(packageJsonLoc, JSON.stringify(packageJson, null, 2)) + ), + +} diff --git a/src/index.js b/src/index.js index ced51a52..4a6b6cd7 100644 --- a/src/index.js +++ b/src/index.js @@ -4,25 +4,34 @@ const conventionalChangelog = require('conventional-changelog') const conventionalRecommendedBump = require('conventional-recommended-bump') const git = require('./helpers/git') +const packageJson = require('./helpers/packageJson') async function run() { try { const githubToken = core.getInput('github-token', { required: true }) const commitMessage = core.getInput('git-message') + const preset = core.getInput('preset') // Make the Github token secret core.setSecret(githubToken) + // conventionalChangelog({ // preset: 'angular', // }).pipe(process.stdout) - conventionalRecommendedBump({ - preset: `angular`, - }, (error, recommendation) => { - console.log(recommendation.releaseType) // 'major' - - core.info(`[recommendation.releaseType]: ${recommendation.releaseType}`) + conventionalRecommendedBump({ preset }, (error, recommendation) => { + if (error) { + core.setFailed(error.message) + + } else { + const packageJson = packageJson.bump( + packageJson.get(), + recommendation.releaseType, + ) + + core.info(`New version: ${packageJson.version}`) + } }) // Get the current version