forked from safe-global/safe-wallet-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-routes.js
36 lines (28 loc) · 1.09 KB
/
generate-routes.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
import * as fs from 'fs'
const isFile = (item) => item.endsWith`.tsx`
const iterate = (folderName, parentRoute, root) => {
const items = fs.readdirSync(folderName)
items
.sort((a, b) => (isFile(a) ? -1 : 1))
.forEach((item) => {
// Skip service files
if (/_app|_document/.test(item)) return
// A folder, continue iterating
if (!isFile(item)) {
const key = item.replace(/-\w/g, (match) => match.replace(/-/g, '').toUpperCase()) // spending-limit -> spendingLimit
root[key] = {}
iterate(`${folderName}/${item}`, `${parentRoute}/${item}`, root[key])
return
}
// A file
const name = item.split('.')[0]
const path = name === 'index' ? parentRoute : `${parentRoute}/${name}`
const key = name
.replace(/-\w/g, (match) => match.replace(/-/g, '').toUpperCase()) // spending-limit -> spendingLimit
.replace(/\W/g, '') // [txId] -> txId
root[key] = path || '/'
})
return root
}
const routes = iterate('src/pages', '', {})
console.log(`export const AppRoutes = ${JSON.stringify(routes, null, 2)}`)