Skip to content

Commit

Permalink
feat(tasks): override args switch, closes #3236
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Apr 13, 2019
1 parent 01d2035 commit bbe4002
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
27 changes: 18 additions & 9 deletions packages/@vue/cli-ui/apollo-server/connectors/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const projects = require('./projects')
const { log } = require('../util/logger')
const { notify } = require('../util/notification')
const { terminate } = require('../util/terminate')
const { parseArgs } = require('../util/parse-args')

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

// Answers
const answers = prompts.getAnswers()
let args = []
let command = task.command

// Process command containing args
if (command.indexOf(' ')) {
const parts = command.split(/\s+/)
command = parts.shift()
args = parts
}
let [command, ...args] = parseArgs(task.command)

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

// Plugin API
if (task.onBeforeRun) {
if (!answers.$_overrideArgs) {
const origPush = args.push.bind(args)
args.push = (...items) => {
if (items.length && args.indexOf(items[0]) !== -1) return items.length
return origPush(...items)
}
}
await task.onBeforeRun({
answers,
args
Expand Down Expand Up @@ -581,6 +581,15 @@ async function restoreParameters ({ id }, context) {
const task = findOne(id, context)
if (task) {
await prompts.reset()
if (task.prompts.length) {
prompts.add({
name: '$_overrideArgs',
type: 'confirm',
default: false,
message: 'org.vue.views.project-task-details.override-args.message',
description: 'org.vue.views.project-task-details.override-args.description'
})
}
task.prompts.forEach(prompts.add)
const data = getSavedData(id, context)
if (data) {
Expand Down
27 changes: 27 additions & 0 deletions packages/@vue/cli-ui/apollo-server/util/parse-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {string} args
*/
exports.parseArgs = function (args) {
const parts = args.split(/\s+|=/)
const result = []
let arg
let index = 0
for (const part of parts) {
const l = part.length
if (!arg && part.charAt(0) === '"') {
arg = part.substr(1)
} else if (part.charAt(l - 1) === '"' && (
l === 1 || part.charAt(l - 2) !== '\\'
)) {
arg += args.charAt(index - 1) + part.substr(0, l - 1)
result.push(arg)
arg = null
} else if (arg) {
arg += args.charAt(index - 1) + part
} else {
result.push(part)
}
index += part.length + 1
}
return result
}
6 changes: 5 additions & 1 deletion packages/@vue/cli-ui/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,11 @@
"command": "Script command",
"parameters": "Parameters",
"more-info": "More Info",
"output": "Output"
"output": "Output",
"override-args": {
"message": "Override hard-coded arguments",
"description": "If enabled, hard-coded arguments in the package.json file will be replaced with the value defined below"
}
},
"project-dependencies": {
"title": "Project dependencies",
Expand Down

0 comments on commit bbe4002

Please sign in to comment.