fix(cli): show memory usage after build for nuxt dev (#5514)

This commit is contained in:
Pim 2019-04-12 19:19:46 +02:00 committed by Pooya Parsa
parent 9980a9d681
commit 19fbbb6ef6
4 changed files with 61 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import opener from 'opener'
import { common, server } from '../options'
import { eventsMapping, formatPath } from '../utils'
import { showBanner } from '../utils/banner'
import { showMemoryUsage } from '../utils/memory'
export default {
name: 'dev',
@ -50,7 +51,7 @@ export default {
await nuxt.server.listen()
// Show banner when listening
showBanner(nuxt)
showBanner(nuxt, false)
// Opens the server listeners url in the default browser (only once)
if (argv.open) {
@ -65,6 +66,9 @@ export default {
// Start Build
await builder.build()
// Print memory usage
showMemoryUsage()
// Return instance
return nuxt
},

View File

@ -1,10 +1,10 @@
import consola from 'consola'
import prettyBytes from 'pretty-bytes'
import env from 'std-env'
import chalk from 'chalk'
import { successBox } from './formatting'
import { getFormattedMemoryUsage } from './memory'
export function showBanner(nuxt) {
export function showBanner(nuxt, showMemoryUsage = true) {
if (env.test) {
return
}
@ -29,9 +29,9 @@ export function showBanner(nuxt) {
titleLines.push(`TypeScript support is ${chalk.green.bold('enabled')}`)
}
// https://nodejs.org/api/process.html#process_process_memoryusage
const { heapUsed, rss } = process.memoryUsage()
titleLines.push(`Memory usage: ${chalk.bold(prettyBytes(heapUsed))} (RSS: ${prettyBytes(rss)})`)
if (showMemoryUsage) {
titleLines.push(getFormattedMemoryUsage())
}
// Listeners
for (const listener of nuxt.server.listeners) {

View File

@ -0,0 +1,18 @@
import chalk from 'chalk'
import consola from 'consola'
import prettyBytes from 'pretty-bytes'
export function getMemoryUsage() {
// https://nodejs.org/api/process.html#process_process_memoryusage
const { heapUsed, rss } = process.memoryUsage()
return { heap: heapUsed, rss }
}
export function getFormattedMemoryUsage() {
const { heap, rss } = getMemoryUsage()
return `Memory usage: ${chalk.bold(prettyBytes(heap))} (RSS: ${prettyBytes(rss)})`
}
export function showMemoryUsage() {
consola.info(getFormattedMemoryUsage())
}

View File

@ -3,6 +3,7 @@ import { consola } from '../utils'
import { loadNuxtConfig } from '../../src/utils/config'
import * as utils from '../../src/utils'
import { showBanner } from '../../src/utils/banner'
import { showMemoryUsage } from '../../src/utils/memory'
import * as fmt from '../../src/utils/formatting'
jest.mock('std-env', () => ({
@ -121,7 +122,7 @@ describe('cli/utils', () => {
expect(fmt.indent(4, '-')).toBe('----')
})
test('showBanner prints full-info box', () => {
test('showBanner prints full-info box with memory usage', () => {
const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
const successBox = jest.fn().mockImplementation((m, t) => t + m)
jest.spyOn(fmt, 'successBox').mockImplementation(successBox)
@ -148,10 +149,41 @@ describe('cli/utils', () => {
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js'))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching(`Listening on: ${listeners[0].url}`))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching(`Listening on: ${listeners[1].url}`))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('badgeMessage'))
stdout.mockRestore()
})
test('showBanner doesnt print memory usage', () => {
const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
const successBox = jest.fn().mockImplementation((m, t) => t + m)
jest.spyOn(fmt, 'successBox').mockImplementation(successBox)
showBanner({
options: {
cli: {
badgeMessages: []
}
},
server: {
listeners: []
}
}, false)
expect(successBox).toHaveBeenCalledTimes(1)
expect(stdout).toHaveBeenCalledTimes(1)
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js'))
expect(stdout).not.toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
stdout.mockRestore()
})
test('showMemoryUsage prints memory usage', () => {
showMemoryUsage()
expect(consola.info).toHaveBeenCalledTimes(1)
expect(consola.info).toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
})
test('forceExit exits after timeout', () => {
jest.useFakeTimers()
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {})