1 | 'use strict'
|
2 |
|
3 | const path = require('path')
|
4 | const fs = require('fs')
|
5 | const PLUGIN_PATH = require('./path').PLUGIN_PATH
|
6 |
|
7 | const filename = 'config.json'
|
8 | const filePath = path.join(PLUGIN_PATH, filename)
|
9 |
|
10 | const formatBoolean = value => {
|
11 | if (value === 'true') {
|
12 | return true
|
13 | } else if (value === 'false') {
|
14 | return false
|
15 | }
|
16 |
|
17 | return value
|
18 | }
|
19 | const requireFile = () => {
|
20 | return require(filePath)
|
21 | }
|
22 |
|
23 | exports.init = () => {
|
24 | if (!fs.existsSync(filePath)) {
|
25 | const config = {
|
26 | template: 'vue',
|
27 | registry: '',
|
28 | updateCheck: true,
|
29 | github: '',
|
30 | author: ''
|
31 | }
|
32 |
|
33 | fs.writeFileSync(filePath, JSON.stringify(config, null, 2))
|
34 | }
|
35 | }
|
36 |
|
37 | exports.get = option => {
|
38 | if (!option) {
|
39 | return requireFile()
|
40 | }
|
41 |
|
42 | return requireFile()[option]
|
43 | }
|
44 |
|
45 | exports.set = (option, value) => {
|
46 | const config = requireFile()
|
47 |
|
48 | if (config[option] !== undefined) {
|
49 | config[option] = formatBoolean(value)
|
50 | fs.writeFileSync(filePath, JSON.stringify(config, null, 2))
|
51 |
|
52 | return true
|
53 | }
|
54 |
|
55 | return false
|
56 | }
|