From c00be4305e2a272d71661d0e1ef0757e2236a2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arwid=20Thornstro=CC=88m?= Date: Fri, 21 Jan 2022 15:23:19 +0100 Subject: [PATCH] added support for current branch, and some cleanup --- .vscode/settings.json | 15 +++++++++ .../deployment/testservers/create/create.ts | 22 ++++--------- .../deployment/testservers/update/update.ts | 27 +++++++--------- src/commands/setup/settings/settings.ts | 20 ++++++++---- src/globals.ts | 32 +++++++++++++++++++ 5 files changed, 78 insertions(+), 38 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cb24c0d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,15 @@ +{ + "sqltools.connections": [ + { + "previewLimit": 50, + "server": "localhost", + "port": 5432, + "driver": "PostgreSQL", + "name": "Local photowall", + "group": "Photowall", + "database": "photowall", + "username": "arwidthornstrom", + "password": "" + } + ] +} diff --git a/src/commands/deployment/testservers/create/create.ts b/src/commands/deployment/testservers/create/create.ts index a44c2c8..4cdca78 100644 --- a/src/commands/deployment/testservers/create/create.ts +++ b/src/commands/deployment/testservers/create/create.ts @@ -3,7 +3,7 @@ const chalk = require('chalk'); const { Input, Confirm } = require('enquirer'); import { Octokit } from "@octokit/core"; import { GluegunPrint } from 'gluegun'; -import { defaultMenuSettings } from '../../../../globals'; +import { defaultMenuSettings, getSettings } from '../../../../globals'; const os = require("os"); const STACK_DEFAULTS = { @@ -119,20 +119,12 @@ module.exports = { const { system, strings, print, filesystem } = toolbox; // Load settings - const config = await filesystem.readAsync(`${os.homedir()}/.pwcli_settings`, "json"); + const config = await getSettings(toolbox); if (!config) { - print.error(` - - No config found, please run [ ${chalk.cyan('pwcli s')} ] or setup from the menu. - - `); - if (toolbox.fromMenu()) { - await toolbox.menu.showMenu(null, defaultMenuSettings) - } else { - return; - } + await toolbox.menu.showMenu('setup', defaultMenuSettings); + return; } - + let branchName = null; while (!branchName) { branchName = await getBranchName(print, config.gh_token); @@ -238,11 +230,11 @@ module.exports = { print.fancy(await system.run(createCommand, {cwd: '/tmp/', trim: true})); } } catch (e) { - console.log(`e`, e) + console.log(`e`, e); } if (toolbox.fromMenu()) { - await toolbox.menu.showMenu('deployment testservers', defaultMenuSettings) + await toolbox.menu.showMenu('deployment testservers', defaultMenuSettings); } } } diff --git a/src/commands/deployment/testservers/update/update.ts b/src/commands/deployment/testservers/update/update.ts index 60c9107..90d322d 100644 --- a/src/commands/deployment/testservers/update/update.ts +++ b/src/commands/deployment/testservers/update/update.ts @@ -3,7 +3,7 @@ const chalk = require('chalk'); const { AutoComplete, Input, Confirm } = require('enquirer'); import { Octokit } from "@octokit/core"; import { GluegunPrint } from 'gluegun'; -import { defaultMenuSettings } from '../../../../globals'; +import { defaultMenuSettings, getCurrentBranch, getSettings } from '../../../../globals'; const os = require("os"); const STACK_DEFAULTS = { @@ -68,11 +68,14 @@ const getUpdateStackCommand = (stackName: string, branch: string, templateFile: return cmd.join(' '); } -const getBranchName = async (print: GluegunPrint, token: string) => { +const getBranchName = async (toolbox: GluegunMenuToolbox, token: string, repoPath: string) => { + const {print} = toolbox; + const currentBranch = await getCurrentBranch(repoPath, toolbox); const input = new Input({ type: 'input', name: 'branch', - message: 'What github branch should be deployed?' + message: 'What github branch should be deployed?', + initial: currentBranch, }); const responseBranch = await input.run(); @@ -114,26 +117,18 @@ module.exports = { description: 'Update stack with different branch (u)', hidden: false, run: async (toolbox: GluegunMenuToolbox) => { - const { system, strings, print, filesystem } = toolbox; + const { system, strings, print } = toolbox; // Load settings - const config = await filesystem.readAsync(`${os.homedir()}/.pwcli_settings`, "json"); + const config = await getSettings(toolbox); if (!config) { - print.error(` - - No config found, please run [ ${chalk.cyan('pwcli s')} ] or setup from the menu. - - `); - if (toolbox.fromMenu()) { - await toolbox.menu.showMenu(null, defaultMenuSettings) - } else { - return; - } + await toolbox.menu.showMenu('setup', defaultMenuSettings); + return; } let branchName = null; while (!branchName) { - branchName = await getBranchName(print, config.gh_token); + branchName = await getBranchName(toolbox, config.gh_token, config.photowall_repo); } const stacks = JSON.parse( diff --git a/src/commands/setup/settings/settings.ts b/src/commands/setup/settings/settings.ts index 8f5097f..848e142 100644 --- a/src/commands/setup/settings/settings.ts +++ b/src/commands/setup/settings/settings.ts @@ -1,7 +1,7 @@ import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; import chalk = require('chalk'); import { system } from 'gluegun'; -import { defaultMenuSettings } from '../../../globals'; +import { defaultMenuSettings, getSettings } from '../../../globals'; const { Confirm, Input } = require('enquirer'); const checkCLIProgram = async (toolbox: GluegunMenuToolbox, cmd: string) => { @@ -33,6 +33,8 @@ module.exports = { `)); + let config = await getSettings(toolbox); + // Check if brew is installed // if (await checkCLIProgram(toolbox, 'brew')) { // print.info(`\nDetected ${chalk.green('brew')}`); @@ -100,17 +102,21 @@ module.exports = { await system.run('touch ~/.pwcli_settings'); print.info(`Created configuration file ${chalk.yellow(`~/.pwcli_settings`)}`); - const config = { - gh_token: '', - photowall_repo: '', - }; + if (!config) { + config = { + gh_token: '', + photowall_repo: '', + }; + } config.gh_token = await (new Input({ - message: `A github accesstoken is required with ${chalk.yellow('Full control of private repositories')}, please insert the key >` + message: `A github accesstoken is required with ${chalk.yellow('Full control of private repositories')}, please insert the key >`, + initial: config.gh_token, })).run(); config.photowall_repo = await (new Input({ - message: `If you want to be able to push active branch in your photowall repository, please insert the absolute path to the project >` + message: `If you want to be able to use current branch in your photowall repository\nwhen creating or updating testservers, please insert\nthe absolute path to the project or leave blank >`, + initial: config.photowall_repo, })).run(); print.info(` diff --git a/src/globals.ts b/src/globals.ts index 3dce568..3f44d88 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -1,4 +1,36 @@ +import chalk = require("chalk"); +const os = require("os"); export const defaultMenuSettings = { showHelp: false }; + +export type TConfig = { + gh_token: string; + photowall_repo: string; +}; +export const getSettings = async (toolbox): Promise => { + const { filesystem, print } = toolbox; + try { + return await filesystem.readAsync(`${os.homedir()}/.pwcli_settings`, "json"); + } catch { + print.error(` + + No config found, please run [ ${chalk.cyan('pwcli s se')} ] or setup > settings from the menu. + + `); + return null; + } +}; + +export const getCurrentBranch = async (path: string, toolbox): Promise => { + const { system } = toolbox; + const config = await getSettings(toolbox); + if (config.photowall_repo) { + // git rev-parse --abbrev-ref HEAD + const branch = await system.run('git rev-parse --abbrev-ref HEAD', {cwd: config.photowall_repo, trim: true}); + console.log(`branch`, branch); + return branch; + } + return null; +};