Skip to content

Commit

Permalink
Use new TS version of danger-plugin-yarn
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Jun 10, 2017
1 parent 8a910fd commit 6dfd16e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 773 deletions.
4 changes: 0 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
"editor.formatOnSave": true,
"prettier.singleQuote": false,
"prettier.trailingComma": "es5",
"prettier.typescriptEnable": [
"typescript",
"typescriptreact"
],
"prettier.semi": false,
"prettier.printWidth": 120
}
167 changes: 2 additions & 165 deletions dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,157 +38,8 @@ if (!hasChangelog && !isTrivial && !isGreenkeeper) {
}
}

// Celebrate when a new release is being shipped
const checkForRelease = packageDiff => {
if (packageDiff.version) {
markdown(":tada:")
}
}

// Initial stab at starting a new dependency information rule
// Just starting simple by showing `yarn why {dep}` for now

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

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)}.`)

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

// Comes as a series of little JSON messages
const usefulJSONContents = output.toString().split(`{"type":"activityEnd","data":{"id":0}}`).pop() as string
const asJSON = usefulJSONContents.split("}\n{").join("},{")

const whyJSON = JSON.parse(`[${asJSON}]`) as any[]
const messages = whyJSON.filter(msg => typeof msg.data === "string").map(m => m.data)
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}`)
}
}
}
}

// Ensure a lockfile change if deps/devDeps changes, in case
// someone has only used `npm install` instead of `yarn.

const checkForLockfileDiff = packageDiff => {
if (packageDiff.dependencies || packageDiff.devDependencies) {
const lockfileChanged = includes(danger.git.modified_files, "yarn.lock")
if (!lockfileChanged) {
const message = "Changes were made to package.json, but not to yarn.lock."
const idea = "Perhaps you need to run `yarn install`?"
warn(`${message}<br/><i>${idea}</i>`)
}
}
}

// Don't ship @types dependencies to consumers of Danger
const checkForTypesInDeps = packageDiff => {
if (packageDiff.dependencies) {
const typesDeps = packageDiff.dependencies.added.filter((dep: string) => dep.startsWith("@types"))
if (typesDeps.length) {
const message = `@types dependencies were added to package.json, as a dependency for others.`
const idea = `You need to move ${sentence(typesDeps)} into "devDependencies"?`
fail(`${message}<br/><i>${idea}</i>`)
}
}
}

// As `JSONDiffForFile` is an async function, we want to add it to Danger's scheduler
// then it can continue after eval has taken place.

schedule(async () => {
const packageDiff = await danger.git.JSONDiffForFile("package.json")
checkForRelease(packageDiff)
await checkForNewDependencies(packageDiff)
checkForLockfileDiff(packageDiff)
checkForTypesInDeps(packageDiff)
})
import yarn from "danger-plugin-yarn"
schedule(yarn())

// Some good old-fashioned maintainance upkeep

Expand Down Expand Up @@ -216,17 +67,3 @@ const missing = names.filter(n => !readme.includes(n))
if (missing.length) {
warn(`These providers are missing from the README: ${sentence(missing)}`)
}

// dangerfile.js
// import yarn from "danger-plugin-yarn"

// yarn()

// dangerfile.js
import jiraIssue from "danger-plugin-jira-issue"

jiraIssue({
key: "JIRA",
url: "https://myjira.atlassian.net/browse",
emoji: ":paperclip:",
})
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@
"babel-plugin-transform-regenerator": "^6.22.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-stage-3": "^6.22.0",
"danger-plugin-jira-issue": "^1.1.0",
"danger-plugin-yarn": "^0.0.1",
"danger-plugin-yarn": "^0.1.0",
"date-fns": "^1.28.3",
"husky": "^0.13.3",
"in-publish": "^2.0.0",
Expand Down

0 comments on commit 6dfd16e

Please sign in to comment.