Danger + Node Library

#Before we get started

This guide continues after “Getting Started” - so you should have seen Danger comment on your PRs.

#Keeping on top of your library

End-users want to understand what has changed between versions of your library, and a CHANGELOG is a great way to keep them up to date. However, it can be easy to forget to add a CHANGELOG entry to any changes to a library. So let’s add a check that a CHANGELOG entry is added on every PR:

import { danger, fail, warn } from "danger"
import includes from "lodash.includes"

const hasCHANGELOGChanges = includes(danger.git.modified_files, "CHANGELOG.md")
if (!hasCHANGELOGChanges) {
  warn("This pull request may need a CHANGELOG entry.")
}

We’re using lodash’s _.include function to check if CHANGELOG.md is in the list of modified files.

We went with warn here because there are a lot of legitimate reasons to not need a CHANGELOG entry (updating typoes, CI and other infrastructure.) We can improve this though, let’s also check that there are changes to the source code for our library.

import { danger, fail, warn } from "danger"
import includes from "lodash.includes"
import first from "lodash.first"

const hasCHANGELOGChanges = includes(danger.git.modified_files, "CHANGELOG.md")
const hasLibraryChanges = first(danger.git.modified_files, path => path.startsWith("lib/"))
if (hasLibraryChanges && !hasCHANGELOGChanges) {
  warn("This pull request may need a CHANGELOG entry.")
}

This is a much more specific rule, now changes to the README won’t warrant a CHANGELOG entry.

#Dependencies

Any dependencies that you use are passed on to all of your library consumers, so you should consider using Danger to keep track of those as they evolve. For more information, see the tutorial on Dependencies.

#Keep your README up to date

An example from Danger itself, is that we want to ensure the README always shows what CI providers will work by default with Danger. As both the app, and Danger use JavaScript, we can import code from the app and use that to create a new rule.

import { danger, fail, warn } from "danger"
import contains from "lodash.contains"

// This is a list of all the CI providers
import { realProviders } from "./source/ci_source/providers"

const readme = fs.readFileSync("README.md", "utf8")
const names = realProviders.map(ci => new ci({}).name)
const missing = names.filter(name => !contains(readme, name))
if (missing.length) {
  warn(`These providers are missing from the README: ${sentence(missing)}`)
}

Danger also uses a similar check to create our type definition files, if any of the public DSL changes then Danger checks that the type definitions have been updated, and recommends how to do so if not.


Got improvements? Help improve this document via sending PRs.