forked from bemusic/bemuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dangerfile.ts
77 lines (71 loc) · 2.3 KB
/
dangerfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { danger, warn, fail, message } from 'danger'
import { CLIEngine } from 'eslint'
import { readFileSync } from 'fs'
import { insert } from 'markdown-toc'
import { getFileInfo, resolveConfig, check, format } from 'prettier'
import minimatch = require('minimatch')
/* eslint no-undef: off */
/* REASON: not compatible with import = require() syntax. */
// No PR is too small to include a description of why you made a change
if (danger.github) {
if (danger.github.pr.body.length < 10) {
warn('Please include a description of your PR changes.')
}
}
const cli = new CLIEngine({})
const filesToCheck = danger.git.created_files
.concat(danger.git.modified_files)
.filter(path => !cli.isPathIgnored(path))
// ESLint
const eslintPattern = '*.{js,jsx,ts,tsx}'
const filesToLint = filesToCheck.filter(path =>
minimatch(path, eslintPattern, { matchBase: true })
)
const report = cli.executeOnFiles(filesToLint)
report.results.forEach(result => {
const { filePath } = result
result.messages.forEach(msg => {
const { line, message, ruleId } = msg
const rule = ruleId || 'N/A'
const messageText = `${filePath} line ${line} – ${message} (${rule})`
if (msg.severity === 1) {
warn(messageText)
} else if (msg.severity === 2) {
fail(messageText)
}
})
})
// Prettier
let prettierFailed = false
const prettierPattern = '*.{js,jsx,ts,tsx,json,scss,css,yml}'
filesToCheck.forEach(filePath => {
const matchesPattern = minimatch(filePath, prettierPattern, {
matchBase: true,
})
if (!matchesPattern) return
const fileInfo = getFileInfo.sync(filePath)
if (fileInfo.ignored) return
if (!fileInfo.inferredParser) return
const source = readFileSync(filePath, 'utf8')
const config = resolveConfig.sync(filePath)
const options = { ...config, parser: fileInfo.inferredParser }
if (!check(source, options)) {
fail(`${filePath} is not formatted using Prettier.`)
prettierFailed = true
}
})
if (prettierFailed) {
message(
'You can run `yarn style:fix` to automatically format all files using Prettier.'
)
}
// Readme
const readme = readFileSync('README.md', 'utf8')
const formattedReadme = format(insert(readme), {
parser: 'markdown',
})
if (formattedReadme !== readme) {
fail(
'Please format the README and update its table of contents using `yarn readme:update`.'
)
}