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