Files
vue-cli/packages/@vue/cli/lib/config.js
Payton Burdette 153c418555 feat: vue config command (#1554)
* fixed broken plugin dev link on contributing guide

* feat(cli-service): vue config command added

* feat(cli-service): vue config command added

* feat(cli-service): added config commands get and delete

* feat(cli-service): added vue edit command and opn dependcy

* feat(cli-service): added vue config set command and vue config check

* feat(cli-service): nested path support and command/logs adjustment

* feat(cli-service): command option descriptions updated with preset

* refactor: object get/set/unset

* feat: json option + fs/JSON fixes
2018-07-12 00:36:59 +02:00

87 lines
2.0 KiB
JavaScript

const fs = require('fs-extra')
const path = require('path')
const homedir = require('os').homedir()
const { get, set, unset, error } = require('@vue/cli-shared-utils')
const launch = require('launch-editor')
async function config (value, options) {
const file = path.resolve(homedir, '.vuerc')
const config = await fs.readJson(file)
if (!options.delete && !options.get && !options.edit && !options.set) {
if (options.json) {
console.log(JSON.stringify({
resolvedPath: file,
content: config
}))
} else {
console.log('Resolved path: ' + file + '\n', JSON.stringify(config, null, 2))
}
}
if (options.get) {
const value = get(config, options.get)
if (options.json) {
console.log(JSON.stringify({
value
}))
} else {
console.log(value)
}
}
if (options.delete) {
unset(config, options.delete)
await fs.writeFile(file, JSON.stringify(config, null, 2), 'utf-8')
if (options.json) {
console.log(JSON.stringify({
deleted: options.delete
}))
} else {
console.log(`You have removed the option: ${options.delete}`)
}
}
if (options.edit) {
launch(file)
}
if (options.set && !value) {
throw new Error(`Make sure you define a value for the option ${options.set}`)
}
if (options.set && value) {
set(config, options.set, value)
if (value.match('[0-9]')) {
set(config, options.set, parseInt(value))
}
if (value === 'true') {
set(config, options.set, true)
}
if (value === 'false') {
set(config, options.set, false)
}
await fs.writeFile(file, JSON.stringify(config, null, 2), 'utf-8')
if (options.json) {
console.log(JSON.stringify({
updated: options.set
}))
} else {
console.log(`You have updated the option: ${options.set} to ${value}`)
}
}
}
module.exports = (...args) => {
return config(...args).catch(err => {
error(err)
if (!process.env.VUE_CLI_TEST) {
process.exit(1)
}
})
}