Skip to content

Commit 20503de

Browse files
committedJun 23, 2024·
refactor: minor code cleanups
1 parent 0335da9 commit 20503de

File tree

7 files changed

+32
-28
lines changed

7 files changed

+32
-28
lines changed
 

‎apps/server/src/app.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
resolveExternalsDirectory,
1818
resolveStylesDirectory,
1919
resolvedPath,
20-
clearUploadfolder,
2120
} from './setup/index.js';
2221
import { ONTIME_VERSION } from './ONTIME_VERSION.js';
2322
import { consoleSuccess, consoleHighlight } from './utils/console.js';
@@ -44,6 +43,9 @@ import { messageService } from './services/message-service/MessageService.js';
4443
import { populateDemo } from './setup/loadDemo.js';
4544
import { getState } from './stores/runtimeState.js';
4645
import { initRundown } from './services/rundown-service/RundownService.js';
46+
47+
// Utilities
48+
import { clearUploadfolder } from './utils/upload.js';
4749
import { generateCrashReport } from './utils/generateCrashReport.js';
4850
import { getNetworkInterfaces } from './utils/networkInterfaces.js';
4951

‎apps/server/src/classes/Logger.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { generateId, millisToString } from 'ontime-utils';
44
import { clock } from '../services/Clock.js';
55
import { isProduction } from '../setup/index.js';
66
import { socket } from '../adapters/WebsocketAdapter.js';
7-
import { consoleSubdued, consoleRed } from '../utils/console.js';
7+
import { consoleSubdued, consoleError } from '../utils/console.js';
88

