Skip to content

Commit

Permalink
refactor(changelog): change version headline
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Richter committed Nov 4, 2020
1 parent 9bb6d64 commit 69ba93e
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 72 deletions.
18 changes: 7 additions & 11 deletions lib/commands/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,18 @@ const getPendingChanges = require('../steps/pending-changes');

const generateChangeLog = require('../steps/changelog');

async function showChangelog(cwd, pkg, options) {
function ensureVersionTag() {
return ensureTag(cwd, `v${pkg.version}`);
}

function runTask(task) {
return task(cwd, pkg, options);
}
function ensureVersionTag(cwd, pkg) {
return ensureTag(cwd, `v${pkg.version}`);
}

async function showChangelog(cwd, pkg, options) {
function printChangelog() {
process.stdout.write(`${options.changelog}\n`);
}

const tasks = [ensureVersionTag, getPendingChanges, generateChangeLog];

for (const task of tasks) await runTask(task);
for (const task of [ensureVersionTag, getPendingChanges, generateChangeLog]) {
await task(cwd, pkg, options);
}

return printChangelog();
}
Expand Down
8 changes: 3 additions & 5 deletions lib/commands/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ function release(cwd, pkg, options) {
publishToNpm,
];

function runTask(task) {
return task(cwd, pkg, options);
}

async function runPublishTasks() {
if (options.pr) {
debug('Never publishing from a PR');
Expand All @@ -109,7 +105,9 @@ function release(cwd, pkg, options) {
publishTasks = [publishToNpm];
}

for (const task of publishTasks) await runTask(task);
for (const task of publishTasks) {
await task(cwd, pkg, options);
}

return null;
}
Expand Down
15 changes: 11 additions & 4 deletions lib/git/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ function parseCommit(commit) {
return { ...data, sha: sha || data.sha, parentSha };
}

/**
*
* @param {{type: string, header: string}} commit
* @return {*|boolean}
*/
function isNotRandomMerge(commit) {
// DotCI adds these :(
return commit.type || `${commit.header}`.indexOf('Merge ') !== 0;
Expand All @@ -111,6 +116,10 @@ function gracefulEmptyState(error) {
throw error;
}

/**
* @param {string} fromRevision
* @return {string|*[]}
*/
function createRange(fromRevision) {
if (fromRevision && fromRevision !== 'v0.0.0') {
return `${fromRevision}..HEAD`;
Expand All @@ -119,15 +128,13 @@ function createRange(fromRevision) {
return [];
}

function getCommits(directory, fromRevision) {
function getCommits(cwd, fromRevision) {
return run(
'git',
['log', '--reverse', '--topo-order', GIT_LOG_FORMAT].concat(
createRange(fromRevision)
),
{
cwd: directory,
}
{ cwd }
).then(parseLogOutput, gracefulEmptyState);
}

Expand Down
10 changes: 8 additions & 2 deletions lib/steps/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,28 @@ ${changelog}`;
*/

function formatCommits(orphans) {
const orphansCommits = orphans.map(formatCommit);
const changes = prs
.map(formatPR)
.concat(orphans.map(formatCommit))
.concat(orphansCommits)
.map(line => {
return `* ${line}`;
});
return changes.join('\n');
}

for (const pr of prs) await addPullRequestCommits(pkg, commits, pr);
for (const pr of prs) {
await addPullRequestCommits(pkg, commits, pr);
}

removeInvalidPRs(prs);

let changelog = removePRCommits(commits, prs);
changelog = formatCommits(changelog);
changelog = prependBreakingChanges(changelog);

options.changelog = changelog;

return changelog;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/steps/pending-changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ function normalizeReferences(meta, commit) {
return commit;
}

function getPendingChanges(cwd, pkg, options) {
async function getPendingChanges(cwd, pkg, options) {
const meta = parseRepository(pkg.repository);
return getCommits(cwd, `v${pkg.version}`).then(commits => {
options.commits = commits.map(normalizeReferences.bind(null, meta));
});
const commits = await getCommits(cwd, `v${pkg.version}`);

options.commits = commits.map(normalizeReferences.bind(null, meta));
}

module.exports = getPendingChanges;
5 changes: 4 additions & 1 deletion lib/steps/version-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,18 @@ function createVersionCommit(cwd, pkg, options) {
changeLogContent = '';
}

changeLogContent = `### ${options.nextVersion} - ${getCurrentDate()}\n\n${
changeLogContent = `### v${options.nextVersion} (${getCurrentDate()})\n\n${
options.changelog
}${changeLogContent}`;
fs.writeFileSync(changeLogFile, `${changeLogContent.trim()}\n`);

const packageJsonFile = path.join(cwd, 'package.json');
pkg.version = options.nextVersion;
fs.writeFileSync(packageJsonFile, `${JSON.stringify(pkg, null, 2)}\n`);

const files = ['CHANGELOG.md', 'package.json'];
updatePackageLockVersion(cwd, pkg.version, files);

return addFiles(cwd, files)
.then(commit.bind(null, cwd, `v${pkg.version}`))
.then(getHEAD.bind(null, cwd))
Expand Down
50 changes: 7 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"conventional-commits-parser": "^3.1.0",
"debug": "^4.2.0",
"glob": "^7.1.6",
"gofer": "^5.0.3",
"gofer": "^5.0.4",
"minimist": "^1.2.5",
"rc": "^1.2.8",
"semver": "^7.3.2"
Expand Down
1 change: 1 addition & 0 deletions test/git/commits.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ describe('getCommits', () => {
assertIssue('Jira', expected);
});
});

describe('with multiple commits', () => {
const dirname = withFixture('multiple-commits');
let allCommits = null;
Expand Down
6 changes: 5 additions & 1 deletion test/steps/version-commit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ describe('createVersionCommit', () => {

before(resetVars);
afterEach(resetVars);

describe('with no package-lock.json', () => {
const dirname = withFixture('multiple-commits');
let currentDate;

before('commits with the original author', done => {
execFile('git', ['show'], { cwd: dirname }, (err, stdout) => {
if (err) return done(err);
Expand Down Expand Up @@ -98,7 +100,7 @@ describe('createVersionCommit', () => {
.readFileSync(`${dirname}/CHANGELOG.md`, 'utf8')
.split('\n');

assert.strictEqual(version, `### 1.0.0 - ${currentDate}`);
assert.strictEqual(version, `### v1.0.0 (${currentDate})`);
assert.strictEqual(commit1, '* New stuff');
assert.strictEqual(commit2, '* Interesting features');
});
Expand All @@ -112,6 +114,7 @@ describe('createVersionCommit', () => {
});
});
});

describe('using package-lock.json', () => {
const dirname = withFixture('with-plock');

Expand All @@ -136,6 +139,7 @@ describe('createVersionCommit', () => {

describe('with unused package-lock.json', () => {
const dirname = withFixture('with-bogus-plock');

before('create version commit', () =>
createVersionCommit(dirname, pkg, options)
);
Expand Down

0 comments on commit 69ba93e

Please sign in to comment.