Skip to content

Commit

Permalink
Merge pull request #1262 from glensc/fn-fileMatch
Browse files Browse the repository at this point in the history
Docs: Update dependencies tutorial to use fileMatch
  • Loading branch information
orta committed Mar 23, 2022
2 parents f32395b + 4724d2b commit 9501798
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
14 changes: 5 additions & 9 deletions docs/tutorials/dependencies.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@ Danger to give us more insight into changes related to our dependencies.

The simplest rule, which we can evolve, is that any time your `package.json` changes you probably want a change to the
[`yarn.lock`][lockfile] or [`shrinkwrap.json`][shrinkwrap] file. Yes, not every change to the `package.json` represents
a dependency update but we're starting simple. You start off your `Dangerfile` like this:
a dependency update, but we're starting simple. You start off your `Dangerfile` like this:

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

const hasPackageChanges = includes(danger.git.modified_files, "package.json")
const hasLockfileChanges = includes(danger.git.modified_files, "yarn.lock")
if (hasPackageChanges && !hasLockfileChanges) {
const packageJson = danger.git.fileMatch("package.json")
const packageLock = danger.git.fileMatch("yarn.lock")
if (packageJson.modified && !packageLock.modified) {
warn("There are package.json changes with no corresponding lockfile changes")
}
```

This uses `lodash`'s `_.includes()` function to see if `danger.git.modified_files` includes the package, but not the
lockfile.

### Vetting New Dependencies

This works, and for a while, this is enough. Time passes and you hear about a node module with a
Expand Down
18 changes: 7 additions & 11 deletions docs/tutorials/node-library.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,24 @@ check that a CHANGELOG entry is added on every PR:

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

const hasCHANGELOGChanges = includes(danger.git.modified_files, "CHANGELOG.md")
if (!hasCHANGELOGChanges) {
const changelogChanges = danger.git.fileMatch("CHANGELOG.md")
if (!changelogChanges.modified) {
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.
We went with `warn` here because there are a lot of legitimate reasons to not need a CHANGELOG entry (updating typos, CI
and other infrastructure.) We can improve this though, let's _also_ check that there are changes to the source code for
our library.

```js
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 changelogChanges = danger.git.fileMatch("CHANGELOG.md")
const hasLibraryChanges = first(danger.git.modified_files, path => path.startsWith("lib/"))
if (hasLibraryChanges && !hasCHANGELOGChanges) {
if (hasLibraryChanges && !changelogChanges.modified) {
warn("This pull request may need a CHANGELOG entry.")
}
```
Expand Down

0 comments on commit 9501798

Please sign in to comment.