Skip to content

feat: adds support for conventional-commits (for CHANGELOG generation and verison bump) - #665

Merged
evocateur merged 8 commits into
lerna:masterfrom
bcoe:conventional-commits
Mar 15, 2017
Merged

evocateur merged 8 commits into
lerna:masterfrom
bcoe:conventional-commits

Conversation

@bcoe

@bcoe bcoe commented Mar 11, 2017 •

Copy link
Copy Markdown
Contributor

Problem

Independent mode forces you to manually bump each package; it can be easy to lose track of whether versions are major, minor, patch.

Solution

Using a conventional-commit-format the git history can be parsed, and used to:

  • determine the appropriate version bump.
  • generate a lovely CHANGELOG, so that users who depend on your library have good forensic information (why the heck was this a major bump?).

Notes

Please note that this functionality is optional, and is turned on by the flag --conventional-commits.

Todo

  • add tests for ConventionalCommitUtilties.js.

addresses #662 (CC: @hzoo, @evocateur).

@bcoe

bcoe commented Mar 12, 2017

Copy link
Copy Markdown
Contributor Author

@Tapppi, @stevemao, this pulls together everything being discussed in conventional-changelog/conventional-changelog#168 -- validating the week of noodling I've been doing in our conventional-changelog repos.

Comment thread src/commands/PublishCommand.js Outdated
// write new package
FileSystemUtilities.writeFileSync(packageJsonLocation, pkg.toJsonString());

// we can now generate the Chnagelog, based on the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: Changelog

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch 👍

