@cocreate/cli 1.30.0 → 1.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # [1.31.0](https://github.com/CoCreate-app/CoCreate-cli/compare/v1.30.0...v1.31.0) (2023-06-09)
2
+
3
+
4
+ ### Features
5
+
6
+ * exposed functions to module.exports ([1a11a4a](https://github.com/CoCreate-app/CoCreate-cli/commit/1a11a4a49041e10b31e4aa9ad358da3da0ec4c01))
7
+ * Refactor configuration loading logic, add prompts for env variables, and allow nested choices ([63c8567](https://github.com/CoCreate-app/CoCreate-cli/commit/63c8567bfb1cd700d1f921685fe7812cb3dc1919))
8
+
1
9
  # [1.30.0](https://github.com/CoCreate-app/CoCreate-cli/compare/v1.29.3...v1.30.0) (2023-06-07)
2
10
 
3
11
 
@@ -1,9 +1,7 @@
1
1
  module.exports = {
2
- "config": {
3
- "organization_id": "",
4
- "key": "",
5
- "host": ""
6
- },
2
+ "organization_id": "",
3
+ "key": "",
4
+ "host": "",
7
5
  "sources": [
8
6
  {
9
7
  "collection": "files",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/cli",
3
- "version": "1.30.0",
3
+ "version": "1.31.0",
4
4
  "description": "Polyrepo management bash CLI tool. Run all git commands and yarn commands on multiple repositories. Also includes a few custom macros for cloning, installing, etc.",
5
5
  "keywords": [
6
6
  "cli",
@@ -53,12 +53,12 @@
53
53
  "type": "GitHub Sponsors ❤",
54
54
  "url": "https://github.com/sponsors/CoCreate-app"
55
55
  },
56
- "main": "index.js",
56
+ "main": "src/commands/index.js",
57
57
  "bin": {
58
58
  "coc": "src/coc.js"
59
59
  },
60
60
  "dependencies": {
61
- "@cocreate/docs": "^1.7.15",
61
+ "@cocreate/docs": "^1.8.2",
62
62
  "@cocreate/file": "^1.0.0",
63
63
  "colors": "latest",
64
64
  "glob": "^7.1.7",
@@ -1,11 +1,9 @@
1
1
  const readline = require('readline');
2
- const { promises: fs } = require("fs");
3
2
  const os = require('os');
4
3
  const path = require('path');
4
+ const fs = require('fs');
5
5
 
6
-
7
- module.exports = async function CoCreateConfig(config = {}) {
8
-
6
+ module.exports = async function CoCreateConfig(items, processEnv = true, updateGlobal = true) {
9
7
  async function promptForInput(question) {
10
8
  const rl = readline.createInterface({
11
9
  input: process.stdin,
@@ -20,47 +18,83 @@ module.exports = async function CoCreateConfig(config = {}) {
20
18
  });
21
19
  }
22
20
 
23
- // Check if the config file exists
24
- const configFilePath = path.join(os.homedir(), 'CoCreateConfig.json');
25
- try {
26
- const configFileContent = await fs.readFile(configFilePath, 'utf8');
27
- config = JSON.parse(configFileContent);
28
- } catch (error) {
29
- // Ignore error if the file doesn't exist
30
- }
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
+ };
31
34
 
32
- // Prompt user for organization ID if not already stored
33
- if (!config.organization_id)
34
- config.organization_id = await promptForInput('Enter your organization_id: ');
35
+ let config = {};
36
+ let update = false;
35
37
 
36
- if (!config.host)
37
- config.host = await promptForInput('Enter the host: ');
38
+ async function getConfig(items) {
39
+ if (!Array.isArray(items)) {
40
+ items = [items];
41
+ }
42
+ for (let i = 0; i < items.length; i++) {
43
+ const { key, prompt, choices } = items[i];
44
+ if (!key) {
45
+ if (!prompt && prompt !== '' || !choices) continue;
46
+ const answer = await promptForInput(prompt || `${key}: `);
47
+ const choice = choices[answer];
48
+ if (choice) {
49
+ await getConfig(choice);
50
+ }
51
+ } else {
38
52
 
53
+ if (process.env[key]) {
54
+ config[key] = process.env[key];
55
+ } else if (localConfig[key]) {
56
+ config[key] = localConfig[key];
57
+ } else if (globalConfig[key]) {
58
+ config[key] = globalConfig[key];
59
+ } else if (prompt || prompt === '') {
60
+ config[key] = await promptForInput(prompt || `${key}: `);
61
+ if (processEnv) process.env[key] = config[key];
62
+ if (updateGlobal) update = true;
63
+ }
64
+ }
65
+ }
66
+ }
39
67
 
40
- async function promptForSignInOrKey() {
41
- const option = await promptForInput('Choose an option:\n1. Sign In\n2. Enter Key\n');
68
+ let localConfig = {};
69
+ const localConfigPath = path.resolve(process.cwd(), 'CoCreate.config.js');
70
+ if (fs.existsSync(localConfigPath)) {
71
+ localConfig = require(localConfigPath);
72
+ }
73
+
74
+ let globalConfig = {};
75
+ const globalConfigPath = path.resolve(os.homedir(), 'CoCreate.config.js');
76
+ if (fs.existsSync(globalConfigPath)) {
77
+ globalConfig = require(globalConfigPath);
78
+ }
42
79
 
43
- if (option === '1') {
44
- if (!config.email)
45
- config.email = await promptForInput('Enter your email: ');
80
+ if (items) {
81
+ await getConfig(items);
46
82
 
47
- if (!config.password)
48
- config.password = await promptForInput('Enter your password: ');
83
+ if (update) {
84
+ const updatedGlobalConfig = {
85
+ ...filterEmptyValues(globalConfig),
86
+ ...filterEmptyValues(config)
87
+ };
49
88
 
50
- } else if (option === '2') {
51
- if (!config.key)
52
- config.key = await promptForInput('Enter the key: ');
53
- } else {
54
- console.log('Invalid option. Please try again.');
55
- await promptForSignInOrKey();
89
+ const globalConfigString = `module.exports = ${JSON.stringify(updatedGlobalConfig, null, 2)};`;
90
+ fs.writeFileSync(globalConfigPath, globalConfigString);
56
91
  }
92
+ } else {
93
+ config = {
94
+ ...filterEmptyValues(globalConfig),
95
+ ...filterEmptyValues(localConfig)
96
+ };
57
97
  }
58
- if (!config.key && (!config.email || !config.password))
59
- await promptForSignInOrKey();
60
-
61
-
62
- // Save the config to the file
63
- await fs.writeFile(configFilePath, JSON.stringify(config, null, 2));
64
98
 
65
- return config
99
+ return config;
66
100
  }
@@ -0,0 +1,3 @@
1
+ const config = require('./config.js');
2
+
3
+ module.exports = { config }
@@ -4,5 +4,5 @@ const fs = require('fs');
4
4
  const file = require('@cocreate/file')
5
5
 
6
6
  module.exports = async function upload(repos, args) {
7
- file
7
+ await file()
8
8
  }