From 3831e8eb513d7ccfade76ae23496762b81ed9a2d Mon Sep 17 00:00:00 2001 From: Douglas Gubert Date: Thu, 17 Jun 2021 16:46:02 -0300 Subject: [PATCH 1/2] Add a script to start Rocket.Chat in HA mode --- .scripts/run-ha.ts | 95 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++ 2 files changed, 98 insertions(+) create mode 100644 .scripts/run-ha.ts diff --git a/.scripts/run-ha.ts b/.scripts/run-ha.ts new file mode 100644 index 000000000000..b0abc6aa4262 --- /dev/null +++ b/.scripts/run-ha.ts @@ -0,0 +1,95 @@ +import { spawn, SpawnOptions } from 'child_process'; +import * as path from 'path'; + +enum ModeParam { + MAIN = 'main', + INSTANCE = 'instance', +} + +interface IConfig extends SpawnOptions { + customEnv: typeof process.env; + parentEnv: typeof process.env; +} + +function isMode(value: any): value is ModeParam { + return Object.values(ModeParam).includes(value); +} + +function buildConfig(cwd: string): IConfig { + const customEnv: IConfig['customEnv'] = { + INSTANCE_IP: '127.0.0.1', + MONGO_URL: 'mongodb://localhost:3001/meteor', + MONGO_OPLOG_URL: 'mongodb://localhost:3001/local', + }; + + return { + cwd, + stdio: 'inherit', + shell: true, + customEnv, + parentEnv: process.env, + env: { + ...customEnv, + ...process.env, + }, + }; +} + +async function runMain(config: IConfig): Promise { + const { customEnv: { INSTANCE_IP }, parentEnv, ...mainConfig } = config; + + const spawnConfig = { + ...mainConfig, + env: { + INSTANCE_IP, + ...parentEnv, + }, + }; + + spawn('meteor', spawnConfig); +} + +async function runInstance(config: IConfig): Promise { + // Desctructuring the unused variables allows us to omit them in the `mainConfig` + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { customEnv, parentEnv, ...mainConfig } = config; + + const env = { + PORT: '3030', + ROOT_URL: '', + ...mainConfig.env, + }; + + env.ROOT_URL = `http://localhost:${ env.PORT }`; + + const spawnConfig = { + ...mainConfig, + env, + }; + + spawn('node', ['.meteor/local/build/main.js'], spawnConfig); +} + +async function main(mode: any): Promise { + if (!isMode(mode)) { + mode = 'main'; + } + + const config = buildConfig( + path.resolve(__dirname, '..'), + ); + + switch (mode) { + case ModeParam.MAIN: + await runMain(config); + break; + case ModeParam.INSTANCE: + await runInstance(config); + break; + } +} + +// First two parameters are the executable and the path to this script +const [, , mode] = process.argv; + +main(mode); diff --git a/package.json b/package.json index 0435b93be840..b3ab873c5390 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,9 @@ ], "scripts": { "start": "meteor", + "ha": "meteor npm run ha:start", + "ha:start": "ts-node .scripts/run-ha.ts main", + "ha:add": "ts-node .scripts/run-ha.ts instance", "debug": "meteor run --inspect", "debug-brk": "meteor run --inspect-brk", "lint": "meteor npm run stylelint && meteor npm run eslint", From cb694cc764617818728706614309e42e88b3fe54 Mon Sep 17 00:00:00 2001 From: Douglas Gubert Date: Wed, 21 Jul 2021 13:47:55 -0300 Subject: [PATCH 2/2] Update .scripts/run-ha.ts --- .scripts/run-ha.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.scripts/run-ha.ts b/.scripts/run-ha.ts index b0abc6aa4262..9efc64bef906 100644 --- a/.scripts/run-ha.ts +++ b/.scripts/run-ha.ts @@ -81,10 +81,10 @@ async function main(mode: any): Promise { switch (mode) { case ModeParam.MAIN: - await runMain(config); + runMain(config); break; case ModeParam.INSTANCE: - await runInstance(config); + runInstance(config); break; } }