Skip to content

Commit

Permalink
support cli option open
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 22, 2019
1 parent d206d44 commit d08e873
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/@vuepress/core/lib/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = async (sourceDir, cliOptions = {}, ctx) => {
console.log(err)
}
})
return { host, port }
}

module.exports.prepare = prepareServer
Expand Down
11 changes: 10 additions & 1 deletion packages/vuepress/lib/registerCoreCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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
}))
})
})

Expand Down
3 changes: 2 additions & 1 deletion packages/vuepress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d08e873

Please sign in to comment.