99
class Logger {
1010
private queue: Log[];
@@ -47,7 +47,7 @@ class Logger {
4747
private _push(log: Log) {
4848
if (this.canLog || log.level === LogLevel.Severe) {
4949
if (log.level === LogLevel.Severe) {
50-
consoleRed(`[${log.level}] \t ${log.origin} \t ${log.text}`);
50+
consoleError(`[${log.level}] \t ${log.origin} \t ${log.text}`);
5151
} else {
5252
consoleSubdued(`[${log.level}] \t ${log.origin} \t ${log.text}`);
5353
}

‎apps/server/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-console */
2-
import { consoleHighlight, consoleRed } from './utils/console.js';
2+
import { consoleHighlight, consoleError } from './utils/console.js';
33
import { initAssets, startIntegrations, startServer } from './app.js';
44

55
async function startOntime() {
@@ -16,7 +16,7 @@ async function startOntime() {
1616
consoleHighlight('Request: Start integrations...');
1717
await startIntegrations();
1818
} catch (error) {
19-
consoleRed(`Request failed: ${error}`);
19+
consoleError(`Request failed: ${error}`);
2020
}
2121
}
2222

‎apps/server/src/setup/index.ts

+11-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { fileURLToPath } from 'url';
2-
import path, { dirname, join } from 'path';
3-
import fs from 'fs';
4-
5-
import { rm } from 'fs/promises';
2+
import { dirname, join } from 'path';
3+
import { readFileSync, writeFileSync } from 'fs';
64

75
import { config } from './config.js';
86
import { ensureDirectory } from '../utils/fileManagement.js';
@@ -17,18 +15,18 @@ import { ensureDirectory } from '../utils/fileManagement.js';
1715
export function getAppDataPath(): string {
1816
// handle docker
1917
if (process.env.ONTIME_DATA) {
20-
return path.join(process.env.ONTIME_DATA);
18+
return join(process.env.ONTIME_DATA);
2119
}
2220

2321
switch (process.platform) {
2422
case 'darwin': {
25-
return path.join(process.env.HOME!, 'Library', 'Application Support', 'Ontime');
23+
return join(process.env.HOME!, 'Library', 'Application Support', 'Ontime');
2624
}
2725
case 'win32': {
28-
return path.join(process.env.APPDATA!, 'Ontime');
26+
return join(process.env.APPDATA!, 'Ontime');
2927
}
3028
case 'linux': {
31-
return path.join(process.env.HOME!, '.Ontime');
29+
return join(process.env.HOME!, '.Ontime');
3230
}
3331
default: {
3432
throw new Error('Could not resolve public folder for platform');
@@ -56,11 +54,11 @@ if (import.meta.url) {
5654
// path to server src folder
5755
const currentDir = dirname(__dirname);
5856
// locally we are in src/setup, in the production build, this is a single file at src
59-
export const srcDirectory = isProduction ? currentDir : path.join(currentDir, '../');
57+
export const srcDirectory = isProduction ? currentDir : join(currentDir, '../');
6058

6159
// resolve path to external
62-
const productionPath = path.join(srcDirectory, 'client/');
63-
const devPath = path.join(srcDirectory, '../../client/build/');
60+
const productionPath = join(srcDirectory, 'client/');
61+
const devPath = join(srcDirectory, '../../client/build/');
6462

6563
export const resolvedPath = (): string => {
6664
if (isTest) {
@@ -83,12 +81,12 @@ export const uploadsFolderPath = join(getAppDataPath(), config.uploads);
8381

8482
const ensureAppState = () => {
8583
ensureDirectory(getAppDataPath());
86-
fs.writeFileSync(appStatePath, JSON.stringify({ lastLoadedProject: 'db.json' }));
84+
writeFileSync(appStatePath, JSON.stringify({ lastLoadedProject: 'db.json' }));
8785
};
8886

8987
const getLastLoadedProject = () => {
9088
try {
91-
const appState = JSON.parse(fs.readFileSync(appStatePath, 'utf8'));
89+
const appState = JSON.parse(readFileSync(appStatePath, 'utf8'));
9290
if (!appState.lastLoadedProject) {
9391
ensureAppState();
9492
}
@@ -142,11 +140,3 @@ export const resolveCrashReportDirectory = getAppDataPath();
142140

143141
// path to projects
144142
export const resolveProjectsDirectory = join(getAppDataPath(), config.projects);
145-
146-
export async function clearUploadfolder() {
147-
try {
148-
await rm(uploadsFolderPath, { recursive: true });
149-
} catch (_) {
150-
//we dont care that there was no folder
151-
}
152-
}

‎apps/server/src/utils/console.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function consoleSuccess(message: string) {
1010
/**
1111
* Utility function to log messages in red
1212
*/
13-
export function consoleRed(message: string) {
13+
export function consoleError(message: string) {
1414
console.error(inRed(message));
1515
}
1616

‎apps/server/src/utils/generateCrashReport.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ import { get } from '../services/rundown-service/rundownCache.js';
66
import { getState } from '../stores/runtimeState.js';
77
import { resolveCrashReportDirectory } from '../setup/index.js';
88

9+
import { ensureDirectory } from './fileManagement.js';
910
/**
1011
* Writes a file to the crash report location
1112
* @param fileName
1213
* @param content
1314
*/
1415
function writeToFile(fileName: string, content: object) {
1516
const path = join(resolveCrashReportDirectory, fileName);
17+
ensureDirectory(resolveCrashReportDirectory);
18+
1619
try {
1720
const textContent = JSON.stringify(content, null, 2);
1821
writeFileSync(path, textContent);
19-
} catch (e_rror) {
22+
} catch (_) {
2023
/** We do not handle the error here */
2124
}
2225
}

‎apps/server/src/utils/upload.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import multer from 'multer';
22
import path from 'path';
33
import fs from 'fs';
4+
import { rm } from 'fs/promises';
45

56
import { ensureDirectory } from './fileManagement.js';
67
import { getAppDataPath, uploadsFolderPath } from '../setup/index.js';
@@ -56,3 +57,11 @@ export const storage = multer.diskStorage({
5657
cb(null, file.originalname);
5758
},
5859
});
60+
61+
export async function clearUploadfolder() {
62+
try {
63+
await rm(uploadsFolderPath, { recursive: true });
64+
} catch (_) {
65+
//we dont care that there was no folder
66+
}
67+
}

0 commit comments

Comments
 (0)
Please sign in to comment.