Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Script to start Rocket.Chat in HA mode during development #22398

Merged
merged 4 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions .scripts/run-ha.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const { customEnv: { INSTANCE_IP }, parentEnv, ...mainConfig } = config;

const spawnConfig = {
...mainConfig,
env: {
INSTANCE_IP,
...parentEnv,
},
};

spawn('meteor', spawnConfig);
}

async function runInstance(config: IConfig): Promise<void> {
// 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<void> {
if (!isMode(mode)) {
mode = 'main';
}

const config = buildConfig(
path.resolve(__dirname, '..'),
);

switch (mode) {
case ModeParam.MAIN:
runMain(config);
break;
case ModeParam.INSTANCE:
runInstance(config);
break;
}
}

// First two parameters are the executable and the path to this script
const [, , mode] = process.argv;

main(mode);
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down