bitbucket-env-manager
Version:
Deploys Bitbucket Environment
118 lines (105 loc) • 3.2 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const inquirer = require('inquirer');
const { names } = require('./config');
const {
guarded_delete_env,
guarded_get_envs,
guarded_get_env,
guarded_get_vars,
} = require('./guarded.api');
const getInfoJsonParams = (infoJsonPath) => {
const infoRaw = fs.readFileSync(infoJsonPath, { encoding: 'utf-8' });
const infoJson = JSON.parse(infoRaw);
if (
!infoJson.workspace ||
!infoJson.repo ||
!infoJson.secured ||
!Array.isArray(infoJson.secured)
) {
throw new Error(
`Please specify {workspace:, repo:, secured:[]} under "${names.infoJsonFile}" file`
);
}
return {
workspace: infoJson.workspace,
repo: infoJson.repo,
secured: infoJson.secured,
};
};
const list = async () => {
try {
inquirer.registerPrompt('file-tree-selection', require('inquirer-file-tree-selection-prompt'));
const basePath = path.join(process.cwd());
const answers = await inquirer.prompt([
/* Pass your questions in here */
{
type: 'file-tree-selection',
name: 'filePath',
loop: false,
pageSize: 20,
message: `\n\x1b[45m\x1b[33mSelect info.json file for the desired repo\n\x1b[0m\x1b[36m"Arrow keys" -> Navigate\n"Enter" -> Select file\n\x1b[0m`,
root: basePath,
validate: (item) => {
const name = item.split(path.sep).pop();
if (name !== 'info.json') {
return 'please select info.json file';
}
return true;
},
onlyShowValid: false,
multiple: false,
},
]);
const filePath = answers.filePath;
const { workspace, repo } = getInfoJsonParams(filePath);
const mainParams = {
repo_slug: repo,
workspace: workspace,
};
// Get existing env details
const envsListString = await guarded_get_envs({
...mainParams,
});
const envsList = JSON.parse(envsListString).values;
if (!envsList.length) {
throw new Error('No Existing Envs found in bitbucket for', repo);
}
const pick = await inquirer.prompt([
/* Pass your questions in here */
{
type: 'list',
name: 'pickedEnv',
loop: false,
pageSize: 20,
message: `\n\x1b[36mSelect env to VIEW from the existing environments in bitbucket\n\n\x1b[0m`,
choices: envsList.map((env) => env.slug),
},
]);
const existingEnv = envsList.find((env) => env.slug === pick.pickedEnv.toLowerCase());
if (existingEnv) {
// const resp1 = await guarded_get_env({
// ...mainParams,
// environment_uuid: existingEnv.uuid,
// });
// console.log(resp1);
const resp = await guarded_get_vars({
...mainParams,
environment_uuid: existingEnv.uuid,
});
JSON.parse(resp).values.forEach((v) => {
v.secured
? console.log('\x1b[36m%s\x1b[0m', `${v.key}="🔒🔒🔒"`)
: console.log(`${v.key}="${v.value}"`);
});
} else {
throw new Error('Env Not Found!');
}
} catch (e) {
console.error(e.message);
}
};
module.exports = {
list,
};