Skip to content

Commit 472326f

Browse files
committed
feat: coc config --keyName=value
1 parent 9f3b803 commit 472326f

3 files changed

Lines changed: 12 additions & 122 deletions

File tree

CoCreate.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ module.exports = {
7878
'path': '../CoCreate-conditional-logic',
7979
'repo': 'github.com/CoCreate-app/CoCreate-conditional-logic.git'
8080
},
81+
{
82+
'path': '../CoCreate-config',
83+
'repo': 'github.com/CoCreate-app/CoCreate-config.git'
84+
},
8185
{
8286
'path': '../CoCreate-crdt',
8387
'repo': 'github.com/CoCreate-app/CoCreate-crdt.git',

src/coc.js

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env node
1+
#!/usr/bin/env node
22
const path = require("path");
33
const fs = require("fs");
44
const execute = require('./execute');

src/commands/config.js

Lines changed: 7 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,12 @@
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')
52

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] }
928
}
939

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)
12411

125-
return config;
12612
}

0 commit comments

Comments
 (0)