Skip to content

Commit bbe4002

Browse files
author
Guillaume Chau
committed
feat(tasks): override args switch, closes #3236
1 parent 01d2035 commit bbe4002

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

packages/@vue/cli-ui/apollo-server/connectors/tasks.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const projects = require('./projects')
1414
const { log } = require('../util/logger')
1515
const { notify } = require('../util/notification')
1616
const { terminate } = require('../util/terminate')
17+
const { parseArgs } = require('../util/parse-args')
1718

1819
const MAX_LOGS = 2000
1920
const VIEW_ID = 'vue-project-tasks'
@@ -237,22 +238,21 @@ async function run (id, context) {
237238

238239
// Answers
239240
const answers = prompts.getAnswers()
240-
let args = []
241-
let command = task.command
242-
243-
// Process command containing args
244-
if (command.indexOf(' ')) {
245-
const parts = command.split(/\s+/)
246-
command = parts.shift()
247-
args = parts
248-
}
241+
let [command, ...args] = parseArgs(task.command)
249242

250243
// Output colors
251244
// See: https://www.npmjs.com/package/supports-color
252245
process.env.FORCE_COLOR = 1
253246

254247
// Plugin API
255248
if (task.onBeforeRun) {
249+
if (!answers.$_overrideArgs) {
250+
const origPush = args.push.bind(args)
251+
args.push = (...items) => {
252+
if (items.length && args.indexOf(items[0]) !== -1) return items.length
253+
return origPush(...items)
254+
}
255+
}
256256
await task.onBeforeRun({
257257
answers,
258258
args
@@ -581,6 +581,15 @@ async function restoreParameters ({ id }, context) {
581581
const task = findOne(id, context)
582582
if (task) {
583583
await prompts.reset()
584+
if (task.prompts.length) {
585+
prompts.add({
586+
name: '$_overrideArgs',
587+
type: 'confirm',
588+
default: false,
589+
message: 'org.vue.views.project-task-details.override-args.message',
590+
description: 'org.vue.views.project-task-details.override-args.description'
591+
})
592+
}
584593
task.prompts.forEach(prompts.add)
585594
const data = getSavedData(id, context)
586595
if (data) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {string} args
3+
*/
4+
exports.parseArgs = function (args) {
5+
const parts = args.split(/\s+|=/)
6+
const result = []
7+
let arg
8+
let index = 0
9+
for (const part of parts) {
10+
const l = part.length
11+
if (!arg && part.charAt(0) === '"') {
12+
arg = part.substr(1)
13+
} else if (part.charAt(l - 1) === '"' && (
14+
l === 1 || part.charAt(l - 2) !== '\\'
15+
)) {
16+
arg += args.charAt(index - 1) + part.substr(0, l - 1)
17+
result.push(arg)
18+
arg = null
19+
} else if (arg) {
20+
arg += args.charAt(index - 1) + part
21+
} else {
22+
result.push(part)
23+
}
24+
index += part.length + 1
25+
}
26+
return result
27+
}

packages/@vue/cli-ui/locales/en.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,11 @@
448448
"command": "Script command",
449449
"parameters": "Parameters",
450450
"more-info": "More Info",
451-
"output": "Output"
451+
"output": "Output",
452+
"override-args": {
453+
"message": "Override hard-coded arguments",
454+
"description": "If enabled, hard-coded arguments in the package.json file will be replaced with the value defined below"
455+
}
452456
},
453457
"project-dependencies": {
454458
"title": "Project dependencies",

0 commit comments

Comments
 (0)