From 3747b41452ff4690684b1d4bc1908c441bab15cf Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 26 Feb 2021 13:48:11 +0000 Subject: [PATCH] Finishing up init with config building rather than downloading. --- packages/cli/.gitignore | 3 +++ packages/cli/package.json | 3 ++- packages/cli/src/hosting/index.js | 6 ++--- packages/cli/src/hosting/makeEnv.js | 38 +++++++++++++++++++++++++++++ packages/cli/src/questions.js | 29 ++++++++++++++++++++++ packages/cli/src/utils.js | 4 +++ packages/cli/yarn.lock | 12 +++++++++ 7 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 packages/cli/src/hosting/makeEnv.js diff --git a/packages/cli/.gitignore b/packages/cli/.gitignore index c2658d7d1b..5e8e556a1e 100644 --- a/packages/cli/.gitignore +++ b/packages/cli/.gitignore @@ -1 +1,4 @@ node_modules/ +docker-compose.yaml +envoy.yaml +hosting.properties diff --git a/packages/cli/package.json b/packages/cli/package.json index 65e2f5d140..c21aff47ea 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -10,7 +10,8 @@ "chalk": "^4.1.0", "commander": "^7.1.0", "inquirer": "^8.0.0", - "lookpath": "^1.1.0" + "lookpath": "^1.1.0", + "randomstring": "^1.1.5" }, "devDependencies": { "eslint": "^7.20.0" diff --git a/packages/cli/src/hosting/index.js b/packages/cli/src/hosting/index.js index 9655812b5c..af87552128 100644 --- a/packages/cli/src/hosting/index.js +++ b/packages/cli/src/hosting/index.js @@ -4,11 +4,11 @@ const { spawn } = require("child_process") const { lookpath } = require("lookpath") const { downloadFile } = require("../utils") const { confirmation } = require("../questions") +const makeEnvFile = require("./makeEnv") const FILE_URLS = [ "https://raw.githubusercontent.com/Budibase/budibase/master/hosting/docker-compose.yaml", - "https://raw.githubusercontent.com/Budibase/budibase/master/hosting/envoy.yaml", - "https://github.com/Budibase/budibase/blob/master/hosting/hosting.properties" + "https://raw.githubusercontent.com/Budibase/budibase/master/hosting/envoy.yaml" ] async function checkDockerConfigured() { @@ -33,7 +33,7 @@ async function init() { promises.push(downloadFile(url, `./${fileName}`)) } await Promise.all(promises) - console.log("Files have been downloaded, ready to start.") + await makeEnvFile() } async function start() { diff --git a/packages/cli/src/hosting/makeEnv.js b/packages/cli/src/hosting/makeEnv.js new file mode 100644 index 0000000000..f6a8ca8615 --- /dev/null +++ b/packages/cli/src/hosting/makeEnv.js @@ -0,0 +1,38 @@ +const { string, number } = require("../questions") +const { getSuccess } = require("../utils") +const fs = require("fs") +const path = require("path") +const randomString = require("randomstring") + +const FILE_PATH = path.resolve("./.env") + +function getContents(port, hostingKey) { + return ` +# Use the main port in the builder for your self hosting URL, e.g. localhost:10000 +MAIN_PORT=${port} + +# Use this password when configuring your self hosting settings +HOSTING_KEY=${hostingKey} + +# This section contains all secrets pertaining to the system +JWT_SECRET=${randomString.generate()} +MINIO_ACCESS_KEY=${randomString.generate()} +MINIO_SECRET_KEY=${randomString.generate()} +COUCH_DB_PASSWORD=${randomString.generate()} +COUCH_DB_USER=${randomString.generate()} + +# This section contains variables that do not need to be altered under normal circumstances +APP_PORT=4002 +WORKER_PORT=4003 +MINIO_PORT=4004 +COUCH_DB_PORT=4005 +BUDIBASE_ENVIRONMENT=PRODUCTION` +} + +module.exports = async () => { + const hostingKey = await string("Please input the password you'd like to use as your hosting key: ") + const hostingPort = await number("Please enter the port on which you want your installation to run: ", 10000) + const fileContents = getContents(hostingKey, hostingPort) + fs.writeFileSync(FILE_PATH, fileContents) + console.log(getSuccess("Configuration has been written successfully - please check .env file for more details.")) +} \ No newline at end of file diff --git a/packages/cli/src/questions.js b/packages/cli/src/questions.js index ba4b766b36..33a652df74 100644 --- a/packages/cli/src/questions.js +++ b/packages/cli/src/questions.js @@ -9,3 +9,32 @@ exports.confirmation = async question => { } return (await inquirer.prompt(config)).confirmation } + +exports.string = async (question, defaultString = null) => { + const config = { + type: "input", + name: "string", + message: question, + } + if (defaultString) { + config.default = defaultString + } + return (await inquirer.prompt(config)).string +} + +exports.number = async (question, defaultNumber) => { + const config = { + type: "input", + name: "number", + message: question, + validate: (value) => { + let valid = !isNaN(parseFloat(value)) + return valid || "Please enter a number" + }, + filter: Number, + } + if (defaultNumber) { + config.default = defaultNumber + } + return (await inquirer.prompt(config)).number +} diff --git a/packages/cli/src/utils.js b/packages/cli/src/utils.js index 5c43bbf9af..c25b8fb213 100644 --- a/packages/cli/src/utils.js +++ b/packages/cli/src/utils.js @@ -32,3 +32,7 @@ exports.getSubHelpDescription = string => { exports.getError = error => { return chalk.red(`Error - ${error}`) } + +exports.getSuccess = success => { + return chalk.green(success) +} diff --git a/packages/cli/yarn.lock b/packages/cli/yarn.lock index 9d25c78635..4503b58bfb 100644 --- a/packages/cli/yarn.lock +++ b/packages/cli/yarn.lock @@ -107,6 +107,11 @@ argparse@^1.0.7: dependencies: sprintf-js "~1.0.2" +array-uniq@1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/array-uniq/-/array-uniq-1.0.2.tgz#5fcc373920775723cfd64d65c64bef53bf9eba6d" + integrity sha1-X8w3OSB3VyPP1k1lxkvvU7+eum0= + astral-regex@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31" @@ -685,6 +690,13 @@ punycode@^2.1.0: resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A== +randomstring@^1.1.5: + version "1.1.5" + resolved "https://registry.yarnpkg.com/randomstring/-/randomstring-1.1.5.tgz#6df0628f75cbd5932930d9fe3ab4e956a18518c3" + integrity sha1-bfBij3XL1ZMpMNn+OrTpVqGFGMM= + dependencies: + array-uniq "1.0.2" + regexpp@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/regexpp/-/regexpp-3.1.0.tgz#206d0ad0a5648cffbdb8ae46438f3dc51c9f78e2"