/
handleUnknownCommand.js
146 lines (118 loc) Β· 3.83 KB
/
handleUnknownCommand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict'
/**
* Module dependencies.
*/
const { createApp } = require('@vuepress/core')
const { path, logger, globby, chalk } = require('@vuepress/shared-utils')
const { isKnownCommand, CLI } = require('./util')
const pwd = process.cwd()
/**
* Expose handleUnknownCommand function.
*/
module.exports = async function (cli, options) {
registerUnknownCommands(cli, options)
const argv = process.argv.slice(2)
const inferredUserDocsDirectory = await inferUserDocsDirectory(pwd)
logger.developer('inferredUserDocsDirectory', inferredUserDocsDirectory)
const needPrepareBeforeLaunchCLI = inferredUserDocsDirectory
&& (isHelpFlag(argv[0]) || isUnknownCommandHelp(argv))
logger.developer('needPrepareBeforeLaunchCLI', needPrepareBeforeLaunchCLI)
if (needPrepareBeforeLaunchCLI) {
let app
let [, sourceDir] = argv
if (!sourceDir || sourceDir.startsWith('-')) {
sourceDir = inferredUserDocsDirectory
} else {
sourceDir = pwd
}
/* Prepare Context START */
logger.setOptions({ logLevel: 1 })
if (sourceDir) {
app = createApp({ sourceDir, ...options })
await app.process()
app.pluginAPI.applySyncOption('extendCli', cli, app)
}
logger.setOptions({ logLevel: 3 })
/* Prepare Context END */
}
}
// When user type `vuepress [customCommand] --help`,
// VuePress will try to detect where user to place docs.
async function inferUserDocsDirectory (cwd) {
const paths = await globby([
'**/.vuepress/config.(js|ts)',
'!**/node_modules/**'
], {
cwd,
dot: true
})
const siteConfigPath = paths && paths[0]
if (siteConfigPath) {
return path.resolve(
cwd,
siteConfigPath.replace(/\.vuepress\/config.(js|ts)/, '')
)
}
return null
}
/**
* Register a command to match all unmatched commands
* @param {CAC} cli
*/
function registerUnknownCommands (cli, options) {
cli.on('command:*', async () => {
const { args, options: commandoptions } = cli
logger.debug('global_options', options)
logger.debug('cli_options', commandoptions)
logger.debug('cli_args', args)
const [commandName] = args
const sourceDir = args[1] ? path.resolve(args[1]) : pwd
const inferredUserDocsDirectory = await inferUserDocsDirectory(pwd)
logger.developer('inferredUserDocsDirectory', inferredUserDocsDirectory)
logger.developer('sourceDir', sourceDir)
if (inferredUserDocsDirectory && sourceDir !== inferredUserDocsDirectory) {
logUnknownCommand(cli)
console.log()
logger.tip(`Did you miss to specify the target docs dir? e.g. ${chalk.cyan(`vuepress ${commandName} [targetDir]`)}.`)
logger.tip(`A custom command registered by a plugin requires VuePress to locate your site configuration like ${chalk.cyan('vuepress dev')} or ${chalk.cyan('vuepress build')}.`)
console.log()
process.exit(1)
}
if (!inferredUserDocsDirectory) {
logUnknownCommand(cli)
process.exit(1)
}
logger.debug('Custom command', chalk.cyan(commandName))
CLI({
async beforeParse (subCli) {
/* Prepare Context START */
logger.setOptions({ logLevel: 1 })
const app = createApp({
sourceDir: sourceDir,
...options,
...commandoptions
})
await app.process()
logger.setOptions({ logLevel: 3 })
/* Prepare Context END */
app.pluginAPI.applySyncOption('extendCli', subCli, app)
console.log()
},
async afterParse (subCli) {
if (!subCli.matchedCommand) {
logUnknownCommand(subCli)
console.log()
}
}
})
})
}
function isHelpFlag (v) {
return v === '--help' || v === '-h'
}
function isUnknownCommandHelp (argv) {
return !isKnownCommand(argv) && isHelpFlag(argv[1])
}
function logUnknownCommand (cli) {
console.error('Unknown command: %s', cli.args.join(' '))
}