Skip to content

Commit f8017ca

Browse files
authored
chore: upgrade commander to v7 (#6242)
1 parent e7985a3 commit f8017ca

File tree

4 files changed

+27
-51
lines changed

4 files changed

+27
-51
lines changed

packages/@vue/cli/bin/vue.js

+21-45
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ program
5757
.option('-x, --proxy <proxyUrl>', 'Use specified proxy when creating project')
5858
.option('-b, --bare', 'Scaffold project without beginner instructions')
5959
.option('--skipGetStarted', 'Skip displaying "Get started" instructions')
60-
.action((name, cmd) => {
61-
const options = cleanArgs(cmd)
62-
60+
.action((name, options) => {
6361
if (minimist(process.argv.slice(3))._.length > 1) {
6462
console.log(chalk.yellow('\n Info: You provided more than one argument. The first one will be used as the app\'s name, the rest are ignored.'))
6563
}
@@ -97,8 +95,8 @@ program
9795
.option('--rules', 'list all module rule names')
9896
.option('--plugins', 'list all plugin names')
9997
.option('-v --verbose', 'Show full function definitions in output')
100-
.action((paths, cmd) => {
101-
require('../lib/inspect')(paths, cleanArgs(cmd))
98+
.action((paths, options) => {
99+
require('../lib/inspect')(paths, options)
102100
})
103101

104102
program
@@ -124,9 +122,9 @@ program
124122
.option('-D, --dev', 'Run in dev mode')
125123
.option('--quiet', `Don't output starting messages`)
126124
.option('--headless', `Don't open browser on start and output port`)
127-
.action((cmd) => {
125+
.action((options) => {
128126
checkNodeVersion('>=8.6', 'vue ui')
129-
require('../lib/ui')(cleanArgs(cmd))
127+
require('../lib/ui')(options)
130128
})
131129

132130
program
@@ -146,16 +144,16 @@ program
146144
.option('-d, --delete <path>', 'delete option from config')
147145
.option('-e, --edit', 'open config with default editor')
148146
.option('--json', 'outputs JSON result only')
149-
.action((value, cmd) => {
150-
require('../lib/config')(value, cleanArgs(cmd))
147+
.action((value, options) => {
148+
require('../lib/config')(value, options)
151149
})
152150

153151
program
154152
.command('outdated')
155153
.description('(experimental) check for outdated vue cli service / plugins')
156154
.option('--next', 'Also check for alpha / beta / rc versions when upgrading')
157-
.action((cmd) => {
158-
require('../lib/outdated')(cleanArgs(cmd))
155+
.action((options) => {
156+
require('../lib/outdated')(options)
159157
})
160158

161159
program
@@ -166,17 +164,16 @@ program
166164
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies')
167165
.option('--all', 'Upgrade all plugins')
168166
.option('--next', 'Also check for alpha / beta / rc versions when upgrading')
169-
.action((packageName, cmd) => {
170-
require('../lib/upgrade')(packageName, cleanArgs(cmd))
167+
.action((packageName, options) => {
168+
require('../lib/upgrade')(packageName, options)
171169
})
172170

173171
program
174172
.command('migrate [plugin-name]')
175173
.description('(experimental) run migrator for an already-installed cli plugin')
176-
// TODO: use `requiredOption` after upgrading to commander 4.x
177-
.option('-f, --from <version>', 'The base version for the migrator to migrate from')
178-
.action((packageName, cmd) => {
179-
require('../lib/migrate')(packageName, cleanArgs(cmd))
174+
.requiredOption('-f, --from <version>', 'The base version for the migrator to migrate from')
175+
.action((packageName, options) => {
176+
require('../lib/migrate')(packageName, options)
180177
})
181178

182179
program
@@ -201,15 +198,13 @@ program
201198
})
202199

203200
// output help information on unknown commands
204-
program
205-
.arguments('<command>')
206-
.action((cmd) => {
207-
program.outputHelp()
208-
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`))
209-
console.log()
210-
suggestCommands(cmd)
211-
process.exitCode = 1
212-
})
201+
program.on('command:*', ([cmd]) => {
202+
program.outputHelp()
203+
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`))
204+
console.log()
205+
suggestCommands(cmd)
206+
process.exitCode = 1
207+
})
213208

214209
// add some useful info on help
215210
program.on('--help', () => {
@@ -259,22 +254,3 @@ function suggestCommands (unknownCommand) {
259254
console.log(` ` + chalk.red(`Did you mean ${chalk.yellow(suggestion)}?`))
260255
}
261256
}
262-
263-
function camelize (str) {
264-
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '')
265-
}
266-
267-
// commander passes the Command object itself as options,
268-
// extract only actual options into a fresh object.
269-
function cleanArgs (cmd) {
270-
const args = {}
271-
cmd.options.forEach(o => {
272-
const key = camelize(o.long.replace(/^--/, ''))
273-
// if an option is not present and Command has a method with the same name
274-
// it should not be copied
275-
if (typeof cmd[key] !== 'function' && typeof cmd[key] !== 'undefined') {
276-
args[key] = cmd[key]
277-
}
278-
})
279-
return args
280-
}

packages/@vue/cli/lib/migrate.js

-5
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ async function runMigrator (context, plugin, pkg = getPkg(context)) {
7979
}
8080

8181
async function migrate (pluginId, { from }, context = process.cwd()) {
82-
// TODO: remove this after upgrading to commander 4.x
83-
if (!from) {
84-
throw new Error(`Required option 'from' not specified`)
85-
}
86-
8782
const pluginName = resolvePluginId(pluginId)
8883
const pluginMigrator = loadModule(`${pluginName}/migrator`, context)
8984
if (!pluginMigrator) {

packages/@vue/cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"@vue/cli-ui-addon-webpack": "^5.0.0-alpha.3",
3333
"@vue/cli-ui-addon-widgets": "^5.0.0-alpha.3",
3434
"boxen": "^4.1.0",
35-
"commander": "^2.20.0",
35+
"commander": "^7.0.0",
3636
"debug": "^4.1.0",
3737
"deepmerge": "^4.2.2",
3838
"download-git-repo": "^3.0.2",

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -7567,6 +7567,11 @@ commander@^6.2.0:
75677567
resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.1.tgz#0792eb682dfbc325999bb2b84fddddba110ac73c"
75687568
integrity sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==
75697569

7570+
commander@^7.0.0:
7571+
version "7.0.0"
7572+
resolved "https://registry.yarnpkg.com/commander/-/commander-7.0.0.tgz#3e2bbfd8bb6724760980988fb5b22b7ee6b71ab2"
7573+
integrity sha512-ovx/7NkTrnPuIV8sqk/GjUIIM1+iUQeqA3ye2VNpq9sVoiZsooObWlQy+OPWGI17GDaEoybuAGJm6U8yC077BA==
7574+
75707575
commander@~2.19.0:
75717576
version "2.19.0"
75727577
resolved "https://registry.yarnpkg.com/commander/-/commander-2.19.0.tgz#f6198aa84e5b83c46054b94ddedbfed5ee9ff12a"

0 commit comments

Comments
 (0)