Skip to content

Commit

Permalink
feat: adds headless mode and command line args (#279)
Browse files Browse the repository at this point in the history
* feat: adds headless mode and command line args
  • Loading branch information
mvdicarlo authored Apr 10, 2024
1 parent 16e6048 commit 37d5a0e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 13 deletions.
7 changes: 4 additions & 3 deletions apps/client-server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ClassTransformOptions } from '@nestjs/common/interfaces/external/class-
import { NestFactory, Reflector } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { PostyBirbDirectories } from '@postybirb/fs';
import { StartupOptions } from '@postybirb/utils/electron';
import { PostyBirbEnvConfig } from '@postybirb/utils/electron';
import compression from 'compression';
import sharp from 'sharp';
import { AppModule } from './app/app.module';
Expand All @@ -32,7 +32,7 @@ class CustomClassSerializer extends ClassSerializerInterceptor {
}
}

async function bootstrap(startupOptions: StartupOptions) {
async function bootstrap() {
let app: INestApplication;
if (!IsTestEnvironment()) {
// TLS/SSL on non-test
Expand Down Expand Up @@ -85,7 +85,7 @@ async function bootstrap(startupOptions: StartupOptions) {
PostyBirbDirectories.initializeDirectories();
sharp.cache({ files: 0 });

const port = startupOptions.port ?? 9487;
const { port } = PostyBirbEnvConfig;

await app.listen(port, () => {
Logger.log(`Listening at https://localhost:${port}/${globalPrefix}`);
Expand All @@ -95,3 +95,4 @@ async function bootstrap(startupOptions: StartupOptions) {
}

export { bootstrap as bootstrapClientServer };

4 changes: 2 additions & 2 deletions apps/postybirb/src/app/api/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ contextBridge.exposeInMainWorld('electron', {
getAppVersion: () => ipcRenderer.invoke('get-app-version'),
pickDirectory: () => ipcRenderer.invoke('pick-directory'),
platform: process.platform,
app_port: process.env.APP_PORT,
app_version: process.env.APP_VERSION,
app_port: process.env.POSTYBIRB_PORT,
app_version: process.env.POSTYBIRB_VERSION,
});
5 changes: 4 additions & 1 deletion apps/postybirb/src/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { INestApplication } from '@nestjs/common';
import {
PostyBirbEnvConfig,
getStartupOptions,
isLinux,
isOSX,
Expand Down Expand Up @@ -146,7 +147,9 @@ export default class PostyBirb {
if (this.isDevelopmentMode()) {
PostyBirb.mainWindow.loadURL(`http://localhost:${rendererAppPort}`);
} else {
PostyBirb.mainWindow.loadURL(`https://localhost:${process.env.APP_PORT}`);
PostyBirb.mainWindow.loadURL(
`https://localhost:${PostyBirbEnvConfig.port}`
);
}
}

Expand Down
20 changes: 13 additions & 7 deletions apps/postybirb/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @nrwl/nx/enforce-module-boundaries */
import { INestApplication } from '@nestjs/common';
import { getStartupOptions } from '@postybirb/utils/electron';
import { PostyBirbEnvConfig } from '@postybirb/utils/electron';
import { bootstrapClientServer } from 'apps/client-server/src/main';
import { app, BrowserWindow, powerSaveBlocker } from 'electron';
import contextMenu from 'electron-context-menu';
Expand All @@ -19,6 +19,10 @@ app.commandLine.appendSwitch('disable-renderer-backgrounding');
app.commandLine.appendSwitch('disable-background-timer-throttling');
app.commandLine.appendSwitch('disable-features', 'CrossOriginOpenerPolicy');

// Inject environment for use in preload
process.env.POSTYBIRB_PORT = PostyBirbEnvConfig.port;
process.env.POSTYBIRB_VERSION = environment.version;

// Setup Metrics
startMetrics();

Expand Down Expand Up @@ -47,9 +51,6 @@ app.on(
}
);

process.env.APP_PORT = process.env.APP_PORT ?? '9487';
process.env.APP_VERSION = environment.version;

contextMenu();

export default class Main {
Expand All @@ -58,7 +59,7 @@ export default class Main {
}

static bootstrapClientServer(): Promise<INestApplication> {
return bootstrapClientServer(getStartupOptions());
return bootstrapClientServer();
}

static bootstrapApp(nestApp: INestApplication) {
Expand All @@ -78,8 +79,13 @@ async function start() {

// bootstrap app
const nestApp = await Main.bootstrapClientServer();
Main.bootstrapApp(nestApp);
Main.bootstrapAppEvents();
if (PostyBirbEnvConfig.headless) {
// eslint-disable-next-line no-console
console.log('Headless mode enabled.');
} else {
Main.bootstrapApp(nestApp);
Main.bootstrapAppEvents();
}
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
Expand Down
2 changes: 2 additions & 0 deletions libs/utils/electron/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './lib/postybirb-env-config';
export * from './lib/startup-options-electron';
export * from './lib/utils-electron';

34 changes: 34 additions & 0 deletions libs/utils/electron/src/lib/postybirb-env-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { app } from 'electron';
import minimist from 'minimist';
import { getStartupOptions } from './startup-options-electron';

export type AppEnvConfig = {
port: string;
headless: boolean;
};

const config: AppEnvConfig = {
port:
process.env.POSTYBIRB_PORT || getStartupOptions().port.toString() || '9487',
headless: process.env.POSTYBIRB_HEADLESS === 'true' || false,
};

const args = minimist(process.argv.slice(process.defaultApp ? 2 : 1));
if (args.port) {
config.port = args.port;
}
if (args.headless) {
config.headless = args.headless;
}

const portNumber = parseInt(config.port, 10);
if (!(portNumber >= 1024 && portNumber <= 65535)) {
// eslint-disable-next-line no-console
console.error(
'Invalid port number. Please provide a port number between 1024 and 65535.'
);
app.quit();
}

export { config as PostyBirbEnvConfig };

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"lodash": "^4.17.21",
"loglayer": "^2.1.0",
"mime": "^3.0.0",
"minimist": "^1.2.8",
"moment": "^2.29.4",
"multer": "^1.4.3",
"node-forge": "^0.10.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13885,6 +13885,11 @@ minimist@^1.2.6:
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz"
integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==

minimist@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

minipass-collect@^1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/minipass-collect/-/minipass-collect-1.0.2.tgz"
Expand Down

0 comments on commit 37d5a0e

Please sign in to comment.