-
Notifications
You must be signed in to change notification settings - Fork 5
/
gen-router.js
62 lines (55 loc) · 1.71 KB
/
gen-router.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
const fs = require('fs')
const os = require('os')
const vueDir = './src/views/'
const routerFile = './src/router.js'
fs.readdir(vueDir, function (err, files) {
if (err) {
console.error('❌ Could not list the directory.', err)
return
}
const routes = []
for (const filename of files) {
if (filename.indexOf('.') < 0) {
continue
}
const [name, ext] = filename.split('.')
if (ext !== 'vue') {
continue
}
const routeName = name.replace(/-([a-z])/g, (_, match) => match.toUpperCase())
let routeDescription = ''
const contentFull = fs.readFileSync(`${vueDir}${filename}`, 'utf-8')
// get route description from first line comment
const match = /<!--\s*(.*)\s*-->/g.exec(contentFull.split(os.EOL)[0])
if (match) {
routeDescription = match[1].trim()
}
routes.push(` {
path: '/${name === 'home' ? '' : name}',
name: '${routeName}',${routeDescription ? `\n meta: { description: '${routeDescription}' },` : ''}
component: () => import(/* webpackChunkName: "${routeName}" */ '@/views/${filename}'),
},`)
}
const result =
`// This file is automatically generated by gen-router.js, please do not modify it manually!
import VueRouter from 'vue-router'
import Vue from 'vue'
const packageInfo = require('../package.json')
Vue.use(VueRouter)
const routes = [
${routes.join(os.EOL)}
]
const router = new VueRouter({
mode: 'hash',
routes,
})
router.afterEach((to) => {
document.title = \`\${to.meta?.description} - \${packageInfo.name} \${packageInfo.version}\`
})
export default router
`
fs.writeFile(routerFile, result, 'utf-8', (err) => {
if (err) throw err
console.log(`✅ Router generated successfully in ${routerFile}`)
})
})