2018-10-17 21:28:25 +00:00
|
|
|
import consola from 'consola'
|
2018-11-08 09:15:56 +00:00
|
|
|
import chalk from 'chalk'
|
2018-10-29 22:16:16 +00:00
|
|
|
import { common, server } from '../options'
|
2018-11-08 09:15:56 +00:00
|
|
|
import { showBanner } from '../utils'
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
export default {
|
|
|
|
name: 'dev',
|
|
|
|
description: 'Start the application in development mode (e.g. hot-code reloading, error reporting)',
|
|
|
|
usage: 'dev <dir>',
|
|
|
|
options: {
|
|
|
|
...common,
|
|
|
|
...server
|
|
|
|
},
|
|
|
|
async run(cmd) {
|
|
|
|
const argv = cmd.getArgv()
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
const errorHandler = (err, instance) => {
|
|
|
|
instance && instance.builder.watchServer()
|
|
|
|
consola.error(err)
|
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
// Start dev
|
|
|
|
async function startDev(oldInstance) {
|
|
|
|
let nuxt, builder
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
try {
|
|
|
|
nuxt = await cmd.getNuxt(
|
|
|
|
await cmd.getNuxtConfig(argv, { dev: true })
|
|
|
|
)
|
|
|
|
builder = await cmd.getBuilder(nuxt)
|
|
|
|
} catch (err) {
|
|
|
|
return errorHandler(err, oldInstance)
|
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2018-11-08 09:15:56 +00:00
|
|
|
const logChanged = (name) => {
|
|
|
|
consola.log({
|
|
|
|
type: 'change',
|
|
|
|
icon: chalk.blue.bold('↻'),
|
|
|
|
message: chalk.blue(name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
nuxt.hook('watch:fileChanged', async (builder, name) => {
|
|
|
|
logChanged(name)
|
|
|
|
await startDev({ nuxt: builder.nuxt, builder })
|
|
|
|
})
|
|
|
|
|
|
|
|
nuxt.hook('bundler:change', (name) => {
|
|
|
|
logChanged(name)
|
|
|
|
})
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
return (
|
|
|
|
Promise.resolve()
|
|
|
|
.then(() => oldInstance && oldInstance.nuxt.clearHook('watch:fileChanged'))
|
|
|
|
.then(() => oldInstance && oldInstance.builder.unwatch())
|
|
|
|
// Start build
|
|
|
|
.then(() => builder.build())
|
|
|
|
// Close old nuxt no matter if build successfully
|
|
|
|
.catch((err) => {
|
|
|
|
oldInstance && oldInstance.nuxt.close()
|
|
|
|
// Jump to errorHandler
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
.then(() => oldInstance && oldInstance.nuxt.close())
|
|
|
|
// Start listening
|
2018-10-30 20:42:53 +00:00
|
|
|
.then(() => nuxt.server.listen())
|
2018-11-08 09:15:56 +00:00
|
|
|
// Show banner
|
|
|
|
.then(() => showBanner(nuxt))
|
|
|
|
// Start watching serverMiddleware changes
|
2018-10-29 22:16:16 +00:00
|
|
|
.then(() => builder.watchServer())
|
|
|
|
// Handle errors
|
|
|
|
.catch(err => errorHandler(err, { builder, nuxt }))
|
2018-10-25 11:22:31 +00:00
|
|
|
)
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await startDev()
|
2018-10-18 15:57:35 +00:00
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|