Skip to content

Commit dde426a

Browse files
author
Guillaume Chau
committed
feat(ui): prompt-list default choice + config field auto-remove
1 parent 3b6c01f commit dde426a

File tree

11 files changed

+92
-44
lines changed

11 files changed

+92
-44
lines changed

packages/@vue/cli-plugin-eslint/ui.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = api => {
2020
message: 'Trailing commas',
2121
description: 'Enforce or disallow trailing commas at the end of the lines',
2222
link: 'https://eslint.org/docs/rules/comma-dangle',
23+
default: JSON.stringify(['error', 'never']),
2324
choices: [
2425
{
2526
name: 'Off',
@@ -42,14 +43,14 @@ module.exports = api => {
4243
value: JSON.stringify(['error', 'only-multiline'])
4344
}
4445
],
45-
value: JSON.stringify(data.rules && data.rules['comma-dangle'] || ['error', 'never'])
46+
value: JSON.stringify(data.rules && data.rules['comma-dangle'])
4647
}
4748
]
4849
}
4950
},
50-
onWrite: ({ file, answers }) => {
51-
file.setData({
52-
'rules.comma-dangle': JSON.parse(answers.rules.commaDangle)
51+
onWrite: ({ api }) => {
52+
api.setData({
53+
'rules.comma-dangle': api.getAnswer('rules.commaDangle', JSON.parse)
5354
})
5455
}
5556
})

packages/@vue/cli-ui/.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
"plugin:vue/essential",
55
"@vue/standard"
66
]
7-
}
7+
}

packages/@vue/cli-ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"lowdb": "^1.0.0",
2929
"lru-cache": "^4.1.2",
3030
"mkdirp": "^0.5.1",
31+
"parse-diff": "^0.4.2",
3132
"rimraf": "^2.6.2",
3233
"semver": "^5.5.0",
3334
"shortid": "^2.2.8",

packages/@vue/cli-ui/src/components/PromptList.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
v-for="(choice, index) of prompt.choices"
2020
:key="index"
2121
:value="value(choice.value)"
22-
:label="choice.name"
22+
:label="generateLabel(choice)"
2323
/>
2424
</VueSelect>
2525
</div>
@@ -33,6 +33,16 @@
3333
import Prompt from './Prompt'
3434
3535
export default {
36-
extends: Prompt
36+
extends: Prompt,
37+
38+
methods: {
39+
generateLabel (choice) {
40+
let label = choice.name
41+
if (choice.isDefault) {
42+
label += ` (${this.$t('components.prompt-list.default')})`
43+
}
44+
return label
45+
}
46+
}
3747
}
3848
</script>

packages/@vue/cli-ui/src/graphql-api/connectors/configurations.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const plugins = require('./plugins')
88
const folders = require('./folders')
99
const prompts = require('./prompts')
1010
// Utils
11-
const { set } = require('../../util/object')
11+
const { get, set, remove } = require('../../util/object')
1212

