1 | #!/usr/bin/env node
|
2 |
|
3 | const fs = require('fs-extra')
|
4 | const path = require('path')
|
5 |
|
6 | const {
|
7 | error,
|
8 | success,
|
9 | } = require('./util')
|
10 |
|
11 | const globalConfigPath = path.join(__dirname, '../config/config.global.json')
|
12 | const globalConfig = require(globalConfigPath)
|
13 |
|
14 | if (!globalConfig) {
|
15 | error('读取全局配置文件失败')
|
16 |
|
17 | process.exit(1)
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const setConfig = async (options = {}, configurations = {}) => {
|
28 | const {
|
29 | type = 'single',
|
30 | target = 'global',
|
31 | key,
|
32 | value,
|
33 | } = options
|
34 | const isGlobal = target === 'global'
|
35 |
|
36 | if (type === 'single') {
|
37 | if (!key || !value) {
|
38 | error('key and value are required.\n')
|
39 |
|
40 | process.exit(1)
|
41 | }
|
42 |
|
43 | if (isGlobal) {
|
44 | globalConfig[key] = value
|
45 | } else {
|
46 |
|
47 | projConfigMap[target] || (projConfigMap[target] = {})
|
48 | projConfigMap[target][key] = value
|
49 | }
|
50 | } else if (type === 'multiple') {
|
51 | if (isGlobal) {
|
52 | globalConfig = {
|
53 | ...globalConfig,
|
54 | ...configurations,
|
55 | }
|
56 | } else {
|
57 | const { target } = options
|
58 | data.projConfigMap[target] = configurations
|
59 | }
|
60 | }
|
61 |
|
62 | const config = JSON.stringify(globalConfig, null, 2)
|
63 |
|
64 | return new Promise((resolve, reject) => {
|
65 | fs.writeFile(globalConfigPath, config, (error) => {
|
66 | if (!error) {
|
67 | if (type === 'single') {
|
68 | success(`set ${target} config: ${key} with value: ${value} success.\n`)
|
69 | } else if (type === 'multiple') {
|
70 | success(`set ${target}'s configurations success.\n`)
|
71 | }
|
72 |
|
73 | resolve()
|
74 | } else {
|
75 | throw error
|
76 | }
|
77 | })
|
78 | })
|
79 | }
|
80 |
|
81 | module.exports = (options, configurations) => setConfig(options, configurations)
|