2017-06-29 16:36:22 +00:00
|
|
|
import { resolve, relative, sep } from 'path'
|
2017-05-18 08:44:31 +00:00
|
|
|
import _ from 'lodash'
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2017-01-11 19:14:59 +00:00
|
|
|
export function encodeHtml (str) {
|
2017-01-11 21:51:52 +00:00
|
|
|
return str.replace(/</g, '<').replace(/>/g, '>')
|
2017-01-11 19:14:59 +00:00
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2017-01-11 19:14:59 +00:00
|
|
|
export function getContext (req, res) {
|
|
|
|
return { req, res }
|
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2017-01-11 19:14:59 +00:00
|
|
|
export function setAnsiColors (ansiHTML) {
|
2016-11-07 01:34:58 +00:00
|
|
|
ansiHTML.setColors({
|
|
|
|
reset: ['efefef', 'a6004c'],
|
|
|
|
darkgrey: '5a012b',
|
|
|
|
yellow: 'ffab07',
|
|
|
|
green: 'aeefba',
|
|
|
|
magenta: 'ff84bf',
|
|
|
|
blue: '3505a0',
|
|
|
|
cyan: '56eaec',
|
|
|
|
red: '4e053a'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-14 18:21:14 +00:00
|
|
|
export async function waitFor (ms) {
|
2016-11-07 01:34:58 +00:00
|
|
|
return new Promise(function (resolve) {
|
|
|
|
setTimeout(resolve, (ms || 0))
|
|
|
|
})
|
|
|
|
}
|
2016-11-10 18:34:59 +00:00
|
|
|
|
2017-01-11 19:14:59 +00:00
|
|
|
export function urlJoin () {
|
2017-02-16 17:16:00 +00:00
|
|
|
return [].slice.call(arguments).join('/').replace(/\/+/g, '/').replace(':/', '://')
|
2016-11-10 18:34:59 +00:00
|
|
|
}
|
2016-11-24 00:47:11 +00:00
|
|
|
|
2017-03-16 17:52:38 +00:00
|
|
|
export function isUrl (url) {
|
|
|
|
return (url.indexOf('http') === 0 || url.indexOf('//') === 0)
|
|
|
|
}
|
|
|
|
|
2017-03-06 19:00:19 +00:00
|
|
|
export function promisifyRoute (fn) {
|
|
|
|
// If routes is an array
|
2016-11-24 00:47:11 +00:00
|
|
|
if (Array.isArray(fn)) {
|
|
|
|
return Promise.resolve(fn)
|
|
|
|
}
|
2017-03-06 19:00:19 +00:00
|
|
|
// If routes is a function expecting a callback
|
2016-11-24 00:47:11 +00:00
|
|
|
if (fn.length === 1) {
|
2016-12-10 11:39:11 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-11-24 00:47:11 +00:00
|
|
|
fn(function (err, routeParams) {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
resolve(routeParams)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2016-12-10 11:39:11 +00:00
|
|
|
let promise = fn()
|
2017-04-06 09:18:27 +00:00
|
|
|
if (!promise || (!(promise instanceof Promise) && (typeof promise.then !== 'function'))) {
|
2016-11-24 00:47:11 +00:00
|
|
|
promise = Promise.resolve(promise)
|
|
|
|
}
|
|
|
|
return promise
|
|
|
|
}
|
2017-05-11 11:23:58 +00:00
|
|
|
|
|
|
|
export function sequence (tasks, fn) {
|
|
|
|
return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve())
|
|
|
|
}
|
2017-05-12 07:57:24 +00:00
|
|
|
|
2017-06-15 22:19:53 +00:00
|
|
|
export function parallel (tasks, fn) {
|
|
|
|
return Promise.all(tasks.map(task => fn(task)))
|
|
|
|
}
|
|
|
|
|
2017-05-12 07:57:24 +00:00
|
|
|
export function chainFn (base, fn) {
|
2017-05-14 23:01:41 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-12 07:57:24 +00:00
|
|
|
if (!(fn instanceof Function)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return function () {
|
|
|
|
if (base instanceof Function) {
|
|
|
|
base.apply(this, arguments)
|
|
|
|
}
|
|
|
|
fn.apply(this, arguments)
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 08:44:31 +00:00
|
|
|
|
2017-05-26 09:04:11 +00:00
|
|
|
export function wp (p) {
|
2017-05-18 08:44:31 +00:00
|
|
|
/* istanbul ignore if */
|
|
|
|
if (/^win/.test(process.platform)) {
|
|
|
|
p = p.replace(/\\/g, '\\\\')
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
const reqSep = /\//g
|
|
|
|
const sysSep = _.escapeRegExp(sep)
|
|
|
|
const normalize = string => string.replace(reqSep, sysSep)
|
|
|
|
|
|
|
|
export function r () {
|
2017-06-15 16:26:13 +00:00
|
|
|
let args = Array.prototype.slice.apply(arguments)
|
2017-06-29 16:36:22 +00:00
|
|
|
let lastArg = _.last(args)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-07-04 14:21:41 +00:00
|
|
|
if (lastArg.includes('@') || lastArg.includes('~')) {
|
2017-06-29 16:36:22 +00:00
|
|
|
return wp(lastArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wp(resolve(...args.map(normalize)))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function relativeTo () {
|
|
|
|
let args = Array.prototype.slice.apply(arguments)
|
|
|
|
let dir = args.shift()
|
|
|
|
|
|
|
|
// Resolve path
|
|
|
|
let path = r(...args)
|
|
|
|
|
|
|
|
// Check if path is an alias
|
|
|
|
if (path.includes('@') || path.includes('~')) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make correct relative path
|
|
|
|
let rp = relative(dir, path)
|
|
|
|
if (rp[0] !== '.') {
|
|
|
|
rp = './' + rp
|
|
|
|
}
|
|
|
|
return wp(rp)
|
2017-05-18 08:44:31 +00:00
|
|
|
}
|
2017-06-14 16:13:43 +00:00
|
|
|
|
|
|
|
export function flatRoutes (router, path = '', routes = []) {
|
|
|
|
router.forEach((r) => {
|
|
|
|
if (!r.path.includes(':') && !r.path.includes('*')) {
|
2017-06-19 15:47:31 +00:00
|
|
|
/* istanbul ignore if */
|
2017-06-14 16:13:43 +00:00
|
|
|
if (r.children) {
|
|
|
|
flatRoutes(r.children, path + r.path + '/', routes)
|
|
|
|
} else {
|
|
|
|
routes.push((r.path === '' && path[path.length - 1] === '/' ? path.slice(0, -1) : path) + r.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cleanChildrenRoutes (routes, isChild = false) {
|
|
|
|
let start = -1
|
|
|
|
let routesIndex = []
|
|
|
|
routes.forEach((route) => {
|
|
|
|
if (/-index$/.test(route.name) || route.name === 'index') {
|
|
|
|
// Save indexOf 'index' key in name
|
|
|
|
let res = route.name.split('-')
|
|
|
|
let s = res.indexOf('index')
|
|
|
|
start = (start === -1 || s < start) ? s : start
|
|
|
|
routesIndex.push(res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
routes.forEach((route) => {
|
|
|
|
route.path = (isChild) ? route.path.replace('/', '') : route.path
|
|
|
|
if (route.path.indexOf('?') > -1) {
|
|
|
|
let names = route.name.split('-')
|
|
|
|
let paths = route.path.split('/')
|
|
|
|
if (!isChild) {
|
|
|
|
paths.shift()
|
|
|
|
} // clean first / for parents
|
|
|
|
routesIndex.forEach((r) => {
|
|
|
|
let i = r.indexOf('index') - start // children names
|
|
|
|
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('/')
|
|
|
|
}
|
|
|
|
route.name = route.name.replace(/-index$/, '')
|
|
|
|
if (route.children) {
|
|
|
|
if (route.children.find((child) => child.path === '')) {
|
|
|
|
delete route.name
|
|
|
|
}
|
|
|
|
route.children = cleanChildrenRoutes(route.children, true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createRoutes (files, srcDir) {
|
|
|
|
let routes = []
|
|
|
|
files.forEach((file) => {
|
2017-08-14 10:09:27 +00:00
|
|
|
let keys = file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\.js$/, '').replace(/\/{2,}/g, '/').split('/').slice(1)
|
2017-06-14 16:13:43 +00:00
|
|
|
let route = { name: '', path: '', component: r(srcDir, file) }
|
|
|
|
let parent = routes
|
|
|
|
keys.forEach((key, i) => {
|
|
|
|
route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '')
|
|
|
|
route.name += (key === '_') ? 'all' : ''
|
|
|
|
let child = _.find(parent, { name: route.name })
|
|
|
|
if (child) {
|
|
|
|
if (!child.children) {
|
|
|
|
child.children = []
|
|
|
|
}
|
|
|
|
parent = child.children
|
|
|
|
route.path = ''
|
|
|
|
} else {
|
|
|
|
if (key === 'index' && (i + 1) === keys.length) {
|
|
|
|
route.path += (i > 0 ? '' : '/')
|
|
|
|
} else {
|
|
|
|
route.path += '/' + (key === '_' ? '*' : key.replace('_', ':'))
|
|
|
|
if (key !== '_' && key.indexOf('_') !== -1) {
|
|
|
|
route.path += '?'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Order Routes path
|
|
|
|
parent.push(route)
|
|
|
|
parent.sort((a, b) => {
|
|
|
|
if (!a.path.length || a.path === '/') {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if (!b.path.length || b.path === '/') {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
let res = 0
|
|
|
|
let _a = a.path.split('/')
|
|
|
|
let _b = b.path.split('/')
|
|
|
|
for (let i = 0; i < _a.length; i++) {
|
|
|
|
if (res !== 0) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
let y = (_a[i].indexOf('*') > -1) ? 2 : (_a[i].indexOf(':') > -1 ? 1 : 0)
|
|
|
|
let z = (_b[i].indexOf('*') > -1) ? 2 : (_b[i].indexOf(':') > -1 ? 1 : 0)
|
|
|
|
res = y - z
|
|
|
|
if (i === _b.length - 1 && res === 0) {
|
|
|
|
res = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res === 0 ? -1 : res
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return cleanChildrenRoutes(routes)
|
|
|
|
}
|