mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-01-12 18:19:39 -06:00
* feat(generator): allow plugins to modify how configs are extracted * refactor(cli): change addConfigTransform parameters Allow plugin author to set config 'descriptions' instead of implementing their own transform functions. * fix(cli): fix missed issues from changing types from array to set * fix: use reserved config transforms to check in API * fix: lines dedupe
66 lines
1.2 KiB
JavaScript
66 lines
1.2 KiB
JavaScript
const transforms = require('./util/configTransforms')
|
|
|
|
class ConfigTransform {
|
|
constructor (options) {
|
|
this.fileDescriptor = options.file
|
|
}
|
|
|
|
transform (value, checkExisting, files, context) {
|
|
let file
|
|
if (checkExisting) {
|
|
file = this.findFile(files)
|
|
}
|
|
if (!file) {
|
|
file = this.getDefaultFile()
|
|
}
|
|
const { type, filename } = file
|
|
|
|
const transform = transforms[type]
|
|
|
|
let source
|
|
let existing
|
|
if (checkExisting) {
|
|
source = files[filename]
|
|
if (source) {
|
|
existing = transform.read({
|
|
source,
|
|
filename,
|
|
context
|
|
})
|
|
}
|
|
}
|
|
|
|
const content = transform.write({
|
|
source,
|
|
filename,
|
|
context,
|
|
value,
|
|
existing
|
|
})
|
|
|
|
return {
|
|
filename,
|
|
content
|
|
}
|
|
}
|
|
|
|
findFile (files) {
|
|
for (const type of Object.keys(this.fileDescriptor)) {
|
|
const descriptors = this.fileDescriptor[type]
|
|
for (const filename of descriptors) {
|
|
if (files[filename]) {
|
|
return { type, filename }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
getDefaultFile () {
|
|
const [type] = Object.keys(this.fileDescriptor)
|
|
const [filename] = this.fileDescriptor[type]
|
|
return { type, filename }
|
|
}
|
|
}
|
|
|
|
module.exports = ConfigTransform
|