|
1 | | -const readline = require('readline'); |
2 | | -const os = require('os'); |
3 | | -const path = require('path'); |
4 | | -const fs = require('fs'); |
| 1 | +const config = require('@cocreate/config') |
5 | 2 |
|
6 | | -module.exports = async function CoCreateConfig(items, processEnv = true, updateGlobal = true) { |
7 | | - async function promptForInput(question) { |
8 | | - const rl = readline.createInterface({ |
9 | | - input: process.stdin, |
10 | | - output: process.stdout |
11 | | - }); |
12 | | - |
13 | | - return new Promise((resolve) => { |
14 | | - rl.question(question, (answer) => { |
15 | | - rl.close(); |
16 | | - resolve(answer.trim()); |
17 | | - }); |
18 | | - }); |
19 | | - } |
20 | | - |
21 | | - const filterEmptyValues = (obj) => { |
22 | | - return Object.fromEntries( |
23 | | - Object.entries(obj).filter(([_, value]) => { |
24 | | - if (typeof value === 'object' && !Array.isArray(value)) { |
25 | | - return Object.keys(value).length > 0; |
26 | | - } else if (Array.isArray(value)) { |
27 | | - return value.length > 0; |
28 | | - } else { |
29 | | - return value !== ''; |
30 | | - } |
31 | | - }) |
32 | | - ); |
33 | | - }; |
34 | | - |
35 | | - let config = {}; |
36 | | - let update = false; |
37 | | - |
38 | | - async function getConfig(items) { |
39 | | - if (!Array.isArray(items)) { |
40 | | - items = [items]; |
41 | | - } |
42 | | - for (let i = 0; i < items.length; i++) { |
43 | | - let { key, prompt, choices } = items[i]; |
44 | | - if (!key) { |
45 | | - if (!prompt && prompt !== '' || !choices) continue; |
46 | | - for (let choice of Object.keys(choices)) { |
47 | | - // if (!Array.isArray(choiceItems)) { |
48 | | - // choiceItems = [choiceItems]; |
49 | | - // } |
50 | | - // for (let choice of choiceItems) { |
51 | | - let choiceKey = choices[choice].key |
52 | | - if (process.env[choiceKey]) { |
53 | | - config[choiceKey] = process.env[choiceKey]; |
54 | | - return; |
55 | | - } else if (localConfig[choiceKey]) { |
56 | | - config[choiceKey] = localConfig[choiceKey]; |
57 | | - return; |
58 | | - |
59 | | - } else if (globalConfig[choiceKey]) { |
60 | | - config[choiceKey] = globalConfig[choiceKey]; |
61 | | - return; |
62 | | - } |
63 | | - } |
64 | | - // } |
65 | | - const answer = await promptForInput(prompt || `${key}: `); |
66 | | - const choice = choices[answer]; |
67 | | - if (choice) { |
68 | | - await getConfig(choice); |
69 | | - } |
70 | | - } else { |
71 | | - |
72 | | - if (process.env[key]) { |
73 | | - config[key] = process.env[key]; |
74 | | - } else { |
75 | | - if (localConfig[key]) { |
76 | | - config[key] = localConfig[key]; |
77 | | - } else if (globalConfig[key]) { |
78 | | - config[key] = globalConfig[key]; |
79 | | - } else if (prompt || prompt === '') { |
80 | | - config[key] = await promptForInput(prompt || `${key}: `); |
81 | | - if (updateGlobal) update = true; |
82 | | - } |
83 | | - if (processEnv) { |
84 | | - if (typeof config[key] === 'object') |
85 | | - process.env[key] = JSON.stringify(config[key]); |
86 | | - else |
87 | | - process.env[key] = config[key]; |
88 | | - } |
89 | | - } |
90 | | - } |
91 | | - } |
| 3 | +module.exports = async function (repos, args) { |
| 4 | + let object = {} |
| 5 | + for (let arg of args) { |
| 6 | + arg = arg.split('=') |
| 7 | + object[arg[0].substring(2)] = { value: arg[1] } |
92 | 8 | } |
93 | 9 |
|
94 | | - let localConfig = {}; |
95 | | - const localConfigPath = path.resolve(process.cwd(), 'CoCreate.config.js'); |
96 | | - if (fs.existsSync(localConfigPath)) { |
97 | | - localConfig = require(localConfigPath); |
98 | | - } |
99 | | - |
100 | | - let globalConfig = {}; |
101 | | - const globalConfigPath = path.resolve(os.homedir(), 'CoCreate.config.js'); |
102 | | - if (fs.existsSync(globalConfigPath)) { |
103 | | - globalConfig = require(globalConfigPath); |
104 | | - } |
105 | | - |
106 | | - if (items) { |
107 | | - await getConfig(items); |
108 | | - |
109 | | - if (update) { |
110 | | - const updatedGlobalConfig = { |
111 | | - ...filterEmptyValues(globalConfig), |
112 | | - ...filterEmptyValues(config) |
113 | | - }; |
114 | | - |
115 | | - const globalConfigString = `module.exports = ${JSON.stringify(updatedGlobalConfig, null, 2)};`; |
116 | | - fs.writeFileSync(globalConfigPath, globalConfigString); |
117 | | - } |
118 | | - } else { |
119 | | - config = { |
120 | | - ...filterEmptyValues(globalConfig), |
121 | | - ...filterEmptyValues(localConfig) |
122 | | - }; |
123 | | - } |
| 10 | + await config(object) |
124 | 11 |
|
125 | | - return config; |
126 | 12 | } |
0 commit comments