import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; import chalk = require('chalk'); import { exit } from 'process'; import { defaultMenuSettings, sleep } from '../../../../globals'; const { AutoComplete } = require('enquirer'); type Pipeline = { name: string; version: number; created: string; updated: string; }; type StageState = { stageName: string; latestExecution: { status: string; }; }; module.exports = { name: 'status', alias: ['s'], description: 'Pipeline Status (s)', hidden: false, run: async (toolbox: GluegunMenuToolbox) => { const { system, strings, print } = toolbox; const pipelines = JSON.parse( strings.trim(await system.run(`aws codepipeline list-pipelines`)), ); // const testPipelines = pipelines.pipelines.filter(item => { // return item.name.indexOf('photowall-test-') > -1; // }); const curratedPipes: Pipeline[] = pipelines.pipelines.filter(Boolean); const options = curratedPipes.map((item) => { return item.name; }); const prompt = new AutoComplete({ name: 'pipelines', message: 'Choose a pipeline', limit: options.length - 1, initial: options.length - 1, choices: options, }); try { const pipelineName = await prompt.run(); const spinner = print.spin(`Tracking pipeline status`); const getArrow = (pos: number) => { const o = '---------------'.split(''); // 0-14 return o.map((char, index) => { if (index === pos) { return chalk.bold.magenta('>'); } return chalk.cyan(char); }); }; while (true) { const pipelineState = JSON.parse( await system.run( `aws codepipeline get-pipeline-state --name ${pipelineName}`, { trim: true }, ), ); const states = {}; for (const state of pipelineState.stageStates) { const s = state.latestExecution.status; switch (s) { case 'InProgress': states[state.stageName] = chalk.blue('IN PROGRESS'); break; case 'Succeeded': states[state.stageName] = chalk.green('DONE'); break; case 'Failed': states[state.stageName] = chalk.red('FAILED'); break; } } let pos = 0; while (pos < 15) { const arrow = getArrow(pos); arrow.splice(15, 0, ` Deploy: [${states['Deploy']}] `); arrow.splice(10, 0, ` Build: [${states['Build']}] `); arrow.splice(5, 0, ` Source: [${states['Source']}] `); spinner.text = arrow.join(''); pos += 1; await sleep(500); } } } catch (e) { console.log(`e`, e); } if (toolbox.fromMenu()) { await toolbox.menu.showMenu('aws pipeline', defaultMenuSettings); } else { exit(); } }, };