From 0c094af27ae69a3dba59a62e8508ee809421478f Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Wed, 17 Mar 2021 10:09:19 +0800 Subject: [PATCH 1/2] feat: Add logger --- lib/commands/generate.js | 10 +++++----- lib/commands/init.js | 7 ++++--- lib/commands/serve.js | 7 ++++--- lib/commands/start.js | 7 ++++--- lib/util/logger.js | 9 +++++++++ 5 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 lib/util/logger.js diff --git a/lib/commands/generate.js b/lib/commands/generate.js index df572dc..2722eb0 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,16 +17,16 @@ 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.`) return false } } - console.error(chalk.red(`${cwdPath}`) + ' directory does not exist.') + logger.error(`${cwdPath} directory does not exist.`) } function genSidebar(cwdPath, sidebarPath) { @@ -61,7 +61,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..1f64cfa 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', @@ -38,7 +39,7 @@ module.exports = function (path = '', local, theme) { } createFile(cwdPath, local, theme) - console.log(msg) + logger.info(msg) }) .catch(console.error) @@ -46,7 +47,7 @@ module.exports = function (path = '', local, theme) { } createFile(cwdPath, local, theme) - console.log(msg) + logger.info(msg) } function createFile(path, local, theme) { diff --git a/lib/commands/serve.js b/lib/commands/serve.js index 13de4b8..61d0f12 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') @@ -35,7 +36,7 @@ module.exports = function ( '\nPlease run ' + chalk.green('docsify init') + ' first.\n' - console.log(msg) + logger.info(msg) process.exit(0) } @@ -66,9 +67,9 @@ module.exports = function ( 'Listening at ' + chalk.green(`http://localhost:${port}`) + '\n' - console.log(msg) + logger.info(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..ec30dda 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() }) @@ -96,5 +97,5 @@ module.exports = function (path, configFile, port) { chalk.green(`http://localhost:${port}`) + '\n' - console.log(msg) + logger.info(msg) } diff --git a/lib/util/logger.js b/lib/util/logger.js new file mode 100644 index 0000000..0e82ff7 --- /dev/null +++ b/lib/util/logger.js @@ -0,0 +1,9 @@ +'use strict' + +const chalk = require('chalk') + +exports.error = msg => console.error(chalk.red(msg)) + +exports.success = msg => console.log(chalk.green(msg)) + +exports.info = msg => console.log(msg) From 23c7a2be98a8cd113a11ec131b7fe4b10ec6d980 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Thu, 18 Mar 2021 13:11:40 +0800 Subject: [PATCH 2/2] remove logger.info --- lib/commands/init.js | 4 ++-- lib/commands/serve.js | 4 ++-- lib/commands/start.js | 2 +- lib/util/logger.js | 1 - 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/commands/init.js b/lib/commands/init.js index 1f64cfa..6488b2f 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -39,7 +39,7 @@ module.exports = function (path = '', local, theme) { } createFile(cwdPath, local, theme) - logger.info(msg) + console.log(msg) }) .catch(console.error) @@ -47,7 +47,7 @@ module.exports = function (path = '', local, theme) { } createFile(cwdPath, local, theme) - logger.info(msg) + console.log(msg) } function createFile(path, local, theme) { diff --git a/lib/commands/serve.js b/lib/commands/serve.js index 61d0f12..9b89633 100644 --- a/lib/commands/serve.js +++ b/lib/commands/serve.js @@ -36,7 +36,7 @@ module.exports = function ( '\nPlease run ' + chalk.green('docsify init') + ' first.\n' - logger.info(msg) + console.log(msg) process.exit(0) } @@ -67,7 +67,7 @@ module.exports = function ( 'Listening at ' + chalk.green(`http://localhost:${port}`) + '\n' - logger.info(msg) + console.log(msg) }) .catch(err => { logger.error(err.message) diff --git a/lib/commands/start.js b/lib/commands/start.js index ec30dda..31defe4 100644 --- a/lib/commands/start.js +++ b/lib/commands/start.js @@ -97,5 +97,5 @@ module.exports = function (path, configFile, port) { chalk.green(`http://localhost:${port}`) + '\n' - logger.info(msg) + console.log(msg) } diff --git a/lib/util/logger.js b/lib/util/logger.js index 0e82ff7..fcdfb09 100644 --- a/lib/util/logger.js +++ b/lib/util/logger.js @@ -6,4 +6,3 @@ exports.error = msg => console.error(chalk.red(msg)) exports.success = msg => console.log(chalk.green(msg)) -exports.info = msg => console.log(msg)