visit
Managing open source projects is way too much work.That’s why your favorite obscure library with 1 or 2 contributors has not merged your pull request. In order to remedy this situation, a suite of automation tools can be employed to free you from the shackles of keeping dependencies up-to-date, ensuring code quality, and releasing new versions of your software. Unfortunately, we can’t automate away all of the maintenance, but we can deal with a lot of it. First of all, you’ll need an npm account, and a github account for these to work. Let’s get started.
When using it correctly, semantic-release
will calculate new version numbers when necessary, taking the emotional part of deciding when to bump a version number out of the equation.
It also publishes you package to npm, and tags a new release in GitHub. This generates a nicely formatted Releases page detailing the changes that make up every new version!
First, install the semantic-release-cli tool: npm i -g semantic-release-cli Then, use it to set up your project. Run the following in the npm module you’re working on:
Here’s the new package.json
file.
{"name": "open-source-setup","version": "0.0.0-development","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1",},"author": "","license": "ISC","repository": {"type": "git","url": ""}}
Running semantic-release-cli setup
added two new scripts to our package.json
file. semantic-release
which calls the devDependency by the same name, is what calculates the version and prepares the build’s package.json
file by replacing 0.0.0-development
with the newly calculated “semantic version”, or “semver”.
The next script travis-deploy-once
will run the actual release portion. However, builds can be run in multiple environments in Travis at once. We need travis-deploy-once
to ensure that the actual release happens only once, when all builds have finished, instead of releasing for every tested Node version.
The setup
command will also add the required repository
section to your config if it is missing, and install the new required devDependencies
.
commitizen
with cz-convential-changelog to capture additional details about each commitsemantic-release
needs a little help to actually calculate the new version, though, given that it doesn’t actually understand what your code does, an obvious prerequisite to understanding if there are breaking changes, and thus still relies on the developer to tell it.
The problem with this is now you’re expecting people to know and/or care about your rules about how you should write commit messages.
Don’t worry, there’s a module for that!
It’s called commitizen, and when a user runs it, it prompts the user with a wizard to create their commit message. It asks a series of questions to do so. First, to install: npm i --save-dev commitizen cz-conventional-changelogcommitizen
is the tool that provides the wizard, and cz-conventional-changelog
is a plugin that describes the desired commit format.
In package.json
make the following changes:
// package.json{//...scripts: {"commit": "git-cz",// ...},//..."config": {"commitizen": {"path": "./node_modules/cz-conventional-changelog"}}}
Now instead of commiting with git commit -m ""
, we can use npm run commit
> [email protected] commit /Users/patrickscott/dev/patrickleet/open-source-setup> git-cz
/Users/patrickscott/dev/patrickleet/open-source-setup/Users/patrickscott/dev/patrickleet/[email protected], [email protected]
Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.
Our nicely formatted commit message
PRO TIP: You’ll still get Pull Requests that don’t follow these rules – in those cases, select “Squash and Merge” instead of the default “Create Merge Commit”. You’ll be able to enter a new commit message that will trigger a new release.
I really like jest
for this. As such, I will add the minimal set up for jest.
Whatever you use, it’s important that you are generating coverage reports! Jest does this out of the box by simply using the --coverage
flag. I also find it’s mocking functionality to be really simple to use.
Many people like putting their jest config in their package.json. I think it’s cluttered enough as is, so I usually use the following jest.config
"scripts": {"commit": "git-cz","test": "jest --config jest.json --coverage","semantic-release": "semantic-release","travis-deploy-once": "travis-deploy-once"},
My test repository has 0 files, and 0 tests, so to fix that quickly, I created two files in . Given it’s a bit of a long article already, and it’s out of scope, feel free to check those files out there.You’ll also want to add coverage
to your .gitignore
file.
Here’s the new output of running npm run test
A new folder coverage
is also generate with the results. Check it out by running open ./coverage/lcov-report/index.html
.
With the jest setup, the project is now outputting a coverage
folder. This is pretty much the only requirement to set up codecov
when you’re project is open-source and running on Travis.
I’m gonna go with the standard named standardjs
which, despite it’s name, is not a standard. But I still dig it.
Let’s install eslint
and run the setup.
Running npx eslint --init
will start a wizard to install and configure the correct dependencies for your selections.
As a result, a very simple config, .eslintrc.json
was generated.
To perform the lint, we need to add some scripts to package.json
"scripts": {"commit": "git-cz","lint": "eslint src __tests__","lint:fix": "eslint --fix src __tests__","test": "jest --config jest.json --coverage","semantic-release": "semantic-release","travis-deploy-once": "travis-deploy-once"},
The command is simple, it takes in a list of folders to lint using the settings in .eslintrc.json
. I like to create the second command with --fix
to autofix problems when I’m running it locally.
> [email protected] lint /Users/patrickscott/dev/patrickleet/open-source-setup> eslint src __tests__
> [email protected] lint:fix /Users/patrickscott/dev/patrickleet/open-source-setup> eslint --fix src __tests__
We can fix this by telling eslint
that these are not issues by modifying .eslintrc.json
.
{"extends": "standard","globals": {"describe": true,"it": true,"expect": true}}
Now running npm run lint
exits with no errors.
I’ve already added this in the first .travis.yml
file as well.
I think they look nice right under the heading, so go ahead in paste it there or at the top. Next, head on over to Codecov. Navigate to your project, and grab the markdown badge showing your repositories code coverage percentage.
It can be found in the Settings | Badge section.
Also apparently they have a GitHub app now which is even better that I should install. Here’s my file: # open source setup [![Build Status](//travis-ci.org/patrickleet/open-source-setup.svg?branch=master)](//travis-ci.org/patrickleet/open-source-setup) [![codecov](//codecov.io/gh/patrickleet/open-source-setup/branch/master/graph/badge.svg)](//codecov.io/gh/patrickleet/open-source-setup) An example repository showing how I set up my open-source npm modules.
GreenKeeper will watch any registry you tell it to by reading it’s package.json file, and any time a new version of one of your package’s dependencies is updated, it will create a new Pull Request, using cz-conventional-changelog
styled commit messages!
This means all you need to do at this point, is press merge if the travis CI build for the Pull Request passed. Which if you’re paying attention, is awesome, because this will trigger a new release using semantic-release
!
I already had the Greenkeeper GitHub App installed It can take a while for the initial Pull Request from GreenKeeper to come in for a new repository.
Because I’ve done this before, and I know what comes next, there are some updates to the .travis.yml
file that we are going to have to make.
Once you’ve merged the PR, greenkeeper will let you know about a dependency that will need to be installed and configured in order for it to be able to keep package-lock.json
up to date.
before_script: greenkeeper-lockfile-update
after_script: greenkeeper-lockfile-upload
If for some reason your initial PR still hasn’t come, and it’s been more than 30 minutes, this likely means something went wrong (according to their docs) and . Once it comes, merge it! The first PR will upgrade ALL of your dependencies to give it an up-to-date baseline, as well as insert the GreenKeeper badge into the README. If this is required, GreenKeeper will let you know.
Please follow me, leave some claps, and check out some of my other articles.Interested in hearing MY DevOps Journey, WITHOUT useless AWS Certifications? Read it now on HackerNoon.
PRO TIP: Did you know that you are allowed to give up to 50 claps to every article you enjoy? Medium uses this information to pay authors for their most useful content! Pretty cool!
You can do this by clicking/tapping, and then holding the clap button.