UNPKG

2.07 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const path = require('path')
3const homedir = require('os').homedir()
4const { get, set, unset, error, launch } = require('@vue/cli-shared-utils')
5
6async function configure (value, options) {
7 const file = path.resolve(homedir, '.vuerc')
8 const config = await fs.readJson(file)
9
10 if (!options.delete && !options.get && !options.edit && !options.set) {
11 if (options.json) {
12 console.log(JSON.stringify({
13 resolvedPath: file,
14 content: config
15 }))
16 } else {
17 console.log('Resolved path: ' + file + '\n', JSON.stringify(config, null, 2))
18 }
19 }
20
21 if (options.get) {
22 // eslint-disable-next-line no-shadow
23 const value = get(config, options.get)
24 if (options.json) {
25 console.log(JSON.stringify({
26 value
27 }))
28 } else {
29 console.log(value)
30 }
31 }
32
33 if (options.delete) {
34 unset(config, options.delete)
35 await fs.writeFile(file, JSON.stringify(config, null, 2), 'utf-8')
36 if (options.json) {
37 console.log(JSON.stringify({
38 deleted: options.delete
39 }))
40 } else {
41 console.log(`You have removed the option: ${options.delete}`)
42 }
43 }
44
45 if (options.edit) {
46 launch(file)
47 }
48
49 if (options.set && !value) {
50 throw new Error(`Make sure you define a value for the option ${options.set}`)
51 }
52
53 if (options.set && value) {
54 set(config, options.set, value)
55
56 if (value.match('[0-9]')) {
57 set(config, options.set, parseInt(value))
58 }
59
60 if (value === 'true') {
61 set(config, options.set, true)
62 }
63
64 if (value === 'false') {
65 set(config, options.set, false)
66 }
67
68 await fs.writeFile(file, JSON.stringify(config, null, 2), 'utf-8')
69 if (options.json) {
70 console.log(JSON.stringify({
71 updated: options.set
72 }))
73 } else {
74 console.log(`You have updated the option: ${options.set} to ${value}`)
75 }
76 }
77}
78
79module.exports = (...args) => {
80 return configure(...args).catch(err => {
81 error(err)
82 if (!process.env.VUE_CLI_TEST) {
83 process.exit(1)
84 }
85 })
86}