From 522d1ec52e86656db17a0de30b7e6c3832060ebe Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Mon, 9 Oct 2017 14:57:24 +0200 Subject: [PATCH] Add unify CLI log --- bin/cli/index.js | 14 ++--------- bin/serve/index.js | 58 +++++++++++++++++++++++++--------------------- bin/serve/log.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++ bin/watch/index.js | 35 +--------------------------- package.json | 1 + 5 files changed, 93 insertions(+), 73 deletions(-) create mode 100644 bin/serve/log.js diff --git a/bin/cli/index.js b/bin/cli/index.js index 87cf499..e25e729 100755 --- a/bin/cli/index.js +++ b/bin/cli/index.js @@ -9,11 +9,6 @@ const getMainFile = require('./get-main-file') require('update-notifier')({ pkg }).notify() -const log = require('acho')({ - types: require('acho-skin-cli'), - keyword: 'symbol' -}) - const cli = require('meow')( { pkg, @@ -37,11 +32,6 @@ const cli = require('meow')( } ) ;(async () => { - try { - const { filename, pkg } = getMainFile(cli) - require('../serve')({ filename, pkg, cli, restarting: false }) - } catch (err) { - log.error(err.message || err) - process.exit(1) - } + const { filename, pkg } = getMainFile(cli) + require('../serve')({ filename, pkg, cli, restarting: false }) })() diff --git a/bin/serve/index.js b/bin/serve/index.js index 66aad34..f4df481 100755 --- a/bin/serve/index.js +++ b/bin/serve/index.js @@ -5,36 +5,40 @@ const path = require('path') const listenMessage = require('./listen-message') const getPort = require('./get-port') +const { error: logError } = require('./log') module.exports = async ({ filename, pkg, cli, restarting }) => { const { userPort, port, inUse } = await getPort(cli) - const filepath = path.resolve(process.cwd(), filename) - const module = require(filepath) - - const express = importCwd('express') - const app = express() - - module(app, express) - - const server = app.listen(port, () => { - if (!restarting) { - const message = listenMessage({ - appName: pkg.name, - port, - inUse, - userPort - }) - console.log(message) - } - }) - - const sockets = [] - - server.on('connection', socket => { - const index = sockets.push(socket) - socket.once('close', () => sockets.splice(index, 1)) - }) - require('../watch')({ filename, pkg, server, cli, sockets }) + try { + const module = require(filepath) + + const express = importCwd('express') + const app = express() + + module(app, express) + + const server = app.listen(port, () => { + if (!restarting) { + const message = listenMessage({ + appName: pkg.name, + port, + inUse, + userPort + }) + console.log(message) + } + }) + + const sockets = [] + + server.on('connection', socket => { + const index = sockets.push(socket) + socket.once('close', () => sockets.splice(index, 1)) + }) + require('../watch')({ filename, pkg, server, cli, sockets }) + } catch (err) { + logError(err) + } } diff --git a/bin/serve/log.js b/bin/serve/log.js new file mode 100644 index 0000000..37f4828 --- /dev/null +++ b/bin/serve/log.js @@ -0,0 +1,58 @@ +'use strict' + +const getTimestamp = require('time-stamp') +const logSymbols = require('log-symbols') +const cleanStack = require('clean-stack') +const logUpdate = require('log-update') +const chalk = require('chalk') +const ora = require('ora') + +const createSpinner = () => ora({ color: 'gray' }) +const getCurrentTimestamp = () => getTimestamp('HH:mm:ss') + +const OFFSET = ' ' + +module.exports = { + error (err) { + const symbol = chalk.blue(logSymbols.error) + const timestamp = chalk.gray(getCurrentTimestamp()) + + const [message, ...stackTraces] = chalk + .gray( + cleanStack(err.stack) + .replace('Error: ', chalk.red('error ')) + .replace('Cannot', 'cannot') + ) + .split('\n') + + const stackTrace = stackTraces.map(msg => `${OFFSET}${msg}`).join('') + const logMessage = `${OFFSET} ${symbol} ${timestamp} ${message}` + console.log(`${logMessage}\n${stackTrace}\n`) + }, + restart ({ filename, forcing }) { + const symbol = chalk.blue(logSymbols.info) + const timestamp = chalk.gray(getCurrentTimestamp()) + const header = chalk.blue(forcing ? 'restart' : 'modified') + const message = chalk.gray(filename || '') + const spinner = createSpinner() + const logMessage = `${OFFSET} ${symbol} ${timestamp} ${header} ${message}` + + let done = false + + const timer = setInterval(() => { + done + ? logUpdate(`${logMessage}`) + : logUpdate(`${logMessage} ${spinner.frame()}`) + }, 50) + + return { + stop: () => { + done = true + setTimeout(() => { + clearInterval(timer) + logUpdate.done() + }, 50) + } + } + } +} diff --git a/bin/watch/index.js b/bin/watch/index.js index a301f2c..63fdc76 100644 --- a/bin/watch/index.js +++ b/bin/watch/index.js @@ -1,48 +1,15 @@ 'use strict' -const getTimestamp = require('time-stamp') -const logSymbols = require('log-symbols') -const logUpdate = require('log-update') const { watch } = require('chokidar') const debounce = require('debounce') -const chalk = require('chalk') +const { restart: logRestart } = require('../serve/log') const getWatchConfig = require('./get-watch-config') const destroySockets = require('./destroy-sockets') const restartServer = require('./restart-server') -const ora = require('ora') -const createSpinner = () => ora({ color: 'gray' }) let firsTime = false -const logRestart = ({ filename, forcing }) => { - const offset = ' ' - const symbol = chalk.blue(logSymbols.info) - const timestamp = chalk.gray(getTimestamp('HH:mm:ss')) - const header = chalk.blue(forcing ? 'restart' : 'modified') - const message = chalk.gray(filename || '') - const spinner = createSpinner() - const logMessage = `${offset} ${symbol} ${timestamp} ${header} ${message}` - - let done = false - - const timer = setInterval(() => { - done - ? logUpdate(`${logMessage}`) - : logUpdate(`${logMessage} ${spinner.frame()}`) - }, 50) - - return { - stop: () => { - done = true - setTimeout(() => { - clearInterval(timer) - logUpdate.done() - }, 50) - } - } -} - const doRestart = ({ ignored, sockets, diff --git a/package.json b/package.json index b8519b0..fb3a7ed 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "boxen": "~1.2.1", "chalk": "~2.1.0", "chokidar": "~1.7.0", + "clean-stack": "~1.3.0", "clear-module": "~2.1.0", "debounce": "~1.0.2", "get-port": "~3.2.0",