fix(cli): restart issues (#299)

This commit is contained in:
pooya parsa 2021-07-02 14:45:15 +02:00 committed by GitHub
parent fb08be37e2
commit e70978d1c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 40 deletions

View File

@ -4,57 +4,42 @@ import debounce from 'debounce-promise'
import { createServer, createLoadingHandler } from '../utils/server'
import { showBanner } from '../utils/banner'
import { requireModule } from '../utils/cjs'
import { error, info } from '../utils/log'
import { diff, printDiff } from '../utils/diff'
import { error } from '../utils/log'
export async function invoke (args) {
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const server = createServer()
const listener = await server.listen({ clipboard: args.clipboard, open: args.open || args.o })
const listener = await server.listen({
clipboard: args.clipboard,
open: args.open || args.o
})
const rootDir = resolve(args._[0] || '.')
const { loadNuxt, buildNuxt } = requireModule('@nuxt/kit', rootDir)
let currentNuxt
const load = async () => {
const load = async (isRestart) => {
try {
showBanner(true)
listener.showURL()
const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false })
if (process.env.DEBUG) {
let configChanges
const message = `${isRestart ? 'Restarting' : 'Starting'} nuxt...`
server.setApp(createLoadingHandler(message, 1))
if (isRestart) {
console.log(message)
}
if (currentNuxt) {
configChanges = diff(currentNuxt.options, newNuxt.options, [
'generate.staticAssets.version',
'env.NITRO_PRESET'
])
server.setApp(createLoadingHandler('Restarting...', 1))
await currentNuxt.close()
}
const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false })
currentNuxt = newNuxt
} else {
currentNuxt = newNuxt
}
if (configChanges) {
if (configChanges.length) {
info('Nuxt config updated:')
printDiff(configChanges)
} else {
info('Restarted nuxt due to config changes')
}
}
} else {
currentNuxt = newNuxt
}
await currentNuxt.ready()
await buildNuxt(currentNuxt)
server.setApp(currentNuxt.server.app)
if (isRestart && args.clear !== false) {
showBanner()
listener.showURL()
}
} catch (err) {
error('Cannot load nuxt.', err)
error(`Cannot ${isRestart ? 'restart' : 'start'} nuxt: `, err)
server.setApp(createLoadingHandler(
'Error while loading nuxt. Please check console and fix errors.'
))
@ -67,11 +52,11 @@ export async function invoke (args) {
const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 1 })
watcher.on('all', (_event, file) => {
if (file.includes('nuxt.config') || file.includes('modules')) {
dLoad()
dLoad(true)
}
})
await load()
await load(false)
}
export const meta = {

View File

@ -8,11 +8,15 @@ import { error } from './utils/log'
async function _main () {
const _argv = process.argv.slice(2)
const args = mri(_argv)
const args = mri(_argv, {
boolean: [
'no-clear'
]
})
// @ts-ignore
let command = args._.shift() || 'usage'
showBanner(command === 'dev')
showBanner(command === 'dev' && args.clear !== false)
if (!(command in commands)) {
console.log('\n' + red('Invalid command ' + command))

View File

@ -2,7 +2,7 @@ import clear from 'clear'
import { green } from 'colorette'
import { version } from '../../package.json'
export function showBanner (_clear: boolean) {
export function showBanner (_clear?: boolean) {
if (_clear) { clear() }
console.log(green(`Nuxt CLI v${version}`))
}