@cocreate/cli 1.30.0 → 1.31.1

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,18 @@
1
+ ## [1.31.1](https://github.com/CoCreate-app/CoCreate-cli/compare/v1.31.0...v1.31.1) (2023-06-10)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Update file dependency version and add console colors, minor code refactoring to CoCreate Config. ([c836d3a](https://github.com/CoCreate-app/CoCreate-cli/commit/c836d3ae070c41704300f5af646b708a01f8f63b))
7
+
8
+ # [1.31.0](https://github.com/CoCreate-app/CoCreate-cli/compare/v1.30.0...v1.31.0) (2023-06-09)
9
+
10
+
11
+ ### Features
12
+
13
+ * exposed functions to module.exports ([1a11a4a](https://github.com/CoCreate-app/CoCreate-cli/commit/1a11a4a49041e10b31e4aa9ad358da3da0ec4c01))
14
+ * Refactor configuration loading logic, add prompts for env variables, and allow nested choices ([63c8567](https://github.com/CoCreate-app/CoCreate-cli/commit/63c8567bfb1cd700d1f921685fe7812cb3dc1919))
15
+
1
16
  # [1.30.0](https://github.com/CoCreate-app/CoCreate-cli/compare/v1.29.3...v1.30.0) (2023-06-07)
2
17
 
3
18
 
@@ -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.1",
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,13 +53,13 @@
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",
62
- "@cocreate/file": "^1.0.0",
61
+ "@cocreate/docs": "^1.8.2",
62
+ "@cocreate/file": "^1.1.0",
63
63
  "colors": "latest",
64
64
  "glob": "^7.1.7",
65
65
  "prettier": "^2.3.2"
package/src/coc.js CHANGED
@@ -1,9 +1,10 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
  const path = require("path");
3
3
  const fs = require("fs");
4
4
  const execute = require('./execute');
5
5
  const argv = process.argv.slice(2);
6
6
  const addMeta = require('./addMeta');
7
+ const colors = require('colors');
7
8
 
8
9
 
9
10
  if (argv.length < 1) {
@@ -57,7 +58,7 @@ if (config['c'] && fs.existsSync(config['c'])) {
57
58
  }];
58
59
  directory = path.dirname(packageJsonPath);
59
60
  console.warn(`using ${packageJsonPath} configuration`.yellow);
60
- }
61
+ }
61
62
  // else {
62
63
  // console.error(`a configuration file can not be found`.red);
63
64
  // process.exit(1);
@@ -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,102 @@ 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
- }
31
-
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: ');
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
+ };
35
34
 
36
- if (!config.host)
37
- config.host = await promptForInput('Enter the host: ');
35
+ let config = {};
36
+ let update = false;
38
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;
39
58
 
40
- async function promptForSignInOrKey() {
41
- const option = await promptForInput('Choose an option:\n1. Sign In\n2. Enter Key\n');
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 {
42
71
 
43
- if (option === '1') {
44
- if (!config.email)
45
- config.email = await promptForInput('Enter your email: ');
72
+ if (process.env[key]) {
73
+ config[key] = process.env[key];
74
+ } else if (localConfig[key]) {
75
+ config[key] = localConfig[key];
76
+ } else if (globalConfig[key]) {
77
+ config[key] = globalConfig[key];
78
+ } else if (prompt || prompt === '') {
79
+ config[key] = await promptForInput(prompt || `${key}: `);
80
+ if (processEnv) process.env[key] = config[key];
81
+ if (updateGlobal) update = true;
82
+ }
83
+ }
84
+ }
85
+ }
46
86
 
47
- if (!config.password)
48
- config.password = await promptForInput('Enter your password: ');
87
+ let localConfig = {};
88
+ const localConfigPath = path.resolve(process.cwd(), 'CoCreate.config.js');
89
+ if (fs.existsSync(localConfigPath)) {
90
+ localConfig = require(localConfigPath);
91
+ }
49
92
 
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();
56
- }
93
+ let globalConfig = {};
94
+ const globalConfigPath = path.resolve(os.homedir(), 'CoCreate.config.js');
95
+ if (fs.existsSync(globalConfigPath)) {
96
+ globalConfig = require(globalConfigPath);
57
97
  }
58
- if (!config.key && (!config.email || !config.password))
59
- await promptForSignInOrKey();
60
98
 
99
+ if (items) {
100
+ await getConfig(items);
61
101
 
62
- // Save the config to the file
63
- await fs.writeFile(configFilePath, JSON.stringify(config, null, 2));
102
+ if (update) {
103
+ const updatedGlobalConfig = {
104
+ ...filterEmptyValues(globalConfig),
105
+ ...filterEmptyValues(config)
106
+ };
107
+
108
+ const globalConfigString = `module.exports = ${JSON.stringify(updatedGlobalConfig, null, 2)};`;
109
+ fs.writeFileSync(globalConfigPath, globalConfigString);
110
+ }
111
+ } else {
112
+ config = {
113
+ ...filterEmptyValues(globalConfig),
114
+ ...filterEmptyValues(localConfig)
115
+ };
116
+ }
64
117
 
65
- return config
118
+ return config;
66
119
  }
@@ -0,0 +1,3 @@
1
+ const config = require('./config.js');
2
+
3
+ module.exports = { config }
@@ -1,8 +1,5 @@
1
- const crud = require('@cocreate/crud-client')
2
- const mime = require('mime-types')
3
- const fs = require('fs');
4
1
  const file = require('@cocreate/file')
5
2
 
6
3
  module.exports = async function upload(repos, args) {
7
- file
4
+ await file()
8
5
  }