mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 22:25:12 +00:00
use pretty error handler
This commit is contained in:
parent
83236d82ce
commit
771945c789
16
bin/nuxt
16
bin/nuxt
@ -1,16 +1,14 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
const { join } = require('path')
|
||||||
|
const { logError } = require('../lib/common/utils')
|
||||||
|
|
||||||
const join = require('path').join
|
// Global error handler
|
||||||
require('../lib/common/cli/errors')
|
process.on('unhandledRejection', _error => {
|
||||||
|
logError(_error) // eslint-disable no-console
|
||||||
|
})
|
||||||
|
|
||||||
const defaultCommand = 'dev'
|
const defaultCommand = 'dev'
|
||||||
const commands = new Set([
|
const commands = new Set([defaultCommand, 'init', 'build', 'start', 'generate'])
|
||||||
defaultCommand,
|
|
||||||
'init',
|
|
||||||
'build',
|
|
||||||
'start',
|
|
||||||
'generate'
|
|
||||||
])
|
|
||||||
|
|
||||||
var cmd = process.argv[2]
|
var cmd = process.argv[2]
|
||||||
|
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
const PrettyError = require('pretty-error')
|
|
||||||
|
|
||||||
// Start default instance
|
|
||||||
const pe = new PrettyError()
|
|
||||||
|
|
||||||
// Console error unhandled promises
|
|
||||||
process.on('unhandledRejection', function (err) {
|
|
||||||
/* eslint-disable no-console */
|
|
||||||
console.log(pe.render(err))
|
|
||||||
})
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
pe
|
|
||||||
}
|
|
@ -1,5 +1,13 @@
|
|||||||
const { resolve, relative, sep } = require('path')
|
const { resolve, relative, sep } = require('path')
|
||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
|
const PrettyError = require('pretty-error')
|
||||||
|
|
||||||
|
const pe = new PrettyError()
|
||||||
|
|
||||||
|
exports.logError = function (_error) {
|
||||||
|
/* eslint-disable no-console */
|
||||||
|
console.log(pe.render(_error))
|
||||||
|
}
|
||||||
|
|
||||||
exports.encodeHtml = function encodeHtml(str) {
|
exports.encodeHtml = function encodeHtml(str) {
|
||||||
return str.replace(/</g, '<').replace(/>/g, '>')
|
return str.replace(/</g, '<').replace(/>/g, '>')
|
||||||
@ -23,15 +31,19 @@ exports.setAnsiColors = function setAnsiColors(ansiHTML) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.waitFor = async function waitFor(ms) {
|
exports.waitFor = async function waitFor(ms) {
|
||||||
return new Promise(resolve => setTimeout(resolve, (ms || 0)))
|
return new Promise(resolve => setTimeout(resolve, ms || 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.urlJoin = function urlJoin() {
|
exports.urlJoin = function urlJoin() {
|
||||||
return [].slice.call(arguments).join('/').replace(/\/+/g, '/').replace(':/', '://')
|
return [].slice
|
||||||
|
.call(arguments)
|
||||||
|
.join('/')
|
||||||
|
.replace(/\/+/g, '/')
|
||||||
|
.replace(':/', '://')
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.isUrl = function isUrl(url) {
|
exports.isUrl = function isUrl(url) {
|
||||||
return (url.indexOf('http') === 0 || url.indexOf('//') === 0)
|
return url.indexOf('http') === 0 || url.indexOf('//') === 0
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.promisifyRoute = function promisifyRoute(fn, ...args) {
|
exports.promisifyRoute = function promisifyRoute(fn, ...args) {
|
||||||
@ -51,14 +63,20 @@ exports.promisifyRoute = function promisifyRoute(fn, ...args) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
let promise = fn(...args)
|
let promise = fn(...args)
|
||||||
if (!promise || (!(promise instanceof Promise) && (typeof promise.then !== 'function'))) {
|
if (
|
||||||
|
!promise ||
|
||||||
|
(!(promise instanceof Promise) && typeof promise.then !== 'function')
|
||||||
|
) {
|
||||||
promise = Promise.resolve(promise)
|
promise = Promise.resolve(promise)
|
||||||
}
|
}
|
||||||
return promise
|
return promise
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.sequence = function sequence(tasks, fn) {
|
exports.sequence = function sequence(tasks, fn) {
|
||||||
return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve())
|
return tasks.reduce(
|
||||||
|
(promise, task) => promise.then(() => fn(task)),
|
||||||
|
Promise.resolve()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.parallel = function parallel(tasks, fn) {
|
exports.parallel = function parallel(tasks, fn) {
|
||||||
@ -79,7 +97,11 @@ exports.chainFn = function chainFn(base, fn) {
|
|||||||
if (baseResult === undefined) {
|
if (baseResult === undefined) {
|
||||||
baseResult = arguments[0]
|
baseResult = arguments[0]
|
||||||
}
|
}
|
||||||
let fnResult = fn.call(this, baseResult, ...Array.prototype.slice.call(arguments, 1))
|
let fnResult = fn.call(
|
||||||
|
this,
|
||||||
|
baseResult,
|
||||||
|
...Array.prototype.slice.call(arguments, 1)
|
||||||
|
)
|
||||||
// Return mutated argument if no result was returned
|
// Return mutated argument if no result was returned
|
||||||
if (fnResult === undefined) {
|
if (fnResult === undefined) {
|
||||||
return baseResult
|
return baseResult
|
||||||
@ -92,15 +114,15 @@ exports.isPureObject = function isPureObject(o) {
|
|||||||
return !Array.isArray(o) && typeof o === 'object'
|
return !Array.isArray(o) && typeof o === 'object'
|
||||||
}
|
}
|
||||||
|
|
||||||
const isWindows = exports.isWindows = /^win/.test(process.platform)
|
const isWindows = (exports.isWindows = /^win/.test(process.platform))
|
||||||
|
|
||||||
const wp = exports.wp = function wp(p = '') {
|
const wp = (exports.wp = function wp(p = '') {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (isWindows) {
|
if (isWindows) {
|
||||||
return p.replace(/\\/g, '\\\\')
|
return p.replace(/\\/g, '\\\\')
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
}
|
})
|
||||||
|
|
||||||
exports.wChunk = function wChunk(p = '') {
|
exports.wChunk = function wChunk(p = '') {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
@ -114,7 +136,7 @@ const reqSep = /\//g
|
|||||||
const sysSep = _.escapeRegExp(sep)
|
const sysSep = _.escapeRegExp(sep)
|
||||||
const normalize = string => string.replace(reqSep, sysSep)
|
const normalize = string => string.replace(reqSep, sysSep)
|
||||||
|
|
||||||
const r = exports.r = function r() {
|
const r = (exports.r = function r() {
|
||||||
let args = Array.prototype.slice.apply(arguments)
|
let args = Array.prototype.slice.apply(arguments)
|
||||||
let lastArg = _.last(args)
|
let lastArg = _.last(args)
|
||||||
|
|
||||||
@ -123,7 +145,7 @@ const r = exports.r = function r() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return wp(resolve(...args.map(normalize)))
|
return wp(resolve(...args.map(normalize)))
|
||||||
}
|
})
|
||||||
|
|
||||||
exports.relativeTo = function relativeTo() {
|
exports.relativeTo = function relativeTo() {
|
||||||
let args = Array.prototype.slice.apply(arguments)
|
let args = Array.prototype.slice.apply(arguments)
|
||||||
@ -146,7 +168,7 @@ exports.relativeTo = function relativeTo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.flatRoutes = function flatRoutes(router, path = '', routes = []) {
|
exports.flatRoutes = function flatRoutes(router, path = '', routes = []) {
|
||||||
router.forEach((r) => {
|
router.forEach(r => {
|
||||||
if (!r.path.includes(':') && !r.path.includes('*')) {
|
if (!r.path.includes(':') && !r.path.includes('*')) {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (r.children) {
|
if (r.children) {
|
||||||
@ -156,7 +178,11 @@ exports.flatRoutes = function flatRoutes(router, path = '', routes = []) {
|
|||||||
flatRoutes(r.children, path + r.path + '/', routes)
|
flatRoutes(r.children, path + r.path + '/', routes)
|
||||||
} else {
|
} else {
|
||||||
path = path.replace(/^\/+$/, '/')
|
path = path.replace(/^\/+$/, '/')
|
||||||
routes.push((r.path === '' && path[path.length - 1] === '/' ? path.slice(0, -1) : path) + r.path)
|
routes.push(
|
||||||
|
(r.path === '' && path[path.length - 1] === '/'
|
||||||
|
? path.slice(0, -1)
|
||||||
|
: path) + r.path
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -166,24 +192,24 @@ exports.flatRoutes = function flatRoutes(router, path = '', routes = []) {
|
|||||||
function cleanChildrenRoutes(routes, isChild = false) {
|
function cleanChildrenRoutes(routes, isChild = false) {
|
||||||
let start = -1
|
let start = -1
|
||||||
let routesIndex = []
|
let routesIndex = []
|
||||||
routes.forEach((route) => {
|
routes.forEach(route => {
|
||||||
if (/-index$/.test(route.name) || route.name === 'index') {
|
if (/-index$/.test(route.name) || route.name === 'index') {
|
||||||
// Save indexOf 'index' key in name
|
// Save indexOf 'index' key in name
|
||||||
let res = route.name.split('-')
|
let res = route.name.split('-')
|
||||||
let s = res.indexOf('index')
|
let s = res.indexOf('index')
|
||||||
start = (start === -1 || s < start) ? s : start
|
start = start === -1 || s < start ? s : start
|
||||||
routesIndex.push(res)
|
routesIndex.push(res)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
routes.forEach((route) => {
|
routes.forEach(route => {
|
||||||
route.path = (isChild) ? route.path.replace('/', '') : route.path
|
route.path = isChild ? route.path.replace('/', '') : route.path
|
||||||
if (route.path.indexOf('?') > -1) {
|
if (route.path.indexOf('?') > -1) {
|
||||||
let names = route.name.split('-')
|
let names = route.name.split('-')
|
||||||
let paths = route.path.split('/')
|
let paths = route.path.split('/')
|
||||||
if (!isChild) {
|
if (!isChild) {
|
||||||
paths.shift()
|
paths.shift()
|
||||||
} // clean first / for parents
|
} // clean first / for parents
|
||||||
routesIndex.forEach((r) => {
|
routesIndex.forEach(r => {
|
||||||
let i = r.indexOf('index') - start // children names
|
let i = r.indexOf('index') - start // children names
|
||||||
if (i < paths.length) {
|
if (i < paths.length) {
|
||||||
for (let a = 0; a <= i; a++) {
|
for (let a = 0; a <= i; a++) {
|
||||||
@ -200,7 +226,7 @@ function cleanChildrenRoutes(routes, isChild = false) {
|
|||||||
}
|
}
|
||||||
route.name = route.name.replace(/-index$/, '')
|
route.name = route.name.replace(/-index$/, '')
|
||||||
if (route.children) {
|
if (route.children) {
|
||||||
if (route.children.find((child) => child.path === '')) {
|
if (route.children.find(child => child.path === '')) {
|
||||||
delete route.name
|
delete route.name
|
||||||
}
|
}
|
||||||
route.children = cleanChildrenRoutes(route.children, true)
|
route.children = cleanChildrenRoutes(route.children, true)
|
||||||
@ -211,13 +237,20 @@ function cleanChildrenRoutes(routes, isChild = false) {
|
|||||||
|
|
||||||
exports.createRoutes = function createRoutes(files, srcDir) {
|
exports.createRoutes = function createRoutes(files, srcDir) {
|
||||||
let routes = []
|
let routes = []
|
||||||
files.forEach((file) => {
|
files.forEach(file => {
|
||||||
let keys = file.replace(/^pages/, '').replace(/\.(vue|js)$/, '').replace(/\/{2,}/g, '/').split('/').slice(1)
|
let keys = file
|
||||||
|
.replace(/^pages/, '')
|
||||||
|
.replace(/\.(vue|js)$/, '')
|
||||||
|
.replace(/\/{2,}/g, '/')
|
||||||
|
.split('/')
|
||||||
|
.slice(1)
|
||||||
let route = { name: '', path: '', component: r(srcDir, file) }
|
let route = { name: '', path: '', component: r(srcDir, file) }
|
||||||
let parent = routes
|
let parent = routes
|
||||||
keys.forEach((key, i) => {
|
keys.forEach((key, i) => {
|
||||||
route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '')
|
route.name = route.name
|
||||||
route.name += (key === '_') ? 'all' : ''
|
? route.name + '-' + key.replace('_', '')
|
||||||
|
: key.replace('_', '')
|
||||||
|
route.name += key === '_' ? 'all' : ''
|
||||||
route.chunkName = file.replace(/\.(vue|js)$/, '')
|
route.chunkName = file.replace(/\.(vue|js)$/, '')
|
||||||
let child = _.find(parent, { name: route.name })
|
let child = _.find(parent, { name: route.name })
|
||||||
if (child) {
|
if (child) {
|
||||||
@ -225,8 +258,8 @@ exports.createRoutes = function createRoutes(files, srcDir) {
|
|||||||
parent = child.children
|
parent = child.children
|
||||||
route.path = ''
|
route.path = ''
|
||||||
} else {
|
} else {
|
||||||
if (key === 'index' && (i + 1) === keys.length) {
|
if (key === 'index' && i + 1 === keys.length) {
|
||||||
route.path += (i > 0 ? '' : '/')
|
route.path += i > 0 ? '' : '/'
|
||||||
} else {
|
} else {
|
||||||
route.path += '/' + (key === '_' ? '*' : key.replace('_', ':'))
|
route.path += '/' + (key === '_' ? '*' : key.replace('_', ':'))
|
||||||
if (key !== '_' && key.indexOf('_') !== -1) {
|
if (key !== '_' && key.indexOf('_') !== -1) {
|
||||||
@ -254,8 +287,8 @@ exports.createRoutes = function createRoutes(files, srcDir) {
|
|||||||
if (res !== 0) {
|
if (res !== 0) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
y = _a[i] === '*' ? 2 : (_a[i].indexOf(':') > -1 ? 1 : 0)
|
y = _a[i] === '*' ? 2 : _a[i].indexOf(':') > -1 ? 1 : 0
|
||||||
z = _b[i] === '*' ? 2 : (_b[i].indexOf(':') > -1 ? 1 : 0)
|
z = _b[i] === '*' ? 2 : _b[i].indexOf(':') > -1 ? 1 : 0
|
||||||
res = y - z
|
res = y - z
|
||||||
// If a.length >= b.length
|
// If a.length >= b.length
|
||||||
if (i === _b.length - 1 && res === 0) {
|
if (i === _b.length - 1 && res === 0) {
|
||||||
|
@ -147,7 +147,7 @@ module.exports = class ModuleContainer {
|
|||||||
const cb = err => {
|
const cb = err => {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.warn(
|
console.warn(
|
||||||
'[Depricated] Consider using async/await for module handlers.' +
|
'[Depricated] Consider using async/await for module handlers. ' +
|
||||||
'Supporting callbacks will be removed in next releases: ' +
|
'Supporting callbacks will be removed in next releases: ' +
|
||||||
src
|
src
|
||||||
)
|
)
|
||||||
|
@ -9,6 +9,7 @@ const { join, resolve } = require('path')
|
|||||||
const { version } = require('../../package.json')
|
const { version } = require('../../package.json')
|
||||||
const ModuleContainer = require('./module')
|
const ModuleContainer = require('./module')
|
||||||
const Renderer = require('./renderer')
|
const Renderer = require('./renderer')
|
||||||
|
const { logError } = require('../common/utils')
|
||||||
|
|
||||||
const debug = Debug('nuxt:')
|
const debug = Debug('nuxt:')
|
||||||
debug.color = 5
|
debug.color = 5
|
||||||
@ -166,24 +167,23 @@ module.exports = class Nuxt {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
errorHandler() {
|
errorHandler(_error) {
|
||||||
// Apply plugins
|
// Call error hook
|
||||||
// eslint-disable-next-line no-console
|
this.callHook('error', _error)
|
||||||
this.callHook('error', ...arguments).catch(console.error)
|
|
||||||
|
|
||||||
// Silent
|
// Silent when errorHandler is set to false
|
||||||
if (this.options.errorHandler === false) {
|
if (this.options.errorHandler === false) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Custom errorHandler
|
// Custom errorHandler
|
||||||
if (typeof this.options.errorHandler === 'function') {
|
if (typeof this.options.errorHandler === 'function') {
|
||||||
return this.options.errorHandler.apply(this, arguments)
|
this.options.errorHandler(_error)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default handler
|
// Default handler
|
||||||
// eslint-disable-next-line no-console
|
logError(_error)
|
||||||
console.error(...arguments)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resolvePath(path) {
|
resolvePath(path) {
|
||||||
|
Loading…
Reference in New Issue
Block a user