2018-03-16 19:11:24 +00:00
|
|
|
import path from 'path'
|
2018-10-18 15:43:44 +00:00
|
|
|
import get from 'lodash/get'
|
2018-09-14 07:06:44 +00:00
|
|
|
import consola from 'consola'
|
2018-03-16 06:26:23 +00:00
|
|
|
|
2018-12-22 21:05:13 +00:00
|
|
|
import { r } from './resolve'
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const flatRoutes = function flatRoutes (router, fileName = '', routes = []) {
|
2018-08-06 00:12:44 +00:00
|
|
|
router.forEach((r) => {
|
2018-10-12 22:31:19 +00:00
|
|
|
if ([':', '*'].some(c => r.path.includes(c))) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (r.children) {
|
2019-04-22 18:50:07 +00:00
|
|
|
if (fileName === '' && r.path === '/') {
|
2018-10-12 22:31:19 +00:00
|
|
|
routes.push('/')
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
2019-04-22 18:50:07 +00:00
|
|
|
return flatRoutes(r.children, fileName + r.path + '/', routes)
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
2019-04-22 18:50:07 +00:00
|
|
|
fileName = fileName.replace(/^\/+$/, '/')
|
2018-10-12 22:31:19 +00:00
|
|
|
routes.push(
|
2019-04-22 18:50:07 +00:00
|
|
|
(r.path === '' && fileName[fileName.length - 1] === '/'
|
|
|
|
? fileName.slice(0, -1)
|
|
|
|
: fileName) + r.path
|
2018-10-12 22:31:19 +00:00
|
|
|
)
|
2017-06-14 16:13:43 +00:00
|
|
|
})
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
function cleanChildrenRoutes (routes, isChild = false, routeNameSplitter = '-') {
|
2017-06-14 16:13:43 +00:00
|
|
|
let start = -1
|
2018-12-20 12:42:42 +00:00
|
|
|
const regExpIndex = new RegExp(`${routeNameSplitter}index$`)
|
2018-08-08 10:54:05 +00:00
|
|
|
const routesIndex = []
|
2018-08-06 00:12:44 +00:00
|
|
|
routes.forEach((route) => {
|
2018-12-20 12:42:42 +00:00
|
|
|
if (regExpIndex.test(route.name) || route.name === 'index') {
|
2017-06-14 16:13:43 +00:00
|
|
|
// Save indexOf 'index' key in name
|
2018-12-20 12:42:42 +00:00
|
|
|
const res = route.name.split(routeNameSplitter)
|
2018-08-08 10:54:05 +00:00
|
|
|
const s = res.indexOf('index')
|
2018-01-11 14:11:49 +00:00
|
|
|
start = start === -1 || s < start ? s : start
|
2017-06-14 16:13:43 +00:00
|
|
|
routesIndex.push(res)
|
|
|
|
}
|
|
|
|
})
|
2018-08-06 00:12:44 +00:00
|
|
|
routes.forEach((route) => {
|
2018-01-11 14:11:49 +00:00
|
|
|
route.path = isChild ? route.path.replace('/', '') : route.path
|
2018-10-12 22:31:19 +00:00
|
|
|
if (route.path.includes('?')) {
|
2018-12-20 12:42:42 +00:00
|
|
|
const names = route.name.split(routeNameSplitter)
|
2018-08-08 10:54:05 +00:00
|
|
|
const paths = route.path.split('/')
|
2017-06-14 16:13:43 +00:00
|
|
|
if (!isChild) {
|
|
|
|
paths.shift()
|
|
|
|
} // clean first / for parents
|
2018-08-06 00:12:44 +00:00
|
|
|
routesIndex.forEach((r) => {
|
2018-08-08 10:54:05 +00:00
|
|
|
const i = r.indexOf('index') - start // children names
|
2017-06-14 16:13:43 +00:00
|
|
|
if (i < paths.length) {
|
|
|
|
for (let a = 0; a <= i; a++) {
|
|
|
|
if (a === i) {
|
|
|
|
paths[a] = paths[a].replace('?', '')
|
|
|
|
}
|
|
|
|
if (a < i && names[a] !== r[a]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
route.path = (isChild ? '' : '/') + paths.join('/')
|
|
|
|
}
|
2018-12-20 12:42:42 +00:00
|
|
|
route.name = route.name.replace(regExpIndex, '')
|
2017-06-14 16:13:43 +00:00
|
|
|
if (route.children) {
|
2018-01-11 14:11:49 +00:00
|
|
|
if (route.children.find(child => child.path === '')) {
|
2017-06-14 16:13:43 +00:00
|
|
|
delete route.name
|
|
|
|
}
|
2018-12-20 12:42:42 +00:00
|
|
|
route.children = cleanChildrenRoutes(route.children, true, routeNameSplitter)
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2018-12-12 06:31:49 +00:00
|
|
|
const DYNAMIC_ROUTE_REGEX = /^\/([:*])/
|
2018-11-14 16:50:06 +00:00
|
|
|
|
2019-09-01 16:24:21 +00:00
|
|
|
export const sortRoutes = function sortRoutes (routes) {
|
2018-11-14 16:50:06 +00:00
|
|
|
routes.sort((a, b) => {
|
|
|
|
if (!a.path.length) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if (!b.path.length) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
// Order: /static, /index, /:dynamic
|
|
|
|
// Match exact route before index: /login before /index/_slug
|
|
|
|
if (a.path === '/') {
|
|
|
|
return DYNAMIC_ROUTE_REGEX.test(b.path) ? -1 : 1
|
|
|
|
}
|
|
|
|
if (b.path === '/') {
|
|
|
|
return DYNAMIC_ROUTE_REGEX.test(a.path) ? 1 : -1
|
|
|
|
}
|
|
|
|
|
|
|
|
let i
|
|
|
|
let res = 0
|
|
|
|
let y = 0
|
|
|
|
let z = 0
|
|
|
|
const _a = a.path.split('/')
|
|
|
|
const _b = b.path.split('/')
|
|
|
|
for (i = 0; i < _a.length; i++) {
|
|
|
|
if (res !== 0) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
y = _a[i] === '*' ? 2 : _a[i].includes(':') ? 1 : 0
|
|
|
|
z = _b[i] === '*' ? 2 : _b[i].includes(':') ? 1 : 0
|
|
|
|
res = y - z
|
|
|
|
// If a.length >= b.length
|
|
|
|
if (i === _b.length - 1 && res === 0) {
|
|
|
|
// unless * found sort by level, then alphabetically
|
|
|
|
res = _a[i] === '*' ? -1 : (
|
|
|
|
_a.length === _b.length ? a.path.localeCompare(b.path) : (_a.length - _b.length)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res === 0) {
|
|
|
|
// unless * found sort by level, then alphabetically
|
|
|
|
res = _a[i - 1] === '*' && _b[i] ? 1 : (
|
|
|
|
_a.length === _b.length ? a.path.localeCompare(b.path) : (_a.length - _b.length)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
|
|
|
|
routes.forEach((route) => {
|
|
|
|
if (route.children) {
|
|
|
|
sortRoutes(route.children)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const createRoutes = function createRoutes ({
|
2019-06-04 19:48:01 +00:00
|
|
|
files,
|
|
|
|
srcDir,
|
|
|
|
pagesDir = '',
|
|
|
|
routeNameSplitter = '-',
|
2019-09-01 14:37:25 +00:00
|
|
|
supportedExtensions = ['vue', 'js'],
|
|
|
|
trailingSlash
|
2019-06-04 19:48:01 +00:00
|
|
|
}) {
|
2018-08-08 10:54:05 +00:00
|
|
|
const routes = []
|
2018-08-06 00:12:44 +00:00
|
|
|
files.forEach((file) => {
|
2018-08-08 10:54:05 +00:00
|
|
|
const keys = file
|
2018-12-15 06:55:08 +00:00
|
|
|
.replace(new RegExp(`^${pagesDir}`), '')
|
|
|
|
.replace(new RegExp(`\\.(${supportedExtensions.join('|')})$`), '')
|
2018-01-11 14:11:49 +00:00
|
|
|
.replace(/\/{2,}/g, '/')
|
|
|
|
.split('/')
|
|
|
|
.slice(1)
|
2018-08-08 10:54:05 +00:00
|
|
|
const route = { name: '', path: '', component: r(srcDir, file) }
|
2017-06-14 16:13:43 +00:00
|
|
|
let parent = routes
|
|
|
|
keys.forEach((key, i) => {
|
2018-03-11 13:59:37 +00:00
|
|
|
// remove underscore only, if its the prefix
|
2018-10-12 22:31:19 +00:00
|
|
|
const sanitizedKey = key.startsWith('_') ? key.substr(1) : key
|
|
|
|
|
2018-01-11 14:11:49 +00:00
|
|
|
route.name = route.name
|
2018-12-20 12:42:42 +00:00
|
|
|
? route.name + routeNameSplitter + sanitizedKey
|
2018-07-18 14:39:48 +00:00
|
|
|
: sanitizedKey
|
2018-01-11 14:11:49 +00:00
|
|
|
route.name += key === '_' ? 'all' : ''
|
2018-12-15 06:55:08 +00:00
|
|
|
route.chunkName = file.replace(new RegExp(`\\.(${supportedExtensions.join('|')})$`), '')
|
2018-10-12 22:31:19 +00:00
|
|
|
const child = parent.find(parentRoute => parentRoute.name === route.name)
|
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
if (child) {
|
2017-08-25 11:54:14 +00:00
|
|
|
child.children = child.children || []
|
2017-06-14 16:13:43 +00:00
|
|
|
parent = child.children
|
|
|
|
route.path = ''
|
2018-08-10 23:24:53 +00:00
|
|
|
} else if (key === 'index' && i + 1 === keys.length) {
|
|
|
|
route.path += i > 0 ? '' : '/'
|
2017-06-14 16:13:43 +00:00
|
|
|
} else {
|
2018-10-12 22:31:19 +00:00
|
|
|
route.path += '/' + getRoutePathExtension(key)
|
|
|
|
|
|
|
|
if (key.startsWith('_') && key.length > 1) {
|
2018-08-10 23:24:53 +00:00
|
|
|
route.path += '?'
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-09-01 14:37:25 +00:00
|
|
|
if (trailingSlash !== undefined) {
|
|
|
|
route.pathToRegexpOptions = { ...route.pathToRegexpOptions, strict: true }
|
|
|
|
route.path = route.path.replace(/\/+$/, '') + (trailingSlash ? '/' : '')
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
parent.push(route)
|
|
|
|
})
|
2018-11-14 16:50:06 +00:00
|
|
|
|
|
|
|
sortRoutes(routes)
|
2018-12-20 12:42:42 +00:00
|
|
|
return cleanChildrenRoutes(routes, false, routeNameSplitter)
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
2018-09-14 07:06:44 +00:00
|
|
|
|
|
|
|
// Guard dir1 from dir2 which can be indiscriminately removed
|
2019-07-10 10:45:49 +00:00
|
|
|
export const guardDir = function guardDir (options, key1, key2) {
|
2018-10-18 15:43:44 +00:00
|
|
|
const dir1 = get(options, key1, false)
|
|
|
|
const dir2 = get(options, key2, false)
|
2018-09-14 07:06:44 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
dir1 &&
|
|
|
|
dir2 &&
|
|
|
|
(
|
|
|
|
dir1 === dir2 ||
|
|
|
|
(
|
|
|
|
dir1.startsWith(dir2) &&
|
|
|
|
!path.basename(dir1).startsWith(path.basename(dir2))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const errorMessage = `options.${key2} cannot be a parent of or same as ${key1}`
|
|
|
|
consola.fatal(errorMessage)
|
|
|
|
throw new Error(errorMessage)
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 12:07:23 +00:00
|
|
|
|
2018-10-12 22:31:19 +00:00
|
|
|
const getRoutePathExtension = (key) => {
|
|
|
|
if (key === '_') {
|
|
|
|
return '*'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.startsWith('_')) {
|
|
|
|
return `:${key.substr(1)}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const promisifyRoute = function promisifyRoute (fn, ...args) {
|
2018-12-22 21:05:13 +00:00
|
|
|
// If routes is an array
|
|
|
|
if (Array.isArray(fn)) {
|
|
|
|
return Promise.resolve(fn)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-22 21:05:13 +00:00
|
|
|
// If routes is a function expecting a callback
|
|
|
|
if (fn.length === arguments.length) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fn((err, routeParams) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
resolve(routeParams)
|
|
|
|
}, ...args)
|
2018-12-15 08:37:31 +00:00
|
|
|
})
|
2018-12-22 21:05:13 +00:00
|
|
|
}
|
|
|
|
let promise = fn(...args)
|
|
|
|
if (
|
|
|
|
!promise ||
|
|
|
|
(!(promise instanceof Promise) && typeof promise.then !== 'function')
|
|
|
|
) {
|
|
|
|
promise = Promise.resolve(promise)
|
|
|
|
}
|
|
|
|
return promise
|
2018-12-15 08:37:31 +00:00
|
|
|
}
|