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
| const fs = require('fs') const path = require('path')
const paths = {}
function getStat(dir, resolve, reject) { fs.stat(path.resolve(dir), (eror, stats) => { if (eror) { if (dir.split('.vue').length > 2) return
typeof reject === 'function' && reject() return } typeof resolve === 'function' && resolve(stats.isFile(), stats.isDirectory()) }) }
function checkName(dir, routeName) { getStat( dir, (isFile, isDir) => { if (isFile) { const content = fs.readFileSync(path.resolve(dir), 'utf-8')
content.replace(/export default {.*\W*.*name: ['"](.+)['"].*/m, (str, $1) => { if ($1 !== routeName) { console.warn(`${dir} 路由名 ${routeName} 与组件名 ${$1} 不一致`) }
return '' }) } else if (isDir) { checkName(dir + '/index.vue', routeName) } }, () => { checkName(dir + '.vue', routeName) } ) }
function getRouteNameInReplace($1, $2, paths) { if (!$1 || !$2) return if (paths[$1]) { console.error(`存在同名路由: ${$1}, ${paths[$1]}, ${$2}`) } else { const dir = $2.replace('@', './src') paths[$1] = dir checkName(dir, $1) } }
function fileDisplay(filePath) { fs.readdir(filePath, (err, files) => { if (err) { console.warn(err) } else { files.forEach(filename => { const filedir = path.join(filePath, filename) getStat(filedir, (isFile, isDir) => { if (isFile) { const content = fs.readFileSync(filedir, 'utf-8')
content .replace(/name: ['"](.+)['"].*\W*.*component:.*\W*.*import\(.*\W*.*['"](.+)['"].*\W*.*\)/gm, (str, $1, $2) => { getRouteNameInReplace($1, $2, paths)
return '' }) .replace(/component:.*\W*.*import\(.*\W*.*['"](.+)['"].*\W*.*\).*\W*.*name: ['"](.+)['"]/gm, (str, $1, $2) => { getRouteNameInReplace($2, $1, paths)
return '' }) } else if (isDir) { fileDisplay(filedir) } }) }) } }) }
fileDisplay(path.resolve('./src/router'))
|