simplify try catch in manage script

This commit is contained in:
Martin McKeaveney 2021-03-23 11:01:33 +00:00
parent 555e6386a7
commit afaa706e07
7 changed files with 121 additions and 186 deletions

View File

@ -7,8 +7,6 @@ const { join, resolve } = require("path")
const initialiseBudibase = require("../../server/src/utilities/initialiseBudibase") const initialiseBudibase = require("../../server/src/utilities/initialiseBudibase")
const cypressConfig = require("../cypress.json") const cypressConfig = require("../cypress.json")
const homedir = join(require("os").homedir(), ".budibase")
process.env.BUDIBASE_API_KEY = "6BE826CB-6B30-4AEC-8777-2E90464633DE" process.env.BUDIBASE_API_KEY = "6BE826CB-6B30-4AEC-8777-2E90464633DE"
process.env.NODE_ENV = "cypress" process.env.NODE_ENV = "cypress"
process.env.ENABLE_ANALYTICS = "false" process.env.ENABLE_ANALYTICS = "false"
@ -17,9 +15,8 @@ process.env.PORT = cypressConfig.env.PORT
// Stop info logs polluting test outputs // Stop info logs polluting test outputs
process.env.LOG_LEVEL = "error" process.env.LOG_LEVEL = "error"
async function run(dir) { async function run() {
process.env.BUDIBASE_DIR = resolve(dir) // require("dotenv").config({ path: resolve(dir, ".env") })
require("dotenv").config({ path: resolve(dir, ".env") })
// dont make this a variable or top level require // dont make this a variable or top level require
// it will cause environment module to be loaded prematurely // it will cause environment module to be loaded prematurely
@ -27,12 +24,15 @@ async function run(dir) {
server.on("close", () => console.log("Server Closed")) server.on("close", () => console.log("Server Closed"))
} }
initialiseBudibase({ dir: homedir, clientId: "cypress-test" }) run()
.then(() => {
delete require.cache[require.resolve("../../server/src/environment")] // TODO: ensure that this still works
const xPlatHomeDir = homedir.startsWith("~") // initialiseBudibase({ dir: homedir, clientId: "cypress-test" })
? join(homedir(), homedir.substring(1)) // .then(() => {
: homedir // delete require.cache[require.resolve("../../server/src/environment")]
run(xPlatHomeDir) // const xPlatHomeDir = homedir.startsWith("~")
}) // ? join(homedir(), homedir.substring(1))
.catch(e => console.error(e)) // : homedir
// run(xPlatHomeDir)
// })
// .catch(e => console.error(e))

View File

@ -65,8 +65,7 @@
"!src/db/tests/**/*", "!src/db/tests/**/*",
"!src/tests/**/*", "!src/tests/**/*",
"!src/automations/tests/**/*", "!src/automations/tests/**/*",
"!src/utilities/fileProcessor.js", "!src/utilities/fileProcessor.js"
"!src/utilities/initialiseBudibase.js"
], ],
"coverageReporters": [ "coverageReporters": [
"lcov", "lcov",

View File

@ -17,31 +17,19 @@ const Commands = {
async function up() { async function up() {
console.log("Spinning up your budibase dev environment... 🔧✨") console.log("Spinning up your budibase dev environment... 🔧✨")
try { await compose.upAll(CONFIG)
await compose.upAll(CONFIG)
} catch (err) {
console.log("Something went wrong:", err.message)
}
} }
async function down() { async function down() {
console.log("Spinning down your budibase dev environment... 🌇") console.log("Spinning down your budibase dev environment... 🌇")
try { await compose.stop(CONFIG)
await compose.stop(CONFIG)
} catch (err) {
console.log("Something went wrong:", err.message)
}
} }
async function nuke() { async function nuke() {
console.log( console.log(
"Clearing down your budibase dev environment, including all containers and volumes... 💥" "Clearing down your budibase dev environment, including all containers and volumes... 💥"
) )
try { await compose.down(CONFIG)
await compose.down(CONFIG)
} catch (err) {
console.log("Something went wrong:", err.message)
}
} }
const managementCommand = process.argv.slice(2)[0] const managementCommand = process.argv.slice(2)[0]
@ -74,6 +62,9 @@ command()
.then(() => { .then(() => {
console.log("Done! 🎉") console.log("Done! 🎉")
}) })
.catch(() => { .catch(err => {
console.log("Error while managing budibase dev environment.") console.error(
"Something went wrong while managing budibase dev environment:",
err.message
)
}) })

View File

@ -1,17 +0,0 @@
// const { join } = require("path")
// const { homedir } = require("os")
// // const initialiseBudibase = require("../src/utilities/initialiseBudibase")
// // const DIRECTORY = "~/.budibase"
// function run() {
// let opts = {}
// // let dir = DIRECTORY
// opts.quiet = true
// // opts.dir = dir.startsWith("~") ? join(homedir(), dir.substring(1)) : dir
// return initialiseBudibase(opts)
// }
// run().then(() => {
// console.log("Init complete.")
// })

View File

@ -1,7 +1,6 @@
const Router = require("@koa/router") const Router = require("@koa/router")
const controller = require("../controllers/static") const controller = require("../controllers/static")
const { budibaseTempDir } = require("../../utilities/budibaseDir") const { budibaseTempDir } = require("../../utilities/budibaseDir")
const env = require("../../environment")
const authorized = require("../../middleware/authorized") const authorized = require("../../middleware/authorized")
const { BUILDER } = require("../../utilities/security/permissions") const { BUILDER } = require("../../utilities/security/permissions")
const usage = require("../../middleware/usageQuota") const usage = require("../../middleware/usageQuota")

View File

@ -1,112 +1,112 @@
const { app, BrowserWindow, shell, dialog } = require("electron") // const { app, BrowserWindow, shell, dialog } = require("electron")
const { join } = require("./utilities/centralPath") // const { join } = require("./utilities/centralPath")
const isDev = require("electron-is-dev") // const isDev = require("electron-is-dev")
const { autoUpdater } = require("electron-updater") // const { autoUpdater } = require("electron-updater")
const unhandled = require("electron-unhandled") // const unhandled = require("electron-unhandled")
const { existsSync } = require("fs-extra") // const { existsSync } = require("fs-extra")
const initialiseBudibase = require("./utilities/initialiseBudibase") // const initialiseBudibase = require("./utilities/initialiseBudibase")
const { budibaseAppsDir } = require("./utilities/budibaseDir") // const { budibaseAppsDir } = require("./utilities/budibaseDir")
const { openNewGitHubIssue, debugInfo } = require("electron-util") // const { openNewGitHubIssue, debugInfo } = require("electron-util")
const eventEmitter = require("./events") // const eventEmitter = require("./events")
const budibaseDir = budibaseAppsDir() // const budibaseDir = budibaseAppsDir()
const envFile = join(budibaseDir, ".env") // const envFile = join(budibaseDir, ".env")
async function startApp() { // async function startApp() {
if (!existsSync(envFile)) { // if (!existsSync(envFile)) {
await initialiseBudibase({ dir: budibaseDir }) // await initialiseBudibase({ dir: budibaseDir })
} // }
// evict environment from cache, so it reloads when next asked // // evict environment from cache, so it reloads when next asked
delete require.cache[require.resolve("./environment")] // delete require.cache[require.resolve("./environment")]
// store the port incase its going to get overridden // // store the port incase its going to get overridden
const port = process.env.PORT // const port = process.env.PORT
require("dotenv").config({ path: envFile }) // require("dotenv").config({ path: envFile })
// overwrite the port - don't want to use dotenv for the port // // overwrite the port - don't want to use dotenv for the port
require("./environment")._set("PORT", port) // require("./environment")._set("PORT", port)
unhandled({ // unhandled({
showDialog: true, // showDialog: true,
reportButton: error => { // reportButton: error => {
openNewGitHubIssue({ // openNewGitHubIssue({
title: error.message, // title: error.message,
user: "Budibase", // user: "Budibase",
labels: ["error-report"], // labels: ["error-report"],
repo: "budibase", // repo: "budibase",
body: `### Error that occurred when using the budibase builder:\n\`\`\`\n${ // body: `### Error that occurred when using the budibase builder:\n\`\`\`\n${
error.stack // error.stack
}\n\`\`\`\n### Operating System Information:\n---\n\n${debugInfo()}`, // }\n\`\`\`\n### Operating System Information:\n---\n\n${debugInfo()}`,
}) // })
}, // },
}) // })
let win // let win
function handleRedirect(e, url) { // function handleRedirect(e, url) {
e.preventDefault() // e.preventDefault()
shell.openExternal(url) // shell.openExternal(url)
} // }
async function createWindow() { // async function createWindow() {
app.server = require("./app") // app.server = require("./app")
eventEmitter.on("internal:port", port => { // eventEmitter.on("internal:port", port => {
const APP_URL = `http://localhost:${port}/_builder` // const APP_URL = `http://localhost:${port}/_builder`
const APP_TITLE = "Budibase Builder" // const APP_TITLE = "Budibase Builder"
win = new BrowserWindow({ // win = new BrowserWindow({
width: 1920, // width: 1920,
height: 1080, // height: 1080,
icon: join(__dirname, "..", "build", "icons", "512x512.png"), // icon: join(__dirname, "..", "build", "icons", "512x512.png"),
}) // })
win.setTitle(APP_TITLE) // win.setTitle(APP_TITLE)
win.loadURL(APP_URL) // win.loadURL(APP_URL)
if (isDev) { // if (isDev) {
win.webContents.openDevTools() // win.webContents.openDevTools()
} else { // } else {
autoUpdater.checkForUpdatesAndNotify() // autoUpdater.checkForUpdatesAndNotify()
} // }
// open _blank in default browser // // open _blank in default browser
win.webContents.on("new-window", handleRedirect) // win.webContents.on("new-window", handleRedirect)
win.webContents.on("will-navigate", handleRedirect) // win.webContents.on("will-navigate", handleRedirect)
}) // })
} // }
app.whenReady().then(createWindow) // app.whenReady().then(createWindow)
// Quit when all windows are closed. // // Quit when all windows are closed.
app.on("window-all-closed", () => { // app.on("window-all-closed", () => {
// On macOS it is common for applications and their menu bar // // On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q // // to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") { // if (process.platform !== "darwin") {
app.server.close() // app.server.close()
app.quit() // app.quit()
} // }
}) // })
app.on("activate", () => { // app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the // // On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open. // // dock icon is clicked and there are no other windows open.
if (win === null) createWindow() // if (win === null) createWindow()
}) // })
} // }
autoUpdater.on("update-downloaded", (event, releaseNotes, releaseName) => { // autoUpdater.on("update-downloaded", (event, releaseNotes, releaseName) => {
const dialogOpts = { // const dialogOpts = {
type: "info", // type: "info",
buttons: ["Restart", "Later"], // buttons: ["Restart", "Later"],
title: "Budibase Update Available", // title: "Budibase Update Available",
message: process.platform === "win32" ? releaseNotes : releaseName, // message: process.platform === "win32" ? releaseNotes : releaseName,
detail: // detail:
"A new version of the budibase builder has been downloaded. Restart the application to apply the updates.", // "A new version of the budibase builder has been downloaded. Restart the application to apply the updates.",
} // }
dialog.showMessageBox(dialogOpts).then(returnValue => { // dialog.showMessageBox(dialogOpts).then(returnValue => {
if (returnValue.response === 0) autoUpdater.quitAndInstall() // if (returnValue.response === 0) autoUpdater.quitAndInstall()
}) // })
}) // })
autoUpdater.on("error", message => { // autoUpdater.on("error", message => {
console.error("There was a problem updating the application") // console.error("There was a problem updating the application")
console.error(message) // console.error(message)
}) // })
startApp() // startApp()

View File

@ -1,37 +0,0 @@
// const { existsSync, readFile, writeFile, ensureDir } = require("fs-extra")
// const { join, resolve } = require("./centralPath")
// const { processString } = require("@budibase/string-templates")
// const uuid = require("uuid")
// module.exports = async opts => {
// // await ensureDir(opts.dir)
// await setCouchDbUrl(opts)
// // need an env file
// await createDevEnvFile(opts)
// }
// const setCouchDbUrl = async opts => {
// if (!opts.couchDbUrl) {
// const dataDir = join(opts.dir, ".data")
// await ensureDir(dataDir)
// opts.couchDbUrl =
// dataDir + (dataDir.endsWith("/") || dataDir.endsWith("\\") ? "" : "/")
// }
// }
// const createDevEnvFile = async opts => {
// const destConfigFile = join(opts.dir, "./.env")
// let createConfig = !existsSync(destConfigFile) || opts.quiet
// if (createConfig) {
// const template = await readFile(
// resolve(__dirname, "..", "..", ".env.template"),
// {
// encoding: "utf8",
// }
// )
// opts.cookieKey1 = opts.cookieKey1 || uuid.v4()
// const config = await processString(template, opts)
// await writeFile(destConfigFile, config, { flag: "w+" })
// }
// }