forked from sketch-hq/SketchAPI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dangerfile.ts
125 lines (105 loc) · 3.34 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as path from 'path'
import { danger, warn, fail } from 'danger'
const touchedFiles = danger.git.modified_files.concat(danger.git.created_files)
const LIB_REGEX = /^Source\/.*\.js$/
const libraryChanges = touchedFiles.filter(
filePath => LIB_REGEX.test(filePath) && filePath.indexOf('__tests__') === -1
)
function hasMatchingTest(change: string) {
const { dir, name } = path.parse(change)
const testPath = path.join(dir, '__tests__', `${name}.test.js`)
return touchedFiles.find(f => f === testPath)
}
function hasMatchingDoc(change: string) {
const { name } = path.parse(change)
const docPath = path.join('docs', 'api', `${name}.md`)
return touchedFiles.find(f => f === docPath)
}
function hasMatchingTypes(change: string) {
// placeholder which will be filed when we have typescript types
return !!change
}
/**
* CHECK FOR CHANGELOG UPDATE
*/
const hasCHANGELOGChanges = touchedFiles.some(
filePath => filePath === 'CHANGELOG.json'
)
const hasLibraryChanges = libraryChanges.length > 0
if (hasLibraryChanges && !hasCHANGELOGChanges) {
warn('This pull request may need a CHANGELOG entry.')
}
/**
* CHECK FOR PACKAGE.JSON UPDATE
*/
const packageChanged = touchedFiles.some(
filePath => filePath === 'package.json'
)
const lockfileChanged = touchedFiles.some(
filePath => filePath === 'package-lock.json'
)
if (packageChanged && !lockfileChanged) {
const message =
'Changes were made to package.json, but not to package-lock.json'
const idea = 'Perhaps you need to run `npm run install`?'
warn(`${message} - <i>${idea}</i>`)
}
const corePackageChanged = touchedFiles.some(
filePath => filePath === 'core-modules/package.json'
)
if (corePackageChanged) {
// eslint-disable-next-line
const coreModules = require('./core-modules/package.json')
if (
Object.keys(coreModules.dependencies).some(
k => coreModules.dependencies[k][0] === '^'
)
) {
fail('core-modules/package.json should only contain pinned dependencies')
}
}
/**
* CHECK FOR TESTS
*/
const ignoredFilesForTests = [
'Source/async/index.js',
'Source/data-supplier/index.js',
'Source/dom/index.js',
'Source/settings/index.js',
'Source/ui/index.js',
'Source/test-utils.js',
'Source/index.js',
]
const ignoredFilesForDocs = [
'Source/async/index.js',
'Source/data-supplier/index.js',
'Source/dom/enums.js',
'Source/dom/Factory.js',
'Source/dom/index.js',
'Source/dom/utils.js',
'Source/dom/wrapNativeObject.js',
'Source/dom/WrappedObject.js',
'Source/settings/index.js',
'Source/ui/index.js',
'Source/test-utils.js',
'Source/index.js',
]
const ignoredFilesForTypes: string[] = []
// Warn if there are library changes, but not tests
libraryChanges.forEach(change => {
const missingMatchingTest =
ignoredFilesForTests.indexOf(change) === -1 && !hasMatchingTest(change)
const missingMatchingDoc =
ignoredFilesForDocs.indexOf(change) === -1 && !hasMatchingDoc(change)
const missingMatchingTypes =
ignoredFilesForTypes.indexOf(change) === -1 && !hasMatchingTypes(change)
if (missingMatchingTest || missingMatchingDoc || missingMatchingTypes) {
warn(
`\`${change}\` changed, but not:
${missingMatchingDoc ? '- 📚 its docs\n' : ''}${
missingMatchingTest ? '- 🧪 its tests\n' : ''
}${missingMatchingTypes ? '- ⌨️ its types\n' : ''}
That's OK as long as you're refactoring.`
)
}
})