Skip to content

Commit

Permalink
Simplify log levels behind --log flag
Browse files Browse the repository at this point in the history
  • Loading branch information
KidkArolis committed Feb 6, 2022
1 parent 084a35d commit 8496590
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Improve error handling, log unexpected errors when serving bundles in development instead of hanging
- Remove the graceful termination fix introduced in 0.21.1 as it does not appear to work in node@16
- **Breaking:** the runtime content is now referenced via `runtime` instead of `assets.runtime` in the template
- **Breaking:** simplified logging behind `--log` flag, choose between `info`, `progress` or `none`, with default being `info,progress`
- **Breaking:** removed support for react-hot-loader, you can still tweak your config to pull it in, but it is no longer automatically configured, use `react-refresh` instead!

# 0.22.0
Expand Down
2 changes: 1 addition & 1 deletion lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = async function build(options, log) {
}

async function run(compiler, { modern }) {
if (!options.quiet) {
if (options.logLevels.info) {
require('./reporter')(compiler, log, {
printAssets: true,
dir: options.dir
Expand Down
28 changes: 14 additions & 14 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const program = require('commander')
const getOptions = require('./options')
const createLogger = require('./logger')
const pkg = require('../package.json')
const webpackPkg = require('webpack/package.json')

module.exports = { run, options }

Expand All @@ -10,25 +11,23 @@ const setCommand = (name) => () => {
command = name
}

const version = `Jetpack ${pkg.version}
Webpack ${webpackPkg.version}`

program
.version(pkg.version)
.version(version, '-v, --version', 'print the version of jetpack and webpack')
.arguments('[path]')
.option('-p, --port <n>', 'port, defaults to 3030', Number)
.option('-d, --dir [path]', 'run jetpack in the context of this directory')
.option('-x, --exec [path]', 'execute an additional process, e.g. an api server')
.option(
'-j, --jsx <pragma>',
'specify jsx pragma, defaults to React.createElement or Preact.h if preact is installed'
)
.option('-c, --config', 'config file to use, defaults to jetpack.config.js')
.option('--jsx <pragma>', 'specify jsx pragma, defaults to React.createElement or Preact.h if preact is installed')
.option('-r, --no-hot', 'disable hot reloading', null)
.option('-u, --no-minify', 'disable minification', null)
.option('-c, --config', 'config file to use, defaults to jetpack.config.js')
.option('-m, --modern', 'build a modern bundle')
.option('-l, --legacy', 'build a legacy bundle')
.option('-i, --print-config', 'print the config used')
.option('-q, --quiet', 'log no output')
.option('--no-progress', 'disable progress logging', null)
.option('-v, --verbose', 'log verbose output')
.option('-x, --exec [path]', 'execute an additional process, e.g. an api server')
.option('-i, --print-config', 'print the webpack config object used in the current command')
.option('--log [levels]', 'select log levels: info, progress, none', 'info,progress')

program.command('dev', { isDefault: true }).description('run the dev server').action(setCommand('dev'))

Expand All @@ -45,12 +44,13 @@ program
program.command('clean').description('remove the dist dir').action(setCommand('clean'))

program.on('--help', function (here) {
const webpackPkg = require('webpack/package.json')
console.log()
console.log('Examples:')
console.log(' jetpack')
console.log(' jetpack --port 3500 --log=all ./my/app')
console.log(' jetpack build')
console.log(' jetpack build --print-config')
console.log(' jetpack inspect ./my/app')
console.log(' jetpack --port 3500 --verbose ./my/app')
console.log(' jetpack --exec')
console.log(" jetpack --exec 'nodemon ./server.js'")
console.log()
Expand Down Expand Up @@ -84,7 +84,7 @@ function options() {

function run() {
const opts = options()
const log = createLogger(opts.verbose, opts.quiet)
const log = createLogger(opts.logLevels)
process.chdir(opts.dir)
require(`./${command}`)(opts, log)
}
15 changes: 3 additions & 12 deletions lib/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ module.exports = async function devServer(options, log) {
// via settings, build the legacy bundle instead
const modern = options.target.modern

log.info(`Jetpack ${pkg.version}${modern ? '' : ' (legacy build)'} 🚀`)
log.verbose(`Webpack ${chalk.magenta(webpackPkg.version)}`)
log.verbose(`Dir ${chalk.magenta(options.dir)}`)

if (options.entry) {
log.verbose(`Entry ${chalk.magenta(options.entry)}`)
}

if (options.exec) {
log.verbose(`Executing ${chalk.magenta(options.exec)}`)
}
log.info(`Jetpack ${pkg.version} • Webpack ${webpackPkg.version}${modern ? '' : ' (legacy build)'} 🚀`)

if (options.printConfig) {
await printConfig({ options, modern, log })
Expand All @@ -33,6 +23,7 @@ module.exports = async function devServer(options, log) {
}

if (options.exec) {
log.info(`Executing ${chalk.magenta(options.exec)} in a subprocess`)
await server({ options, log })
}

Expand All @@ -57,7 +48,7 @@ async function client({ options, log, modern }) {
})
)

if (!options.quiet) {
if (options.logLevels.info) {
require('./reporter')(compiler, log, { dir: options.dir })
}

Expand Down
19 changes: 6 additions & 13 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,29 @@ function replace(...args) {
}
}

module.exports = function (shouldLogVerbose, quiet, prefix = 'jetpack ›') {
return { info, verbose, warn, error, status }
module.exports = function logger(logLevels, prefix = 'jetpack ›') {
return { info, warn, error, status }

function info(...args) {
if (quiet) return
if (!logLevels.info) return
append(chalk.green(prefix), ...args)
lastType = 'info'
}

function verbose(...args) {
if (quiet) return
if (!shouldLogVerbose) return
append(chalk.yellow(prefix), ...args)
lastType = 'verbose'
}

function warn(...args) {
if (quiet) return
if (!logLevels.info) return
append(chalk.yellow(prefix), ...args)
lastType = 'warn'
}

function error(...args) {
if (quiet) return
if (!logLevels.info) return
append(chalk.red(prefix), ...args)
lastType = 'error'
}

function status(...args) {
if (quiet) return
if (!logLevels.progress) return
replace(chalk.green(prefix), ...args.map(chalk.gray))
lastType = 'status'
}
Expand Down
30 changes: 13 additions & 17 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ module.exports = function options(command = 'dev', program = {}) {
if (opts.minify === null) {
delete opts.minify
}
if (opts.progress === null) {
delete opts.progress
}

const dir = opts.dir ? path.resolve(opts.dir) : process.cwd()
let entry = args && typeof args[0] === 'string' ? args[0] : null
Expand All @@ -32,7 +29,7 @@ module.exports = function options(command = 'dev', program = {}) {
const options = Object.assign(
{},
configFromFile,
pick(opts, ['port', 'dir', 'exec', 'jsx', 'hot', 'config', 'minify', 'quiet', 'verbose', 'progress'])
pick(opts, ['port', 'dir', 'exec', 'jsx', 'hot', 'config', 'minify', 'log'])
)

// if specified in config file
Expand Down Expand Up @@ -125,14 +122,8 @@ module.exports = function options(command = 'dev', program = {}) {
// can be an object or a function
proxy: options.proxy || {},

// no logs
quiet: options.quiet,

// more detailed logs
verbose: options.verbose,

// log build progress
progress: options.progress === undefined ? true : options.progress,
// log levels to output
logLevels: parseLogLevels(options.log),

// url paths to all of the entrypoints files to be embedded into the html
assets: {
Expand Down Expand Up @@ -224,11 +215,6 @@ function assets({ publicPath, entrypoints }) {
const other = []
const runtime = []

if (!entrypoints) {
console.log('WTF?')
return { js, css, runtime, other }
}

// process all of the entrypoints into a format
// that is easy to embed into the template
// where we inline the runtime, outpu css as link tags
Expand Down Expand Up @@ -310,3 +296,13 @@ function ifString(str) {
return false
}
}

function parseLogLevels(input) {
const levels = input.split(',').map((l) => l.trim())
const result = {}
for (const level of ['info', 'progress']) {
result[level] = levels.includes(level) || levels.includes('all')
}
result.none = levels.includes('none')
return result
}
2 changes: 1 addition & 1 deletion lib/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function createConfig(options, log) {
}
}

if (options.progress && log) {
if (options.logLevels.progress && log) {
config.plugins.push(progress(log))
}

Expand Down

0 comments on commit 8496590

Please sign in to comment.