-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
dangerfile.ts
288 lines (234 loc) · 11.2 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* eslint-disable no-await-in-loop */
import { danger, fail, message, warn } from 'danger'
import corePackageJson from './packages/core/package.json' assert { type: 'json' }
import { ConstantsUtil } from './packages/scaffold-utils/src/ConstantsUtil'
// -- Constants ---------------------------------------------------------------
const TYPE_COMMENT = `// -- Types --------------------------------------------- //`
const STATE_COMMENT = `// -- State --------------------------------------------- //`
const CONTROLLER_COMMENT = `// -- Controller ---------------------------------------- //`
const RENDER_COMMENT = `// -- Render -------------------------------------------- //`
const STATE_PROPERTIES_COMMENT = `// -- State & Properties -------------------------------- //`
const PRIVATE_COMMENT = `// -- Private ------------------------------------------- //`
const PACKAGE_VERSION = ConstantsUtil.VERSION
// -- Data --------------------------------------------------------------------
const { modified_files, created_files, deleted_files, diffForFile } = danger.git
const updated_files = [...modified_files, ...created_files].filter(f => !f.includes('dangerfile'))
const all_files = [...updated_files, ...created_files, ...deleted_files].filter(
f => !f.includes('dangerfile')
)
// -- Dependency Checks -------------------------------------------------------
async function checkPackageJsons() {
const packageJsons = all_files.filter(f => f.includes('package.json'))
const packageLock = updated_files.find(f => f.includes('package-lock.json'))
const yarnLock = updated_files.find(f => f.includes('yarn.lock'))
const pnpmLock = updated_files.find(f => f.includes('pnpm-lock.yaml'))
if (packageJsons.length && !packageLock) {
warn('Changes were made to one or more package.json(s), but not to package-lock.json')
}
if (yarnLock || pnpmLock) {
fail('Non npm lockfile(s) detected (yarn / pnpm), please use npm')
}
for (const f of packageJsons) {
const diff = await diffForFile(f)
if (diff?.added.includes('^') || diff?.added.includes('~')) {
fail(`Loose dependency versions in ${f}, please use strict versioning`)
}
}
}
checkPackageJsons()
// -- Ui Package Checks -------------------------------------------------------
async function checkUiPackage() {
const created_ui_components = created_files.filter(f => f.includes('ui/src/components'))
const created_ui_composites = created_files.filter(f => f.includes('ui/src/composites'))
const created_ui_layout = created_files.filter(f => f.includes('ui/src/layout'))
const created_ui_files = [
...created_ui_components,
...created_ui_composites,
...created_ui_layout
]
const created_ui_index_files = created_ui_files.filter(f => f.includes('index.ts'))
const created_ui_style_files = created_ui_files.filter(f => f.includes('styles.ts'))
for (const f of created_ui_index_files) {
const diff = await diffForFile(f)
if (!diff?.added.includes('[resetStyles')) {
fail(`${f} does not apply resetStyles`)
}
if (diff?.added.includes('@state(')) {
fail(`${f} is using @state decorator, which is not allowed in ui package`)
}
if (diff?.added.includes('import @web3modal/core')) {
fail(`${f} is importing @web3modal/core, which is not allowed in ui package`)
}
if (!diff?.added.includes(RENDER_COMMENT) && diff?.added.includes('render()')) {
fail(`${f} is missing \`${RENDER_COMMENT}\` comment`)
}
if (diff?.added.includes('@property(') && !diff.added.includes(STATE_PROPERTIES_COMMENT)) {
fail(`${f} is missing \`${STATE_PROPERTIES_COMMENT}\` comment`)
}
if (diff?.added.includes('private ') && !diff.added.includes(PRIVATE_COMMENT)) {
message(
`${f} is missing \`${PRIVATE_COMMENT}\` comment, but seems to have private members. Check if this is correct`
)
}
if (!diff?.added.includes(`@customElement('wui-`)) {
fail(`${f} is a ui element, but does not define wui- prefix`)
}
if (diff?.added.includes('@web3modal/ui/')) {
fail(`${f} should use relative imports instead of direct package access`)
}
}
for (const f of created_ui_style_files) {
const diff = await diffForFile(f)
if (diff?.added.includes(':host') && !diff.added.includes('display: ')) {
fail(`${f} uses :host container, but does not set display style on it`)
}
}
const created_ui_components_stories = created_files.filter(f =>
f.includes('gallery/stories/components')
)
const created_ui_composites_stories = created_files.filter(f =>
f.includes('gallery/stories/composites')
)
const created_ui_layout_stories = created_files.filter(f => f.includes('gallery/stories/layout'))
const ui_index = modified_files.find(f => f.includes('ui/index.ts'))
const ui_index_diff = ui_index ? await diffForFile(ui_index) : undefined
const jsx_index = modified_files.find(f => f.includes('ui/utils/JSXTypesUtil.ts'))
const jsx_index_diff = jsx_index ? await diffForFile(jsx_index) : undefined
const created_ui_components_index_ts = created_ui_components.filter(f => f.endsWith('index.ts'))
const created_ui_composites_index_ts = created_ui_composites.filter(f => f.endsWith('index.ts'))
const created_ui_layout_index_ts = created_ui_layout.filter(f => f.endsWith('index.ts'))
if (created_ui_components_index_ts.length && !ui_index_diff?.added.includes('src/components')) {
fail('New components were added, but not exported in ui/index.ts')
}
if (created_ui_composites_index_ts.length && !ui_index_diff?.added.includes('src/composites')) {
fail('New composites were added, but not exported in ui/index.ts')
}
if (created_ui_layout_index_ts.length && !ui_index_diff?.added.includes('src/layout')) {
fail('New layout components were added, but not exported in ui/index.ts')
}
if (created_ui_components_index_ts.length && !jsx_index_diff?.added.includes('../components')) {
fail('New components were added, but not exported in ui/utils/JSXTypeUtil.ts')
}
if (created_ui_composites_index_ts.length && !jsx_index_diff?.added.includes('../composites')) {
fail('New composites were added, but not exported in ui/utils/JSXTypeUtil.ts')
}
if (created_ui_layout_index_ts.length && !jsx_index_diff?.added.includes('../layout')) {
fail('New layout components were added, but not exported in ui/utils/JSXTypeUtil.ts')
}
if (created_ui_components.length && !created_ui_components_stories.length) {
fail('New components were added, but no stories were created')
}
if (created_ui_composites.length && !created_ui_composites_stories.length) {
fail('New composites were added, but no stories were created')
}
if (created_ui_layout.length && !created_ui_layout_stories.length) {
fail('New layout components were added, but no stories were created')
}
}
checkUiPackage()
// -- Core Package Checks -----------------------------------------------------
async function checkCorePackage() {
const created_core_controllers = created_files.filter(f => f.includes('core/src/controllers'))
const created_core_controllers_tests = created_files.filter(f =>
f.includes('core/tests/controllers')
)
for (const f of created_core_controllers) {
const diff = await diffForFile(f)
if (diff?.added.includes('import @web3modal/ui')) {
fail(`${f} is importing @web3modal/ui, which is not allowed in core package`)
}
if (!diff?.added.includes(TYPE_COMMENT)) {
fail(`${f} is missing \`${TYPE_COMMENT}\` comment`)
}
if (!diff?.added.includes(STATE_COMMENT)) {
fail(`${f} is missing \`${STATE_COMMENT}\` comment`)
}
if (!diff?.added.includes(CONTROLLER_COMMENT)) {
fail(`${f} is missing \`${CONTROLLER_COMMENT}\` comment`)
}
if (diff?.added.includes('this.state')) {
fail(`${f} is using this.state, use just state`)
}
if (diff?.added.includes('@web3modal/core/')) {
fail(`${f} should use relative imports instead of direct package access`)
}
if (diff?.added.includes("'valtio'")) {
fail(`${f} is importing valtio, but should use valtio/vanilla`)
}
}
if (created_core_controllers.length && !created_core_controllers_tests.length) {
fail('New controllers were added, but no tests were created')
}
}
checkCorePackage()
// -- Scaffold Html Package Checks --------------------------------------------
async function checkScaffoldHtmlPackage() {
const created_modal = created_files.filter(f => f.includes('scaffold/src/modal'))
const created_pages = created_files.filter(f => f.includes('scaffold/src/pages'))
const created_partials = created_files.filter(f => f.includes('scaffold/src/partials'))
const created_scaffold_files = [...created_modal, ...created_pages, ...created_partials]
const created_scaffold_index_files = created_scaffold_files.filter(f => f.includes('index.ts'))
for (const f of created_scaffold_index_files) {
const diff = await diffForFile(f)
if (!diff?.added.includes(RENDER_COMMENT) && diff?.added.includes('render()')) {
fail(`${f} is missing \`${RENDER_COMMENT}\` comment`)
}
if (
(diff?.added.includes('@state(') || diff?.added.includes('@property(')) &&
!diff.added.includes(STATE_PROPERTIES_COMMENT)
) {
fail(`${f} is missing \`${STATE_PROPERTIES_COMMENT}\` comment`)
}
if (diff?.added.includes('private ') && !diff.added.includes(PRIVATE_COMMENT)) {
message(
`${f} is missing \`${PRIVATE_COMMENT}\` comment, but seems to have private members. Check if this is correct`
)
}
if (!diff?.added.includes(`@customElement('w3m-`)) {
fail(`${f} is a scaffold element, but does not define w3m- prefix`)
}
if (diff?.added.includes('.subscribe') && !diff.added.includes('this.unsubscribe.forEach')) {
fail(`${f} is subscribing to controller states without unsubscribe logic`)
}
if (
diff?.added.includes('@web3modal/core/') ||
diff?.added.includes('@web3modal/ui/') ||
diff?.added.includes('@web3modal/scaffold/')
) {
fail(`${f} should use relative imports instead of direct package access`)
}
}
}
checkScaffoldHtmlPackage()
// -- Client(s) Package Checks ----------------------------------------------------
async function checkClientPackages() {
const wagmi_files = modified_files.filter(f => f.includes('/wagmi/'))
for (const f of wagmi_files) {
const diff = await diffForFile(f)
if (diff?.added.includes('@web3modal/core')) {
fail(`${f} is not allowed to import from @web3modal/core`)
}
if (diff?.added.includes('@web3modal/ui')) {
fail(`${f} is not allowed to import from @web3modal/ui`)
}
}
}
checkClientPackages()
// -- Check sdkVersion ------------------------------------------------------------
function checkSdkVersion() {
if (PACKAGE_VERSION !== corePackageJson.version) {
fail(`VERSION in utils/constants does't match core package.json version`)
}
}
checkSdkVersion()
// -- Check left over development constants ---------------------------------------
async function checkDevelopmentConstants() {
for (const f of updated_files) {
const diff = await diffForFile(f)
const fileContent = await danger.github.utils.fileContents(f)
if (diff?.added.includes('localhost:') && !fileContent.includes('// Allow localhost')) {
fail(`${f} uses localhost: which is likely a mistake`)
}
}
}
checkDevelopmentConstants()