From 47d75ebc2e5a00ce4e403f8fa973a840e04af275 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 26 Feb 2021 17:08:28 +0000 Subject: [PATCH] Updating for consistent use of chalk and logs, as well as adding an option to update docker-compose and envoy files. --- packages/cli/.gitignore | 1 + packages/cli/src/hosting/index.js | 37 +++++++++++++++++--------- packages/cli/src/hosting/makeEnv.js | 4 +-- packages/cli/src/structures/Command.js | 6 ++--- packages/cli/src/utils.js | 8 ++++-- 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/packages/cli/.gitignore b/packages/cli/.gitignore index e794bca25b..c47f898f26 100644 --- a/packages/cli/.gitignore +++ b/packages/cli/.gitignore @@ -3,3 +3,4 @@ docker-compose.yaml envoy.yaml hosting.properties build/ +docker-error.log diff --git a/packages/cli/src/hosting/index.js b/packages/cli/src/hosting/index.js index 1c2260d9fb..4175556720 100644 --- a/packages/cli/src/hosting/index.js +++ b/packages/cli/src/hosting/index.js @@ -1,12 +1,11 @@ const Command = require("../structures/Command") const { CommandWords } = require("../constants") const { lookpath } = require("lookpath") -const { downloadFile, logErrorToFile } = require("../utils") +const { downloadFile, logErrorToFile, success, info } = require("../utils") const { confirmation } = require("../questions") const fs = require("fs") const compose = require("docker-compose") const envFile = require("./makeEnv") -const chalk = require("chalk") const BUDIBASE_SERVICES = ["app-service", "worker-service"] const ERROR_FILE = "docker-error.log" @@ -15,6 +14,15 @@ const FILE_URLS = [ "https://raw.githubusercontent.com/Budibase/budibase/master/hosting/envoy.yaml" ] +async function downloadFiles() { + const promises = [] + for (let url of FILE_URLS) { + const fileName = url.split("/").slice(-1)[0] + promises.push(downloadFile(url, `./${fileName}`)) + } + await Promise.all(promises) +} + async function checkDockerConfigured() { const error = "docker/docker-compose has not been installed, please follow instructions at: https://docs.budibase.com/self-hosting/hosting-methods/docker-compose#installing-docker" const docker = await lookpath("docker") @@ -48,28 +56,25 @@ async function init() { console.log("Stopping.") return } - const promises = [] - for (let url of FILE_URLS) { - const fileName = url.split("/").slice(-1)[0] - promises.push(downloadFile(url, `./${fileName}`)) - } - await Promise.all(promises) + await downloadFiles() await envFile.make() } async function start() { await checkDockerConfigured() checkInitComplete() + console.log(info("Starting services, this may take a moment.")) const port = envFile.get("MAIN_PORT") await handleError(async () => { await compose.upAll({cwd: "./", log: false}) }) - console.log(chalk.green(`Services started, please go to http://localhost:${port} for next steps.`)) + console.log(success(`Services started, please go to http://localhost:${port} for next steps.`)) } async function status() { await checkDockerConfigured() checkInitComplete() + console.log(info("Budibase status")) await handleError(async () => { const response = await compose.ps() console.log(response.out) @@ -79,23 +84,31 @@ async function status() { async function stop() { await checkDockerConfigured() checkInitComplete() + console.log(info("Stopping services, this may take a moment.")) await handleError(async () => { await compose.stop() }) + console.log(success("Services have been stopped successfully.")) } async function update() { await checkDockerConfigured() checkInitComplete() + if (await confirmation("Do you wish to update you docker-compose.yaml and envoy.yaml?")) { + await downloadFiles() + } await handleError(async () => { const status = await compose.ps() const parts = status.out.split("\n") const isUp = parts[2] && parts[2].indexOf("Up") !== -1 - await compose.stop() - console.log(chalk.cyan("Beginning update, this may take a few minutes.")) + if (isUp) { + console.log(info("Stopping services, this may take a moment.")) + await compose.stop() + } + console.log(info("Beginning update, this may take a few minutes.")) await compose.pullMany(BUDIBASE_SERVICES, {log: true}) if (isUp) { - console.log(chalk.green("Update complete, restarting services...")) + console.log(success("Update complete, restarting services...")) await start() } }) diff --git a/packages/cli/src/hosting/makeEnv.js b/packages/cli/src/hosting/makeEnv.js index 67bea1522e..b3c32c3d3c 100644 --- a/packages/cli/src/hosting/makeEnv.js +++ b/packages/cli/src/hosting/makeEnv.js @@ -1,5 +1,5 @@ const { string, number } = require("../questions") -const { getSuccess } = require("../utils") +const { success } = require("../utils") const fs = require("fs") const path = require("path") const randomString = require("randomstring") @@ -36,7 +36,7 @@ module.exports.make = async () => { const hostingPort = await number("Please enter the port on which you want your installation to run: ", 10000) const fileContents = getContents(hostingPort, hostingKey) fs.writeFileSync(FILE_PATH, fileContents) - console.log(getSuccess("Configuration has been written successfully - please check .env file for more details.")) + console.log(success("Configuration has been written successfully - please check .env file for more details.")) } module.exports.get = property => { diff --git a/packages/cli/src/structures/Command.js b/packages/cli/src/structures/Command.js index c036c432b5..e911e0e328 100644 --- a/packages/cli/src/structures/Command.js +++ b/packages/cli/src/structures/Command.js @@ -1,7 +1,7 @@ const { getSubHelpDescription, getHelpDescription, - getError, + error, } = require("../utils") class Command { @@ -52,11 +52,11 @@ class Command { } } if (!executed) { - console.log(getError(`Unknown ${this.command} option.`)) + console.log(error(`Unknown ${this.command} option.`)) command.help() } } catch (err) { - console.log(getError(err)) + console.log(error(err)) } }) } diff --git a/packages/cli/src/utils.js b/packages/cli/src/utils.js index cd99e79746..9ed0fa821f 100644 --- a/packages/cli/src/utils.js +++ b/packages/cli/src/utils.js @@ -29,14 +29,18 @@ exports.getSubHelpDescription = string => { return chalk.green(string) } -exports.getError = error => { +exports.error = error => { return chalk.red(`Error - ${error}`) } -exports.getSuccess = success => { +exports.success = success => { return chalk.green(success) } +exports.info = info => { + return chalk.cyan(info) +} + exports.logErrorToFile = (file, error) => { fs.writeFileSync(path.resolve(`./${file}`), `Budiase Error\n${error}`) }