Skip to content

Commit

Permalink
feat: workdir ok
Browse files Browse the repository at this point in the history
  • Loading branch information
hrenaud committed Jul 19, 2024
1 parent 16959d3 commit 5e86a2c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
24 changes: 11 additions & 13 deletions electron-app/ecoindex-app/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
import { ChildProcess, spawn } from 'child_process'
import { channels, utils } from '../shared/constants'
import { chomp, chunksToLinesAsync } from '@rauschma/stringio'
import { getWorkDir, setWorkDir } from '../shared/memory'

import fixPath from 'fix-path'
import fs from 'fs'
import os from 'os'
import packageJson from '../../package.json'
import path from 'path'
import { shellEnv } from 'shell-env'

// const execFile = util.promisify(_execFile);
Expand All @@ -25,8 +25,6 @@ import { shellEnv } from 'shell-env'
declare const MAIN_WINDOW_WEBPACK_ENTRY: string
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string

let workDir = ''

const runfixPath = () => {
console.log(`RUN fixPath and shellEnv`)
fixPath()
Expand Down Expand Up @@ -179,10 +177,10 @@ let logStream: fs.WriteStream = null
* @param optionalParams string[]
*/
async function _sendMessageToLogFile(message?: any, ...optionalParams: any[]) {
if (!workDir) {
workDir = await _getHomeDir()
if (!getWorkDir()) {
setWorkDir(await _getHomeDir())
}
const logFilePath = `${workDir}/logfile.txt`
const logFilePath = `${getWorkDir()}/logfile.txt`
if (!logStream) {
logStream = fs.createWriteStream(logFilePath)
logStream.write('')
Expand Down Expand Up @@ -298,12 +296,12 @@ const handleWorkDir = async (event: IpcMainEvent, newDir: string) => {
logStream = null
console.log(`Reset logStream`)

workDir = newDir
setWorkDir(newDir)
} else {
workDir = await _getHomeDir()
setWorkDir(await _getHomeDir())
}
// console.log(`workDir: ${workDir}`)
return await workDir
return await getWorkDir()
}

const handleIsJsonConfigFileExist = async (
Expand Down Expand Up @@ -333,7 +331,7 @@ async function _prepareJsonCollect(): Promise<{
}> {
// create stream to log the output. TODO: use specified path
try {
const _workDir = await workDir
const _workDir = await getWorkDir()
if (!_workDir || _workDir === '') {
throw new Error('Work dir not found')
}
Expand Down Expand Up @@ -520,7 +518,7 @@ const handleJsonSaveAndCollect = async (
_debugLogs('Json save or/and collect start...')

try {
const _workDir = await workDir
const _workDir = await getWorkDir()
if (!_workDir || _workDir === '') {
throw new Error('Work dir not found')
}
Expand Down Expand Up @@ -625,7 +623,7 @@ const handleJsonReadAndReload = async (event: IpcMainEvent) => {
body: 'Process intialization.',
})
try {
const _workDir = await workDir
const _workDir = await getWorkDir()
if (!_workDir || _workDir === '') {
throw new Error('Work dir not found')
}
Expand Down Expand Up @@ -789,7 +787,7 @@ async function handleSelectFolder() {
}
const { canceled, filePaths } = await dialog.showOpenDialog(options)
if (!canceled) {
workDir = filePaths[0]
setWorkDir(filePaths[0])
return filePaths[0]
}
}
Expand Down
26 changes: 26 additions & 0 deletions electron-app/ecoindex-app/src/shared/memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { BrowserWindow } from 'electron'
import fs from 'fs'
let workDir = ''
let logStream: fs.WriteStream = null
let mainWindow: BrowserWindow = null
let logStreamPath = ''

export const getWorkDir = () => {
return workDir
}
export const setWorkDir = (value: string) => {
workDir = value
}
export const getMainWindow = () => {
return mainWindow
}
export const setMainWindow = (value: BrowserWindow) => {
mainWindow = value
}
export const getLogSteam = () => {
return logStream
}
export const setLogStream = (path: string) => {
if (path) logStreamPath = path
logStream = fs.createWriteStream(logStreamPath)
}

0 comments on commit 5e86a2c

Please sign in to comment.