Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add logger utility #138

Merged
merged 3 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/commands/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const fs = require('fs')
const os = require('os')
const {cwd, exists} = require('../util')
const chalk = require('chalk')
const path = require('path')
const logger = require('../util/logger')
const ignoreFiles = ['_navbar', '_coverpage', '_sidebar']

// eslint-disable-next-line
Expand All @@ -17,17 +17,17 @@ module.exports = function (path = '', sidebar) {

if (!exists(sidebarPath)) {
genSidebar(cwdPath, sidebarPath)
console.log(chalk.green(`Successfully generated the sidebar file '${sidebar}'.`))
logger.success(`Successfully generated the sidebar file '${sidebar}'.`)
return true
}

console.error(chalk.red(`The sidebar file '${sidebar}' already exists.`))
logger.error(`The sidebar file '${sidebar}' already exists.`)
process.exitCode = 1
return false
}
}

console.error(chalk.red(`${cwdPath}`) + ' directory does not exist.')
logger.error(`${cwdPath} directory does not exist.`)
}

function genSidebar(cwdPath, sidebarPath) {
Expand Down Expand Up @@ -62,7 +62,7 @@ function genSidebar(cwdPath, sidebarPath) {
})
fs.writeFile(sidebarPath, tree, 'utf8', err => {
if (err) {
console.error(chalk.red(`Couldn't generate the sidebar file, error: ${err.message}`))
logger.error(`Couldn't generate the sidebar file, error: ${err.message}`)
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const fs = require('fs')
const cp = require('cp-file').sync
const chalk = require('chalk')
const logger = require('../util/logger')
const {prompt} = require('enquirer')
const {cwd, exists, pkg, pwd, read, resolve} = require('../util')

Expand All @@ -22,7 +23,7 @@ module.exports = function (path = '', local, theme) {
const cwdPath = cwd(path || '.')

if (exists(cwdPath)) {
console.log(chalk.red(`${path || '.'} already exists.`))
logger.error(`${path || '.'} already exists.`)

prompt({
type: 'confirm',
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const livereload = require('connect-livereload')
const lrserver = require('livereload')
const open = require('open')
const chalk = require('chalk')
const logger = require('../util/logger')
const {exists, resolve} = require('../util')
const getPort = require('get-port')

Expand Down Expand Up @@ -69,6 +70,6 @@ module.exports = function (
console.log(msg)
})
.catch(err => {
console.error(err.message)
logger.error(err.message)
})
}
5 changes: 3 additions & 2 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const serveStatic = require('serve-static')
const Renderer = require('docsify-server-renderer')
const util = require('../util')
const chalk = require('chalk')
const logger = require('../util/logger')
const LRU = require('lru-cache')

const defaultConfig = {
Expand All @@ -29,7 +30,7 @@ function loadConfig(config) {
try {
return require(util.cwd(config))
} catch (e) {
console.log(chalk.red(`${e.message} in ${config}`))
logger.error(`${e.message} in ${config}`)
process.exit(1)
}
}
Expand Down Expand Up @@ -78,7 +79,7 @@ module.exports = function (path, configFile, port) {
res.end(html)
})
.catch(function (err) {
console.error(err)
logger.error(err)
res.writeHead(404)
res.end()
})
Expand Down
8 changes: 8 additions & 0 deletions lib/util/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

const chalk = require('chalk')

exports.error = msg => console.error(chalk.red(msg))

exports.success = msg => console.log(chalk.green(msg))