Skip to content

Commit e963731

Browse files
authored
feat($core): registerCommand Plugin Option API (#1069)
1 parent 5ee2b2b commit e963731

20 files changed

+376
-207
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"boot": "node scripts/bootstrap.js",
1313
"dev": "yarn workspace docs dev",
1414
"build": "yarn workspace docs build",
15+
"show-help": "yarn workspace docs show-help",
1516
"dev:blog": "yarn workspace blog dev",
1617
"build:blog": "yarn workspace blog build",
1718
"register-vuepress": "lerna exec --scope vuepress -- yarn link",

packages/@vuepress/cli/.npmignore

-3
This file was deleted.

packages/@vuepress/cli/README.md

-18
This file was deleted.

packages/@vuepress/cli/index.js

-134
This file was deleted.

packages/@vuepress/cli/package.json

-39
This file was deleted.

packages/@vuepress/core/lib/plugin-api/constants.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const PLUGIN_OPTION_META_MAP = {
1919
ADDITIONAL_PAGES: { name: 'additionalPages', types: [Function, Array] },
2020
GLOBAL_UI_COMPONENTS: { name: 'globalUIComponents', types: [String, Array] },
2121
DEFINE: { name: 'define', types: [Function, Object] },
22-
ALIAS: { name: 'alias', types: [Function, Object] }
22+
ALIAS: { name: 'alias', types: [Function, Object] },
23+
EXTEND_CLI: { name: 'extendCli', types: [Function] }
2324
}
2425

2526
const PLUGIN_OPTION_MAP = {}

packages/@vuepress/core/lib/plugin-api/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ module.exports = class PluginAPI {
206206
additionalPages,
207207
globalUIComponents,
208208
define,
209-
alias
209+
alias,
210+
extendCli
210211
}) {
211212
const isInternalPlugin = pluginName.startsWith('@vuepress/internal-')
212213
logger[isInternalPlugin ? 'debug' : 'tip'](pluginLog(pluginName, shortcut))
@@ -229,6 +230,7 @@ module.exports = class PluginAPI {
229230
.registerOption(PLUGIN_OPTION_MAP.GLOBAL_UI_COMPONENTS.key, globalUIComponents, pluginName)
230231
.registerOption(PLUGIN_OPTION_MAP.DEFINE.key, define, pluginName)
231232
.registerOption(PLUGIN_OPTION_MAP.ALIAS.key, alias, pluginName)
233+
.registerOption(PLUGIN_OPTION_MAP.EXTEND_CLI.key, extendCli, pluginName)
232234
}
233235
}
234236