1313
const fileTypes = ['js', 'json', 'yaml']
1414
let current = {}
@@ -116,15 +116,34 @@ function save (id, context) {
116116
config.onWrite({
117117
answers,
118118
data,
119-
file: {
120-
...config.file,
119+
file: config.file,
120+
api: {
121121
assignData: newData => {
122122
Object.assign(data, newData)
123123
},
124124
setData: newData => {
125125
Object.keys(newData).forEach(key => {
126-
set(data, key, newData[key])
126+
const value = newData[key]
127+
if (typeof value === 'undefined') {
128+
remove(data, key)
129+
} else {
130+
set(data, key, value)
131+
}
127132
})
133+
},
134+
getAnswer: (id, mapper) => {
135+
const prompt = prompts.findOne(id)
136+
if (prompt) {
137+
const defaultValue = prompts.getDefaultValue(prompt)
138+
console.log(defaultValue, prompt.rawValue)
139+
if (defaultValue !== prompt.rawValue) {
140+
let value = get(answers, prompt.id)
141+
if (mapper) {
142+
value = mapper(value)
143+
}
144+
return value
145+
}
146+
}
128147
}
129148
}
130149
})

packages/@vue/cli-ui/src/graphql-api/connectors/prompts.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ const ObjectUtil = require('../../util/object')
33
let answers = {}
44
let prompts = []
55

6-
function getPrompt (id) {
7-
return prompts.find(
8-
p => p.id === id
9-
)
10-
}
11-
126
function generatePromptError (value) {
137
let message
148
if (typeof value === 'string') {
@@ -21,28 +15,6 @@ function generatePromptError (value) {
2115
}
2216
}
2317

24-
function getDefaultValue (prompt) {
25-
if (typeof prompt.raw.value !== 'undefined') {
26-
return prompt.raw.value
27-
}
28-
const defaultValue = prompt.raw.default
29-
if (typeof defaultValue === 'function') {
30-
return defaultValue(answers)
31-
} else if (prompt.type === 'checkbox') {
32-
const choices = getChoices(prompt)
33-
if (choices) {
34-
return choices.filter(
35-
c => c.checked
36-
).map(
37-
c => c.value
38-
)
39-
}
40-
} else if (prompt.type === 'confirm') {
41-
return false
42-
}
43-
return defaultValue
44-
}
45-
4618
function getEnabled (value) {
4719
const type = typeof value
4820
if (type === 'function') {
@@ -78,12 +50,13 @@ function getDisplayedValue (prompt, value) {
7850
return JSON.stringify(value)
7951
}
8052

81-
function generatePromptChoice (prompt, data) {
53+
function generatePromptChoice (prompt, data, defaultValue) {
8254
return {
8355
value: getDisplayedValue(prompt, data.value),
8456
name: data.name,
8557
checked: data.checked,
86-
disabled: data.disabled
58+
disabled: data.disabled,
59+
isDefault: data.value === defaultValue
8760
}
8861
}
8962

@@ -99,8 +72,9 @@ function getChoices (prompt) {
9972
} else {
10073
result = data
10174
}
75+
const defaultValue = getDefaultValue(prompt)
10276
return result.map(
103-
item => generatePromptChoice(prompt, item)
77+
item => generatePromptChoice(prompt, item, defaultValue)
10478
)
10579
}
10680

@@ -149,10 +123,13 @@ function updatePrompts () {
149123
const answer = getAnswer(prompt.id)
150124
if (typeof answer !== 'undefined') {
151125
value = answer
126+
} else if (typeof prompt.raw.value !== 'undefined') {
127+
value = prompt.raw.value
152128
} else {
153129
value = getDefaultValue(prompt)
154130
}
155131
prompt.value = getDisplayedValue(prompt, value)
132+
prompt.rawValue = value
156133
setAnswer(prompt.id, getValue(prompt, value))
157134
}
158135
}
@@ -197,7 +174,7 @@ function remove (id) {
197174
}
198175

199176
function setValue ({ id, value }) {
200-
const prompt = getPrompt(id)
177+
const prompt = findOne(id)
201178
if (!prompt) {
202179
console.warn(`Prompt '${prompt}' not found`)
203180
return null
@@ -210,13 +187,39 @@ function setValue ({ id, value }) {
210187
prompt.error = null
211188
}
212189
prompt.value = getDisplayedValue(prompt, value)
190+
prompt.rawValue = value
213191
const finalValue = getValue(prompt, value)
214192
prompt.valueChanged = true
215193
setAnswer(prompt.id, finalValue)
216194
updatePrompts()
217195
return prompt
218196
}
219197

198+
function findOne (id) {
199+
return prompts.find(
200+
p => p.id === id
201+
)
202+
}
203+
204+
function getDefaultValue (prompt) {
205+
const defaultValue = prompt.raw.default
206+
if (typeof defaultValue === 'function') {
207+
return defaultValue(answers)
208+
} else if (prompt.type === 'checkbox') {
209+
const choices = getChoices(prompt)
210+
if (choices) {
211+
return choices.filter(
212+
c => c.checked
213+
).map(
214+
c => c.value
215+
)
216+
}
217+
} else if (prompt.type === 'confirm') {
218+
return defaultValue || false
219+
}
220+
return defaultValue
221+
}
222+
220223
module.exports = {
221224
setAnswers,
222225
changeAnswers,
@@ -226,5 +229,7 @@ module.exports = {
226229
add,
227230
remove,
228231
start,
229-
setValue
232+
setValue,
233+
findOne,
234+
getDefaultValue
230235
}

packages/@vue/cli-ui/src/graphql-api/type-defs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ type PromptChoice {
124124
name: String
125125
checked: Boolean
126126
disabled: Boolean
127+
isDefault: Boolean
127128
}
128129
129130
type PromptError {

packages/@vue/cli-ui/src/graphql/promptChoiceFragment.gql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ fragment promptChoice on PromptChoice {
33
name
44
checked
55
disabled
6+
isDefault
67
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
"update": "Update {target}"
5858
}
5959
},
60+
"prompt-list": {
61+
"default": "Default"
62+
},
6063
"prompts-list": {
6164
"empty": "No configuration"
6265
},

packages/@vue/cli-ui/src/locales/fr.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
"update": "Mettre à jour {target}"
5858
}
5959
},
60+
"prompt-list": {
61+
"default": "Par défaut"
62+
},
6063
"prompts-list": {
6164
"empty": "Pas de configuration"
6265
},

0 commit comments

Comments
 (0)