From e70978d1c07ae80734ce1b05f697cdb5ee445e99 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Fri, 2 Jul 2021 14:45:15 +0200 Subject: [PATCH] fix(cli): restart issues (#299) --- packages/cli/src/commands/dev.ts | 59 ++++++++++++-------------------- packages/cli/src/index.ts | 8 +++-- packages/cli/src/utils/banner.ts | 2 +- 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/packages/cli/src/commands/dev.ts b/packages/cli/src/commands/dev.ts index 041cd031d4..44b7520eb4 100644 --- a/packages/cli/src/commands/dev.ts +++ b/packages/cli/src/commands/dev.ts @@ -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 - if (currentNuxt) { - configChanges = diff(currentNuxt.options, newNuxt.options, [ - 'generate.staticAssets.version', - 'env.NITRO_PRESET' - ]) - server.setApp(createLoadingHandler('Restarting...', 1)) - await currentNuxt.close() - 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 + const message = `${isRestart ? 'Restarting' : 'Starting'} nuxt...` + server.setApp(createLoadingHandler(message, 1)) + if (isRestart) { + console.log(message) } - + if (currentNuxt) { + await currentNuxt.close() + } + const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false }) + 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 = { diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 92232f5f85..5dacedcd8a 100755 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -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)) diff --git a/packages/cli/src/utils/banner.ts b/packages/cli/src/utils/banner.ts index 753ac99f09..13410e1eb8 100644 --- a/packages/cli/src/utils/banner.ts +++ b/packages/cli/src/utils/banner.ts @@ -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}`)) }