UNPKG

1.92 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require('fs-extra')
4const path = require('path')
5
6const {
7 error,
8 success,
9} = require('./util')
10
11const globalConfigPath = path.join(__dirname, '../config/config.global.json')
12const globalConfig = require(globalConfigPath)
13
14if (!globalConfig) {
15 error('读取全局配置文件失败')
16
17 process.exit(1)
18}
19
20/**
21 * 设置配置
22 * 保留了以前的老逻辑,现在只能设置全局配置
23 * 具体项目的配置由各个项目自己维护,工具不应该维护具体业务逻辑
24 * @param {object} options
25 * @param {object} configurations
26 */
27const 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
81module.exports = (options, configurations) => setConfig(options, configurations)