Skip to content

Commit

Permalink
feat: use consola for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Mar 31, 2018
1 parent a9e05b4 commit 59ff0df
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 286 deletions.
10 changes: 5 additions & 5 deletions bin/nuxt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env node

const { join } = require('path')
const semver = require('semver')

const { Utils } = require('..')
const consola = require('consola')
const { name, engines } = require('../package.json')

const logger = consola.withScope('nuxt')

// Global error handler
process.on('unhandledRejection', _error => {
Utils.printError(_error)
logger.error(_error)
})

if (!semver.satisfies(process.version, engines.node)) {
Utils.fatalError(
logger.fatal(
`The engine "node" is incompatible with ${name}. Expected version "${
engines.node
}".`
Expand Down
37 changes: 7 additions & 30 deletions bin/nuxt-build
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env node
/* eslint-disable no-console */

const parseArgs = require('minimist')
const debug = require('debug')('nuxt:build')
debug.color = 2 // Force green color

const { Nuxt, Builder, Generator, Utils } = require('..')
const consola = require('consola')
const { Nuxt, Builder, Generator } = require('..')
const { loadNuxtConfig } = require('./common/utils')

const logger = consola.withScope('nuxt:build')

const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
Expand All @@ -24,7 +22,7 @@ const argv = parseArgs(process.argv.slice(2), {
})

if (argv.help) {
console.log(`
process.stderr.write(`
Description
Compiles the application for production deployment
Usage
Expand All @@ -51,12 +49,11 @@ if (argv.analyze) {
options.build.analyze = true
}

debug('Building...')
const nuxt = new Nuxt(options)
const builder = new Builder(nuxt)

// Setup hooks
nuxt.hook('error', (_err, from) => Utils.fatalError(_err, from))
nuxt.hook('error', (_err) => logger.fatal(_err))

// Close function
const close = () => {
Expand All @@ -74,28 +71,8 @@ if (!argv.generate) {
builder
.build()
.then(() => close())
.catch(Utils.fatalError)
.catch(error => logger.fatal(error))
} else {
// -- Build and generate --
const s = Date.now()

nuxt.hook('generate:distRemoved', () => debug('Destination folder cleaned'))

nuxt.hook('generate:distCopied', () => debug('Static & build files copied'))

nuxt.hook('generate:page', page => debug('Generate file: ' + page.path))

nuxt.hook('generate:done', (generator, errors) => {
const duration = Math.round((Date.now() - s) / 100) / 10

debug(`HTML Files generated in ${duration}s`)

if (errors.length) {
/* eslint-disable no-console */
console.log('\n' + errors.toString())
}
})

// Generate dist for SPA static deployment
new Generator(nuxt, builder).generate({ build: true }).then(() => {
close()
Expand Down
22 changes: 9 additions & 13 deletions bin/nuxt-dev
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#!/usr/bin/env node
/* eslint-disable no-console */

const defaultsDeep = require('lodash/defaultsDeep')
const debug = require('debug')('nuxt:build')
const parseArgs = require('minimist')
const chokidar = require('chokidar')

const consola = require('consola')
const { version } = require('../package.json')
const { Nuxt, Builder, Utils } = require('..')

const { Nuxt, Builder } = require('..')
const { loadNuxtConfig, getLatestHost, nuxtConfigFile } = require('./common/utils')

debug.color = 2 // force green color
const logger = consola.withScope('nuxt:dev')

const argv = parseArgs(process.argv.slice(2), {
alias: {
Expand All @@ -31,16 +27,16 @@ const argv = parseArgs(process.argv.slice(2), {
})

if (argv.version) {
console.log(version)
process.stderr.write(version + '\n')
process.exit(0)
}

if (argv.hostname === '') {
Utils.fatalError('Provided hostname argument has no value')
logger.fatal('Provided hostname argument has no value')
}

if (argv.help) {
console.log(`
process.stderr.write(`
Description
Starts the application in development mode (hot-code reloading, error
reporting, etc)
Expand Down Expand Up @@ -69,14 +65,14 @@ let needToRestart = false
chokidar
.watch(nuxtConfigFile(argv), nuxtConfig.watchers.chokidar)
.on('all', () => {
debug('[nuxt.config.js] changed')
logger.debug('[nuxt.config.js] changed')
needToRestart = true

dev = dev.then(instance => {
if (needToRestart === false) return instance
needToRestart = false

debug('Rebuilding the app...')
logger.debug('Rebuilding the app...')
return startDev(instance)
})
})
Expand All @@ -87,7 +83,7 @@ function startDev(oldInstance) {

// Error handler
const onError = (err, instance) => {
Utils.printError(err)
logger.error(err)
return Promise.resolve(instance) // Wait for next reload
}

Expand Down
39 changes: 6 additions & 33 deletions bin/nuxt-generate
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/usr/bin/env node
/* eslint-disable no-console */

const parseArgs = require('minimist')
const debug = require('debug')('nuxt:generate')

const { Nuxt, Builder, Generator, Utils } = require('..')
const consola = require('consola')
const { Nuxt, Builder, Generator } = require('..')
const { loadNuxtConfig } = require('./common/utils')

const logger = consola.withScope('nuxt:generate')

const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
Expand All @@ -23,7 +22,7 @@ const argv = parseArgs(process.argv.slice(2), {
})

if (argv.help) {
console.log(`
process.stderr.write(`
Description
Generate a static web application (server-rendered)
Usage
Expand All @@ -42,7 +41,6 @@ const options = loadNuxtConfig(argv)

options.dev = false // Force production mode (no webpack middleware called)

debug('Generating...')
const nuxt = new Nuxt(options)
const builder = new Builder(nuxt)
const generator = new Generator(nuxt, builder)
Expand All @@ -52,34 +50,9 @@ const generateOptions = {
build: argv['build']
}

const s = Date.now()

// Setup hooks

nuxt.hook('error', (_err, from) => Utils.fatalError(_err, from))

nuxt.hook('generate:distRemoved', () => debug('Destination folder cleaned'))

nuxt.hook('generate:distCopied', () => debug('Static & build files copied'))

nuxt.hook('generate:page', page => debug('Generate file: ' + page.path))

nuxt.hook('generate:done', (generator, errors) => {
const duration = Math.round((Date.now() - s) / 100) / 10

debug(`HTML Files generated in ${duration}s`)

if (errors.length) {
/* eslint-disable no-console */
console.log('\n Generate errors summary:')
console.log('\n' + errors.toString())
}
})

generator
.generate(generateOptions)
.then(() => {
debug('Generate done')
process.exit(0)
})
.catch(Utils.fatalError)
.catch(error => logger.fatal(error))
18 changes: 9 additions & 9 deletions bin/nuxt-start
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env node
/* eslint-disable no-console */

const fs = require('fs')
const { resolve } = require('path')
const parseArgs = require('minimist')

const { Nuxt, Utils } = require('..')
const consola = require('consola')
const { Nuxt } = require('..')
const { loadNuxtConfig, getLatestHost } = require('./common/utils')

const logger = consola.withScope('nuxt:start')

const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
Expand All @@ -25,11 +25,11 @@ const argv = parseArgs(process.argv.slice(2), {
})

if (argv.hostname === '') {
Utils.fatalError('Provided hostname argument has no value')
logger.fatal('Provided hostname argument has no value')
}

if (argv.help) {
console.log(`
process.stderr.write(`
Description
Starts the application in production mode.
The application should be compiled with \`nuxt build\` first.
Expand All @@ -54,7 +54,7 @@ options.dev = false
const nuxt = new Nuxt(options)

// Setup hooks
nuxt.hook('error', (_err, from) => Utils.fatalError(_err, from))
nuxt.hook('error', (_err) => logger.fatal(_err))

// Check if project is built for production
const distDir = resolve(
Expand All @@ -63,7 +63,7 @@ const distDir = resolve(
'dist'
)
if (!fs.existsSync(distDir)) {
Utils.fatalError(
logger.fatal(
'No build files found, please run `nuxt build` before launching `nuxt start`'
)
}
Expand All @@ -72,7 +72,7 @@ if (!fs.existsSync(distDir)) {
if (nuxt.options.render.ssr === true) {
const ssrBundlePath = resolve(distDir, 'server-bundle.json')
if (!fs.existsSync(ssrBundlePath)) {
Utils.fatalError(
logger.fatal(
'No SSR build! Please start with `nuxt start --spa` or build using `nuxt build --universal`'
)
}
Expand Down
Loading

0 comments on commit 59ff0df

Please sign in to comment.