-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathdangerfile.js
143 lines (137 loc) · 4.39 KB
/
dangerfile.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import danger, { message, fail, markdown } from 'danger'
const scssFiles = danger.git.modified_files
.concat(danger.git.created_files)
.filter(file => file.search(/src\/(.*).scss/) > -1)
const parseCssLine = line => {
if (line.search(':') === -1) {
return false
}
const items = line.split(':')
const values = line[1].split(' ')
return {
name: items[0],
values,
}
}
const checkFiles = () => {
return new Promise((resolve, reject) => {
const scssErrors = {
noSpacing: [],
toRem: [],
fontSize: [],
}
scssFiles.forEach((file, index) => {
danger.git.structuredDiffForFile(file).then(diff => {
diff.chunks.forEach(chunk => {
if (file.search('src/stories/') > -1) {
return
}
chunk.changes.forEach(change => {
if (
change.type === 'add' &&
change.content.search('ignore-style-rule') === -1
) {
const parsedLine = parseCssLine(change.content)
if (!parsedLine) {
return
}
const { name } = parsedLine
if (
(name.search('margin') > -1 || name.search('padding') > -1) &&
(change.content.search('px') > -1 ||
change.content.search('rem') > -1 ||
change.content.search('%') > -1)
) {
// Capture any margin or padding that doesn't use spacer
if (typeof scssErrors.noSpacing[file] === 'undefined') {
scssErrors.noSpacing[file] = []
}
scssErrors.noSpacing[file].push({
line: change.ln,
content: change.content,
})
}
if (
(name.search('margin') > -1 || name.search('padding') > -1) &&
change.content.search('toRem') > -1
) {
// Capture any margin or padding that uses toRem
if (typeof scssErrors.toRem[file] === 'undefined') {
scssErrors.toRem[file] = []
}
scssErrors.toRem[file].push({
line: change.ln,
content: change.content,
})
}
if (name == 'font-size') {
// Capture any font-size
if (typeof scssErrors.fontSize[file] === 'undefined') {
scssErrors.fontSize[file] = []
}
scssErrors.fontSize[file].push({
line: change.ln,
content: change.content,
})
}
}
})
})
if (index === scssFiles.length - 1) {
resolve(scssErrors)
}
})
})
})
}
checkFiles().then(errors => {
if (Object.keys(errors.noSpacing).length) {
fail(
`There are ${
Object.keys(errors.noSpacing).length
} SCSS changes that use absolute values for margin or padding. Use \`spacer()\` mixin instead.`,
)
markdown('## Margin and padding')
markdown(
'The following lines use margin and padding without our `spacer` mixins:',
)
Object.keys(errors.noSpacing).forEach(file => {
markdown(`\`${file}\``)
errors.noSpacing[file].forEach(line => {
markdown(`- **Line ${line.line}**: \`${line.content}\``)
})
})
}
if (Object.keys(errors.fontSize).length) {
fail(
`There are ${
Object.keys(errors.fontSize).length
} SCSS changes that use \`font-size\` instead of the \`type-size\` mixin.`,
)
markdown('## Font size')
markdown(
'The following lines use `font-size` instead of the `type-size` mixin:',
)
Object.keys(errors.fontSize).forEach(file => {
markdown(`\`${file}\``)
errors.fontSize[file].forEach(line => {
markdown(`- **Line ${line.line}**: \`${line.content}\``)
})
})
}
if (Object.keys(errors.toRem).length) {
message(
`There are ${
Object.keys(errors.noStoRempacing).length
} SCSS changes that use \`toRem\` instead of the \`spacer\` mixin.`,
)
markdown('## toRem')
markdown('The following lines use `toRem`:')
Object.keys(errors.noSpacing).forEach(file => {
markdown(`\`${file}\``)
errors.noSpacing[file].forEach(line => {
markdown(`- **Line ${line.line}**: \`${line.content}\``)
})
})
}
})