From bf6a3c94c900038efb81274f67ddb0fc4d3ae5de Mon Sep 17 00:00:00 2001 From: ali asaria <aliasaria@users.noreply.github.com> Date: Fri, 16 Aug 2024 10:56:07 -0400 Subject: [PATCH] generate the log file path in a function --- src/main/main.ts | 25 +++++++++++++------------ src/main/util.ts | 9 +++++++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/main.ts b/src/main/main.ts index caa3aca1..10f8b949 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -10,15 +10,7 @@ * `./src/main.js` using webpack. This gives us some performance wins. */ import path from 'path'; -import { - app, - BrowserWindow, - shell, - ipcMain, - utilityProcess, - Menu, - dialog, -} from 'electron'; +import { app, BrowserWindow, shell, ipcMain, dialog } from 'electron'; import { autoUpdater } from 'electron-updater'; import log from 'electron-log'; import Store from 'electron-store'; @@ -35,9 +27,10 @@ import { checkIfCondaEnvironmentExists, checkDependencies, checkIfCondaBinExists, - getTransformerLabCodeDir, + getLogFilePath, } from './util'; +const fs = require('fs'); const Tail = require('tail').Tail; // //////////// @@ -133,8 +126,16 @@ console.log('setting up listening to log file'); const startListeningToServerLog = async () => { // Now listen to the log file and send updates to the renderer - const server_dir = await getTransformerLabCodeDir(); - const logFile = path.join(server_dir, 'local_server.log'); + const logFile = await getLogFilePath(); + //create the file if it doesn't exist: + if (!fs.existsSync(logFile)) { + // first make the directory: + const logDir = path.dirname(logFile); + if (!fs.existsSync(logDir)) { + fs.mkdirSync(logDir, { recursive: true }); + } + fs.writeFileSync(logFile, ''); + } let tail = new Tail(logFile); let currentlySubscribed = false; diff --git a/src/main/util.ts b/src/main/util.ts index d6e18a3c..0963bc19 100644 --- a/src/main/util.ts +++ b/src/main/util.ts @@ -1,6 +1,7 @@ /* eslint import/prefer-default-export: off */ import { URL } from 'url'; import path from 'path'; +import { log } from 'console'; const fs = require('fs'); const os = require('os'); const { spawn, exec, ChildProcess } = require('child_process'); @@ -42,6 +43,10 @@ export async function getTransformerLabCodeDir() { : transformerLabDir; } +export async function getLogFilePath() { + return path.join(await getTransformerLabCodeDir(), 'local_server.log'); +} + export function resolveHtmlPath(htmlFileName: string) { if (process.env.NODE_ENV === 'development') { const port = process.env.PORT || 1212; @@ -106,7 +111,7 @@ export async function checkLocalServerVersion() { export async function startLocalServer() { const server_dir = await getTransformerLabCodeDir(); - const logFilePath = path.join(server_dir, 'local_server.log'); + const logFilePath = await getLogFilePath(); const out = fs.openSync(logFilePath, 'a'); const err = fs.openSync(logFilePath, 'a'); @@ -363,7 +368,7 @@ function truncate(str: string, max: number) { */ export async function executeInstallStep(argument: string) { const server_dir = await getTransformerLabCodeDir(); - const logFilePath = path.join(server_dir, 'local_server.log'); + const logFilePath = await getLogFilePath(); const out = fs.openSync(logFilePath, 'a'); const err = fs.openSync(logFilePath, 'a'); -- GitLab