UNPKG

bitbucket-env-manager

Version:

Deploys Bitbucket Environment

123 lines (109 loc) 3.44 kB
#!/usr/bin/env node const fs = require('fs-extra'); const path = require('path'); const inquirer = require('inquirer'); const { names } = require('./config'); const { guarded_delete_env, guarded_get_envs } = 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 delete_env = 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[31mSelect env to DELETE from the existing environments in bitbucket\n\n\x1b[0m`, choices: envsList.map((env) => env.slug), }, ]); const conf = await inquirer.prompt([ /* Pass your questions in here */ { type: 'confirm', name: 'confirm', message: `\n\x1b[31mAre you sure to DELETE "${pick.pickedEnv}" and all variables from "${repo}" of "${workspace}"?\n\n\x1b[0m`, }, ]); if (!conf.confirm) { console.log('Skipped!!'); return; } const existingEnv = envsList.find((env) => env.slug === pick.pickedEnv.toLowerCase()); if (existingEnv) { // Remove Existing Env if forced console.warn('\nRemoving env!', existingEnv.name, existingEnv.uuid); // Remove env await guarded_delete_env({ ...mainParams, environment_uuid: existingEnv.uuid, }); console.log( '\x1b[32m%s\x1b[0m', `\n\n------ REMOVED ENV "${existingEnv.name}" for "${repo}" ---------` ); } else { throw new Error('Env Not Found!'); } } catch (e) { console.error(e.message); } }; module.exports = { delete_env, };