packages/@vuepress/core/lib/prepare/AppContext.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module.exports = class AppContext {
4343
*/
4444

4545
constructor (sourceDir, cliOptions = {}, isProd) {
46+
logger.debug('sourceDir', sourceDir)
4647
this.sourceDir = sourceDir
4748
this.cliOptions = cliOptions
4849
this.isProd = isProd

packages/@vuepress/core/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@
6868
"webpack-chain": "^4.6.0",
6969
"webpack-merge": "^4.1.2",
7070
"webpack-serve": "^1.0.2",
71-
"webpackbar": "^2.6.1"
71+
"webpackbar": "^2.6.1",
72+
"semver": "^5.5.0",
73+
"cac": "^6.3.9"
7274
},
7375
"engines": {
74-
"node": ">=8"
76+
"node": ">=8.6"
7577
},
7678
"browserslist": [
7779
">1%"

packages/docs/docs/miscellaneous/design-concepts.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,8 @@ Then the final route of i18n UI is `/i18n/`.
187187

188188
## Others
189189

190-
With the goal of decoupling, we were able to separate VuePress into the following libraries by introducing monorepo:
190+
With the goal of decoupling, we were able to separate VuePress into the following two libraries by introducing monorepo:
191191

192-
- [@vuepress/cli](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/cli): Management of command line;
193192
- [@vuepress/core](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/core):Including the core implementation of `dev`, `build` and `Plugin API`;
194193
- [@vuepress/theme-default](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/theme-default):The default theme you see now.
195194

packages/docs/docs/plugin/option-api.md

+26
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,29 @@ Then, VuePress will automatically inject these components behind the layout comp
434434
</div>
435435
</div>
436436
```
437+
438+
## registerCommand
439+
440+
- Type: `function`
441+
- Default: `undefined`
442+
443+
Register a extra command to enhance the CLI of vuepress. The function will be called with a [CAC](https://github.com/cacjs/cac)'s instance as the first argument.
444+
445+
```js
446+
module.exports = {
447+
registerCommand (cli) {
448+
cli
449+
.command('info [targetDir]', '')
450+
.option('--debug', 'display info in debug mode')
451+
.action((dir = '.') => {
452+
console.log('Display info of your website')
453+
})
454+
}
455+
}
456+
```
457+
458+
Now you can use `vuepress info [targetDir]` a in your project!
459+
460+
::: tip
461+
Note that a custom command registered by a plugin requires VuePress to locate your site configuration like `vuepress dev` and `vuepress build`, so when developing a command, be sure to lead the user to pass `targetDir` as an CLI argument.
462+
:::

packages/docs/docs/zh/miscellaneous/design-concepts.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,8 @@ i18n UI 最终的路由将是 `/i18n/`.
187187

188188
## 其他
189189

190-
本着解耦的目标,引入 monorepo 后,我们也得以将 VuePress 分离成以下几个库
190+
本着解耦的目标,引入 monorepo 后,我们也得以将 VuePress 分离成以下两个库
191191

192-
- [@vuepress/cli](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/cli): 命令行指令的管理;
193192
- [@vuepress/core](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/core):包含 dev、build 的核心实现和 Plugin API;
194193
- [@vuepress/theme-default](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/theme-default):你现在所看到的默认主题。
195194

packages/docs/docs/zh/plugin/option-api.md

+27
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,30 @@ VuePress 将会自动将这些组件注入到布局组件的隔壁:
436436
</div>
437437
</div>
438438
```
439+
440+
## registerCommand
441+
442+
- 类型: `function`
443+
- 默认值: `undefined`
444+
445+
注册一个额外的 command 来增强 vuepress 的 CLI。这个函数将会以一个 [CAC](https://github.com/cacjs/cac) 的实例作为第一个参数被调用。
446+
447+
```js
448+
module.exports = {
449+
registerCommand (cli) {
450+
cli
451+
.command('info [targetDir]', '')
452+
.option('--debug', 'display info in debug mode')
453+
.action((dir = '.') => {
454+
console.log('Display info of your website')
455+
})
456+
}
457+
}
458+
```
459+
460+
现在你可以在你项目中使用 `vuepress info [targetDir]` 了!
461+
462+
::: tip
463+
值得注意的是,一个自定义的 command 需要 VuePress 像 `vuepress dev``vuepress build` 去定位到你的站点配置,所以在开发一个 command 时,请确保引导用户去传入 `targetDir` 作为 CLI 参数的一部分。
464+
:::
465+

packages/docs/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"description": "docs of VuePress",
66
"scripts": {
77
"dev": "vuepress dev docs --temp .temp",
8-
"build": "vuepress build docs --temp .temp"
8+
"build": "vuepress build docs --temp .temp",
9+
"show-help": "vuepress --help"
910
},
1011
"repository": {
1112
"type": "git",

packages/vuepress/lib/checkEnv.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const { chalk } = require('@vuepress/shared-utils')
8+
const semver = require('semver')
9+
10+
/**
11+
* Expose handleUnknownCommand function.
12+
*/
13+
14+
module.exports = function checkEnv (pkg) {
15+
const requiredVersion = pkg.engines.node
16+
17+
if (!semver.satisfies(process.version, requiredVersion)) {
18+
console.log(chalk.red(
19+
`\n[vuepress] minimum Node version not met:` +
20+
`\nYou are using Node ${process.version}, but VuePress ` +
21+
`requires Node ${requiredVersion}.\nPlease upgrade your Node version.\n`
22+
))
23+
process.exit(1)
24+
}
25+
}

0 commit comments

Comments
 (0)