From a88ce0d608a5b80255f795d63c9ce52d11f79b82 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 3 Feb 2021 16:09:48 +0000 Subject: [PATCH] Attempting to fix the issue with 4001, this updates the cypress tests so that it is easier to change the port on which it runs as well as updating the electron usage of the app to use port 0 (an ephermeral port). --- packages/builder/cypress.json | 7 +++- .../cypress/integration/createApp.spec.js | 4 +- .../integration/createAutomation.spec.js | 2 +- .../cypress/integration/createBinding.spec.js | 2 +- .../integration/createComponents.spec.js | 2 +- .../cypress/integration/createTable.spec.js | 2 +- .../cypress/integration/createUser.spec.js | 2 +- .../cypress/integration/createView.spec.js | 2 +- .../cypress/integration/screens.spec.js | 2 +- packages/builder/cypress/setup.js | 2 + packages/builder/package.json | 4 +- .../automation/Shared/WebhookDisplay.svelte | 3 +- packages/server/.env.template | 5 +-- packages/server/package.json | 2 +- packages/server/src/app.js | 4 +- packages/server/src/electron.js | 42 +++++++++++-------- packages/server/src/events/index.js | 4 ++ 17 files changed, 53 insertions(+), 38 deletions(-) diff --git a/packages/builder/cypress.json b/packages/builder/cypress.json index a68cea053f..d76078f509 100644 --- a/packages/builder/cypress.json +++ b/packages/builder/cypress.json @@ -1,5 +1,8 @@ { - "baseUrl": "http://localhost:4001/_builder/", + "baseUrl": "http://localhost:4005/_builder/", "video": true, - "projectId": "bmbemn" + "projectId": "bmbemn", + "env": { + "PORT": "4005" + } } diff --git a/packages/builder/cypress/integration/createApp.spec.js b/packages/builder/cypress/integration/createApp.spec.js index 5382aa04cc..a32782867a 100644 --- a/packages/builder/cypress/integration/createApp.spec.js +++ b/packages/builder/cypress/integration/createApp.spec.js @@ -2,7 +2,7 @@ context('Create an Application', () => { beforeEach(() => { cy.server() - cy.visit('localhost:4001/_builder') + cy.visit(`localhost:${Cypress.env("PORT")}/_builder`) }) // https://on.cypress.io/interacting-with-elements @@ -11,7 +11,7 @@ context('Create an Application', () => { // https://on.cypress.io/type cy.createApp('My Cool App', 'This is a description') - cy.visit('localhost:4001/_builder') + cy.visit(`localhost:${Cypress.env("PORT")}/_builder`) cy.contains('My Cool App').should('exist') }) diff --git a/packages/builder/cypress/integration/createAutomation.spec.js b/packages/builder/cypress/integration/createAutomation.spec.js index 92d1f907bd..2ba99fa340 100644 --- a/packages/builder/cypress/integration/createAutomation.spec.js +++ b/packages/builder/cypress/integration/createAutomation.spec.js @@ -1,7 +1,7 @@ context("Create a automation", () => { before(() => { cy.server() - cy.visit("localhost:4001/_builder") + cy.visit(`localhost:${Cypress.env("PORT")}/_builder`) cy.createApp( "Automation Test App", diff --git a/packages/builder/cypress/integration/createBinding.spec.js b/packages/builder/cypress/integration/createBinding.spec.js index 971df3ed75..4074aca112 100644 --- a/packages/builder/cypress/integration/createBinding.spec.js +++ b/packages/builder/cypress/integration/createBinding.spec.js @@ -1,6 +1,6 @@ xcontext('Create a Binding', () => { before(() => { - cy.visit('localhost:4001/_builder') + cy.visit(`localhost:${Cypress.env("PORT")}/_builder`) cy.createApp('Binding App', 'Binding App Description') cy.navigateToFrontend() }) diff --git a/packages/builder/cypress/integration/createComponents.spec.js b/packages/builder/cypress/integration/createComponents.spec.js index 6931fdb86b..b87e863e53 100644 --- a/packages/builder/cypress/integration/createComponents.spec.js +++ b/packages/builder/cypress/integration/createComponents.spec.js @@ -1,7 +1,7 @@ xcontext("Create Components", () => { before(() => { cy.server() - cy.visit("localhost:4001/_builder") + cy.visit(`localhost:${Cypress.env("PORT")}/_builder`) // https://on.cypress.io/type cy.createApp("Table App", "Table App Description") cy.createTable("dog", "name", "age") diff --git a/packages/builder/cypress/integration/createTable.spec.js b/packages/builder/cypress/integration/createTable.spec.js index 27560aaeb9..d87cd0fd4c 100644 --- a/packages/builder/cypress/integration/createTable.spec.js +++ b/packages/builder/cypress/integration/createTable.spec.js @@ -1,6 +1,6 @@ context("Create a Table", () => { before(() => { - cy.visit("localhost:4001/_builder") + cy.visit(`localhost:${Cypress.env("PORT")}/_builder`) cy.createApp("Table App", "Table App Description") }) diff --git a/packages/builder/cypress/integration/createUser.spec.js b/packages/builder/cypress/integration/createUser.spec.js index a5f9934dd7..45a1ffcc68 100644 --- a/packages/builder/cypress/integration/createUser.spec.js +++ b/packages/builder/cypress/integration/createUser.spec.js @@ -2,7 +2,7 @@ context('Create a User', () => { before(() => { cy.server() - cy.visit('localhost:4001/_builder') + cy.visit(`localhost:${Cypress.env("PORT")}/_builder`) // https://on.cypress.io/type cy.createApp('User App', 'This app is used to test user creation') }) diff --git a/packages/builder/cypress/integration/createView.spec.js b/packages/builder/cypress/integration/createView.spec.js index 60bd8c15d1..23927b55fa 100644 --- a/packages/builder/cypress/integration/createView.spec.js +++ b/packages/builder/cypress/integration/createView.spec.js @@ -1,6 +1,6 @@ context("Create a View", () => { before(() => { - cy.visit("localhost:4001/_builder") + cy.visit(`localhost:${Cypress.env("PORT")}/_builder`) cy.createApp("View App", "View App Description") cy.createTable("data") cy.addColumn("data", "group", "Text") diff --git a/packages/builder/cypress/integration/screens.spec.js b/packages/builder/cypress/integration/screens.spec.js index 6df8af3602..d219e7daa7 100644 --- a/packages/builder/cypress/integration/screens.spec.js +++ b/packages/builder/cypress/integration/screens.spec.js @@ -2,7 +2,7 @@ context('Screen Tests', () => { before(() => { cy.server() - cy.visit('localhost:4001/_builder') + cy.visit(`localhost:${Cypress.env("PORT")}/_builder`) cy.createApp('Conor Cy App', 'Table App Description') cy.navigateToFrontend() }) diff --git a/packages/builder/cypress/setup.js b/packages/builder/cypress/setup.js index 7f20eff88c..1b94100fb3 100644 --- a/packages/builder/cypress/setup.js +++ b/packages/builder/cypress/setup.js @@ -7,6 +7,7 @@ const rimraf = require("rimraf") const { join, resolve } = require("path") // const run = require("../../cli/src/commands/run/runHandler") const initialiseBudibase = require("../../server/src/utilities/initialiseBudibase") +const cypressConfig = require("../cypress.json") const homedir = join(require("os").homedir(), ".budibase") @@ -15,6 +16,7 @@ rimraf.sync(homedir) process.env.BUDIBASE_API_KEY = "6BE826CB-6B30-4AEC-8777-2E90464633DE" process.env.NODE_ENV = "cypress" process.env.ENABLE_ANALYTICS = "false" +process.env.PORT = cypressConfig.env.PORT // Stop info logs polluting test outputs process.env.LOG_LEVEL = "error" diff --git a/packages/builder/package.json b/packages/builder/package.json index a43a251c91..f3e99a2cab 100644 --- a/packages/builder/package.json +++ b/packages/builder/package.json @@ -14,8 +14,8 @@ "cy:run": "cypress run", "cy:open": "cypress open", "cy:run:ci": "cypress run --browser electron --record --key f308590b-6070-41af-b970-794a3823d451", - "cy:test": "start-server-and-test cy:setup http://localhost:4001/_builder cy:run", - "cy:ci": "start-server-and-test cy:setup http://localhost:4001/_builder cy:run:ci" + "cy:test": "start-server-and-test cy:setup http://localhost:4005/_builder cy:run", + "cy:ci": "start-server-and-test cy:setup http://localhost:4005/_builder cy:run:ci" }, "jest": { "globals": { diff --git a/packages/builder/src/components/automation/Shared/WebhookDisplay.svelte b/packages/builder/src/components/automation/Shared/WebhookDisplay.svelte index 2edcbec3f1..18fff8153d 100644 --- a/packages/builder/src/components/automation/Shared/WebhookDisplay.svelte +++ b/packages/builder/src/components/automation/Shared/WebhookDisplay.svelte @@ -13,7 +13,8 @@ if (production) { return `${appUrl}/${uri}` } else { - return `http://localhost:4001/${uri}` + + return `${window.location.origin}/${uri}` } } diff --git a/packages/server/.env.template b/packages/server/.env.template index 1b1ff2e854..b2ff5be3f4 100644 --- a/packages/server/.env.template +++ b/packages/server/.env.template @@ -8,13 +8,10 @@ CLIENT_ID={{clientId}} # used to create cookie hashes JWT_SECRET={{cookieKey1}} -# port to run http server on -PORT=4001 - # error level for koa-pino LOG_LEVEL=info DEPLOYMENT_CREDENTIALS_URL="https://dt4mpwwap8.execute-api.eu-west-1.amazonaws.com/prod/" DEPLOYMENT_DB_URL="https://couchdb.budi.live:5984" SENTRY_DSN=https://a34ae347621946bf8acded18e5b7d4b8@o420233.ingest.sentry.io/5338131 -ENABLE_ANALYTICS="true" \ No newline at end of file +ENABLE_ANALYTICS="true" diff --git a/packages/server/package.json b/packages/server/package.json index 16f2441cd1..3ba6707677 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -36,7 +36,7 @@ "test:integration": "jest routes --runInBand", "test:watch": "jest --watch", "run:docker": "node src/index", - "dev:builder": "nodemon src/index.js", + "dev:builder": "PORT=4001 nodemon src/index.js", "electron": "electron src/electron.js", "build:electron": "electron-builder --dir", "publish:electron": "electron-builder -mwl --publish always", diff --git a/packages/server/src/app.js b/packages/server/src/app.js index a5ef3132af..6bd1816ac2 100644 --- a/packages/server/src/app.js +++ b/packages/server/src/app.js @@ -50,8 +50,10 @@ destroyable(server) server.on("close", () => console.log("Server Closed")) -module.exports = server.listen(env.PORT || 4001, async () => { +module.exports = server.listen(env.PORT || 0, async () => { console.log(`Budibase running on ${JSON.stringify(server.address())}`) + env._set("PORT", server.address().port) + eventEmitter.emitPort(env.PORT) automations.init() if (env.SELF_HOSTED) { await selfhost.init() diff --git a/packages/server/src/electron.js b/packages/server/src/electron.js index a6fa3f5e90..290c13e6a0 100644 --- a/packages/server/src/electron.js +++ b/packages/server/src/electron.js @@ -7,6 +7,7 @@ const { existsSync } = require("fs-extra") const initialiseBudibase = require("./utilities/initialiseBudibase") const { budibaseAppsDir } = require("./utilities/budibaseDir") const { openNewGitHubIssue, debugInfo } = require("electron-util") +const eventEmitter = require("./events") const budibaseDir = budibaseAppsDir() const envFile = join(budibaseDir, ".env") @@ -17,7 +18,11 @@ async function startApp() { } // evict environment from cache, so it reloads when next asked delete require.cache[require.resolve("./environment")] + // store the port incase its going to get overridden + const port = process.env.PORT require("dotenv").config({ path: envFile }) + // overwrite the port - don't want to use dotenv for the port + require("./environment")._set("PORT", port) unhandled({ showDialog: true, @@ -34,9 +39,6 @@ async function startApp() { }, }) - const APP_URL = "http://localhost:4001/_builder" - const APP_TITLE = "Budibase Builder" - let win function handleRedirect(e, url) { @@ -46,22 +48,26 @@ async function startApp() { async function createWindow() { app.server = require("./app") - 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() - } + 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() + } - // open _blank in default browser - win.webContents.on("new-window", handleRedirect) - win.webContents.on("will-navigate", handleRedirect) + // open _blank in default browser + win.webContents.on("new-window", handleRedirect) + win.webContents.on("will-navigate", handleRedirect) + }) } app.whenReady().then(createWindow) diff --git a/packages/server/src/events/index.js b/packages/server/src/events/index.js index 990ec1e16a..fe51b0780a 100644 --- a/packages/server/src/events/index.js +++ b/packages/server/src/events/index.js @@ -19,6 +19,10 @@ class BudibaseEmitter extends EventEmitter { emitTable(eventName, appId, table = null) { tableEmission({ emitter: this, eventName, appId, table }) } + + emitPort(portNumber) { + this.emit("internal:port", portNumber) + } } const emitter = new BudibaseEmitter()