2018-03-16 19:11:24 +00:00
|
|
|
import path from 'path'
|
2018-10-18 15:43:44 +00:00
|
|
|
import escapeRegExp from 'lodash/escapeRegExp'
|
|
|
|
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-03-16 16:12:06 +00:00
|
|
|
export const 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
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const getContext = function getContext(req, res) {
|
2017-01-11 19:14:59 +00:00
|
|
|
return { req, res }
|
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const 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
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
export const isString = obj => typeof obj === 'string' || obj instanceof String
|
|
|
|
|
|
|
|
export const isNonEmptyString = obj => obj && isString(obj)
|
|
|
|
|
2018-10-12 22:31:19 +00:00
|
|
|
export const startsWithAlias = aliasArray => str => aliasArray.some(c => str.startsWith(c))
|
|
|
|
|
|
|
|
export const startsWithSrcAlias = startsWithAlias(['@', '~'])
|
|
|
|
|
|
|
|
export const startsWithRootAlias = startsWithAlias(['@@', '~~'])
|
|
|
|
|
2018-03-14 08:18:40 +00:00
|
|
|
async function promiseFinally(fn, finalFn) {
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
if (typeof fn === 'function') {
|
|
|
|
result = await fn()
|
|
|
|
} else {
|
|
|
|
result = await fn
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
finalFn()
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const timeout = function timeout(fn, ms, msg) {
|
2018-03-14 08:18:40 +00:00
|
|
|
let timerId
|
|
|
|
const warpPromise = promiseFinally(fn, () => clearTimeout(timerId))
|
|
|
|
const timerPromise = new Promise((resolve, reject) => {
|
|
|
|
timerId = setTimeout(() => reject(new Error(msg)), ms)
|
|
|
|
})
|
|
|
|
return Promise.race([warpPromise, timerPromise])
|
|
|
|
}
|
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const 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
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const isUrl = function isUrl(url) {
|
2018-10-12 22:31:19 +00:00
|
|
|
return ['http', '//'].some(str => url.startsWith(str))
|
2017-03-16 17:52:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const 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
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const 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
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const parallel = function parallel(tasks, fn) {
|
2018-07-20 15:42:31 +00:00
|
|
|
return Promise.all(tasks.map(fn))
|
2017-06-15 22:19:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const chainFn = function chainFn(base, fn) {
|
2017-05-14 23:01:41 +00:00
|
|
|
/* istanbul ignore if */
|
2018-03-18 23:14:27 +00:00
|
|
|
if (typeof fn !== 'function') {
|
|
|
|
return base
|
2017-05-12 07:57:24 +00:00
|
|
|
}
|
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-08-08 10:54:05 +00:00
|
|
|
const fnResult = fn.call(
|
2018-01-11 14:11:49 +00:00
|
|
|
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
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const isPureObject = function isPureObject(o) {
|
2017-08-15 01:06:56 +00:00
|
|
|
return !Array.isArray(o) && typeof o === 'object'
|
|
|
|
}
|
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const isWindows = /^win/.test(process.platform)
|
2017-08-17 17:18:56 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const 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-03-16 16:12:06 +00:00
|
|
|
}
|
2017-08-17 17:18:56 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const 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
|
2018-10-18 15:43:44 +00:00
|
|
|
const sysSep = escapeRegExp(path.sep)
|
2017-05-18 08:44:31 +00:00
|
|
|
const normalize = string => string.replace(reqSep, sysSep)
|
|
|
|
|
2018-10-12 22:31:19 +00:00
|
|
|
export const r = function r(...args) {
|
|
|
|
const lastArg = args[args.length - 1]
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2018-10-12 22:31:19 +00:00
|
|
|
if (startsWithSrcAlias(lastArg)) {
|
2017-06-29 16:36:22 +00:00
|
|
|
return wp(lastArg)
|
|
|
|
}
|
|
|
|
|
2018-03-16 19:11:24 +00:00
|
|
|
return wp(path.resolve(...args.map(normalize)))
|
2018-03-16 16:12:06 +00:00
|
|
|
}
|
2017-06-29 16:36:22 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const relativeTo = function relativeTo() {
|
2018-08-08 10:54:05 +00:00
|
|
|
const args = Array.prototype.slice.apply(arguments)
|
|
|
|
const dir = args.shift()
|
2017-06-29 16:36:22 +00:00
|
|
|
|
2018-05-09 15:49:21 +00:00
|
|
|
// Keep webpack inline loader intact
|
2018-10-12 22:31:19 +00:00
|
|
|
if (args[0].includes('!')) {
|
2018-05-09 15:49:21 +00:00
|
|
|
const loaders = args.shift().split('!')
|
|
|
|
|
|
|
|
return loaders.concat(relativeTo(dir, loaders.pop(), ...args)).join('!')
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:36:22 +00:00
|
|
|
// Resolve path
|
2018-08-08 10:54:05 +00:00
|
|
|
const _path = r(...args)
|
2017-06-29 16:36:22 +00:00
|
|
|
|
|
|
|
// Check if path is an alias
|
2018-10-12 22:31:19 +00:00
|
|
|
if (startsWithSrcAlias(_path)) {
|
2018-03-16 19:11:24 +00:00
|
|
|
return _path
|
2017-06-29 16:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make correct relative path
|
2018-03-16 19:11:24 +00:00
|
|
|
let rp = path.relative(dir, _path)
|
2017-06-29 16:36:22 +00:00
|
|
|
if (rp[0] !== '.') {
|
|
|
|
rp = './' + rp
|
|
|
|
}
|
2018-03-16 19:11:24 +00:00
|
|
|
|
2017-06-29 16:36:22 +00:00
|
|
|
return wp(rp)
|
2017-05-18 08:44:31 +00:00
|
|
|
}
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2018-03-16 19:11:24 +00:00
|
|
|
export const flatRoutes = function flatRoutes(router, _path = '', 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
|
|
|
|
}
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (r.children) {
|
|
|
|
if (_path === '' && r.path === '/') {
|
|
|
|
routes.push('/')
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
2018-10-12 22:31:19 +00:00
|
|
|
return flatRoutes(r.children, _path + r.path + '/', routes)
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
2018-10-12 22:31:19 +00:00
|
|
|
_path = _path.replace(/^\/+$/, '/')
|
|
|
|
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
|
2018-08-08 10:54:05 +00:00
|
|
|
const routesIndex = []
|
2018-08-06 00:12:44 +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
|
2018-08-08 10:54:05 +00:00
|
|
|
const res = route.name.split('-')
|
|
|
|
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-08-08 10:54:05 +00:00
|
|
|
const names = route.name.split('-')
|
|
|
|
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('/')
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-12 06:31:49 +00:00
|
|
|
const DYNAMIC_ROUTE_REGEX = /^\/([:*])/
|
2018-11-14 16:50:06 +00:00
|
|
|
|
|
|
|
const sortRoutes = function sortRoutes(routes) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export const createRoutes = function createRoutes(files, srcDir, pagesDir) {
|
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-02-02 17:15:57 +00:00
|
|
|
.replace(RegExp(`^${pagesDir}`), '')
|
2018-01-11 14:11:49 +00:00
|
|
|
.replace(/\.(vue|js)$/, '')
|
|
|
|
.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-07-18 14:39:48 +00:00
|
|
|
? route.name + '-' + sanitizedKey
|
|
|
|
: sanitizedKey
|
2018-01-11 14:11:49 +00:00
|
|
|
route.name += key === '_' ? 'all' : ''
|
2017-12-08 09:32:55 +00:00
|
|
|
route.chunkName = file.replace(/\.(vue|js)$/, '')
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
parent.push(route)
|
|
|
|
})
|
2018-11-14 16:50:06 +00:00
|
|
|
|
|
|
|
sortRoutes(routes)
|
2017-06-14 16:13:43 +00:00
|
|
|
return cleanChildrenRoutes(routes)
|
|
|
|
}
|
2018-09-14 07:06:44 +00:00
|
|
|
|
|
|
|
// Guard dir1 from dir2 which can be indiscriminately removed
|
|
|
|
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
|
|
|
|
|
|
|
export const determineGlobals = function determineGlobals(globalName, globals) {
|
|
|
|
const _globals = {}
|
|
|
|
for (const global in globals) {
|
|
|
|
if (typeof globals[global] === 'function') {
|
|
|
|
_globals[global] = globals[global](globalName)
|
|
|
|
} else {
|
|
|
|
_globals[global] = globals[global]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _globals
|
|
|
|
}
|
2018-10-12 22:31:19 +00:00
|
|
|
|
|
|
|
const getRoutePathExtension = (key) => {
|
|
|
|
if (key === '_') {
|
|
|
|
return '*'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.startsWith('_')) {
|
|
|
|
return `:${key.substr(1)}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps value in array if it is not already an array
|
|
|
|
*
|
|
|
|
* @param {any} value
|
|
|
|
* @return {array}
|
|
|
|
*/
|
|
|
|
export const wrapArray = value => Array.isArray(value) ? value : [value]
|
2018-10-24 13:46:06 +00:00
|
|
|
|
|
|
|
const WHITESPACE_REPLACEMENTS = [
|
|
|
|
[/[ \t\f\r]+\n/g, '\n'], // strip empty indents
|
|
|
|
[/{\n{2,}/g, '{\n'], // strip start padding from blocks
|
|
|
|
[/\n{2,}([ \t\f\r]*})/g, '\n$1'], // strip end padding from blocks
|
|
|
|
[/\n{3,}/g, '\n\n'], // strip multiple blank lines (1 allowed)
|
|
|
|
[/\n{2,}$/g, '\n'] // strip blank lines EOF (0 allowed)
|
|
|
|
]
|
|
|
|
|
|
|
|
export const stripWhitespace = function stripWhitespace(string) {
|
|
|
|
WHITESPACE_REPLACEMENTS.forEach(([regex, newSubstr]) => {
|
|
|
|
string = string.replace(regex, newSubstr)
|
|
|
|
})
|
|
|
|
return string
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
|
|
|
|
export function defineAlias(src, target, prop, opts = {}) {
|
|
|
|
const { bind = true, warn = false } = opts
|
|
|
|
|
|
|
|
if (Array.isArray(prop)) {
|
|
|
|
for (const p of prop) {
|
|
|
|
defineAlias(src, target, p, opts)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let targetVal = target[prop]
|
|
|
|
if (bind && typeof targetVal === 'function') {
|
|
|
|
targetVal = targetVal.bind(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
let warned = false
|
|
|
|
|
|
|
|
Object.defineProperty(src, prop, {
|
|
|
|
get: () => {
|
|
|
|
if (warn && !warned) {
|
|
|
|
warned = true
|
|
|
|
consola.warn({
|
|
|
|
message: `'${prop}' is deprecated'`,
|
|
|
|
additional: new Error().stack.split('\n').splice(2).join('\n')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return targetVal
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|