From 2f65711078c208f025d490cff7b651f061f774f5 Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Tue, 16 Jul 2024 19:48:03 +0200 Subject: [PATCH] refactor: dev label --- apps/server/package.json | 10 +++++----- apps/server/src/app.ts | 1 + .../src/classes/data-provider/DataProvider.ts | 13 ++++-------- .../app-state-service/AppStateService.ts | 13 ++++-------- apps/server/src/utils/development.ts | 20 +++++++++++++++++++ 5 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 apps/server/src/utils/development.ts diff --git a/apps/server/package.json b/apps/server/package.json index 74e772a60..ad6e72431 100644 --- a/apps/server/package.json +++ b/apps/server/package.json @@ -51,11 +51,11 @@ "dev": "cross-env NODE_ENV=development tsx watch ./src/index.ts", "dev:inspect": "cross-env NODE_ENV=development tsx watch --inspect ./src/index.ts", "dev:test": "cross-env IS_TEST=true tsx ./src/index.ts", - "build": "esbuild src/app.ts --log-level=error --platform=node --format=cjs --bundle --minify --legal-comments=external --outfile=dist/index.cjs", - "build:electron": "esbuild src/app.ts --log-level=error --platform=node --format=cjs --bundle --minify --legal-comments=external --outfile=dist/index.cjs", - "build:local": "esbuild src/app.ts --log-level=error --platform=node --format=cjs --bundle --minify --legal-comments=external --outfile=dist/index.cjs", - "build:docker": "esbuild src/index.ts --log-level=error --platform=node --format=cjs --minify --bundle --legal-comments=external --outfile=dist/docker.cjs", - "build:localdocker": "cross-env NODE_ENV=local esbuild src/index.ts --log-level=error --platform=node --format=cjs --minify --bundle --legal-comments=external --outfile=dist/docker.cjs", + "build": "esbuild src/app.ts --log-level=error --platform=node --format=cjs --bundle --minify --legal-comments=external --drop-labels=DEV --outfile=dist/index.cjs", + "build:electron": "esbuild src/app.ts --log-level=error --platform=node --format=cjs --bundle --minify --legal-comments=external --drop-labels=DEV --outfile=dist/index.cjs", + "build:local": "esbuild src/app.ts --log-level=error --platform=node --format=cjs --bundle --minify --legal-comments=external --drop-labels=DEV --outfile=dist/index.cjs", + "build:docker": "esbuild src/index.ts --log-level=error --platform=node --format=cjs --minify --bundle --legal-comments=external --drop-labels=DEV --outfile=dist/docker.cjs", + "build:localdocker": "cross-env NODE_ENV=local esbuild src/index.ts --log-level=error --platform=node --format=cjs --minify --bundle --legal-comments=external --drop-labels=DEV --outfile=dist/docker.cjs", "build:debug": "esbuild src/app.ts --platform=node --format=cjs --bundle --legal-comments=external --outfile=dist/index.cjs", "lint": "eslint . --quiet", "test": "cross-env IS_TEST=true vitest", diff --git a/apps/server/src/app.ts b/apps/server/src/app.ts index 9aa099284..2ab452647 100644 --- a/apps/server/src/app.ts +++ b/apps/server/src/app.ts @@ -263,6 +263,7 @@ export const shutdown = async (exitCode = 0) => { // clear the restore file if it was a normal exit // 0 means it was a SIGNAL // 1 means crash -> keep the file + // 2 means dev crash -> do nothing // 99 means there was a shutdown request from the UI if (exitCode === 0 || exitCode === 99) { await restoreService.clear(); diff --git a/apps/server/src/classes/data-provider/DataProvider.ts b/apps/server/src/classes/data-provider/DataProvider.ts index fac55619a..2645dc145 100644 --- a/apps/server/src/classes/data-provider/DataProvider.ts +++ b/apps/server/src/classes/data-provider/DataProvider.ts @@ -13,24 +13,19 @@ import { import type { Low } from 'lowdb'; import { JSONFilePreset } from 'lowdb/node'; -import { isProduction, isTest } from '../../setup/index.js'; +import { isTest } from '../../setup/index.js'; import { isPath } from '../../utils/fileManagement.js'; -import { consoleError } from '../../utils/console.js'; import { safeMerge } from './DataProvider.utils.js'; +import { shouldCrashDev } from '../../utils/development.js'; type ReadonlyPromise = Promise>; let db = {} as Low; export async function initPersistence(filePath: string, fallbackData: DatabaseModel) { - if (!isProduction) { - if (!isPath(filePath)) { - consoleError(filePath); - consoleError(new Error('initPersistence should be called with a path').stack); - process.exit(0); - } - } + // eslint-disable-next-line no-unused-labels -- dev code path + DEV: shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path'); const newDb = await JSONFilePreset(filePath, fallbackData); // Read the database to initialize it diff --git a/apps/server/src/services/app-state-service/AppStateService.ts b/apps/server/src/services/app-state-service/AppStateService.ts index b1deffc11..a6728d342 100644 --- a/apps/server/src/services/app-state-service/AppStateService.ts +++ b/apps/server/src/services/app-state-service/AppStateService.ts @@ -1,9 +1,9 @@ import { Low } from 'lowdb'; import { JSONFile } from 'lowdb/node'; -import { appStatePath, isProduction, isTest } from '../../setup/index.js'; +import { appStatePath, isTest } from '../../setup/index.js'; import { isPath } from '../../utils/fileManagement.js'; -import { consoleError } from '../../utils/console.js'; +import { shouldCrashDev } from '../../utils/development.js'; interface AppState { lastLoadedProject?: string; @@ -27,13 +27,8 @@ export async function getLastLoadedProject(): Promise { export async function setLastLoadedProject(filename: string): Promise { if (isTest) return; - if (!isProduction) { - if (isPath(filename)) { - consoleError(filename); - consoleError(new Error('setLastLoadedProject should not be called with a path').stack); - process.exit(0); - } - } + // eslint-disable-next-line no-unused-labels -- dev code path + DEV: shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path'); config.data.lastLoadedProject = filename; await config.write(); diff --git a/apps/server/src/utils/development.ts b/apps/server/src/utils/development.ts new file mode 100644 index 000000000..acaab902b --- /dev/null +++ b/apps/server/src/utils/development.ts @@ -0,0 +1,20 @@ +import { isProduction } from '../setup/index.js'; +import { consoleError } from '../utils/console.js'; + +/** + * Milestone checker for dev environment + * will terminate process if check returns true + * Ideally we would like to remove the call to this function on build + */ +export function shouldCrashDev(check: boolean, reason: string) { + if (isProduction) { + return; + } + + if (!check) { + return; + } + + consoleError(new Error(reason).stack ?? ''); + process.exit(2); +}