Skip to content

Commit

Permalink
feat(cli): basic, changelog without md links (#24)
Browse files Browse the repository at this point in the history
* cli, expose basic option
* files, isBasic, prevent comparison link
* parse, isBasic, prevent hash, pr links
  • Loading branch information
cdcabrera authored Dec 23, 2022
1 parent b9d6aeb commit 61347ce
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ or Yarn
Generate a CHANGELOG.md with conventional commit types.
Usage: changelog [options]
Options:
-b, --basic Keep updates to CHANGELOG.md basic, skip all markdown link
syntax [boolean] [default: false]
-c, --commit Commit CHANGELOG.md and package.json with a release commit
[boolean] [default: true]
-d, --date CHANGELOG.md release date in the form of a valid date
Expand Down
12 changes: 10 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { commitChangelog, OPTIONS } = require('../src');
* Setup yargs
*/
const {
basic: isBasic,
commit: isCommit,
'commit-path': commitPath,
'compare-path': comparePath,
Expand All @@ -23,6 +24,12 @@ const {
.alias('h', 'help')
.version('version', packageJson.version)
.alias('v', 'version')
.option('b', {
alias: 'basic',
default: false,
describe: 'Keep updates to CHANGELOG.md basic, skip all markdown link syntax',
type: 'boolean'
})
.option('c', {
alias: 'commit',
default: true,
Expand Down Expand Up @@ -81,15 +88,16 @@ const {
* Set global OPTIONS
*
* @type {{comparePath: string, date: string, commitPath: string, isOverrideVersion: boolean,
* isAllowNonConventionalCommits: boolean, isDryRun: boolean,
* remoteUrl: string, prPath: string, isCommit: boolean, overrideVersion: string|*}}
* isAllowNonConventionalCommits: boolean, isDryRun: boolean, remoteUrl: string,
* prPath: string, isCommit: boolean, overrideVersion: string|*, isBasic: boolean}}
* @private
*/
OPTIONS._set = {
commitPath,
comparePath,
date,
isAllowNonConventionalCommits,
isBasic,
isDryRun,
isCommit,
isOverrideVersion: overrideVersion !== undefined,
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/__snapshots__/files.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ All notable changes to this project will be documented in this file.
"
`;

exports[`Files should create, and update CHANGELOG.md version with a comparison urls: urls and paths, release commit, no markdown links 1`] = `
"# Changelog
All notable changes to this project will be documented in this file.
## 1.0.0 (2022-10-01)
### Features
* **dolor** issues/20 sit enhancements (#8) (53a1234)
### Code Refactoring
* **file** lorem updates (#8) (LASTg45)
### Chores
* **build** npm packages (#15) (e5c456e)
### Bug Fixes
* **build** eslint, jsdoc updates (#16) (d123453)
"
`;

exports[`Files should create, and update a basic CHANGELOG.md: changelog 1`] = `
"# Changelog
All notable changes to this project will be documented in this file.
Expand Down
48 changes: 48 additions & 0 deletions src/__tests__/__snapshots__/parse.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,54 @@ exports[`Parse should get the first, last commits: first and last, release commi

exports[`Parse should parse a commit listing using conventional commit types and semver: parsed commits 1`] = `
{
"basicCommitsNoMarkdownLinks": {
"build": {
"commits": [
"* **deps** bump codecov/codecov-action from 1.1.0 to 1.1.1 (#19) (fe7d312)",
"* **deps** bump actions/cache from 1 to 2 (#12) (112345c)",
"* **deps** bump actions/checkout from 1 to 2 (#11) (512345d)",
"* **deps** aggregated checks, updates (#10) (12345d7)",
"* **deps** bump actions/github-script from 5 to 6 (#9) (1412345)",
],
"description": "Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)",
"title": "Builds",
"value": "build",
},
"chore": {
"commits": [
"* **build** npm packages (#15) (e5c456e)",
"* **build** npm packages (#15) (6f0f55d)",
],
"description": "Other changes that don't modify src or test files",
"title": "Chores",
"value": "chore",
},
"feat": {
"commits": [
"* **dolor** issues/20 sit enhancements (#8) (53a1234)",
],
"description": "A new feature",
"title": "Features",
"value": "feat",
},
"fix": {
"commits": [
"* **build** npm packages (#18) (6112345)",
"* **build** eslint, jsdoc updates (#16) (d123453)",
],
"description": "A bug fix",
"title": "Bug Fixes",
"value": "fix",
},
"refactor": {
"commits": [
"* **file** lorem updates (#8) (1f12345)",
],
"description": "A code change that neither fixes a bug nor adds a feature",
"title": "Code Refactoring",
"value": "refactor",
},
},
"commits": {
"build": {
"commits": [
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ describe('Files', () => {
}
)
).toMatchSnapshot('urls and paths, release commit');

expect(
updateChangelog(
commitObj,
'1.0.0',
{
date: '2022-10-01',
isBasic: true
},
{
getComparisonCommitHashes: () => comparisonObjReleaseCommit,
getRemoteUrls: () => urlObj
}
)
).toMatchSnapshot('urls and paths, release commit, no markdown links');
});

it('should update a package.json', () => {
Expand Down
30 changes: 24 additions & 6 deletions src/__tests__/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ describe('Parse', () => {
const commitMessageNonCC = '1f12345b597123453031234555b6d25574ccacee Initial commit';

expect({
refactor: formatChangelogMessage(parseCommitMessage(commitMessageRefactor), {
getRemoteUrls: () => ({
commitUrl: 'https://localhost/lorem/ipsum/commitsmock/',
prUrl: 'https://localhost/lorem/ipsum/prmock/'
})
}),
refactor: formatChangelogMessage(
parseCommitMessage(commitMessageRefactor),
{},
{
getRemoteUrls: () => ({
commitUrl: 'https://localhost/lorem/ipsum/commitsmock/',
prUrl: 'https://localhost/lorem/ipsum/prmock/'
})
}
),
feat: formatChangelogMessage(parseCommitMessage(commitMessageFeature)),
general: formatChangelogMessage(parseCommitMessage(commitMessageNonCC))
}).toMatchSnapshot('formatChangelogMessages');
Expand All @@ -82,8 +86,10 @@ describe('Parse', () => {
12345dd312345d421231231312312345dca11235 Initial commit
`;

// Basic
const commitObj = parseCommits({ getGit: () => commitLog });

// Url path
const { mockClear: urlPathObjClear } = mockObjectProperty(OPTIONS, {
commitPath: 'sit',
prPath: 'dolor',
Expand All @@ -92,6 +98,17 @@ describe('Parse', () => {
const urlPathObj = parseCommits({ getGit: () => commitLog });
urlPathObjClear();

// No markdown links
const { mockClear: basicCommitsNoMarkdownLinksObjClear } = mockObjectProperty(OPTIONS, {
commitPath: 'sit',
isBasic: true,
prPath: 'dolor',
remoteUrl: 'https://localhost/lorem/ipsum'
});
const basicCommitsNoMarkdownLinksObj = parseCommits({ getGit: () => commitLog });
basicCommitsNoMarkdownLinksObjClear();

// Allow general commit messages
const { mockClear: generalCommitsObjClear } = mockObjectProperty(OPTIONS, {
isAllowNonConventionalCommits: true,
remoteUrl: 'https://localhost/lorem/ipsum'
Expand All @@ -103,6 +120,7 @@ describe('Parse', () => {
default: parseCommits(),
commits: commitObj,
urls: urlPathObj,
basicCommitsNoMarkdownLinks: basicCommitsNoMarkdownLinksObj,
generalCommits: generalCommitsObj
}).toMatchSnapshot('parsed commits');
expect({ default: semverBump(), commits: semverBump(commitObj) }).toMatchSnapshot('semver bump');
Expand Down
5 changes: 3 additions & 2 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { getComparisonCommitHashes } = require('./parse');
* @param {*|string} packageVersion
* @param {object} options
* @param {string} options.date
* @param {boolean} options.isBasic
* @param {boolean} options.isDryRun
* @param {object} settings
* @param {string} settings.fallbackPackageVersion
Expand All @@ -26,7 +27,7 @@ const { getComparisonCommitHashes } = require('./parse');
const updateChangelog = (
parsedCommits = {},
packageVersion,
{ date, isDryRun = false } = OPTIONS,
{ date, isBasic = false, isDryRun = false } = OPTIONS,
{
fallbackPackageVersion = '¯\\_(ツ)_/¯',
filePath = join(OPTIONS.contextPath, `/CHANGELOG.md`),
Expand Down Expand Up @@ -58,7 +59,7 @@ const updateChangelog = (
if (packageVersion) {
version = packageVersion;

if (compareUrl) {
if (!isBasic && compareUrl) {
const { first, last } = getAliasComparisonCommitHashes();
if (first && last) {
version = `[${version}](${compareUrl}${first}...${last})`;
Expand Down
7 changes: 5 additions & 2 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ const parseCommitMessage = (message, { getCommitType: getAliasCommitType = getCo
* @param {string} params.description
* @param {string|number|*} params.prNumber
* @param {string} params.hash
* @param {object} options
* @param {boolean} options.isBasic
* @param {object} settings
* @param {Function} settings.getRemoteUrls
* @returns {string}
*/
const formatChangelogMessage = (
{ scope, description, prNumber, hash } = {},
{ isBasic } = OPTIONS,
{ getRemoteUrls: getAliasRemoteUrls = getRemoteUrls } = {}
) => {
const { commitUrl, prUrl } = getAliasRemoteUrls();
Expand All @@ -106,11 +109,11 @@ const formatChangelogMessage = (
let updatedPr = (prNumber && `(#${prNumber})`) || '';
let updatedHash = (hash && `(${hash.substring(0, 7)})`) || '';

if (prUrl && updatedPr) {
if (!isBasic && prUrl && updatedPr) {
updatedPr = `([#${prNumber}](${new URL(prNumber, prUrl).href}))`;
}

if (commitUrl && updatedHash) {
if (!isBasic && commitUrl && updatedHash) {
updatedHash = `([${hash.substring(0, 7)}](${new URL(hash, commitUrl).href}))`;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/code.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`General code checks should only have specific console.[warn|log|info|er
"cmds.js:20: console.error(color.RED, errorMessage.replace('{0}', e.message), color.NOCOLOR)",
"cmds.js:138: console.error(color.RED, \`Semver: \${e.message}\`, color.NOCOLOR)",
"cmds.js:168: console.error(color.RED, \`Semver: \${e.message}\`, color.NOCOLOR)",
"files.js:73: console.info(\` \${updatedBody}\`)",
"files.js:74: console.info(\` \${updatedBody}\`)",
"global.js:64: console.error(color.RED, \`Conventional commit types: \${e.message}\`, color.NOCOLOR)",
"index.js:47: console.info( index.js:61: console.info(color.NOCOLOR)",
]
Expand Down

0 comments on commit 61347ce

Please sign in to comment.