Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Add unify CLI log
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Oct 9, 2017
1 parent 01f6098 commit 522d1ec
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 73 deletions.
14 changes: 2 additions & 12 deletions bin/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 })
})()
58 changes: 31 additions & 27 deletions bin/serve/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
58 changes: 58 additions & 0 deletions bin/serve/log.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
35 changes: 1 addition & 34 deletions bin/watch/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 522d1ec

Please sign in to comment.