Skip to content

Commit

Permalink
feat(cli): allow override version
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Oct 13, 2022
1 parent 3827018 commit cf8bff1
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ or Yarn
Uses system new Date([your date])
[string] [default: new Date().toISOString()]
-r, --dry-run Generate CHANGELOG.md sample output [boolean] [default: false]
-o, --override Use a version you define. [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
```
Expand Down
13 changes: 10 additions & 3 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const { commitChangelog } = require('../src');
const {
commit: isCommit,
date,
'dry-run': isDryRun
'dry-run': isDryRun,
override: overrideVersion
} = yargs
.usage('Generate a CHANGELOG.md with conventional commit types.\n\nUsage: changelog [options]')
.help('help')
Expand All @@ -29,6 +30,11 @@ const {
describe: 'CHANGELOG.md release date in the form of a valid date string. Uses system new Date([your date])',
type: 'string'
})
.option('o', {
alias: 'override',
describe: 'Use a version you define.',
type: 'string'
})
.option('r', {
alias: 'dry-run',
default: false,
Expand All @@ -37,11 +43,12 @@ const {
}).argv;

if (process.env.NODE_ENV === 'test') {
process.stdout.write(JSON.stringify({ date, isDryRun, isCommit }));
process.stdout.write(JSON.stringify({ date, isDryRun, isCommit, overrideVersion }));
} else {
commitChangelog({
date,
isDryRun,
isCommit
isCommit,
overrideVersion
});
}
6 changes: 6 additions & 0 deletions src/__tests__/__snapshots__/cmds.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ exports[`Commands should attempt to run commands: commands 1`] = `
{
"getGit": "<execSync>["git log <execSync>[\\"git..HEAD --pretty=oneline"]</execSync>",
},
{
"getOverrideVersion": {
"clean": "<semverClean>[null]</semverClean>",
"version": undefined,
},
},
{
"getReleaseCommit": "<execSync>["git log --grep=\\"chore(release)\\" --pretty=oneline -1"]</execSync>",
},
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/files.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Files should create, and update a CHANGELOG.md: changelog 1`] = `
"# Changelog
All notable changes to this project will be documented in this file.
## 0.1.0 (2022-10-01)
## ¯\\_(ツ)_/¯ (2022-10-01)
### Features
* **dolor** issues/20 sit enhancements (#8) (53a12345479ef91123456e921234548ac4123450)
Expand Down
27 changes: 27 additions & 0 deletions src/cmds.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ const getGit = ({ getReleaseCommit: getAliasReleaseCommit = getReleaseCommit } =
return runCmd(`git log ${releaseCommitHash}..HEAD --pretty=oneline`, 'Skipping commit "get" check... {0}');
};

/**
* Determine if override version is valid semver and return
*
* @param {string|*} version
* @return {{clean: string, version: string}}
*/
const getOverrideVersion = version => {
let updatedVersion;
let clean;

try {
clean = semverClean(version);
} catch (e) {
console.error(color.RED, `Semver: ${e.message}`, color.NOCOLOR);
}

if (clean) {
updatedVersion = version;
}

return {
version: updatedVersion,
clean
};
};

/**
* Set package.json version using npm version
*
Expand Down Expand Up @@ -102,6 +128,7 @@ const getVersion = (versionBump, { getCurrentVersion: getAliasCurrentVersion = g
module.exports = {
commitFiles,
getGit,
getOverrideVersion,
getReleaseCommit,
getVersion,
runCmd
Expand Down
12 changes: 9 additions & 3 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ const { _COMMIT_CHANGELOG_CONTEXT_PATH: CONTEXT_PATH } = global;
* @param {object} parsedCommits
* @param {*|string} packageVersion
* @param {object} options
* @param {string} options.fallbackPackageVersion
* @param {string} options.filePath
* @param {boolean} options.isDryRun
* @returns {string}
*/
const updateChangelog = (
parsedCommits = {},
packageVersion = '0.1.0',
{ date, filePath = join(CONTEXT_PATH, `/CHANGELOG.md`), isDryRun = false } = {}
packageVersion,
{
date,
fallbackPackageVersion = '¯\\_(ツ)_/¯',
filePath = join(CONTEXT_PATH, `/CHANGELOG.md`),
isDryRun = false
} = {}
) => {
const systemTimestamp = ((date && new Date(date)) || new Date()).toLocaleDateString('fr-CA', {
timeZone: 'UTC'
Expand All @@ -35,7 +41,7 @@ const updateChangelog = (
const displayCommits = Object.values(parsedCommits)
.sort(({ title: titleA }, { title: titleB }) => titleB.localeCompare(titleA))
.reduce((str, { title, commits = [] }) => `${str}\n### ${title}\n${commits.join('\n')}\n`, '');
const updatedBody = `## ${packageVersion} (${systemTimestamp})\n${displayCommits}`;
const updatedBody = `## ${packageVersion || fallbackPackageVersion} (${systemTimestamp})\n${displayCommits}`;
const output = `${header}\n${updatedBody}\n${body}`;

if (isDryRun) {
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { color } = require('./global');
const { commitFiles, getVersion } = require('./cmds');
const { commitFiles, getOverrideVersion, getVersion } = require('./cmds');
const { parseCommits, semverBump } = require('./parse');
const { updateChangelog, updatePackage } = require('./files');
const { _COMMIT_CHANGELOG_CONTEXT_PATH: CONTEXT_PATH } = global;
Expand All @@ -12,15 +12,16 @@ const { _COMMIT_CHANGELOG_CONTEXT_PATH: CONTEXT_PATH } = global;
* @param {string} params.date
* @param {boolean} params.isCommit
* @param {boolean} params.isDryRun
* @param {string} params.overrideVersion
*/
const commitChangelog = ({ contextPath = CONTEXT_PATH, date, isCommit, isDryRun } = {}) => {
const commitChangelog = ({ contextPath = CONTEXT_PATH, date, isCommit, isDryRun, overrideVersion } = {}) => {
if (process.env.NODE_ENV !== 'test') {
process.chdir(contextPath);
}

const parsedCommits = parseCommits();
const { bump, weight } = semverBump(parsedCommits);
const { clean: cleanVersion, version } = getVersion(bump);
const { clean: cleanVersion, version } = (overrideVersion && getOverrideVersion(overrideVersion)) || getVersion(bump);

if (isDryRun) {
console.info(
Expand Down
7 changes: 4 additions & 3 deletions tests/__snapshots__/code.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
exports[`General code checks should only have specific console.[warn|log|info|error] methods: console methods 1`] = `
[
"cmds.js:21: console.error(color.RED, errorMessage.replace('{0}', e.message), color.NOCOLOR)",
"cmds.js:93: console.error(color.RED, \`Semver: \${e.message}\`, color.NOCOLOR)",
"files.js:42: console.info(\` \${output}\`)",
"index.js:26: console.info( index.js:40: console.info(color.NOCOLOR)",
"cmds.js:89: console.error(color.RED, \`Semver: \${e.message}\`, color.NOCOLOR)",
"cmds.js:119: console.error(color.RED, \`Semver: \${e.message}\`, color.NOCOLOR)",
"files.js:48: console.info(\` \${output}\`)",
"index.js:27: console.info( index.js:41: console.info(color.NOCOLOR)",
"parse.js:19: console.error(color.RED, \`Conventional commit types: \${e.message}\`, color.NOCOLOR)",
]
`;

0 comments on commit cf8bff1

Please sign in to comment.