mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +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
|
||||
const { join } = require('path')
|
||||
const { logError } = require('../lib/common/utils')
|
||||
|
||||
const join = require('path').join
|
||||
require('../lib/common/cli/errors')
|
||||
// Global error handler
|
||||
process.on('unhandledRejection', _error => {
|
||||
logError(_error) // eslint-disable no-console
|
||||
})
|
||||
|
||||
const defaultCommand = 'dev'
|
||||
const commands = new Set([
|
||||
defaultCommand,
|
||||
'init',
|
||||
'build',
|
||||
'start',
|
||||
'generate'
|
||||
])
|
||||
const commands = new Set([defaultCommand, 'init', 'build', 'start', 'generate'])
|
||||
|
||||
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 _ = 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) {
|
||||
return str.replace(/</g, '<').replace(/>/g, '>')
|
||||
@ -23,15 +31,19 @@ exports.setAnsiColors = function setAnsiColors(ansiHTML) {
|
||||
}
|
||||
|
||||
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() {
|
||||
return [].slice.call(arguments).join('/').replace(/\/+/g, '/').replace(':/', '://')
|
||||
return [].slice
|
||||
.call(arguments)
|
||||
.join('/')
|
||||
.replace(/\/+/g, '/')
|
||||
.replace(':/', '://')
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -51,14 +63,20 @@ exports.promisifyRoute = function promisifyRoute(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)
|
||||
}
|
||||
return promise
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -79,7 +97,11 @@ exports.chainFn = function chainFn(base, fn) {
|
||||
if (baseResult === undefined) {
|
||||
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
|
||||
if (fnResult === undefined) {
|
||||
return baseResult
|
||||
@ -92,15 +114,15 @@ exports.isPureObject = function isPureObject(o) {
|
||||
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 */
|
||||
if (isWindows) {
|
||||
return p.replace(/\\/g, '\\\\')
|
||||
}
|
||||
return p
|
||||
}
|
||||
})
|
||||
|
||||
exports.wChunk = function wChunk(p = '') {
|
||||
/* istanbul ignore if */
|
||||
@ -114,7 +136,7 @@ const reqSep = /\//g
|
||||
const sysSep = _.escapeRegExp(sep)
|
||||
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 lastArg = _.last(args)
|
||||
|
||||
@ -123,7 +145,7 @@ const r = exports.r = function r() {
|
||||
}
|
||||
|
||||
return wp(resolve(...args.map(normalize)))
|
||||
}
|
||||
})
|
||||
|
||||
exports.relativeTo = function relativeTo() {
|
||||
let args = Array.prototype.slice.apply(arguments)
|
||||
@ -146,7 +168,7 @@ exports.relativeTo = function relativeTo() {
|
||||
}
|
||||
|
||||
exports.flatRoutes = function flatRoutes(router, path = '', routes = []) {
|
||||
router.forEach((r) => {
|
||||
router.forEach(r => {
|
||||
if (!r.path.includes(':') && !r.path.includes('*')) {
|
||||
/* istanbul ignore if */
|
||||
if (r.children) {
|
||||
@ -156,7 +178,11 @@ exports.flatRoutes = function flatRoutes(router, path = '', routes = []) {
|
||||
flatRoutes(r.children, path + r.path + '/', routes)
|
||||
} else {
|
||||
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) {
|
||||
let start = -1
|
||||
let routesIndex = []
|
||||
routes.forEach((route) => {
|
||||
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
|
||||
start = start === -1 || s < start ? s : start
|
||||
routesIndex.push(res)
|
||||
}
|
||||
})
|
||||
routes.forEach((route) => {
|
||||
route.path = (isChild) ? route.path.replace('/', '') : route.path
|
||||
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) => {
|
||||
routesIndex.forEach(r => {
|
||||
let i = r.indexOf('index') - start // children names
|
||||
if (i < paths.length) {
|
||||
for (let a = 0; a <= i; a++) {
|
||||
@ -200,7 +226,7 @@ function cleanChildrenRoutes(routes, isChild = false) {
|
||||
}
|
||||
route.name = route.name.replace(/-index$/, '')
|
||||
if (route.children) {
|
||||
if (route.children.find((child) => child.path === '')) {
|
||||
if (route.children.find(child => child.path === '')) {
|
||||
delete route.name
|
||||
}
|
||||
route.children = cleanChildrenRoutes(route.children, true)
|
||||
@ -211,13 +237,20 @@ function cleanChildrenRoutes(routes, isChild = false) {
|
||||
|
||||
exports.createRoutes = function createRoutes(files, srcDir) {
|
||||
let routes = []
|
||||
files.forEach((file) => {
|
||||
let keys = file.replace(/^pages/, '').replace(/\.(vue|js)$/, '').replace(/\/{2,}/g, '/').split('/').slice(1)
|
||||
files.forEach(file => {
|
||||
let keys = file
|
||||
.replace(/^pages/, '')
|
||||
.replace(/\.(vue|js)$/, '')
|
||||
.replace(/\/{2,}/g, '/')
|
||||
.split('/')
|
||||
.slice(1)
|
||||
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' : ''
|
||||
route.name = route.name
|
||||
? route.name + '-' + key.replace('_', '')
|
||||
: key.replace('_', '')
|
||||
route.name += key === '_' ? 'all' : ''
|
||||
route.chunkName = file.replace(/\.(vue|js)$/, '')
|
||||
let child = _.find(parent, { name: route.name })
|
||||
if (child) {
|
||||
@ -225,8 +258,8 @@ exports.createRoutes = function createRoutes(files, srcDir) {
|
||||
parent = child.children
|
||||
route.path = ''
|
||||
} else {
|
||||
if (key === 'index' && (i + 1) === keys.length) {
|
||||
route.path += (i > 0 ? '' : '/')
|
||||
if (key === 'index' && i + 1 === keys.length) {
|
||||
route.path += i > 0 ? '' : '/'
|
||||
} else {
|
||||
route.path += '/' + (key === '_' ? '*' : key.replace('_', ':'))
|
||||
if (key !== '_' && key.indexOf('_') !== -1) {
|
||||
@ -254,8 +287,8 @@ exports.createRoutes = function createRoutes(files, srcDir) {
|
||||
if (res !== 0) {
|
||||
break
|
||||
}
|
||||
y = _a[i] === '*' ? 2 : (_a[i].indexOf(':') > -1 ? 1 : 0)
|
||||
z = _b[i] === '*' ? 2 : (_b[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
|
||||
res = y - z
|
||||
// If a.length >= b.length
|
||||
if (i === _b.length - 1 && res === 0) {
|
||||
|
@ -147,7 +147,7 @@ module.exports = class ModuleContainer {
|
||||
const cb = err => {
|
||||
// eslint-disable-next-line no-console
|
||||
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: ' +
|
||||
src
|
||||
)
|
||||
|
@ -9,6 +9,7 @@ const { join, resolve } = require('path')
|
||||
const { version } = require('../../package.json')
|
||||
const ModuleContainer = require('./module')
|
||||
const Renderer = require('./renderer')
|
||||
const { logError } = require('../common/utils')
|
||||
|
||||
const debug = Debug('nuxt:')
|
||||
debug.color = 5
|
||||
@ -166,24 +167,23 @@ module.exports = class Nuxt {
|
||||
})
|
||||
}
|
||||
|
||||
errorHandler() {
|
||||
// Apply plugins
|
||||
// eslint-disable-next-line no-console
|
||||
this.callHook('error', ...arguments).catch(console.error)
|
||||
errorHandler(_error) {
|
||||
// Call error hook
|
||||
this.callHook('error', _error)
|
||||
|
||||
// Silent
|
||||
// Silent when errorHandler is set to false
|
||||
if (this.options.errorHandler === false) {
|
||||
return
|
||||
}
|
||||
|
||||
// Custom errorHandler
|
||||
if (typeof this.options.errorHandler === 'function') {
|
||||
return this.options.errorHandler.apply(this, arguments)
|
||||
this.options.errorHandler(_error)
|
||||
return
|
||||
}
|
||||
|
||||
// Default handler
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(...arguments)
|
||||
logError(_error)
|
||||
}
|
||||
|
||||
resolvePath(path) {
|
||||
|
Loading…
Reference in New Issue
Block a user