diff --git a/lib/commands/generate.js b/lib/commands/generate.js index 92b4635..95943dc 100644 --- a/lib/commands/generate.js +++ b/lib/commands/generate.js @@ -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 @@ -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) { @@ -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}`) } }) } diff --git a/lib/commands/init.js b/lib/commands/init.js index fca59c2..6488b2f 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -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') @@ -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', diff --git a/lib/commands/serve.js b/lib/commands/serve.js index 13de4b8..9b89633 100644 --- a/lib/commands/serve.js +++ b/lib/commands/serve.js @@ -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') @@ -69,6 +70,6 @@ module.exports = function ( console.log(msg) }) .catch(err => { - console.error(err.message) + logger.error(err.message) }) } diff --git a/lib/commands/start.js b/lib/commands/start.js index b7205a8..31defe4 100644 --- a/lib/commands/start.js +++ b/lib/commands/start.js @@ -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 = { @@ -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) } } @@ -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() }) diff --git a/lib/util/logger.js b/lib/util/logger.js new file mode 100644 index 0000000..fcdfb09 --- /dev/null +++ b/lib/util/logger.js @@ -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)) +