2018-10-17 21:28:25 +00:00
|
|
|
import consola from 'consola'
|
2018-11-08 09:15:56 +00:00
|
|
|
import chalk from 'chalk'
|
2019-02-05 21:37:59 +00:00
|
|
|
import opener from 'opener'
|
2018-10-29 22:16:16 +00:00
|
|
|
import { common, server } from '../options'
|
2019-04-11 10:04:21 +00:00
|
|
|
import { eventsMapping, formatPath } from '../utils'
|
|
|
|
import { showBanner } from '../utils/banner'
|
2019-04-12 17:19:46 +00:00
|
|
|
import { showMemoryUsage } from '../utils/memory'
|
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,
|
2019-02-05 21:37:59 +00:00
|
|
|
...server,
|
|
|
|
open: {
|
|
|
|
alias: 'o',
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'Opens the server listeners url in the default browser'
|
|
|
|
}
|
2018-10-29 22:16:16 +00:00
|
|
|
},
|
2018-12-09 10:42:22 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async run (cmd) {
|
2019-01-17 21:18:29 +00:00
|
|
|
const { argv } = cmd
|
2019-02-05 21:37:59 +00:00
|
|
|
|
2019-03-20 09:17:53 +00:00
|
|
|
await this.startDev(cmd, argv, argv.open)
|
2018-12-09 10:42:22 +00:00
|
|
|
},
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async startDev (cmd, argv) {
|
2019-09-29 08:11:32 +00:00
|
|
|
let nuxt
|
2018-12-09 10:42:22 +00:00
|
|
|
try {
|
2019-09-29 08:11:32 +00:00
|
|
|
nuxt = await this._listenDev(cmd, argv)
|
|
|
|
} catch (error) {
|
|
|
|
consola.fatal(error)
|
|
|
|
return
|
|
|
|
}
|
2019-02-05 21:37:59 +00:00
|
|
|
|
2019-09-29 08:11:32 +00:00
|
|
|
try {
|
|
|
|
await this._buildDev(cmd, argv, nuxt)
|
2018-12-09 10:42:22 +00:00
|
|
|
} catch (error) {
|
2019-09-29 08:11:32 +00:00
|
|
|
await nuxt.callHook('cli:buildError', error)
|
2018-12-09 10:42:22 +00:00
|
|
|
consola.error(error)
|
2018-10-29 22:16:16 +00:00
|
|
|
}
|
2019-09-29 08:11:32 +00:00
|
|
|
|
|
|
|
return nuxt
|
2018-12-09 10:42:22 +00:00
|
|
|
},
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2019-09-29 08:11:32 +00:00
|
|
|
async _listenDev (cmd, argv) {
|
2019-04-01 15:48:24 +00:00
|
|
|
const config = await cmd.getNuxtConfig({ dev: true, _build: true })
|
2018-12-09 10:42:22 +00:00
|
|
|
const nuxt = await cmd.getNuxt(config)
|
|
|
|
|
|
|
|
// Setup hooks
|
2019-05-10 13:03:07 +00:00
|
|
|
nuxt.hook('watch:restart', payload => this.onWatchRestart(payload, { nuxt, cmd, argv }))
|
2018-12-09 10:42:22 +00:00
|
|
|
nuxt.hook('bundler:change', changedFileName => this.onBundlerChange(changedFileName))
|
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
// Wait for nuxt to be ready
|
|
|
|
await nuxt.ready()
|
|
|
|
|
|
|
|
// Start listening
|
|
|
|
await nuxt.server.listen()
|
|
|
|
|
2019-03-20 09:17:53 +00:00
|
|
|
// Show banner when listening
|
2019-04-12 17:19:46 +00:00
|
|
|
showBanner(nuxt, false)
|
2019-03-20 09:17:53 +00:00
|
|
|
|
|
|
|
// Opens the server listeners url in the default browser (only once)
|
|
|
|
if (argv.open) {
|
|
|
|
argv.open = false
|
|
|
|
const openerPromises = nuxt.server.listeners.map(listener => opener(listener.url))
|
|
|
|
await Promise.all(openerPromises)
|
|
|
|
}
|
|
|
|
|
2019-09-29 08:11:32 +00:00
|
|
|
// Return instance
|
|
|
|
return nuxt
|
|
|
|
},
|
|
|
|
|
|
|
|
async _buildDev (cmd, argv, nuxt) {
|
2019-03-20 09:17:53 +00:00
|
|
|
// Create builder instance
|
|
|
|
const builder = await cmd.getBuilder(nuxt)
|
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
// Start Build
|
|
|
|
await builder.build()
|
|
|
|
|
2019-04-12 17:19:46 +00:00
|
|
|
// Print memory usage
|
|
|
|
showMemoryUsage()
|
|
|
|
|
2020-01-23 12:13:14 +00:00
|
|
|
// Display server urls after the build
|
|
|
|
for (const listener of nuxt.server.listeners) {
|
|
|
|
consola.info(chalk.bold('Listening on: ') + listener.url)
|
|
|
|
}
|
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
// Return instance
|
|
|
|
return nuxt
|
|
|
|
},
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
logChanged ({ event, path }) {
|
2018-12-09 10:42:22 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-07-10 10:45:49 +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
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
onBundlerChange (path) {
|
2018-12-09 11:47:57 +00:00
|
|
|
this.logChanged({ event: 'change', path })
|
2018-10-18 15:57:35 +00:00
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|