Skip to content

Commit

Permalink
Expand Danger's dep checking
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Apr 18, 2017
1 parent 13e87fe commit fc97c3f
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 57 deletions.
98 changes: 89 additions & 9 deletions dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ declare function schedule(promise: () => Promise<any | void>): void

import * as fs from "fs"
import * as child_process from "child_process"
import fetch from "node-fetch"
import { distanceInWords } from "date-fns"

// For some reason we're getting type errors on this includes module?
// Wonder if we could move to the includes function in ES2015?
Expand Down Expand Up @@ -46,13 +48,13 @@ const checkForRelease = (packageDiff) => {

// This is a great candidate for being a Danger plugin.

const checkForNewDependencies = (packageDiff) => {
const checkForNewDependencies = async (packageDiff) => {
if (packageDiff.dependencies) {
if (packageDiff.dependencies.added.length) {
const newDependencies = packageDiff.dependencies.added as string[]
warn(`New dependencies added: ${sentence(newDependencies)}.`)

newDependencies.forEach(dep => {
for (const dep of newDependencies) {
const output = child_process.execSync(`yarn why ${dep} --json`)

// Comes as a series of little JSON messages
Expand All @@ -61,12 +63,90 @@ const checkForNewDependencies = (packageDiff) => {

const whyJSON = JSON.parse(`[${asJSON}]`) as any[]
const messages = whyJSON.filter(msg => typeof msg.data === "string").map(m => m.data)
markdown(`
## ${dep}
- ${messages.join("\n\n - ")}
`)
})
const yarnDetails = `
<details>
<summary><code>yarn why ${dep}</code> output</summary>
<p><code><ul><li>${messages.join("</li><li>")}
</li></ul></code></p>
</details>
`
// Grab the NPM metadata
let npmMetadata = ""
const npmResponse = await fetch(`https://registry.npmjs.org/${dep}`)
if (npmResponse.ok) {
const tableDeets = [] as [{ name: string, message: string}]
const npm = await npmResponse.json()

if (npm.time && npm.time.created) {
const distance = distanceInWords(new Date(npm.time.created), new Date)
tableDeets.push ({ name: "Created", message: `${distance} ago` })
}

if (npm.time && npm.time.modified) {
const distance = distanceInWords(new Date(npm.time.modified), new Date)
tableDeets.push ({ name: "Last Updated", message: `${distance} ago` })
}

if (npm.license) {
tableDeets.push({ name: "License", message: npm.license })
} else {
tableDeets.push({ name: "License", message: "<b>NO LICENSE FOUND</b>" })
}

if (npm.maintainers) {
tableDeets.push({ name: "Maintainers", message: npm.maintainers.length })
}

if (npm["dist-tags"] && npm["dist-tags"]["latest"]) {
const currentTag = npm["dist-tags"]["latest"]
const tag = npm.versions[currentTag]
tableDeets.push ({ name: "Releases", message: String(Object.keys(npm.versions).length) })
if (tag.dependencies) {
const deps = Object.keys(tag.dependencies)
const depLinks = deps.map(d => `<a href='http: //npmjs.com/package/${d}'>${d}</a>`)
tableDeets.push ({ name: "Direct Dependencies", message: sentence(depLinks) })
}
}

if (npm.keywords && npm.keywords.length) {
tableDeets.push({ name: "Keywords", message: sentence(npm.keywords) })
}

let readme = "This README is too long to show."
if (npm.readme && npm.readme.length < 10000) {
readme = `
<details>
<summary><code>README</code></summary>
${npm.readme}
</details>
`
}

const homepage = npm.homepage ? npm.homepage : `http: //npmjs.com/package/${dep}`

npmMetadata = `
<h2><a href="${homepage}">${dep}</a></h2>
Author: ${npm.author && npm.author.name ? npm.author.name : "Unknown"}
Description: ${npm.description}
Repo: ${homepage}
<table>
<thead><tr><th></th><th width="100%"></th></tr></thead>
${tableDeets.map(deet => `<tr><td>${deet.name}</td><td>${deet.message}</td></tr>`).join("")}
</table>
${readme}
`
} else {
const errorMessage = await npmResponse.text()
npmMetadata = `Error getting NPM details: ${errorMessage}`
}

markdown(`${npmMetadata} ${yarnDetails}`)
}
}
}
}
Expand Down Expand Up @@ -103,7 +183,7 @@ const checkForTypesInDeps = (packageDiff) => {
schedule(async () => {
const packageDiff = await danger.git.JSONDiffForFile("package.json")
checkForRelease(packageDiff)
checkForNewDependencies(packageDiff)
await checkForNewDependencies(packageDiff)
checkForLockfileDiff(packageDiff)
checkForTypesInDeps(packageDiff)
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"babel-plugin-transform-regenerator": "^6.22.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-stage-3": "^6.22.0",
"date-fns": "^1.28.3",
"husky": "^0.13.3",
"in-publish": "^2.0.0",
"jest": "^19.0.2",
Expand Down
88 changes: 40 additions & 48 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ babel-code-frame@^6.22.0:
esutils "^2.0.2"
js-tokens "^3.0.0"

babel-core@^6.0.0, babel-core@^6.24.0:
babel-core@^6.0.0:
version "6.24.0"
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.24.0.tgz#8f36a0a77f5c155aed6f920b844d23ba56742a02"
dependencies:
Expand Down Expand Up @@ -287,26 +287,26 @@ babel-core@^6.24.1:
slash "^1.0.0"
source-map "^0.5.0"

babel-generator@^6.18.0, babel-generator@^6.24.0:
version "6.24.0"
resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.24.0.tgz#eba270a8cc4ce6e09a61be43465d7c62c1f87c56"
babel-generator@^6.18.0, babel-generator@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.24.1.tgz#e715f486c58ded25649d888944d52aa07c5d9497"
dependencies:
babel-messages "^6.23.0"
babel-runtime "^6.22.0"
babel-types "^6.23.0"
babel-types "^6.24.1"
detect-indent "^4.0.0"
jsesc "^1.3.0"
lodash "^4.2.0"
source-map "^0.5.0"
trim-right "^1.0.1"

babel-generator@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.24.1.tgz#e715f486c58ded25649d888944d52aa07c5d9497"
babel-generator@^6.24.0:
version "6.24.0"
resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.24.0.tgz#eba270a8cc4ce6e09a61be43465d7c62c1f87c56"
dependencies:
babel-messages "^6.23.0"
babel-runtime "^6.22.0"
babel-types "^6.24.1"
babel-types "^6.23.0"
detect-indent "^4.0.0"
jsesc "^1.3.0"
lodash "^4.2.0"
Expand Down Expand Up @@ -749,19 +749,7 @@ babel-preset-stage-3@^6.22.0:
babel-plugin-transform-exponentiation-operator "^6.22.0"
babel-plugin-transform-object-rest-spread "^6.22.0"

babel-register@^6.24.0:
version "6.24.0"
resolved "https://registry.yarnpkg.com/babel-register/-/babel-register-6.24.0.tgz#5e89f8463ba9970356d02eb07dabe3308b080cfd"
dependencies:
babel-core "^6.24.0"
babel-runtime "^6.22.0"
core-js "^2.4.0"
home-or-tmp "^2.0.0"
lodash "^4.2.0"
mkdirp "^0.5.1"
source-map-support "^0.4.2"

babel-register@^6.24.1:
babel-register@^6.24.0, babel-register@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-register/-/babel-register-6.24.1.tgz#7e10e13a2f71065bdfad5a1787ba45bca6ded75f"
dependencies:
Expand All @@ -780,66 +768,66 @@ babel-runtime@^6.0.0, babel-runtime@^6.18.0, babel-runtime@^6.22.0:
core-js "^2.4.0"
regenerator-runtime "^0.10.0"

babel-template@^6.16.0, babel-template@^6.22.0, babel-template@^6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.23.0.tgz#04d4f270adbb3aa704a8143ae26faa529238e638"
babel-template@^6.16.0, babel-template@^6.22.0, babel-template@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.24.1.tgz#04ae514f1f93b3a2537f2a0f60a5a45fb8308333"
dependencies:
babel-runtime "^6.22.0"
babel-traverse "^6.23.0"
babel-types "^6.23.0"
babel-traverse "^6.24.1"
babel-types "^6.24.1"
babylon "^6.11.0"
lodash "^4.2.0"

babel-template@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.24.1.tgz#04ae514f1f93b3a2537f2a0f60a5a45fb8308333"
babel-template@^6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.23.0.tgz#04d4f270adbb3aa704a8143ae26faa529238e638"
dependencies:
babel-runtime "^6.22.0"
babel-traverse "^6.24.1"
babel-types "^6.24.1"
babel-traverse "^6.23.0"
babel-types "^6.23.0"
babylon "^6.11.0"
lodash "^4.2.0"

babel-traverse@^6.18.0, babel-traverse@^6.22.0, babel-traverse@^6.23.0, babel-traverse@^6.23.1:
version "6.23.1"
resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.23.1.tgz#d3cb59010ecd06a97d81310065f966b699e14f48"
babel-traverse@^6.18.0, babel-traverse@^6.22.0, babel-traverse@^6.23.0, babel-traverse@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.24.1.tgz#ab36673fd356f9a0948659e7b338d5feadb31695"
dependencies:
babel-code-frame "^6.22.0"
babel-messages "^6.23.0"
babel-runtime "^6.22.0"
babel-types "^6.23.0"
babel-types "^6.24.1"
babylon "^6.15.0"
debug "^2.2.0"
globals "^9.0.0"
invariant "^2.2.0"
lodash "^4.2.0"

babel-traverse@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.24.1.tgz#ab36673fd356f9a0948659e7b338d5feadb31695"
babel-traverse@^6.23.1:
version "6.23.1"
resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.23.1.tgz#d3cb59010ecd06a97d81310065f966b699e14f48"
dependencies:
babel-code-frame "^6.22.0"
babel-messages "^6.23.0"
babel-runtime "^6.22.0"
babel-types "^6.24.1"
babel-types "^6.23.0"
babylon "^6.15.0"
debug "^2.2.0"
globals "^9.0.0"
invariant "^2.2.0"
lodash "^4.2.0"

babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.22.0, babel-types@^6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.23.0.tgz#bb17179d7538bad38cd0c9e115d340f77e7e9acf"
babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.22.0, babel-types@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.24.1.tgz#a136879dc15b3606bda0d90c1fc74304c2ff0975"
dependencies:
babel-runtime "^6.22.0"
esutils "^2.0.2"
lodash "^4.2.0"
to-fast-properties "^1.0.1"

babel-types@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.24.1.tgz#a136879dc15b3606bda0d90c1fc74304c2ff0975"
babel-types@^6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.23.0.tgz#bb17179d7538bad38cd0c9e115d340f77e7e9acf"
dependencies:
babel-runtime "^6.22.0"
esutils "^2.0.2"
Expand Down Expand Up @@ -1166,6 +1154,10 @@ date-fns@^1.27.2:
version "1.28.2"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.28.2.tgz#19e4192d68875c0bf7c9537e3f296a8ec64853ef"

date-fns@^1.28.3:
version "1.28.3"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.28.3.tgz#145d87adc3f5a82c6bda668de97eee1132c97ea1"

dateformat@^1.0.11:
version "1.0.12"
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-1.0.12.tgz#9f124b67594c937ff706932e4a642cca8dbbfee9"
Expand Down Expand Up @@ -3259,7 +3251,7 @@ read-pkg@^1.0.0:
normalize-package-data "^2.3.2"
path-type "^1.0.0"

"readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.1.4:
"readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.1, readable-stream@^2.1.4:
version "2.2.6"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.6.tgz#8b43aed76e71483938d12a8d46c6cf1a00b1f816"
dependencies:
Expand All @@ -3271,7 +3263,7 @@ read-pkg@^1.0.0:
string_decoder "~0.10.x"
util-deprecate "~1.0.1"

readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@~2.0.0:
readable-stream@^2.0.2, readable-stream@~2.0.0:
version "2.0.6"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e"
dependencies:
Expand Down

0 comments on commit fc97c3f

Please sign in to comment.