Skip to content

Commit

Permalink
feat(ui): prompt-list default choice + config field auto-remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Mar 26, 2018
1 parent 3b6c01f commit dde426a
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 44 deletions.
9 changes: 5 additions & 4 deletions packages/@vue/cli-plugin-eslint/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = api => {
message: 'Trailing commas',
description: 'Enforce or disallow trailing commas at the end of the lines',
link: 'https://eslint.org/docs/rules/comma-dangle',
default: JSON.stringify(['error', 'never']),
choices: [
{
name: 'Off',
Expand All @@ -42,14 +43,14 @@ module.exports = api => {
value: JSON.stringify(['error', 'only-multiline'])
}
],
value: JSON.stringify(data.rules && data.rules['comma-dangle'] || ['error', 'never'])
value: JSON.stringify(data.rules && data.rules['comma-dangle'])
}
]
}
},
onWrite: ({ file, answers }) => {
file.setData({
'rules.comma-dangle': JSON.parse(answers.rules.commaDangle)
onWrite: ({ api }) => {
api.setData({
'rules.comma-dangle': api.getAnswer('rules.commaDangle', JSON.parse)
})
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-ui/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"plugin:vue/essential",
"@vue/standard"
]
}
}
1 change: 1 addition & 0 deletions packages/@vue/cli-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"lowdb": "^1.0.0",
"lru-cache": "^4.1.2",
"mkdirp": "^0.5.1",
"parse-diff": "^0.4.2",
"rimraf": "^2.6.2",
"semver": "^5.5.0",
"shortid": "^2.2.8",
Expand Down
14 changes: 12 additions & 2 deletions packages/@vue/cli-ui/src/components/PromptList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
v-for="(choice, index) of prompt.choices"
:key="index"
:value="value(choice.value)"
:label="choice.name"
:label="generateLabel(choice)"
/>
</VueSelect>
</div>
Expand All @@ -33,6 +33,16 @@
import Prompt from './Prompt'
export default {
extends: Prompt
extends: Prompt,
methods: {
generateLabel (choice) {
let label = choice.name
if (choice.isDefault) {
label += ` (${this.$t('components.prompt-list.default')})`
}
return label
}
}
}
</script>
27 changes: 23 additions & 4 deletions packages/@vue/cli-ui/src/graphql-api/connectors/configurations.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const plugins = require('./plugins')
const folders = require('./folders')
const prompts = require('./prompts')
// Utils
const { set } = require('../../util/object')
const { get, set, remove } = require('../../util/object')

const fileTypes = ['js', 'json', 'yaml']
let current = {}
Expand Down Expand Up @@ -116,15 +116,34 @@ function save (id, context) {
config.onWrite({
answers,
data,
file: {
...config.file,
file: config.file,
api: {
assignData: newData => {
Object.assign(data, newData)
},
setData: newData => {
Object.keys(newData).forEach(key => {
set(data, key, newData[key])
const value = newData[key]
if (typeof value === 'undefined') {
remove(data, key)
} else {
set(data, key, value)
}
})
},
getAnswer: (id, mapper) => {
const prompt = prompts.findOne(id)
if (prompt) {
const defaultValue = prompts.getDefaultValue(prompt)
console.log(defaultValue, prompt.rawValue)
if (defaultValue !== prompt.rawValue) {
let value = get(answers, prompt.id)
if (mapper) {
value = mapper(value)
}
return value
}
}
}
}
})
Expand Down
71 changes: 38 additions & 33 deletions packages/@vue/cli-ui/src/graphql-api/connectors/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ const ObjectUtil = require('../../util/object')
let answers = {}
let prompts = []

function getPrompt (id) {
return prompts.find(
p => p.id === id
)
}

function generatePromptError (value) {
let message
if (typeof value === 'string') {
Expand All @@ -21,28 +15,6 @@ function generatePromptError (value) {
}
}

function getDefaultValue (prompt) {
if (typeof prompt.raw.value !== 'undefined') {
return prompt.raw.value
}
const defaultValue = prompt.raw.default
if (typeof defaultValue === 'function') {
return defaultValue(answers)
} else if (prompt.type === 'checkbox') {
const choices = getChoices(prompt)
if (choices) {
return choices.filter(
c => c.checked
).map(
c => c.value
)
}
} else if (prompt.type === 'confirm') {
return false
}
return defaultValue
}

function getEnabled (value) {
const type = typeof value
if (type === 'function') {
Expand Down Expand Up @@ -78,12 +50,13 @@ function getDisplayedValue (prompt, value) {
return JSON.stringify(value)
}

function generatePromptChoice (prompt, data) {
function generatePromptChoice (prompt, data, defaultValue) {
return {
value: getDisplayedValue(prompt, data.value),
name: data.name,
checked: data.checked,
disabled: data.disabled
disabled: data.disabled,
isDefault: data.value === defaultValue
}
}

Expand All @@ -99,8 +72,9 @@ function getChoices (prompt) {
} else {
result = data
}
const defaultValue = getDefaultValue(prompt)
return result.map(
item => generatePromptChoice(prompt, item)
item => generatePromptChoice(prompt, item, defaultValue)
)
}

Expand Down Expand Up @@ -149,10 +123,13 @@ function updatePrompts () {
const answer = getAnswer(prompt.id)
if (typeof answer !== 'undefined') {
value = answer
} else if (typeof prompt.raw.value !== 'undefined') {
value = prompt.raw.value
} else {
value = getDefaultValue(prompt)
}
prompt.value = getDisplayedValue(prompt, value)
prompt.rawValue = value
setAnswer(prompt.id, getValue(prompt, value))
}
}
Expand Down Expand Up @@ -197,7 +174,7 @@ function remove (id) {
}

function setValue ({ id, value }) {
const prompt = getPrompt(id)
const prompt = findOne(id)
if (!prompt) {
console.warn(`Prompt '${prompt}' not found`)
return null
Expand All @@ -210,13 +187,39 @@ function setValue ({ id, value }) {
prompt.error = null
}
prompt.value = getDisplayedValue(prompt, value)
prompt.rawValue = value
const finalValue = getValue(prompt, value)
prompt.valueChanged = true
setAnswer(prompt.id, finalValue)
updatePrompts()
return prompt
}

function findOne (id) {
return prompts.find(
p => p.id === id
)
}

function getDefaultValue (prompt) {
const defaultValue = prompt.raw.default
if (typeof defaultValue === 'function') {
return defaultValue(answers)
} else if (prompt.type === 'checkbox') {
const choices = getChoices(prompt)
if (choices) {
return choices.filter(
c => c.checked
).map(
c => c.value
)
}
} else if (prompt.type === 'confirm') {
return defaultValue || false
}
return defaultValue
}

module.exports = {
setAnswers,
changeAnswers,
Expand All @@ -226,5 +229,7 @@ module.exports = {
add,
remove,
start,
setValue
setValue,
findOne,
getDefaultValue
}
1 change: 1 addition & 0 deletions packages/@vue/cli-ui/src/graphql-api/type-defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type PromptChoice {
name: String
checked: Boolean
disabled: Boolean
isDefault: Boolean
}
type PromptError {
Expand Down
1 change: 1 addition & 0 deletions packages/@vue/cli-ui/src/graphql/promptChoiceFragment.gql
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ fragment promptChoice on PromptChoice {
name
checked
disabled
isDefault
}
3 changes: 3 additions & 0 deletions packages/@vue/cli-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"update": "Update {target}"
}
},
"prompt-list": {
"default": "Default"
},
"prompts-list": {
"empty": "No configuration"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/@vue/cli-ui/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"update": "Mettre à jour {target}"
}
},
"prompt-list": {
"default": "Par défaut"
},
"prompts-list": {
"empty": "Pas de configuration"
},
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7846,6 +7846,10 @@ parse-asn1@^5.0.0:
evp_bytestokey "^1.0.0"
pbkdf2 "^3.0.3"

parse-diff@^0.4.2:
version "0.4.2"
resolved "https://registry.yarnpkg.com/parse-diff/-/parse-diff-0.4.2.tgz#b173390e916564e8c70ccd37756047941e5b3ef2"

parse-github-repo-url@^1.3.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/parse-github-repo-url/-/parse-github-repo-url-1.4.1.tgz#9e7d8bb252a6cb6ba42595060b7bf6df3dbc1f50"
Expand Down

0 comments on commit dde426a

Please sign in to comment.