import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; import { exit } from 'process'; import { getTestStacksInfo } from '../../../../common/testservers'; const chalk = require('chalk'); const { AutoComplete, Input, Confirm } = require('enquirer'); import { defaultMenuSettings, getSettings } from '../../../../globals'; import { getRepoBranches } from '../../../../services/github_rest'; const getConfirmation = async (branchName: string, stackName: string) => { const url = stackName.replace('photowall-', ''); const confirm = new Confirm({ name: 'confirm', message: `Will create ${chalk.yellow( url + '.photowall-test.xx', )} and publish ${chalk.green(branchName)} to it, continue?`, }); try { return confirm.run(); } catch (e) { console.error(e); return false; } }; module.exports = { name: 'create', alias: ['c'], description: 'Create new testserver on aws (c)', hidden: false, run: async (toolbox: GluegunMenuToolbox) => { const { system, strings, print } = toolbox; // Load settings const config = await getSettings(toolbox); if (!config || !config.photowall_repo || config.photowall_repo === '') { print.error(` The create command requires that you set an absolute path to your local photowall repository. `); await toolbox.menu.showMenu('setup', defaultMenuSettings); return; } const branches = await getRepoBranches( toolbox, config.gh_token, 'Photowall', 'photowall', ); const branchOptions = branches.map((item) => item.name); const promptBranch = new AutoComplete({ name: 'branchName', message: 'Select a branch', limit: 10, initial: 10, choices: branchOptions, }); const branchName = await promptBranch.run(); const testStacks = await getTestStacksInfo(toolbox); const options = testStacks.map((item) => { return item.name; }); const input = new Input({ type: 'input', name: 'stack', message: `Subdomain of new server (${chalk.yellow('test-xxx')})`, }); const stacksuffix = await input.run(); const stackName = `photowall-${stacksuffix}`; if (options.includes(stackName)) { print.error(` The stack ${chalk.yellow( stackName, )} is already taken, try the update command instead. `); if (toolbox.fromMenu()) { await toolbox.menu.showMenu('deployment testservers'); } else { exit(); } } else if (stackName.indexOf('photowall-test-') !== 0) { print.error(` The stack must begin with ${chalk.yellow( 'test-', )} and then something after like ${chalk.yellow('test-yourname-1')}. `); if (toolbox.fromMenu()) { await toolbox.menu.showMenu( 'deployment testservers', defaultMenuSettings, ); } else { exit(); } } try { const shouldRun = await getConfirmation(branchName, stackName); if (shouldRun) { const cmd = `make create-stack STACK_ENVIRONMENT_NAME=${stacksuffix} GITHUB_BRANCH=${branchName}`; print.fancy( `Running [${chalk.yellow( cmd, )}] in folder [${`${config.photowall_repo}/cloudformation/`}]`, ); const output = await system.run(cmd, { cwd: `${config.photowall_repo}/cloudformation/`, trim: true, }); print.fancy(output); } } catch (e) { console.log(`e`, e); } if (toolbox.fromMenu()) { await toolbox.menu.showMenu( 'deployment testservers', defaultMenuSettings, ); } }, };