2017-12-12 09:42:29 +00:00
|
|
|
const { resolve, relative, sep } = require('path')
|
|
|
|
const _ = require('lodash')
|
2018-01-11 14:11:49 +00:00
|
|
|
const PrettyError = require('pretty-error')
|
2018-01-11 15:13:52 +00:00
|
|
|
const Chalk = require('chalk')
|
2018-01-11 14:11:49 +00:00
|
|
|
|
2018-01-11 18:20:35 +00:00
|
|
|
exports.pe = new PrettyError()
|
2018-01-11 14:11:49 +00:00
|
|
|
|
2018-01-11 15:13:52 +00:00
|
|
|
exports.printWarn = function (msg, from) {
|
2018-01-11 14:11:49 +00:00
|
|
|
/* eslint-disable no-console */
|
2018-01-11 15:13:52 +00:00
|
|
|
const fromStr = from ? Chalk.yellow(` ${from}\n\n`) : ' '
|
2018-01-13 05:29:47 +00:00
|
|
|
console.warn('\n' + Chalk.bgYellow.black(' WARN ') + fromStr + msg + '\n')
|
2018-01-11 15:13:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 18:20:35 +00:00
|
|
|
exports.renderError = function (_error, from) {
|
|
|
|
const errStr = exports.pe.render(_error)
|
2018-01-11 16:11:50 +00:00
|
|
|
const fromStr = from ? Chalk.red(` ${from}`) : ''
|
2018-01-11 19:16:26 +00:00
|
|
|
return '\n' + Chalk.bgRed.black(' ERROR ') + fromStr + '\n\n' + errStr
|
2018-01-11 18:20:35 +00:00
|
|
|
}
|
2018-01-11 15:13:52 +00:00
|
|
|
|
2018-01-11 18:20:35 +00:00
|
|
|
exports.printError = function () {
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.error(exports.renderError(...arguments))
|
2018-01-11 14:11:49 +00:00
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2018-01-11 16:11:50 +00:00
|
|
|
exports.fatalError = function () {
|
2018-01-11 18:20:35 +00:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.error(exports.renderError(...arguments))
|
2018-01-11 16:11:50 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.encodeHtml = 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-12-12 09:42:29 +00:00
|
|
|
exports.getContext = function getContext(req, res) {
|
2017-01-11 19:14:59 +00:00
|
|
|
return { req, res }
|
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.setAnsiColors = 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'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-21 10:19:20 +00:00
|
|
|
exports.waitFor = function waitFor(ms) {
|
2018-01-11 14:11:49 +00:00
|
|
|
return new Promise(resolve => setTimeout(resolve, ms || 0))
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
2016-11-10 18:34:59 +00:00
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.urlJoin = function urlJoin() {
|
2018-01-11 14:11:49 +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-12-12 09:42:29 +00:00
|
|
|
exports.isUrl = function isUrl(url) {
|
2018-01-11 14:11:49 +00:00
|
|
|
return url.indexOf('http') === 0 || url.indexOf('//') === 0
|
2017-03-16 17:52:38 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.promisifyRoute = function promisifyRoute(fn, ...args) {
|
2017-03-06 19:00:19 +00:00
|
|
|
// 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
|
2017-11-24 17:02:53 +00:00
|
|
|
if (fn.length === arguments.length) {
|
2016-12-10 11:39:11 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-24 17:02:53 +00:00
|
|
|
fn((err, routeParams) => {
|
2016-11-24 00:47:11 +00:00
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
resolve(routeParams)
|
2017-11-24 17:02:53 +00:00
|
|
|
}, ...args)
|
2016-11-24 00:47:11 +00:00
|
|
|
})
|
|
|
|
}
|
2017-11-24 17:02:53 +00:00
|
|
|
let promise = fn(...args)
|
2018-01-11 14:11:49 +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
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.sequence = function sequence(tasks, fn) {
|
2018-01-11 14:11:49 +00:00
|
|
|
return tasks.reduce(
|
|
|
|
(promise, task) => promise.then(() => fn(task)),
|
|
|
|
Promise.resolve()
|
|
|
|
)
|
2017-05-11 11:23:58 +00:00
|
|
|
}
|
2017-05-12 07:57:24 +00:00
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.parallel = function parallel(tasks, fn) {
|
2017-06-15 22:19:53 +00:00
|
|
|
return Promise.all(tasks.map(task => fn(task)))
|
|
|
|
}
|
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.chainFn = 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
|
|
|
|
}
|
2017-08-22 22:50:27 +00:00
|
|
|
return function () {
|
|
|
|
if (typeof base !== 'function') {
|
|
|
|
return fn.apply(this, arguments)
|
|
|
|
}
|
|
|
|
let baseResult = base.apply(this, arguments)
|
|
|
|
// Allow function to mutate the first argument instead of returning the result
|
|
|
|
if (baseResult === undefined) {
|
|
|
|
baseResult = arguments[0]
|
|
|
|
}
|
2018-01-11 14:11:49 +00:00
|
|
|
let fnResult = fn.call(
|
|
|
|
this,
|
|
|
|
baseResult,
|
|
|
|
...Array.prototype.slice.call(arguments, 1)
|
|
|
|
)
|
2017-08-22 22:50:27 +00:00
|
|
|
// Return mutated argument if no result was returned
|
|
|
|
if (fnResult === undefined) {
|
|
|
|
return baseResult
|
|
|
|
}
|
|
|
|
return fnResult
|
2017-05-12 07:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-18 08:44:31 +00:00
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.isPureObject = function isPureObject(o) {
|
2017-08-15 01:06:56 +00:00
|
|
|
return !Array.isArray(o) && typeof o === 'object'
|
|
|
|
}
|
|
|
|
|
2018-01-11 14:11:49 +00:00
|
|
|
const isWindows = (exports.isWindows = /^win/.test(process.platform))
|
2017-08-17 17:18:56 +00:00
|
|
|
|
2018-01-11 14:11:49 +00:00
|
|
|
const wp = (exports.wp = function wp(p = '') {
|
2017-05-18 08:44:31 +00:00
|
|
|
/* istanbul ignore if */
|
2017-08-17 17:18:56 +00:00
|
|
|
if (isWindows) {
|
|
|
|
return p.replace(/\\/g, '\\\\')
|
|
|
|
}
|
|
|
|
return p
|
2018-01-11 14:11:49 +00:00
|
|
|
})
|
2017-08-17 17:18:56 +00:00
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.wChunk = function wChunk(p = '') {
|
2017-08-17 17:18:56 +00:00
|
|
|
/* istanbul ignore if */
|
|
|
|
if (isWindows) {
|
2017-11-21 21:34:17 +00:00
|
|
|
return p.replace(/\//g, '_')
|
2017-05-18 08:44:31 +00:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
const reqSep = /\//g
|
|
|
|
const sysSep = _.escapeRegExp(sep)
|
|
|
|
const normalize = string => string.replace(reqSep, sysSep)
|
|
|
|
|
2018-01-11 14:11:49 +00:00
|
|
|
const r = (exports.r = 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-12-03 20:37:03 +00:00
|
|
|
if (lastArg.indexOf('@') === 0 || lastArg.indexOf('~') === 0) {
|
2017-06-29 16:36:22 +00:00
|
|
|
return wp(lastArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wp(resolve(...args.map(normalize)))
|
2018-01-11 14:11:49 +00:00
|
|
|
})
|
2017-06-29 16:36:22 +00:00
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.relativeTo = function relativeTo() {
|
2017-06-29 16:36:22 +00:00
|
|
|
let args = Array.prototype.slice.apply(arguments)
|
|
|
|
let dir = args.shift()
|
|
|
|
|
|
|
|
// Resolve path
|
|
|
|
let path = r(...args)
|
|
|
|
|
|
|
|
// Check if path is an alias
|
2017-12-03 20:37:03 +00:00
|
|
|
if (path.indexOf('@') === 0 || path.indexOf('~') === 0) {
|
2017-06-29 16:36:22 +00:00
|
|
|
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
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.flatRoutes = function flatRoutes(router, path = '', routes = []) {
|
2018-01-11 14:11:49 +00:00
|
|
|
router.forEach(r => {
|
2017-06-14 16:13:43 +00:00
|
|
|
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) {
|
2017-12-02 13:45:55 +00:00
|
|
|
if (path === '' && r.path === '/') {
|
|
|
|
routes.push('/')
|
|
|
|
}
|
2017-06-14 16:13:43 +00:00
|
|
|
flatRoutes(r.children, path + r.path + '/', routes)
|
|
|
|
} else {
|
2017-12-02 13:45:55 +00:00
|
|
|
path = path.replace(/^\/+$/, '/')
|
2018-01-11 14:11:49 +00:00
|
|
|
routes.push(
|
|
|
|
(r.path === '' && path[path.length - 1] === '/'
|
|
|
|
? path.slice(0, -1)
|
|
|
|
: path) + r.path
|
|
|
|
)
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
function cleanChildrenRoutes(routes, isChild = false) {
|
2017-06-14 16:13:43 +00:00
|
|
|
let start = -1
|
|
|
|
let routesIndex = []
|
2018-01-11 14:11:49 +00:00
|
|
|
routes.forEach(route => {
|
2017-06-14 16:13:43 +00:00
|
|
|
if (/-index$/.test(route.name) || route.name === 'index') {
|
|
|
|
// Save indexOf 'index' key in name
|
|
|
|
let res = route.name.split('-')
|
|
|
|
let 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-01-11 14:11:49 +00:00
|
|
|
routes.forEach(route => {
|
|
|
|
route.path = isChild ? route.path.replace('/', '') : route.path
|
2017-06-14 16:13:43 +00:00
|
|
|
if (route.path.indexOf('?') > -1) {
|
|
|
|
let names = route.name.split('-')
|
|
|
|
let paths = route.path.split('/')
|
|
|
|
if (!isChild) {
|
|
|
|
paths.shift()
|
|
|
|
} // clean first / for parents
|
2018-01-11 14:11:49 +00:00
|
|
|
routesIndex.forEach(r => {
|
2017-06-14 16:13:43 +00:00
|
|
|
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) {
|
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
|
|
|
|
}
|
|
|
|
route.children = cleanChildrenRoutes(route.children, true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
exports.createRoutes = function createRoutes(files, srcDir) {
|
2017-06-14 16:13:43 +00:00
|
|
|
let routes = []
|
2018-01-11 14:11:49 +00:00
|
|
|
files.forEach(file => {
|
|
|
|
let keys = file
|
|
|
|
.replace(/^pages/, '')
|
|
|
|
.replace(/\.(vue|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) => {
|
2018-01-11 14:11:49 +00:00
|
|
|
route.name = route.name
|
|
|
|
? route.name + '-' + key.replace('_', '')
|
|
|
|
: key.replace('_', '')
|
|
|
|
route.name += key === '_' ? 'all' : ''
|
2017-12-08 09:32:55 +00:00
|
|
|
route.chunkName = file.replace(/\.(vue|js)$/, '')
|
2017-06-14 16:13:43 +00:00
|
|
|
let child = _.find(parent, { name: route.name })
|
|
|
|
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 = ''
|
|
|
|
} else {
|
2018-01-11 14:11:49 +00:00
|
|
|
if (key === 'index' && i + 1 === keys.length) {
|
|
|
|
route.path += i > 0 ? '' : '/'
|
2017-06-14 16:13:43 +00:00
|
|
|
} else {
|
|
|
|
route.path += '/' + (key === '_' ? '*' : key.replace('_', ':'))
|
|
|
|
if (key !== '_' && key.indexOf('_') !== -1) {
|
|
|
|
route.path += '?'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Order Routes path
|
|
|
|
parent.push(route)
|
|
|
|
parent.sort((a, b) => {
|
2018-01-23 07:20:56 +00:00
|
|
|
if (!a.path.length) {
|
2017-06-14 16:13:43 +00:00
|
|
|
return -1
|
|
|
|
}
|
2018-01-23 07:20:56 +00:00
|
|
|
if (!b.path.length) {
|
2017-06-14 16:13:43 +00:00
|
|
|
return 1
|
|
|
|
}
|
2018-01-23 07:20:56 +00:00
|
|
|
// Order: /static, /index, /:dynamic
|
|
|
|
// Match exact route before index: /login before /index/_slug
|
|
|
|
if (a.path === '/') {
|
|
|
|
return /^\/(:|\*)/.test(b.path) ? -1 : 1
|
|
|
|
}
|
|
|
|
if (b.path === '/') {
|
|
|
|
return /^\/(:|\*)/.test(a.path) ? 1 : -1
|
|
|
|
}
|
2017-08-14 12:01:10 +00:00
|
|
|
let i = 0
|
2017-06-14 16:13:43 +00:00
|
|
|
let res = 0
|
2017-08-14 12:01:10 +00:00
|
|
|
let y = 0
|
|
|
|
let z = 0
|
|
|
|
const _a = a.path.split('/')
|
|
|
|
const _b = b.path.split('/')
|
|
|
|
for (i = 0; i < _a.length; i++) {
|
2017-06-14 16:13:43 +00:00
|
|
|
if (res !== 0) {
|
|
|
|
break
|
|
|
|
}
|
2018-01-11 14:11:49 +00:00
|
|
|
y = _a[i] === '*' ? 2 : _a[i].indexOf(':') > -1 ? 1 : 0
|
|
|
|
z = _b[i] === '*' ? 2 : _b[i].indexOf(':') > -1 ? 1 : 0
|
2017-06-14 16:13:43 +00:00
|
|
|
res = y - z
|
2017-08-14 12:01:10 +00:00
|
|
|
// If a.length >= b.length
|
2017-06-14 16:13:43 +00:00
|
|
|
if (i === _b.length - 1 && res === 0) {
|
2017-08-14 12:01:10 +00:00
|
|
|
// change order if * found
|
|
|
|
res = _a[i] === '*' ? -1 : 1
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-14 12:01:10 +00:00
|
|
|
return res === 0 ? (_a[i - 1] === '*' && _b[i] ? 1 : -1) : res
|
2017-06-14 16:13:43 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
return cleanChildrenRoutes(routes)
|
|
|
|
}
|