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-12-09 10:42:22 +00:00
|
|
|
import { showBanner, eventsMapping, formatPath } 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
|
|
|
|
},
|
2018-12-09 10:42:22 +00:00
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
async run(cmd) {
|
|
|
|
const argv = cmd.getArgv()
|
2018-12-09 10:42:22 +00:00
|
|
|
await this.startDev(cmd, argv)
|
|
|
|
},
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
async startDev(cmd, argv) {
|
|
|
|
try {
|
|
|
|
await this._startDev(cmd, argv)
|
|
|
|
} catch (error) {
|
|
|
|
consola.error(error)
|
2018-10-29 22:16:16 +00:00
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
},
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
async _startDev(cmd, argv) {
|
|
|
|
// Load config
|
|
|
|
const config = await cmd.getNuxtConfig(argv, { dev: true })
|
|
|
|
|
|
|
|
// Initialize nuxt instance
|
|
|
|
const nuxt = await cmd.getNuxt(config)
|
|
|
|
|
|
|
|
// Setup hooks
|
|
|
|
nuxt.hook('watch:restart', payload => this.onWatchRestart(payload, { nuxt, builder, cmd, argv }))
|
|
|
|
nuxt.hook('bundler:change', changedFileName => this.onBundlerChange(changedFileName))
|
|
|
|
|
|
|
|
// Start listening
|
|
|
|
await nuxt.server.listen()
|
|
|
|
|
|
|
|
// Create builder instance
|
|
|
|
const builder = await cmd.getBuilder(nuxt)
|
|
|
|
|
|
|
|
// Start Build
|
|
|
|
await builder.build()
|
|
|
|
|
|
|
|
// Show banner after build
|
|
|
|
showBanner(nuxt)
|
|
|
|
|
|
|
|
// Return instance
|
|
|
|
return nuxt
|
|
|
|
},
|
|
|
|
|
|
|
|
logChanged({ event, path }) {
|
|
|
|
const { icon, color, action } = eventsMapping[event] || eventsMapping.change
|
2018-12-09 11:47:57 +00:00
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
consola.log({
|
|
|
|
type: event,
|
|
|
|
icon: chalk[color].bold(icon),
|
2018-12-09 11:47:57 +00:00
|
|
|
message: `${action} ${chalk.cyan(formatPath(path))}`
|
2018-12-09 10:42:22 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
async onWatchRestart({ event, path }, { nuxt, cmd, argv }) {
|
2018-12-09 11:47:57 +00:00
|
|
|
this.logChanged({ event, path })
|
2018-12-09 10:42:22 +00:00
|
|
|
|
|
|
|
await nuxt.close()
|
|
|
|
|
|
|
|
await this.startDev(cmd, argv)
|
|
|
|
},
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2018-12-09 11:47:57 +00:00
|
|
|
onBundlerChange(path) {
|
|
|
|
this.logChanged({ event: 'change', path })
|
2018-10-18 15:57:35 +00:00
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|