@cocreate/cli
Advanced tools
Comparing version 1.30.0 to 1.31.0
@@ -0,1 +1,9 @@ | ||
# [1.31.0](https://github.com/CoCreate-app/CoCreate-cli/compare/v1.30.0...v1.31.0) (2023-06-09) | ||
### Features | ||
* exposed functions to module.exports ([1a11a4a](https://github.com/CoCreate-app/CoCreate-cli/commit/1a11a4a49041e10b31e4aa9ad358da3da0ec4c01)) | ||
* Refactor configuration loading logic, add prompts for env variables, and allow nested choices ([63c8567](https://github.com/CoCreate-app/CoCreate-cli/commit/63c8567bfb1cd700d1f921685fe7812cb3dc1919)) | ||
# [1.30.0](https://github.com/CoCreate-app/CoCreate-cli/compare/v1.29.3...v1.30.0) (2023-06-07) | ||
@@ -2,0 +10,0 @@ |
module.exports = { | ||
"config": { | ||
"organization_id": "", | ||
"key": "", | ||
"host": "" | ||
}, | ||
"organization_id": "", | ||
"key": "", | ||
"host": "", | ||
"sources": [ | ||
@@ -8,0 +6,0 @@ { |
{ | ||
"name": "@cocreate/cli", | ||
"version": "1.30.0", | ||
"version": "1.31.0", | ||
"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.", | ||
@@ -56,3 +56,3 @@ "keywords": [ | ||
}, | ||
"main": "index.js", | ||
"main": "src/commands/index.js", | ||
"bin": { | ||
@@ -62,3 +62,3 @@ "coc": "src/coc.js" | ||
"dependencies": { | ||
"@cocreate/docs": "^1.7.15", | ||
"@cocreate/docs": "^1.8.2", | ||
"@cocreate/file": "^1.0.0", | ||
@@ -65,0 +65,0 @@ "colors": "latest", |
const readline = require('readline'); | ||
const { promises: fs } = require("fs"); | ||
const os = require('os'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
module.exports = async function CoCreateConfig(config = {}) { | ||
module.exports = async function CoCreateConfig(items, processEnv = true, updateGlobal = true) { | ||
async function promptForInput(question) { | ||
@@ -23,45 +21,81 @@ const rl = readline.createInterface({ | ||
// Check if the config file exists | ||
const configFilePath = path.join(os.homedir(), 'CoCreateConfig.json'); | ||
try { | ||
const configFileContent = await fs.readFile(configFilePath, 'utf8'); | ||
config = JSON.parse(configFileContent); | ||
} catch (error) { | ||
// Ignore error if the file doesn't exist | ||
} | ||
const filterEmptyValues = (obj) => { | ||
return Object.fromEntries( | ||
Object.entries(obj).filter(([_, value]) => { | ||
if (typeof value === 'object' && !Array.isArray(value)) { | ||
return Object.keys(value).length > 0; | ||
} else if (Array.isArray(value)) { | ||
return value.length > 0; | ||
} else { | ||
return value !== ''; | ||
} | ||
}) | ||
); | ||
}; | ||
// Prompt user for organization ID if not already stored | ||
if (!config.organization_id) | ||
config.organization_id = await promptForInput('Enter your organization_id: '); | ||
let config = {}; | ||
let update = false; | ||
if (!config.host) | ||
config.host = await promptForInput('Enter the host: '); | ||
async function getConfig(items) { | ||
if (!Array.isArray(items)) { | ||
items = [items]; | ||
} | ||
for (let i = 0; i < items.length; i++) { | ||
const { key, prompt, choices } = items[i]; | ||
if (!key) { | ||
if (!prompt && prompt !== '' || !choices) continue; | ||
const answer = await promptForInput(prompt || `${key}: `); | ||
const choice = choices[answer]; | ||
if (choice) { | ||
await getConfig(choice); | ||
} | ||
} else { | ||
if (process.env[key]) { | ||
config[key] = process.env[key]; | ||
} else if (localConfig[key]) { | ||
config[key] = localConfig[key]; | ||
} else if (globalConfig[key]) { | ||
config[key] = globalConfig[key]; | ||
} else if (prompt || prompt === '') { | ||
config[key] = await promptForInput(prompt || `${key}: `); | ||
if (processEnv) process.env[key] = config[key]; | ||
if (updateGlobal) update = true; | ||
} | ||
} | ||
} | ||
} | ||
async function promptForSignInOrKey() { | ||
const option = await promptForInput('Choose an option:\n1. Sign In\n2. Enter Key\n'); | ||
let localConfig = {}; | ||
const localConfigPath = path.resolve(process.cwd(), 'CoCreate.config.js'); | ||
if (fs.existsSync(localConfigPath)) { | ||
localConfig = require(localConfigPath); | ||
} | ||
if (option === '1') { | ||
if (!config.email) | ||
config.email = await promptForInput('Enter your email: '); | ||
let globalConfig = {}; | ||
const globalConfigPath = path.resolve(os.homedir(), 'CoCreate.config.js'); | ||
if (fs.existsSync(globalConfigPath)) { | ||
globalConfig = require(globalConfigPath); | ||
} | ||
if (!config.password) | ||
config.password = await promptForInput('Enter your password: '); | ||
if (items) { | ||
await getConfig(items); | ||
} else if (option === '2') { | ||
if (!config.key) | ||
config.key = await promptForInput('Enter the key: '); | ||
} else { | ||
console.log('Invalid option. Please try again.'); | ||
await promptForSignInOrKey(); | ||
if (update) { | ||
const updatedGlobalConfig = { | ||
...filterEmptyValues(globalConfig), | ||
...filterEmptyValues(config) | ||
}; | ||
const globalConfigString = `module.exports = ${JSON.stringify(updatedGlobalConfig, null, 2)};`; | ||
fs.writeFileSync(globalConfigPath, globalConfigString); | ||
} | ||
} else { | ||
config = { | ||
...filterEmptyValues(globalConfig), | ||
...filterEmptyValues(localConfig) | ||
}; | ||
} | ||
if (!config.key && (!config.email || !config.password)) | ||
await promptForSignInOrKey(); | ||
// Save the config to the file | ||
await fs.writeFile(configFilePath, JSON.stringify(config, null, 2)); | ||
return config | ||
return config; | ||
} |
@@ -7,3 +7,3 @@ const crud = require('@cocreate/crud-client') | ||
module.exports = async function upload(repos, args) { | ||
file | ||
await file() | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
169770
45
2382
41
Updated@cocreate/docs@^1.8.2