Skip to content

Commit

Permalink
feat(cli): changelog, package, expose output (#29)
Browse files Browse the repository at this point in the history
* cli, expose changelog, package options
* cmds, files, index, pass changelog, packagePaths
* global, allow prop functions
  • Loading branch information
cdcabrera authored Jan 14, 2023
1 parent 52f1a1c commit 7106ec7
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 80 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,40 @@ or Yarn
Usage: changelog [options]

Options:
-b, --basic Keep updates to CHANGELOG.md basic, skip all markdown
-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
-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
-d, --date [CHANGELOG.md] release date in the form of a valid date
string. Uses system new Date([your date])
[string] [default: "2022-12-17T02:28:23.456Z"]
[string] [default: "2023-01-03T02:22:35.984Z"]
-n, --non-cc Allow non-conventional commits to apply a semver weight
and appear in CHANGELOG.md under a general type
and appear in [CHANGELOG.md] under a general type
description. [boolean] [default: false]
-o, --override Use a version you define. [string]
-r, --dry-run Generate CHANGELOG.md sample output
-r, --dry-run Generate [CHANGELOG.md] sample output
[boolean] [default: false]
--commit-path CHANGELOG.md path used for commits. This will be
--changelog Changelog output filename and relative path
[string] [default: "./CHANGELOG.md"]
--commit-path [CHANGELOG.md] path used for commits. This will be
"joined" with "remote-url". Defaults to the commits
path for GitHub. [string] [default: "commit/"]
--compare-path CHANGELOG.md path used for version comparison. This
--compare-path [CHANGELOG.md] path used for version comparison. This
will be "joined" with "remote-url". Defaults to the
comparison path for GitHub.
[string] [default: "compare/"]
--pr-path CHANGELOG.md path used for PRs/MRs. This will be
--package package.json read, output and relative path
[string] [default: "./package.json"]
--pr-path [CHANGELOG.md] path used for PRs/MRs. This will be
"joined" with "remote-url". Defaults to the PR path for
GitHub. [string] [default: "pull/"]
--release-message A list of prefix release scope commit messages. First
list item is the new commit message prefix, the second
list item searches for the prior release message prefix
for range. [write new, search old]
[array] [default: "chore(release)"]
--remote-url Git remote get-url for updating CHANGELOG.md base urls.
This should start with "http". Defaults to "$ git
--remote-url Git remote get-url for updating [CHANGELOG.md] base
urls. This should start with "http". Defaults to "$ git
remote get-url origin" [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Expand Down
47 changes: 34 additions & 13 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

const { join } = require('path');
const yargs = require('yargs');
const packageJson = require('../package');
const { commitChangelog, OPTIONS } = require('../src');
Expand All @@ -9,13 +10,15 @@ const { commitChangelog, OPTIONS } = require('../src');
*/
const {
basic: isBasic,
changelog: changelogFile,
commit: isCommit,
'commit-path': commitPath,
'compare-path': comparePath,
date,
'dry-run': isDryRun,
'non-cc': isAllowNonConventionalCommits,
override: overrideVersion,
package: packageFile,
'pr-path': prPath,
'release-message': releaseTypeScope,
'remote-url': remoteUrl
Expand All @@ -28,26 +31,26 @@ const {
.option('b', {
alias: 'basic',
default: false,
describe: 'Keep updates to CHANGELOG.md basic, skip all markdown link syntax',
describe: 'Keep updates to [CHANGELOG.md] basic, skip all markdown link syntax',
type: 'boolean'
})
.option('c', {
alias: 'commit',
default: true,
describe: 'Commit CHANGELOG.md and package.json with a release commit',
describe: 'Commit [CHANGELOG.md] and package.json with a release commit',
type: 'boolean'
})
.option('d', {
alias: 'date',
default: new Date().toISOString(),
describe: 'CHANGELOG.md release date in the form of a valid date string. Uses system new Date([your date])',
describe: '[CHANGELOG.md] release date in the form of a valid date string. Uses system new Date([your date])',
type: 'string'
})
.option('n', {
alias: 'non-cc',
default: false,
describe:
'Allow non-conventional commits to apply a semver weight and appear in CHANGELOG.md under a general type description.',
'Allow non-conventional commits to apply a semver weight and appear in [CHANGELOG.md] under a general type description.',
type: 'boolean'
})
.option('o', {
Expand All @@ -58,25 +61,35 @@ const {
.option('r', {
alias: 'dry-run',
default: false,
describe: 'Generate CHANGELOG.md sample output',
describe: 'Generate [CHANGELOG.md] sample output',
type: 'boolean'
})
.option('changelog', {
default: './CHANGELOG.md',
describe: 'Changelog output filename and relative path',
type: 'string'
})
.option('commit-path', {
default: 'commit/',
describe:
'CHANGELOG.md path used for commits. This will be "joined" with "remote-url". Defaults to the commits path for GitHub.',
'[CHANGELOG.md] path used for commits. This will be "joined" with "remote-url". Defaults to the commits path for GitHub.',
type: 'string'
})
.option('compare-path', {
default: 'compare/',
describe:
'CHANGELOG.md path used for version comparison. This will be "joined" with "remote-url". Defaults to the comparison path for GitHub.',
'[CHANGELOG.md] path used for version comparison. This will be "joined" with "remote-url". Defaults to the comparison path for GitHub.',
type: 'string'
})
.option('package', {
default: './package.json',
describe: 'package.json read, output and relative path',
type: 'string'
})
.option('pr-path', {
default: 'pull/',
describe:
'CHANGELOG.md path used for PRs/MRs. This will be "joined" with "remote-url". Defaults to the PR path for GitHub.',
'[CHANGELOG.md] path used for PRs/MRs. This will be "joined" with "remote-url". Defaults to the PR path for GitHub.',
type: 'string'
})
.option('release-message', {
Expand All @@ -87,20 +100,24 @@ const {
})
.option('remote-url', {
describe:
'Git remote get-url for updating CHANGELOG.md base urls. This should start with "http". Defaults to "$ git remote get-url origin"',
'Git remote get-url for updating [CHANGELOG.md] base urls. This should start with "http". Defaults to "$ git remote get-url origin"',
type: 'string'
}).argv;

/**
* Set global OPTIONS
*
* @type {{comparePath: string, date: string, commitPath: string, isOverrideVersion: boolean,
* isAllowNonConventionalCommits: boolean, releaseTypeScope: string[], isDryRun: boolean,
* remoteUrl: string, prPath: string, isCommit: boolean, overrideVersion: string|*,
* isBasic: boolean}}
* @type {{comparePath: string, date: string, packagePath: Function, isOverrideVersion: boolean,
* packageFile: string, isAllowNonConventionalCommits: boolean, prPath: string, isCommit: boolean,
* overrideVersion: string|*, changelogPath: Function, commitPath: string, changelogFile: string,
* releaseTypeScope: string[]|string, isDryRun: boolean, remoteUrl: string, isBasic: boolean}}
* @private
*/
OPTIONS._set = {
changelogFile,
changelogPath: function () {
return join(this.contextPath, changelogFile);
},
commitPath,
comparePath,
date,
Expand All @@ -110,6 +127,10 @@ OPTIONS._set = {
isCommit,
isOverrideVersion: overrideVersion !== undefined,
overrideVersion,
packageFile,
packagePath: function () {
return join(this.contextPath, packageFile);
},
prPath,
releaseTypeScope,
remoteUrl
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/global.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ exports[`Global should set a one-time mutable OPTIONS object: immutable 1`] = `
"contextPath": "../src/__fixtures__",
"dolor": "magna",
"lorem": "ipsum",
"sit": "function test ../src/__fixtures__",
},
"isFrozen": true,
}
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/cmds.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
const { join } = require('path');
const cmds = require('../cmds');
const { OPTIONS } = require('../global');

describe('Commands', () => {
const { mockClear } = mockObjectProperty(OPTIONS, {
date: '2022-10-01',
changelogPath: join(OPTIONS.contextPath, 'CHANGELOG.md'),
packagePath: join(OPTIONS.contextPath, 'package.json')
});

afterAll(() => {
mockClear();
});

it('should attempt to run commands', () => {
const { mockClear } = mockObjectProperty(OPTIONS, {
releaseTypeScope: 'chore(release)'
Expand Down
46 changes: 22 additions & 24 deletions src/__tests__/files.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
// spell-checker: disable
const { join } = require('path');
const { updateChangelog, updatePackage } = require('../files');
const { getComparisonCommitHashes, parseCommits } = require('../parse');
const { OPTIONS } = require('../global');

describe('Files', () => {
const { mockClear } = mockObjectProperty(OPTIONS, {
date: new Date('2022-10-01').toISOString(),
changelogPath: join(OPTIONS.contextPath, 'CHANGELOG.md'),
packagePath: join(OPTIONS.contextPath, 'package.json')
});

afterAll(() => {
mockClear();
});

it('should create, and update a basic CHANGELOG.md', () => {
const commitLog = `
1f12345b597123453031234555b6d25574ccacee refactor(file): lorem updates (#8)
Expand All @@ -13,7 +25,7 @@ describe('Files', () => {
`;

const commitObj = parseCommits({ getGit: () => commitLog });
expect(updateChangelog(commitObj, undefined, { date: '2022-10-01' })).toMatchSnapshot('changelog');
expect(updateChangelog(commitObj, undefined)).toMatchSnapshot('changelog');
});

it('should create, and update CHANGELOG.md version with a comparison urls', () => {
Expand All @@ -36,17 +48,10 @@ describe('Files', () => {
});

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

const comparisonObjReleaseCommit = getComparisonCommitHashes({
Expand All @@ -55,25 +60,18 @@ describe('Files', () => {
});

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

expect(
updateChangelog(
commitObj,
'1.0.0',
{
date: '2022-10-01',
...OPTIONS,
isBasic: true
},
{
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/global.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ describe('Global', () => {
const { OPTIONS } = global;
OPTIONS.lorem = 'et all';
OPTIONS.dolor = 'magna';
OPTIONS._set = { lorem: 'ipsum' };
OPTIONS._set = {
lorem: 'ipsum',
sit: function () {
return `function test ${this.contextPath}`;
}
};
OPTIONS.lorem = 'hello world';
OPTIONS.dolor = 'sit';

Expand Down
16 changes: 11 additions & 5 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
const { join } = require('path');
const { commitChangelog } = require('../index');
const { OPTIONS } = require('../global');

describe('Commit Changelog', () => {
it('should parse commits to produce a CHANGELOG.md', () => {
const { mockClear } = mockObjectProperty(OPTIONS, { date: new Date('2022-10-01').toISOString() });

expect(commitChangelog()).toMatchSnapshot('Commit Changelog');
const { mockClear } = mockObjectProperty(OPTIONS, {
date: new Date('2022-10-01').toISOString(),
changelogPath: join(OPTIONS.contextPath, 'CHANGELOG.md'),
packagePath: join(OPTIONS.contextPath, 'package.json')
});

afterAll(() => {
mockClear();
});

it('should parse commits to produce a CHANGELOG.md', () => {
expect(commitChangelog()).toMatchSnapshot('Commit Changelog');
});

it('should parse commits to produce a CHANGELOG.md with an override version', () => {
const { mockClear } = mockObjectProperty(OPTIONS, {
date: new Date('2022-10-01').toISOString(),
isOverrideVersion: true,
overrideVersion: '15.0.0'
});
Expand Down
18 changes: 9 additions & 9 deletions src/cmds.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ const runCmd = (cmd, { errorMessage = 'Skipping... {0}' } = {}) => {
*
* @param {*|string} version
* @param {object} options
* @param {string} options.contextPath
* @param {string} options.changelogPath
* @param {string} options.packagePath
* @param {string[]|string} options.releaseTypeScope
* @returns {string}
*/
const commitFiles = (version, { contextPath, releaseTypeScope } = OPTIONS) => {
const commitFiles = (version, { changelogPath, packagePath, releaseTypeScope } = OPTIONS) => {
const isArray = Array.isArray(releaseTypeScope);

return runCmd(
`git add ${join(contextPath, 'package.json')} ${join(contextPath, 'CHANGELOG.md')} && git commit ${join(
contextPath,
'package.json'
)} ${join(contextPath, 'CHANGELOG.md')} -m "${(isArray && releaseTypeScope?.[0]) || releaseTypeScope}: ${version}"`,
`git add ${packagePath} ${changelogPath} && git commit ${packagePath} ${changelogPath} -m "${
(isArray && releaseTypeScope?.[0]) || releaseTypeScope
}: ${version}"`,
'Skipping release commit... {0}'
);
};
Expand All @@ -48,11 +48,11 @@ const commitFiles = (version, { contextPath, releaseTypeScope } = OPTIONS) => {
* Get current package.json version
*
* @param {object} options
* @param {string} options.contextPath
* @param {string} options.packagePath
* @returns {*}
*/
const getCurrentVersion = ({ contextPath } = OPTIONS) => {
const { version } = require(join(contextPath, 'package.json'));
const getCurrentVersion = ({ packagePath } = OPTIONS) => {
const { version } = require(packagePath);
return version;
};

Expand Down
Loading

0 comments on commit 7106ec7

Please sign in to comment.