-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
dangerfile.ts
421 lines (344 loc) · 15.8 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* eslint-disable no-await-in-loop */
import { danger, fail, message, warn } from 'danger'
// -- 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 RELATIVE_IMPORT_SAME_DIR = `'./`
const RELATIVE_IMPORT_PARENT_DIR = `'../`
const RELATIVE_IMPORT_EXTENSION = `.js'`
const PRIVATE_FUNCTION_REGEX = /private\s+(?:\w+)\s*\(\s*\)/gu
// -- 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 pnpmLock = updated_files.find(f => f.includes('pnpm-lock.yaml'))
const yarnLock = updated_files.find(f => f.includes('yarn.lock'))
const npmLock = updated_files.find(f => f.includes('package-lock.json'))
if (packageJsons.length && !pnpmLock) {
warn('Changes were made to one or more package.json(s), but not to pnpm-lock.yaml')
}
if (yarnLock || npmLock) {
fail('Non pnpm lockfile(s) detected (yarn / npm), please use pnpm')
}
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 deleted_ui_composites = deleted_files.filter(f => f.includes('ui/src/composites'))
const created_ui_layout = created_files.filter(f => f.includes('ui/src/layout'))
const deleted_ui_layout = deleted_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 @reown/appkit-core')) {
fail(`${f} is importing @reown/appkit-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`)
}
const privateFunctionsAdded = diff?.added.match(PRIVATE_FUNCTION_REGEX)?.length
if (privateFunctionsAdded && !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('@reown/appkit-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 types_util_index = modified_files.find(f => f.includes('ui/src/utils/JSXTypeUtil.ts'))
const types_util_diff = types_util_index ? await diffForFile(types_util_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 deleted_ui_composites_index_ts = deleted_ui_composites.filter(f => f.endsWith('index.ts'))
const is_new_composites_added =
created_ui_composites_index_ts.length > deleted_ui_composites_index_ts.length
const created_ui_layout_index_ts = created_ui_layout.filter(f => f.endsWith('index.ts'))
const deleted_ui_layout_index_ts = deleted_ui_layout.filter(f => f.endsWith('index.ts'))
const is_new_layout_added = created_ui_layout_index_ts.length > deleted_ui_layout_index_ts.length
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 (is_new_composites_added && !types_util_diff) {
fail('New composites were added, but JSXTypeUtil.ts is not modified')
}
if (is_new_composites_added && !types_util_diff?.added.includes('../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 && !types_util_diff?.added.includes('../components')) {
fail(
`New components were added, but not exported in ui/utils/JSXTypeUtil.ts: ${created_ui_components.join(
', '
)}`
)
}
if (is_new_composites_added && !types_util_diff?.added.includes('../composites')) {
fail('New composites were added, but not exported in ui/utils/JSXTypeUtil.ts')
}
if (is_new_layout_added && !types_util_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 (is_new_composites_added && !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')
)
const modified_core_controllers = modified_files.filter(f => f.includes('core/src/controllers'))
const modified_core_controllers_tests = modified_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 @reown/appkit-ui')) {
fail(`${f} is importing @reown/appkit-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('@reown/appkit-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')
}
if (modified_core_controllers.length && !modified_core_controllers_tests) {
message(`
The following controllers were modified, but not tests were changed:
${modified_core_controllers.join('\n')}
`)
}
}
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`)
}
const privateFunctionsAdded = diff?.added.match(PRIVATE_FUNCTION_REGEX)?.length
if (privateFunctionsAdded && !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('@reown/appkit-core/') ||
diff?.added.includes('@reown/appkit-ui/') ||
diff?.added.includes('@reown/scaffold/')
) {
fail(`${f} should use relative imports instead of direct package access`)
}
}
}
checkScaffoldHtmlPackage()
// -- Client(s) Package Checks ----------------------------------------------------
// -- Helper functions
function isRelativeImport(addition: string | undefined) {
const sameDir = addition?.includes(RELATIVE_IMPORT_SAME_DIR)
const parentDir = addition?.includes(RELATIVE_IMPORT_PARENT_DIR)
return sameDir || parentDir
}
function containsRelativeImportWithoutJSExtension(addition: string | undefined) {
const hasImportStatement = addition?.includes('import')
const lacksJSExtension = !addition?.includes(RELATIVE_IMPORT_EXTENSION)
const hasRelativePath = isRelativeImport(addition)
return hasImportStatement && lacksJSExtension && hasRelativePath
}
async function checkClientPackages() {
const client_files = modified_files.filter(f =>
/\/packages\/(?:wagmi|solana|ethers|ethers5)\//u.test(f)
)
for (const f of client_files) {
const diff = await diffForFile(f)
if (diff?.added.includes("from '@reown/appkit-core")) {
fail(`${f} is not allowed to import from @reown/appkit-core`)
}
if (diff?.added.includes("from '@reown/appkit-ui")) {
fail(`${f} is not allowed to import from @reown/appkit-ui`)
}
if (containsRelativeImportWithoutJSExtension(diff?.added)) {
fail(`${f} contains relative imports without .js extension`)
}
}
}
checkClientPackages()
// -- Check wallet ------------------------------------------------------------
async function checkWallet() {
const wallet_files = modified_files.filter(f => f.includes('/wallet/'))
for (const f of wallet_files) {
const diff = await diffForFile(f)
if (f.includes('W3mFrameConstants') && diff?.added.includes('SECURE_SITE_SDK')) {
warn('Secure site URL has been changed')
}
}
}
checkWallet()
// -- Check laboratory ------------------------------------------------------------
async function checkLaboratory() {
const lab_files = modified_files.filter(f => f.includes('/laboratory/'))
for (const f of lab_files) {
const diff = await diffForFile(f)
if (f.includes('project') && (diff?.removed.includes('spec') || diff?.added.includes('spec'))) {
warn('Testing spec changed')
}
}
// Check that no .only is present in tests
const test_files = lab_files.filter(f => f.includes('.spec.ts'))
for (const f of test_files) {
const fileContent = await danger.github.utils.fileContents(f)
if (fileContent.includes('.only')) {
fail(`${f} contains .only, please remove it`)
}
}
}
checkLaboratory()
// -- Check left over development constants ---------------------------------------
async function checkDevelopmentConstants() {
for (const f of updated_files) {
if (f.includes('README.md') || f.includes('.yml')) {
// eslint-disable-next-line no-continue
continue
}
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()
// -- Check changesets ------------------------------------------------------------
async function checkChangesetFiles() {
const changesetFiles = updated_files
.filter(f => f.startsWith('.changeset/'))
.filter(f => f.endsWith('.md') && !f.startsWith('README.md'))
const ignoredChangesetFiles = [
'@apps/gallery-new',
'@reown/appkit-ui-new',
'@reown/appkit-scaffold-ui-new',
'@apps/laboratory-new',
'@reown/appkit-new'
]
for (const f of changesetFiles) {
const fileContent = await danger.github.utils.fileContents(f)
if (fileContent.includes('@examples/')) {
fail(`Changeset file ${f} cannot include @examples/* packages as part of the changeset`)
}
if (ignoredChangesetFiles.some(ignored => fileContent.includes(ignored))) {
fail(
`Changeset file ${f} cannot include ${ignoredChangesetFiles
.map(changesetFile => changesetFile)
.join(', ')} packages as part of the changeset`
)
}
}
}
checkChangesetFiles()
// -- Check Workflows ------------------------------------------------------------
function checkWorkflows() {
const updatedWorkflows = updated_files.filter(f => f.includes('.github/workflows/'))
const deletedWorkflows = deleted_files.filter(f => f.includes('.github/workflows/'))
for (const f of deletedWorkflows) {
fail(`Workflow file(s) ${f} has been deleted`)
}
for (const f of updatedWorkflows) {
warn(`Workflow file ${f} has been modified`)
}
}
checkWorkflows()