UNPKG

bitbucket-env-manager

Version:

Deploys Bitbucket Environment

114 lines (91 loc) 2.83 kB
#!/usr/bin/env node const fs = require('fs-extra'); const path = require('path'); const { select, spinner, log, note, isCancel, cancel } = require('@clack/prompts'); const color = require('picocolors'); const { names } = require('./config'); const { guarded_get_envs, guarded_get_vars } = 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 list = async () => { try { const filePath = await selectInfoJson('Select repository to view'); 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 view', options: envsList.map((env) => ({ value: env.slug, label: env.name, hint: env.uuid, })), }); if (isCancel(pickedEnv)) { cancel('Operation cancelled.'); return; } const existingEnv = envsList.find((env) => env.slug === pickedEnv.toLowerCase()); if (!existingEnv) { log.error('Environment not found'); return; } s.start('Fetching variables...'); const resp = await guarded_get_vars({ ...mainParams, environment_uuid: existingEnv.uuid, }); s.stop('Variables loaded'); const vars = JSON.parse(resp).values; if (vars.length === 0) { log.info('No variables in this environment'); return; } // Find max key length for alignment const maxKeyLen = Math.max(...vars.map((v) => v.key.length), 10); const varLines = vars.map((v) => { const key = v.key.padEnd(maxKeyLen); if (v.secured) { return `${color.cyan(key)} = ${color.yellow('(secured)')}`; } return `${key} = "${v.value}"`; }); note(varLines.join('\n'), `${existingEnv.name} (${vars.length} variables)`); } catch (e) { log.error(e.message); } }; module.exports = { list, };