From d08e873c74c9070d43ab3a36babe8a9c9fc92f74 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Fri, 22 Feb 2019 15:09:37 +0800 Subject: [PATCH] support cli option open --- packages/@vuepress/core/lib/dev.js | 1 + packages/vuepress/lib/registerCoreCommands.js | 11 ++++++++++- packages/vuepress/package.json | 3 ++- yarn.lock | 7 +++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/@vuepress/core/lib/dev.js b/packages/@vuepress/core/lib/dev.js index c8051d3978..95895b781d 100644 --- a/packages/@vuepress/core/lib/dev.js +++ b/packages/@vuepress/core/lib/dev.js @@ -7,6 +7,7 @@ module.exports = async (sourceDir, cliOptions = {}, ctx) => { console.log(err) } }) + return { host, port } } module.exports.prepare = prepareServer diff --git a/packages/vuepress/lib/registerCoreCommands.js b/packages/vuepress/lib/registerCoreCommands.js index 2705cbf690..3a6bf10d87 100644 --- a/packages/vuepress/lib/registerCoreCommands.js +++ b/packages/vuepress/lib/registerCoreCommands.js @@ -7,6 +7,8 @@ const { dev, build, eject } = require('@vuepress/core') const { path, logger, env } = require('@vuepress/shared-utils') const { wrapCommand } = require('./util') +const opn = require('opn') +const url = require('url') /** * Expose registerCoreCommands function. @@ -24,7 +26,7 @@ module.exports = function (cli, options) { .option('--silent', 'start development server in silent mode') .option('--open', 'open browser when ready') .action((sourceDir = '.', commandOptions) => { - const { debug, silent } = commandOptions + const { debug, silent, open } = commandOptions logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 }) logger.debug('global_options', options) @@ -34,6 +36,13 @@ module.exports = function (cli, options) { wrapCommand(dev)(path.resolve(sourceDir), { ...options, ...commandOptions + }).then((urlObject) => { + if (!open) return + opn(url.format({ + protocol: 'http', + hostname: urlObject.host, + port: urlObject.port + })) }) }) diff --git a/packages/vuepress/package.json b/packages/vuepress/package.json index 1b0006875d..4ac7016607 100644 --- a/packages/vuepress/package.json +++ b/packages/vuepress/package.json @@ -31,7 +31,8 @@ "dependencies": { "@vuepress/core": "^1.0.0-alpha.39", "@vuepress/theme-default": "^1.0.0-alpha.39", - "cac": "^6.3.9" + "cac": "^6.3.9", + "opn": "^5.4.0" }, "engines": { "node": ">=8" diff --git a/yarn.lock b/yarn.lock index 64877e1baa..59b5bbb576 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7369,6 +7369,13 @@ opn@^5.1.0: dependencies: is-wsl "^1.1.0" +opn@^5.4.0: + version "5.4.0" + resolved "https://registry.yarnpkg.com/opn/-/opn-5.4.0.tgz#cb545e7aab78562beb11aa3bfabc7042e1761035" + integrity sha512-YF9MNdVy/0qvJvDtunAOzFw9iasOQHpVthTCvGzxt61Il64AYSGdK+rYwld7NAfk9qJ7dt+hymBNSc9LNYS+Sw== + dependencies: + is-wsl "^1.1.0" + optimist@^0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686"