Comment thread src/ConventionalCommitUtilties.js Outdated
// run conventional-changelog-cli to generate the markdown
// for the upcoming release.
const newEntry = ChildProcessUtilities.execSync(
`${require.resolve("conventional-changelog-cli/cli.js")} -l ${name} --commit-path=${pkgPath} --pkg=${pkgPath} -p angular`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason we don't use the api module, conventional-changelog, here?

@bcoe bcoe Mar 12, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my feeling that relying on the conventional-recommended-bump cli, and conventional-changelog-cli fits well with the existing patterns in the codebase; simply shelling out to npm with NpmUtilities.js , or git with GitUtilities.js:

I'm not completely married to this approach, but it feels pretty elegant to me that the lerna codebase doesn't need any domain specific knowledge of the conventional-changelog API -- as far as this codebase is concerned it's just calling another command line application.

Comment thread src/ConventionalCommitUtilties.js Outdated

// CHANGELOG entries start with <a name=, we remove
// the header if it exists by starting at the first entry.
if (changelogContents.indexOf("<a name=") !== -1) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could tweak the template here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevemao mind going into details don't completely follow?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevemao I think we have slightly different use-cases in mind; I like the idea of adding in a header that explains the conventional-changelog format to curious parties -- I've found this is very useful for evangelizing the approach --

the annoying thing is it means we need to avoid adding the header multiple times as the CHANGELOG is updated; is there an existing module that solves this use-case?

@stevemao stevemao Mar 12, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: I added this comment before your second comment

We could change the handlebars template for the header. It's easy to do it with the api module

conventionalChangelog(options, context, gitRawCommitsOpts, parserOpts, {
    headerPartial: '{{#if isPatch~}}...' // without '<a name='
})

But with the cli at the moment we need to provide a config file to change this option. The presets are good example of overwriting the default template partials

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevemao I think this is the header for specific commits? I'm talking about the use-case:

# Overall Title

Header that introduces conventional commits and doesn't change.

* commit.
* commit.
* commit

Why don't we add the actual CHANGELOG merging logic to conventional-changelog-cli; then we could use the same logic for lerna and for standard-version; I think this would be a good solution because it DRYs up both codebases logic.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The header of conventional-changelog-writer is per release. I'm not talking about the CHANGELOG_HEADER here and I think this is done right. I'm just saying stripping <a name= manually shouldn't be needed since we could just overwrite the headerPartial template.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevemao gotcha; I think my comment is just confusing.

I don't want to strip the generated header; I just want to insert the new commit message between CHANGELOG_HEADER and the old commit messages; this is the same logic used by standard-version., and might be worth DRYing up.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops sorry, misread the code :)

@Tapppi

Tapppi commented Mar 12, 2017

Copy link
Copy Markdown

@bcoe This is awesome, I think enabling a workflow as easy as standard-version is for regular repos for individual mode would be a big thing. E.g. this is a blocker in our organisations move to lerna repos.. I'll definitely try to help with reviews / PR's as much as I have time (or at least lurk around leaving comments 😊 ).

@evocateur evocateur left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than supporting the configurable packages location, this looks ready to go. Thanks @bcoe!

Comment thread src/ConventionalCommitUtilties.js Outdated

${newEntry}

${changelogContents}`.replace(/\n+$/, "\n")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orthogonal to this PR, but I really like dedent for readable indentation of multi-line template strings.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evocateur that's a really smart idea.

Comment thread src/ConventionalCommitUtilties.js Outdated
@logger.logifySync()
static recommendVersion(name, version) {
try {
const pkgPath = `./packages/${name}/`;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't support our customizable (and potentially multiple) packages config. I would recommend instead simply passing the Package instance in and dereferencing the fields directly (such as pkg.location, which will give you what you want here).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evocateur yeah, I noticed this when I started writing tests; wasn't sure how complex pkg.location could become.

It feels like changelog.location should then be something like path.resolve(pkg.location, 'changelog.md')?

Comment thread src/commands/PublishCommand.js Outdated
// the updated version that we're about to release.
if (this.flags.conventionalCommits) {
ConventionalCommitUtilties.updateChangelog(update.package.name, changelogLocation);
changedFiles.push(changelogLocation);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to encapsulate the changelogLocation assignment inside updateChangelog(). Perhaps return the changed location for pushing into changedFiles, then?

In fact, changelogs being important enough, regardless of how one generates them, I can justify adding a changelogLocation getter to the Package class, thus removing the need for a return value.

diff --git a/src/Package.js b/src/Package.js
index 76a88da..d4d4bee 100644
--- a/src/Package.js
+++ b/src/Package.js
@@ -17,6 +17,10 @@ export default class Package {
     return this._location;
   }
 
+  get changelogLocation() {
+    return path.join(this._location, "CHANGELOG.md");
+  }
+
   get nodeModulesLocation() {
     return path.join(this._location, "node_modules");
   }

@jamiebuilds

Copy link
Copy Markdown
Contributor

Hi @bcoe, thanks for opening this.

I just wanted to make you aware of some plans for the future. We're planning on merging Lerna into Yarn through "workspaces" (sounds like npm has some related plans). I'm not sure a feature like this makes sense to be built directly into Yarn or npm, but definitely on top of it.

I'm wondering how we can enable you to build features like this on top of Lerna and later Yarn so that you don't have to rethink everything in the future.

One thing I've always wanted in Lerna which is part of the plan for Yarn is lerna version. This would match npm/yarn version but for Lerna/Yarn workspaces. This would let you stage changes before publishing them.

I think this functionality could be built on top of that and give you a lot more flexibility in the future.

@evocateur

Copy link
Copy Markdown
Member

@thejameskyle Is there a planning wiki/issue for "workspaces" that we can follow?

@bcoe

bcoe commented Mar 13, 2017

Copy link
Copy Markdown
Contributor Author

@thejameskyle @evocateur I was thinking events resembling package lifecycle scripts would get you pretty far ... but, one killer feature of a conventional changelog I couldn't think of an elegant way to facilitate, was recommending the version bump:

If you're using a conventional changelog format, we know when the release should be a patch, minor or major based on commit format -- it would be nice to be able to delegate to an external script for deciding on the semver bump that lerna is applying.

I've been talking to @iarna about my requirements around a monorepo, and was aware of the plans of rolling this workflow (at least in some form) into yarn -- my two cents, I think that the tagging and publication workflow that lerna facilitates in an abstract way is quite powerful, it would be cool to continue having a library that lives separately from your npm client of choice to facilitate the advanced workflows I rely on as an open-source maintainer: git-tagging, CHANGELOG generation, etc.

@evocateur

Copy link
Copy Markdown
Member

I'm not sure yarn or npm should be gaining a bunch of git-related abilities (counting commits, measuring tags, diffing a branch, etc), but anything to support easier parallel operations (publishing, managing dist-tags, and perhaps even hoisting) would be welcome.

@jamiebuilds

Copy link
Copy Markdown
Contributor

I think Lerna should as much as possible try to model what would be built into Yarn or npm. People have and are going to continue building a lot of tooling on top of this functionality and it would be nice if that tooling didn't lock people in place.

There's a lot we can build into Lerna as an independent tool, but what's maybe more interesting is using Lerna as an experiment into how we can build features like workspaces into our package managers while finding the right extension points.

If we can build conventional-changelog tooling on top of Lerna, that tool can be easily reused if people want to switch to Yarn or npm.

@bcoe

bcoe commented Mar 13, 2017 •

Copy link
Copy Markdown
Contributor Author

@thejameskyle @evocateur here's how I think I could offer this functionality in a more abstract way:

  1. have a pretag lifecycle script, if this script returns a semver valid tag on stdout use this tag in lieu of displaying a prompt in independent mode.
  2. have a precommit hook; I would then be able to stage the updated CHANGELOG here.

With regards to this pull request, should @evocateur and I continue moving this feature forward without this abstraction (it feels like it will take longer to build out this lifecycle abstraction, and I sure wouldn't complain if I had conventional-changelog support int he interim).

@evocateur

evocateur commented Mar 14, 2017 via email

Copy link
Copy Markdown
Member

@bcoe
bcoe force-pushed the conventional-commits branch from 3a604c2 to 2a83b5f Compare March 14, 2017 22:42
Comment thread test/ConventionalCommitUtilities.js Outdated
]],
[FileSystemUtilities, "writeFileSync", {}, [
{
args: ["/foo/bar/CHANGELOG.md", dedent(`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path here needs to use path.normalize so windows doesn't whine

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evocateur how did I not know about path.normalize? I've been using path.sep all over the place in a ton of my repos.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's implicitly called behind the scenes when you use path.join() or path.resolve(), too, though for this purpose a direct normalize call seems appropriate.

@bcoe

bcoe commented Mar 14, 2017

Copy link
Copy Markdown
Contributor Author

@evocateur I've addressed your code review; I think all the suggestions were great, and things feel cleaner post refactoring.

I've also added tests for the ConventionalCommitUtility; which helped cleanup the API further.

I've been testing against this repo. I'm really happy with how simple the workflow feels.

@evocateur

Copy link
Copy Markdown
Member

I'm satisfied, let's hope Windows is too! Pretty excited for this, it's gonna make independent mode so much smoother.

@evocateur

Copy link
Copy Markdown
Member

I'm hoping #635 helps iron out some of those flapping tests. Thanks for your patience with the path issues (try saying that six times fast)!

@evocateur
evocateur merged commit 5043ad2 into lerna:master Mar 15, 2017
@bcoe

bcoe commented Mar 15, 2017

Copy link
Copy Markdown
Contributor Author

@evocateur awesome 💃 thanks for your help on this, I can't wait to use this with conventional-changelog and istanbul ... at which point I can get back to actually fixing really hard to figure out instrumentation bugs ...

@evocateur evocateur modified the milestone: v2.0.0 Mar 15, 2017
@evocateur

Copy link
Copy Markdown
Member

Whoops, we forgot to update yarn.lock 😨

evocateur pushed a commit to evocateur/lerna that referenced this pull request Mar 15, 2017
@evocateur

Copy link
Copy Markdown
Member

follow up in #676

@bestander

Copy link
Copy Markdown
Contributor

FYI, I have created a task in Yarn where I aggregate the progress on Yarn workspaces and related PRs, RFC and discussions yarnpkg/yarn#3294

@evocateur

Copy link
Copy Markdown
Member

Thanks!

@lock

lock Bot commented Dec 27, 2018

Copy link
Copy Markdown

This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock Bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants