Skip to content

Instantly share code, notes, and snippets.

@Lruihao
Created June 19, 2023 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lruihao/d8f2984525dc9e78dd6a49e15f49cf38 to your computer and use it in GitHub Desktop.
Save Lruihao/d8f2984525dc9e78dd6a49e15f49cf38 to your computer and use it in GitHub Desktop.
自动生成 Vue 路由
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 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'
Vue.use(VueRouter)
const routes = [
${routes.join(os.EOL)}
]
const router = new VueRouter({
mode: 'hash',
routes,
})
export default router
`
fs.writeFile(routerFile, result, 'utf-8', (err) => {
if (err) throw err
console.log(` Router generated successfully in ${routerFile}`)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment