From 6bf2ceb46f89ff327f5e5c40f30d4a408694e0d9 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 11 Mar 2019 01:34:41 +0800 Subject: [PATCH] refactor($core): add more comments --- packages/@vuepress/core/lib/node/dev/index.js | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/packages/@vuepress/core/lib/node/dev/index.js b/packages/@vuepress/core/lib/node/dev/index.js index d8d1cdbb35..4267b3bfc0 100644 --- a/packages/@vuepress/core/lib/node/dev/index.js +++ b/packages/@vuepress/core/lib/node/dev/index.js @@ -188,6 +188,11 @@ module.exports = class DevProcess extends EventEmitter { this.webpackConfig = config } + /** + * Create dev server + * @returns {module.DevProcess} + */ + createServer () { const contentBase = path.resolve(this.context.sourceDir, '.vuepress/public') @@ -236,16 +241,30 @@ module.exports = class DevProcess extends EventEmitter { return this } - listen () { - this.server.listen(this.port, this.host, err => { - if (err) { - console.log(err) + /** + * delegate listen call. + * + * @param callback handler when connection is ready. + * @returns {module.DevProcess} + */ + + listen (callback) { + this.server.listen(this.port, this.host, (err) => { + if (callback) { + callback(err) } }) return this } } +/** + * Resolve host. + * + * @param {string} host user's host + * @returns {{displayHost: string, host: string}} + */ + function resolveHost (host) { const defaultHost = 'localhost' host = host || defaultHost @@ -258,6 +277,13 @@ function resolveHost (host) { } } +/** + * Resolve port. + * + * @param {number} port user's port + * @returns {Promise} + */ + async function resolvePort (port) { const portfinder = require('portfinder') portfinder.basePort = parseInt(port) || 8080 @@ -265,6 +291,14 @@ async function resolvePort (port) { return port } +/** + * Normalize file path and always return relative path, + * + * @param {string} filepath user's path + * @param {string} baseDir source directory + * @returns {string} + */ + function normalizeWatchFilePath (filepath, baseDir) { const { isAbsolute, relative } = require('path') if (isAbsolute(filepath)) {