From d85b2a5e5d160c80bf8dc7cb5aafa436ca39a0c4 Mon Sep 17 00:00:00 2001 From: Camden Phalen Date: Thu, 28 Sep 2023 17:29:31 -0400 Subject: [PATCH 1/7] Set up dev server manager to use port manager --- packages/cli/lib/DevServerManager.js | 22 ++++++++ packages/cli/package.json | 1 + yarn.lock | 80 ++++++++++++++++++++++++++-- 3 files changed, 100 insertions(+), 3 deletions(-) diff --git a/packages/cli/lib/DevServerManager.js b/packages/cli/lib/DevServerManager.js index 6a09795dd..702d2e0b0 100644 --- a/packages/cli/lib/DevServerManager.js +++ b/packages/cli/lib/DevServerManager.js @@ -4,6 +4,15 @@ const { COMPONENT_TYPES } = require('./projectStructure'); const { i18n } = require('./lang'); const { promptUser } = require('./prompts/promptUtils'); const { DevModeInterface } = require('@hubspot/ui-extensions-dev-server'); +const { + startPortManagerServer, + portManagerHasActiveServers, + stopPortManagerServer, + getServerInstanceId, +} = require('@hubspot/local-dev-lib/portManager'); +const { + assignPortToServerInstance, +} = require('../../../../hubspot-local-dev-lib/dist/lib/portManager'); const i18nKey = 'cli.lib.DevServerManager'; @@ -61,6 +70,7 @@ class DevServerManager { this.debug = debug; this.componentsByType = this.arrangeComponentsByType(components); + await startPortManagerServer(); await this.iterateDevServers( async (serverInterface, compatibleComponents) => { if (serverInterface.setup) { @@ -81,11 +91,19 @@ class DevServerManager { if (this.initialized) { await this.iterateDevServers(async serverInterface => { if (serverInterface.start) { + const serverInstanceId = getServerInstanceId( + serverInterface.getServerId(), + projectConfig.name + ); + + const port = await assignPortToServerInstance(serverInstanceId); + await serverInterface.start({ accountId, debug: this.debug, httpClient, projectConfig, + port, }); } }); @@ -113,6 +131,10 @@ class DevServerManager { await serverInterface.cleanup(); } }); + + if (!portManagerHasActiveServers()) { + stopPortManagerServer(); + } } } } diff --git a/packages/cli/package.json b/packages/cli/package.json index 17c96f0a0..57cee5369 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -9,6 +9,7 @@ }, "dependencies": { "@hubspot/cli-lib": "^5.0.1", + "@hubspot/local-dev-lib": "^0.0.5", "@hubspot/serverless-dev-runtime": "4.2.1-beta.3", "@hubspot/ui-extensions-dev-server": "^0.7.2", "archiver": "^5.3.0", diff --git a/yarn.lock b/yarn.lock index 27bb71ae3..2173983da 100644 --- a/yarn.lock +++ b/yarn.lock @@ -499,6 +499,43 @@ table "^6.6.0" unixify "1.0.0" +"@hubspot/cli-lib@^5.0.1": + version "5.0.1" + resolved "https://registry.yarnpkg.com/@hubspot/cli-lib/-/cli-lib-5.0.1.tgz#2f87101fb26007f5ed659320b68a076293c43da4" + integrity sha512-Zb7cuy/gBsNDzw7ift3KPAXh1OSMYfw0t+C4HBrPyZpebhY0Zvj28YJZxZz2j8ZzQu+c/XsANQUwkEBu6qDuKw== + dependencies: + chalk "^2.4.2" + chokidar "^3.0.1" + content-disposition "^0.5.3" + debounce "^1.2.0" + extract-zip "^1.6.7" + findup-sync "^3.0.0" + fs-extra "^8.1.0" + ignore "^5.1.4" + jest "^26.6.3" + js-yaml "^4.1.0" + moment "^2.24.0" + p-queue "^6.0.2" + prettier "^1.19.1" + request "^2.87.0" + request-promise-native "^1.0.7" + semver "^6.3.0" + table "^6.6.0" + unixify "1.0.0" + +"@hubspot/local-dev-lib@^0.0.5": + version "0.0.5" + resolved "https://registry.yarnpkg.com/@hubspot/local-dev-lib/-/local-dev-lib-0.0.5.tgz#e0c646994254fdc127da37c0503ad786c965a527" + integrity sha512-/5VpjaYCn5hHmPSWDSAf0/4+lQJ7UKRQQ5xoHRmA190q/u9P9ZK0+BtIxOINsrBjdTvrmc21aKCy3XdeiuGbtQ== + dependencies: + axios "^1.3.5" + extract-zip "^2.0.1" + findup-sync "^5.0.0" + fs-extra "^11.1.0" + js-yaml "^4.1.0" + moment "^2.29.4" + unixify "^1.0.0" + "@hubspot/ui-extensions-dev-server@^0.7.2": version "0.7.2" resolved "https://registry.yarnpkg.com/@hubspot/ui-extensions-dev-server/-/ui-extensions-dev-server-0.7.2.tgz#c63e9c3c6502db5a4dde652eb5fda5fc3af51cb8" @@ -1461,6 +1498,13 @@ dependencies: "@types/yargs-parser" "*" +"@types/yauzl@^2.9.1": + version "2.10.1" + resolved "https://registry.yarnpkg.com/@types/yauzl/-/yauzl-2.10.1.tgz#4e8f299f0934d60f36c74f59cb5a8483fd786691" + integrity sha512-CHzgNU3qYBnp/O4S3yv2tXPlvMTq0YWSTVg2/JYLqWZGHwwgJGAwd00poay/11asPq8wLFwHzubyInqHIFmmiw== + dependencies: + "@types/node" "*" + "@typescript-eslint/types@4.33.0": version "4.33.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.33.0.tgz#a1e59036a3b53ae8430ceebf2a919dc7f9af6d72" @@ -1884,6 +1928,15 @@ axios@^1.0.0: form-data "^4.0.0" proxy-from-env "^1.1.0" +axios@^1.3.5: + version "1.5.1" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.5.1.tgz#11fbaa11fc35f431193a9564109c88c1f27b585f" + integrity sha512-Q28iYCWzNHjAm+yEAot5QaAMxhMghWLFVf7rRdwhUI+c2jix2DUXjAHXVi+s1ibs3mjPO/cCgbA++3BjD0vP/A== + dependencies: + follow-redirects "^1.15.0" + form-data "^4.0.0" + proxy-from-env "^1.1.0" + babel-jest@^26.6.3: version "26.6.3" resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-26.6.3.tgz#d87d25cb0037577a0c89f82e5755c5d293c01056" @@ -3792,6 +3845,17 @@ extract-zip@^1.6.7: mkdirp "^0.5.4" yauzl "^2.10.0" +extract-zip@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/extract-zip/-/extract-zip-2.0.1.tgz#663dca56fe46df890d5f131ef4a06d22bb8ba13a" + integrity sha512-GDhU9ntwuKyGXdZBUgTIe+vXnWj0fppUEtMDL0+idd5Sta8TGpHssn/eusA9mrPr9qNDym6SxAYZjNvCn/9RBg== + dependencies: + debug "^4.1.1" + get-stream "^5.1.0" + yauzl "^2.10.0" + optionalDependencies: + "@types/yauzl" "^2.9.1" + extsprintf@1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05" @@ -3992,6 +4056,16 @@ findup-sync@^4.0.0: micromatch "^4.0.2" resolve-dir "^1.0.1" +findup-sync@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/findup-sync/-/findup-sync-5.0.0.tgz#54380ad965a7edca00cc8f63113559aadc541bd2" + integrity sha512-MzwXju70AuyflbgeOhzvQWAvvQdo1XL0A9bVvlXsYcFEBM87WR4OakL4OfZq+QRmr+duJubio+UtNQCPsVESzQ== + dependencies: + detect-file "^1.0.0" + is-glob "^4.0.3" + micromatch "^4.0.4" + resolve-dir "^1.0.1" + flat-cache@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-3.0.4.tgz#61b0338302b2fe9f957dcc32fc2a87f1c3048b11" @@ -5040,7 +5114,7 @@ is-generator-fn@^2.0.0: resolved "https://registry.yarnpkg.com/is-generator-fn/-/is-generator-fn-2.1.0.tgz#7d140adc389aaf3011a8f2a2a4cfa6faadffb118" integrity sha512-cTIB4yPYL/Grw0EaSzASzg6bBy9gqCofvWN8okThAYIxKJZC+udlRAmGbM0XLeniEJSs8uEgHPGuHSe1XsOLSQ== -is-glob@^4.0.0, is-glob@^4.0.1, is-glob@~4.0.1: +is-glob@^4.0.0, is-glob@^4.0.1, is-glob@^4.0.3, is-glob@~4.0.1: version "4.0.3" resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.3.tgz#64f61e42cbbb2eec2071a9dac0b28ba1e65d5084" integrity sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg== @@ -6634,7 +6708,7 @@ module-lookup-amd@^7.0.1: requirejs "^2.3.5" requirejs-config-file "^4.0.0" -moment@^2.24.0, moment@^2.29.1: +moment@^2.24.0, moment@^2.29.1, moment@^2.29.4: version "2.29.4" resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108" integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w== @@ -9417,7 +9491,7 @@ universalify@^2.0.0: resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717" integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ== -unixify@1.0.0: +unixify@1.0.0, unixify@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unixify/-/unixify-1.0.0.tgz#3a641c8c2ffbce4da683a5c70f03a462940c2090" integrity sha512-6bc58dPYhCMHHuwxldQxO3RRNZ4eCogZ/st++0+fcC1nr0jiGUtAdBJ2qzmLQWSxbtz42pWt4QQMiZ9HvZf5cg== From c55868aa477c73de857fb7dd096ccfcefd6d59f1 Mon Sep 17 00:00:00 2001 From: Camden Phalen Date: Fri, 29 Sep 2023 14:13:23 -0400 Subject: [PATCH 2/7] Pass ports to dev server --- packages/cli/lib/DevServerManager.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/cli/lib/DevServerManager.js b/packages/cli/lib/DevServerManager.js index 702d2e0b0..3dcf00064 100644 --- a/packages/cli/lib/DevServerManager.js +++ b/packages/cli/lib/DevServerManager.js @@ -91,19 +91,26 @@ class DevServerManager { if (this.initialized) { await this.iterateDevServers(async serverInterface => { if (serverInterface.start) { - const serverInstanceId = getServerInstanceId( - serverInterface.getServerId(), - projectConfig.name - ); + const serverIds = serverInterface.getServerIds(); - const port = await assignPortToServerInstance(serverInstanceId); + const ports = {}; + + for (let i = 0; i < serverIds.length; i++) { + const serverId = serverIds[i]; + const instanceId = getServerInstanceId( + serverId, + projectConfig.name + ); + const port = await assignPortToServerInstance(instanceId); + ports[serverId] = port; + } await serverInterface.start({ accountId, debug: this.debug, httpClient, projectConfig, - port, + ports, }); } }); From 58b8371592e8d33f8b6b9337f28334935c463abd Mon Sep 17 00:00:00 2001 From: Camden Phalen Date: Wed, 4 Oct 2023 14:23:39 -0400 Subject: [PATCH 3/7] Just use serverIds as instanceIds for now --- packages/cli/lib/DevServerManager.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/cli/lib/DevServerManager.js b/packages/cli/lib/DevServerManager.js index 3dcf00064..4336cf852 100644 --- a/packages/cli/lib/DevServerManager.js +++ b/packages/cli/lib/DevServerManager.js @@ -8,7 +8,7 @@ const { startPortManagerServer, portManagerHasActiveServers, stopPortManagerServer, - getServerInstanceId, + // getServerInstanceId, } = require('@hubspot/local-dev-lib/portManager'); const { assignPortToServerInstance, @@ -97,11 +97,14 @@ class DevServerManager { for (let i = 0; i < serverIds.length; i++) { const serverId = serverIds[i]; - const instanceId = getServerInstanceId( - serverId, - projectConfig.name - ); - const port = await assignPortToServerInstance(instanceId); + + // For now, only support one instance of each server + // const instanceId = getServerInstanceId( + // serverId, + // projectConfig.name + // ); + + const port = await assignPortToServerInstance(serverId); ports[serverId] = port; } From 25deb95038e611fe3b77c60685885a65606253c3 Mon Sep 17 00:00:00 2001 From: Camden Phalen Date: Wed, 18 Oct 2023 15:00:35 -0400 Subject: [PATCH 4/7] move port requesting logic to ui-extensions-dev-server --- packages/cli/lib/DevServerManager.js | 32 ---------------------------- 1 file changed, 32 deletions(-) diff --git a/packages/cli/lib/DevServerManager.js b/packages/cli/lib/DevServerManager.js index e37b13916..0e71be473 100644 --- a/packages/cli/lib/DevServerManager.js +++ b/packages/cli/lib/DevServerManager.js @@ -8,11 +8,7 @@ const { startPortManagerServer, portManagerHasActiveServers, stopPortManagerServer, - // getServerInstanceId, } = require('@hubspot/local-dev-lib/portManager'); -const { - assignPortToServerInstance, -} = require('../../../../hubspot-local-dev-lib/dist/lib/portManager'); const i18nKey = 'cli.lib.DevServerManager'; @@ -91,39 +87,11 @@ class DevServerManager { if (this.initialized) { await this.iterateDevServers(async serverInterface => { if (serverInterface.start) { - const serverIds = serverInterface.getServerIds(); - - const ports = {}; - - for (let i = 0; i < serverIds.length; i++) { - const serverId = serverIds[i]; - - // For now, only support one instance of each server - // const instanceId = getServerInstanceId( - // serverId, - // projectConfig.name - // ); - const instanceId = serverId; - - try { - const port = await assignPortToServerInstance(instanceId); - ports[serverId] = port; - } catch (e) { - if (e.response.status === 409) { - throw new Error( - `Failed to start dev server with instanceId ${instanceId}. An instance of this server is already running.` - ); - } - throw e; - } - } - await serverInterface.start({ accountId, debug: this.debug, httpClient, projectConfig, - ports, }); } }); From 9e65c3b9f5f14fa81e0d30591a28acb47dbfa2d4 Mon Sep 17 00:00:00 2001 From: Camden Phalen Date: Wed, 18 Oct 2023 16:23:48 -0400 Subject: [PATCH 5/7] pass requestPort to dev server --- packages/cli/lib/DevServerManager.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/cli/lib/DevServerManager.js b/packages/cli/lib/DevServerManager.js index 0e71be473..991d930c8 100644 --- a/packages/cli/lib/DevServerManager.js +++ b/packages/cli/lib/DevServerManager.js @@ -8,6 +8,7 @@ const { startPortManagerServer, portManagerHasActiveServers, stopPortManagerServer, + requestPortForSeverInstance, } = require('@hubspot/local-dev-lib/portManager'); const i18nKey = 'cli.lib.DevServerManager'; @@ -92,6 +93,7 @@ class DevServerManager { debug: this.debug, httpClient, projectConfig, + requestPort: requestPortForSeverInstance, }); } }); From ad1677457322c53be6b2d382fbe0173dd93126ec Mon Sep 17 00:00:00 2001 From: Camden Phalen Date: Wed, 18 Oct 2023 16:54:38 -0400 Subject: [PATCH 6/7] requestPorts --- packages/cli/lib/DevServerManager.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/lib/DevServerManager.js b/packages/cli/lib/DevServerManager.js index 991d930c8..3b468f410 100644 --- a/packages/cli/lib/DevServerManager.js +++ b/packages/cli/lib/DevServerManager.js @@ -8,7 +8,7 @@ const { startPortManagerServer, portManagerHasActiveServers, stopPortManagerServer, - requestPortForSeverInstance, + requestPorts, } = require('@hubspot/local-dev-lib/portManager'); const i18nKey = 'cli.lib.DevServerManager'; @@ -93,7 +93,7 @@ class DevServerManager { debug: this.debug, httpClient, projectConfig, - requestPort: requestPortForSeverInstance, + requestPorts, }); } }); From a900bcf65651b7f4a3bdd27be897a1acaf6bc434 Mon Sep 17 00:00:00 2001 From: Camden Phalen Date: Tue, 7 Nov 2023 16:25:17 -0500 Subject: [PATCH 7/7] handle async port manager methods --- packages/cli/lib/DevServerManager.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/cli/lib/DevServerManager.js b/packages/cli/lib/DevServerManager.js index 3b468f410..ee774f6de 100644 --- a/packages/cli/lib/DevServerManager.js +++ b/packages/cli/lib/DevServerManager.js @@ -122,8 +122,10 @@ class DevServerManager { } }); - if (!portManagerHasActiveServers()) { - stopPortManagerServer(); + const hasActiveServers = await portManagerHasActiveServers(); + + if (!hasActiveServers) { + await stopPortManagerServer(); } } }