bitbucket-env-manager
Version:
Deploys Bitbucket Environment
104 lines (84 loc) • 2.6 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const { select, confirm, spinner, log, isCancel, cancel } = require('@clack/prompts');
const color = require('picocolors');
const { names } = require('./config');
const { guarded_delete_env, guarded_get_envs } = require('./guarded.api');
const { selectInfoJson } = require('./ui');
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 delete_env = async () => {
try {
const filePath = await selectInfoJson('Select repository');
if (!filePath) return;
const { workspace, repo } = getInfoJsonParams(filePath);
const mainParams = {
repo_slug: repo,
workspace: workspace,
};
const s = spinner();
s.start('Fetching environments...');
// Get existing env details
const envsListString = await guarded_get_envs({ ...mainParams });
const envsList = JSON.parse(envsListString).values;
s.stop('Environments loaded');
if (!envsList.length) {
log.warn(`No environments found for ${repo}`);
return;
}
const pickedEnv = await select({
message: `Select environment to ${color.red('DELETE')}`,
options: envsList.map((env) => ({
value: env.slug,
label: env.name,
hint: env.uuid,
})),
});
if (isCancel(pickedEnv)) {
cancel('Operation cancelled.');
return;
}
const shouldDelete = await confirm({
message: `Delete "${color.red(pickedEnv)}" and all variables from "${color.cyan(repo)}"?`,
initialValue: false,
});
if (isCancel(shouldDelete) || !shouldDelete) {
log.info('Skipped!');
return;
}
const existingEnv = envsList.find((env) => env.slug === pickedEnv.toLowerCase());
if (!existingEnv) {
log.error('Environment not found');
return;
}
s.start('Deleting environment...');
await guarded_delete_env({
...mainParams,
environment_uuid: existingEnv.uuid,
});
s.stop(color.green(`Removed "${existingEnv.name}" from ${repo}`));
} catch (e) {
log.error(e.message);
}
};
module.exports = {
delete_env,
};