refactor: minor code cleanups

This commit is contained in:
Carlos Valente
2024-06-23 21:50:40 +02:00
committed by Carlos Valente
parent 0335da9786
commit 20503de6fd
7 changed files with 32 additions and 28 deletions
+3 -1
View File
@@ -17,7 +17,6 @@ import {
resolveExternalsDirectory, resolveExternalsDirectory,
resolveStylesDirectory, resolveStylesDirectory,
resolvedPath, resolvedPath,
clearUploadfolder,
} from './setup/index.js'; } from './setup/index.js';
import { ONTIME_VERSION } from './ONTIME_VERSION.js'; import { ONTIME_VERSION } from './ONTIME_VERSION.js';
import { consoleSuccess, consoleHighlight } from './utils/console.js'; import { consoleSuccess, consoleHighlight } from './utils/console.js';
@@ -44,6 +43,9 @@ import { messageService } from './services/message-service/MessageService.js';
import { populateDemo } from './setup/loadDemo.js'; import { populateDemo } from './setup/loadDemo.js';
import { getState } from './stores/runtimeState.js'; import { getState } from './stores/runtimeState.js';
import { initRundown } from './services/rundown-service/RundownService.js'; import { initRundown } from './services/rundown-service/RundownService.js';
// Utilities
import { clearUploadfolder } from './utils/upload.js';
import { generateCrashReport } from './utils/generateCrashReport.js'; import { generateCrashReport } from './utils/generateCrashReport.js';
import { getNetworkInterfaces } from './utils/networkInterfaces.js'; import { getNetworkInterfaces } from './utils/networkInterfaces.js';
+2 -2
View File
@@ -4,7 +4,7 @@ import { generateId, millisToString } from 'ontime-utils';
import { clock } from '../services/Clock.js'; import { clock } from '../services/Clock.js';
import { isProduction } from '../setup/index.js'; import { isProduction } from '../setup/index.js';
import { socket } from '../adapters/WebsocketAdapter.js'; import { socket } from '../adapters/WebsocketAdapter.js';
import { consoleSubdued, consoleRed } from '../utils/console.js'; import { consoleSubdued, consoleError } from '../utils/console.js';
class Logger { class Logger {
private queue: Log[]; private queue: Log[];
@@ -47,7 +47,7 @@ class Logger {
private _push(log: Log) { private _push(log: Log) {
if (this.canLog || log.level === LogLevel.Severe) { if (this.canLog || log.level === LogLevel.Severe) {
if (log.level === LogLevel.Severe) { if (log.level === LogLevel.Severe) {
consoleRed(`[${log.level}] \t ${log.origin} \t ${log.text}`); consoleError(`[${log.level}] \t ${log.origin} \t ${log.text}`);
} else { } else {
consoleSubdued(`[${log.level}] \t ${log.origin} \t ${log.text}`); consoleSubdued(`[${log.level}] \t ${log.origin} \t ${log.text}`);
} }
+2 -2
View File
@@ -1,5 +1,5 @@
/* eslint-disable no-console */ /* eslint-disable no-console */
import { consoleHighlight, consoleRed } from './utils/console.js'; import { consoleHighlight, consoleError } from './utils/console.js';
import { initAssets, startIntegrations, startServer } from './app.js'; import { initAssets, startIntegrations, startServer } from './app.js';
async function startOntime() { async function startOntime() {
@@ -16,7 +16,7 @@ async function startOntime() {
consoleHighlight('Request: Start integrations...'); consoleHighlight('Request: Start integrations...');
await startIntegrations(); await startIntegrations();
} catch (error) { } catch (error) {
consoleRed(`Request failed: ${error}`); consoleError(`Request failed: ${error}`);
} }
} }
+11 -21
View File
@@ -1,8 +1,6 @@
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import path, { dirname, join } from 'path'; import { dirname, join } from 'path';
import fs from 'fs'; import { readFileSync, writeFileSync } from 'fs';
import { rm } from 'fs/promises';
import { config } from './config.js'; import { config } from './config.js';
import { ensureDirectory } from '../utils/fileManagement.js'; import { ensureDirectory } from '../utils/fileManagement.js';
@@ -17,18 +15,18 @@ import { ensureDirectory } from '../utils/fileManagement.js';
export function getAppDataPath(): string { export function getAppDataPath(): string {
// handle docker // handle docker
if (process.env.ONTIME_DATA) { if (process.env.ONTIME_DATA) {
return path.join(process.env.ONTIME_DATA); return join(process.env.ONTIME_DATA);
} }
switch (process.platform) { switch (process.platform) {
case 'darwin': { case 'darwin': {
return path.join(process.env.HOME!, 'Library', 'Application Support', 'Ontime'); return join(process.env.HOME!, 'Library', 'Application Support', 'Ontime');
} }
case 'win32': { case 'win32': {
return path.join(process.env.APPDATA!, 'Ontime'); return join(process.env.APPDATA!, 'Ontime');
} }
case 'linux': { case 'linux': {
return path.join(process.env.HOME!, '.Ontime'); return join(process.env.HOME!, '.Ontime');
} }
default: { default: {
throw new Error('Could not resolve public folder for platform'); throw new Error('Could not resolve public folder for platform');
@@ -56,11 +54,11 @@ if (import.meta.url) {
// path to server src folder // path to server src folder
const currentDir = dirname(__dirname); const currentDir = dirname(__dirname);
// locally we are in src/setup, in the production build, this is a single file at src // locally we are in src/setup, in the production build, this is a single file at src
export const srcDirectory = isProduction ? currentDir : path.join(currentDir, '../'); export const srcDirectory = isProduction ? currentDir : join(currentDir, '../');
// resolve path to external // resolve path to external
const productionPath = path.join(srcDirectory, 'client/'); const productionPath = join(srcDirectory, 'client/');
const devPath = path.join(srcDirectory, '../../client/build/'); const devPath = join(srcDirectory, '../../client/build/');
export const resolvedPath = (): string => { export const resolvedPath = (): string => {
if (isTest) { if (isTest) {
@@ -83,12 +81,12 @@ export const uploadsFolderPath = join(getAppDataPath(), config.uploads);
const ensureAppState = () => { const ensureAppState = () => {
ensureDirectory(getAppDataPath()); ensureDirectory(getAppDataPath());
fs.writeFileSync(appStatePath, JSON.stringify({ lastLoadedProject: 'db.json' })); writeFileSync(appStatePath, JSON.stringify({ lastLoadedProject: 'db.json' }));
}; };
const getLastLoadedProject = () => { const getLastLoadedProject = () => {
try { try {
const appState = JSON.parse(fs.readFileSync(appStatePath, 'utf8')); const appState = JSON.parse(readFileSync(appStatePath, 'utf8'));
if (!appState.lastLoadedProject) { if (!appState.lastLoadedProject) {
ensureAppState(); ensureAppState();
} }
@@ -142,11 +140,3 @@ export const resolveCrashReportDirectory = getAppDataPath();
// path to projects // path to projects
export const resolveProjectsDirectory = join(getAppDataPath(), config.projects); export const resolveProjectsDirectory = join(getAppDataPath(), config.projects);
export async function clearUploadfolder() {
try {
await rm(uploadsFolderPath, { recursive: true });
} catch (_) {
//we dont care that there was no folder
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ export function consoleSuccess(message: string) {
/** /**
* Utility function to log messages in red * Utility function to log messages in red
*/ */
export function consoleRed(message: string) { export function consoleError(message: string) {
console.error(inRed(message)); console.error(inRed(message));
} }
+4 -1
View File
@@ -6,6 +6,7 @@ import { get } from '../services/rundown-service/rundownCache.js';
import { getState } from '../stores/runtimeState.js'; import { getState } from '../stores/runtimeState.js';
import { resolveCrashReportDirectory } from '../setup/index.js'; import { resolveCrashReportDirectory } from '../setup/index.js';
import { ensureDirectory } from './fileManagement.js';
/** /**
* Writes a file to the crash report location * Writes a file to the crash report location
* @param fileName * @param fileName
@@ -13,10 +14,12 @@ import { resolveCrashReportDirectory } from '../setup/index.js';
*/ */
function writeToFile(fileName: string, content: object) { function writeToFile(fileName: string, content: object) {
const path = join(resolveCrashReportDirectory, fileName); const path = join(resolveCrashReportDirectory, fileName);
ensureDirectory(resolveCrashReportDirectory);
try { try {
const textContent = JSON.stringify(content, null, 2); const textContent = JSON.stringify(content, null, 2);
writeFileSync(path, textContent); writeFileSync(path, textContent);
} catch (e_rror) { } catch (_) {
/** We do not handle the error here */ /** We do not handle the error here */
} }
} }
+9
View File
@@ -1,6 +1,7 @@
import multer from 'multer'; import multer from 'multer';
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
import { rm } from 'fs/promises';
import { ensureDirectory } from './fileManagement.js'; import { ensureDirectory } from './fileManagement.js';
import { getAppDataPath, uploadsFolderPath } from '../setup/index.js'; import { getAppDataPath, uploadsFolderPath } from '../setup/index.js';
@@ -56,3 +57,11 @@ export const storage = multer.diskStorage({
cb(null, file.originalname); cb(null, file.originalname);
}, },
}); });
export async function clearUploadfolder() {
try {
await rm(uploadsFolderPath, { recursive: true });
} catch (_) {
//we dont care that there was no folder
}
}