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,
resolveStylesDirectory,
resolvedPath,
clearUploadfolder,
} from './setup/index.js';
import { ONTIME_VERSION } from './ONTIME_VERSION.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 { getState } from './stores/runtimeState.js';
import { initRundown } from './services/rundown-service/RundownService.js';
// Utilities
import { clearUploadfolder } from './utils/upload.js';
import { generateCrashReport } from './utils/generateCrashReport.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 { isProduction } from '../setup/index.js';
import { socket } from '../adapters/WebsocketAdapter.js';
import { consoleSubdued, consoleRed } from '../utils/console.js';
import { consoleSubdued, consoleError } from '../utils/console.js';
class Logger {
private queue: Log[];
@@ -47,7 +47,7 @@ class Logger {
private _push(log: Log) {
if (this.canLog || 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 {
consoleSubdued(`[${log.level}] \t ${log.origin} \t ${log.text}`);
}
+2 -2
View File
@@ -1,5 +1,5 @@
/* 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';
async function startOntime() {
@@ -16,7 +16,7 @@ async function startOntime() {
consoleHighlight('Request: Start integrations...');
await startIntegrations();
} catch (error) {
consoleRed(`Request failed: ${error}`);
consoleError(`Request failed: ${error}`);
}
}
+11 -21
View File
@@ -1,8 +1,6 @@
import { fileURLToPath } from 'url';
import path, { dirname, join } from 'path';
import fs from 'fs';
import { rm } from 'fs/promises';
import { dirname, join } from 'path';
import { readFileSync, writeFileSync } from 'fs';
import { config } from './config.js';
import { ensureDirectory } from '../utils/fileManagement.js';
@@ -17,18 +15,18 @@ import { ensureDirectory } from '../utils/fileManagement.js';
export function getAppDataPath(): string {
// handle docker
if (process.env.ONTIME_DATA) {
return path.join(process.env.ONTIME_DATA);
return join(process.env.ONTIME_DATA);
}
switch (process.platform) {
case 'darwin': {
return path.join(process.env.HOME!, 'Library', 'Application Support', 'Ontime');
return join(process.env.HOME!, 'Library', 'Application Support', 'Ontime');
}
case 'win32': {
return path.join(process.env.APPDATA!, 'Ontime');
return join(process.env.APPDATA!, 'Ontime');
}
case 'linux': {
return path.join(process.env.HOME!, '.Ontime');
return join(process.env.HOME!, '.Ontime');
}
default: {
throw new Error('Could not resolve public folder for platform');
@@ -56,11 +54,11 @@ if (import.meta.url) {
// path to server src folder
const currentDir = dirname(__dirname);
// 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
const productionPath = path.join(srcDirectory, 'client/');
const devPath = path.join(srcDirectory, '../../client/build/');
const productionPath = join(srcDirectory, 'client/');
const devPath = join(srcDirectory, '../../client/build/');
export const resolvedPath = (): string => {
if (isTest) {
@@ -83,12 +81,12 @@ export const uploadsFolderPath = join(getAppDataPath(), config.uploads);
const ensureAppState = () => {
ensureDirectory(getAppDataPath());
fs.writeFileSync(appStatePath, JSON.stringify({ lastLoadedProject: 'db.json' }));
writeFileSync(appStatePath, JSON.stringify({ lastLoadedProject: 'db.json' }));
};
const getLastLoadedProject = () => {
try {
const appState = JSON.parse(fs.readFileSync(appStatePath, 'utf8'));
const appState = JSON.parse(readFileSync(appStatePath, 'utf8'));
if (!appState.lastLoadedProject) {
ensureAppState();
}
@@ -142,11 +140,3 @@ export const resolveCrashReportDirectory = getAppDataPath();
// path to 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
*/
export function consoleRed(message: string) {
export function consoleError(message: string) {
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 { resolveCrashReportDirectory } from '../setup/index.js';
import { ensureDirectory } from './fileManagement.js';
/**
* Writes a file to the crash report location
* @param fileName
@@ -13,10 +14,12 @@ import { resolveCrashReportDirectory } from '../setup/index.js';
*/
function writeToFile(fileName: string, content: object) {
const path = join(resolveCrashReportDirectory, fileName);
ensureDirectory(resolveCrashReportDirectory);
try {
const textContent = JSON.stringify(content, null, 2);
writeFileSync(path, textContent);
} catch (e_rror) {
} catch (_) {
/** We do not handle the error here */
}
}
+9
View File
@@ -1,6 +1,7 @@
import multer from 'multer';
import path from 'path';
import fs from 'fs';
import { rm } from 'fs/promises';
import { ensureDirectory } from './fileManagement.js';
import { getAppDataPath, uploadsFolderPath } from '../setup/index.js';
@@ -56,3 +57,11 @@ export const storage = multer.diskStorage({
cb(null, file.originalname);
},
});
export async function clearUploadfolder() {
try {
await rm(uploadsFolderPath, { recursive: true });
} catch (_) {
//we dont care that there was no folder
}
}