Skip to content

Instantly share code, notes, and snippets.

@AndreiCalazans
Created May 1, 2019 17:05
Show Gist options
  • Save AndreiCalazans/e7a23c067646f8060dcf1e6281183d5c to your computer and use it in GitHub Desktop.
Save AndreiCalazans/e7a23c067646f8060dcf1e6281183d5c to your computer and use it in GitHub Desktop.
import { danger, schedule, message, warn, fail, markdown } from 'danger'
const { readFileSync } = require('fs')
const child_process = require('child_process')
const { sentence } = danger.utils
const verifyBundleStats = () => {
const currentBuildResults = JSON.parse(
readFileSync('./build/bundle-stats.json')
)
if (currentBuildResults.warnings.length > 0) {
warn(currentBuildResults.warnings)
}
if (currentBuildResults.errors.length > 0) {
currentBuildResults.errors.forEach(error => {
fail(error)
})
}
}
const verifyDependencies = async () => {
const hasChangedFile = file => danger.git.modified_files.indexOf(file) !== -1
const getPackageInfos = dep => {
const output = child_process.execSync(`yarn why ${dep} --json`)
const asJSON = output
.toString()
.split(`{"type":"activityEnd","data":{"id":0}}`)
.pop()
.split('}\n{')
.join('},{')
const whyJSON = JSON.parse(`[${asJSON}]`)
return whyJSON
.map(({ data }) => data)
.filter(info => typeof info === 'string')
}
const packageChanged = hasChangedFile('package.json')
const lockfileChanged = hasChangedFile('yarn.lock')
if (packageChanged && !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} - <i>${idea}</i>`)
}
if (packageChanged) {
const packageDiff = await danger.git.JSONDiffForFile('package.json')
const devDependencies = packageDiff.devDependencies
? packageDiff.devDependencies.added
: []
const dependencies = packageDiff.dependencies
? packageDiff.dependencies.added
: []
if (devDependencies.length > 0) {
warn(`New devDependencies added: ${sentence(devDependencies)}.`)
}
if (dependencies.length > 0) {
warn(`New dependencies added: ${sentence(dependencies)}.`)
dependencies.forEach(dep => {
const infos = getPackageInfos(dep)
markdown(`
## ${dep}
${infos.join('\n\n - ')}
`)
})
}
}
}
const run = () => {
const { additions = 0, deletions = 0 } = danger.github.pr
message(`:tada: The PR added ${additions} and removed ${deletions} lines. \n`)
verifyBundleStats()
schedule(async () => {
await verifyDependencies()
})
}
run()
Collapse
2:05 PM
@AndreiCalazans
Copy link
Author

To run it
export DANGER_GITHUB_API_TOKEN="Your token here"
yarn danger pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment