Skip to content

Commit

Permalink
feat: continue Nouvelle initialisation de l'application #47
Browse files Browse the repository at this point in the history
  • Loading branch information
hrenaud committed Aug 21, 2024
1 parent 770d60a commit 9452952
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 72 deletions.
4 changes: 2 additions & 2 deletions electron-app/ecoindex-app/src/class/ConfigData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Object used to transport datas from `Back` to `Front`.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class ConfigData {
export class ConfigData {
static WORKDIR = 'workDir'
static HOMEDIR = 'homeDir'
static APP_READY = 'appReady'
Expand All @@ -22,7 +22,7 @@ class ConfigData {
/**
* The error if fail.
*/
error?: string | object | Error
error?: any
/**
* A message if needed.
*/
Expand Down
9 changes: 8 additions & 1 deletion electron-app/ecoindex-app/src/interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface IElectronAPI {
sendLogToFront: (callback) => string
sendMessageToFrontLog: (callback) => object
sendDatasToFront: (callback) => object
sendConfigDatasToFront: (callback) => ConfigData
// Front → Main
getInitialTranslations: () => Promise<object>
handleSetFolderOuput: () => Promise<string>
Expand All @@ -40,6 +39,13 @@ export interface IElectronAPI {
hideHelloWindow: () => Promise<void>
}

export interface IInitalization {
// Front → Main
initializeApplication: () => Promise<boolean>
// Main → Front
sendConfigDatasToFront: (callback) => ConfigData
}

declare global {
export interface IJsonMesureData {
'extra-header': object | null
Expand Down Expand Up @@ -67,5 +73,6 @@ declare global {
versions: IVersionsAPI
electronAPI: IElectronAPI
store: IStoreAPI
initialisationAPI: IInitalization
}
}
53 changes: 53 additions & 0 deletions electron-app/ecoindex-app/src/main/handlers/Initalization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { IpcMainEvent, IpcMainInvokeEvent } from 'electron'

import Store from 'electron-store'
import { channels } from '../../shared/constants'
import { getMainLog } from '../main'
import { getMainWindow } from '../../shared/memory'
import { initIsNodeInstalled } from './initHandlers/IsNodeInstalled'
import { initIsNodeNodeVersionOK } from './initHandlers/isNodeVersionOK'

const store = new Store()

type initializedDatas = {
initIsNodeInstalled?: boolean
initIsNodeNodeVersionOK?: boolean
}

const readInitalizedDatas = (value: initializedDatas): boolean => {
return value.initIsNodeInstalled && value.initIsNodeNodeVersionOK
}

export const initialization = async (
event: IpcMainEvent | IpcMainInvokeEvent
) => {
const mainLog = getMainLog().scope('main/initialization')
const initializedDatas: initializedDatas = {}
try {
mainLog.log(`Initialization start...`)
mainLog.log(`1. Node installed start...`)
// #region Node installed
const isNodeReturned = await initIsNodeInstalled(event)
initializedDatas.initIsNodeInstalled = isNodeReturned.result as boolean
mainLog.log(isNodeReturned.toString())
getMainWindow().webContents.send(
channels.INITIALIZATION_DATAS,
isNodeReturned
)
mainLog.log(`2. Node Version upper or equal to 20 start...`)
// #region Node installed
const isNode20Returned = await initIsNodeNodeVersionOK(event)
initializedDatas.initIsNodeNodeVersionOK =
isNode20Returned.result as boolean
mainLog.log(isNode20Returned.toString())
getMainWindow().webContents.send(
channels.INITIALIZATION_DATAS,
isNode20Returned
)

return readInitalizedDatas(initializedDatas)
} catch (error) {
mainLog.error(error)
return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { IpcMainEvent, IpcMainInvokeEvent } from 'electron'
import { getTryNode, setTryNode } from '../../../shared/memory'

import { ConfigData } from '../../../class/ConfigData'
import { channels } from '../../../shared/constants'
import { getMainLog } from '../../main'
import { handle_CMD_Actions } from '../HandleCMDActions'
import { sleep } from '../../../main/utils/Sleep'

/**
*
* @param _event
* @returns {string}
*/
const fetchNode = async (
_event: IpcMainEvent | IpcMainInvokeEvent
): Promise<string> => {
const _nodeDir = await handle_CMD_Actions(
_event,
channels.IS_NODE_INSTALLED
)
// #region tries read node
// give some tries before return false
if (_nodeDir === '' && getTryNode() > 0) {
await sleep(2000)
setTryNode()
return fetchNode(_event)
}
return _nodeDir as string
}

export const initIsNodeInstalled = async (
_event: IpcMainEvent | IpcMainInvokeEvent
) => {
const mainLog = getMainLog().scope(
'main/initialization/initIsNodeInstalled'
)
try {
const returned: string = await fetchNode(_event)

const output = new ConfigData('node_installed')
if (returned === '') {
output.error = `Node not found`
} else {
output.result = true
output.message = `Node is Installed in ${returned}`
}
mainLog.debug(output)
return new Promise<ConfigData>((resolve, reject) => {
if (output.error) reject(output)
else resolve(output)
})
} catch (error) {
mainLog.error(error)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { IpcMainEvent, IpcMainInvokeEvent } from 'electron'

import { ConfigData } from '../../../class/ConfigData'
import { channels } from '../../../shared/constants'
import { getMainLog } from '../../main'
import { handle_CMD_Actions } from '../HandleCMDActions'

export const initIsNodeNodeVersionOK = async (
_event: IpcMainEvent | IpcMainInvokeEvent
) => {
const mainLog = getMainLog().scope(
'main/initialization/initIsNodeNodeVersionOK'
)
try {
const returned: string = await handle_CMD_Actions(
_event,
channels.GET_NODE_VERSION
)
const major = returned.replace('v', '').split('.')[0]

const output = new ConfigData('node_version_is_ok')
if (returned === '') {
output.error = `Node version not found`
} else {
output.result = Number(major) >= 20
output.message = `Node version is ${returned} and it's upper or equal to 20=${output.result}`
}
mainLog.debug(output)
return new Promise<ConfigData>((resolve, reject) => {
if (output.error) reject(output)
else resolve(output)
})
} catch (error) {
mainLog.error(error)
}
}
2 changes: 2 additions & 0 deletions electron-app/ecoindex-app/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { handleSelectFolder } from './handlers/HandleSelectFolder'
import { handleWorkDir } from './handlers/HandleWorkDir'
import { handle_CMD_Actions } from './handlers/HandleCMDActions'
import i18n from '../configs/i18next.config'
import { initialization } from './handlers/Initalization'
import { isLighthouseEcoindexInstalled } from './handlers/IsLighthouseEcoindexInstalled'
import { isPupperteerBrowserInstalled } from './handlers/IsPuppeteerBrowserInstalled'
import log from 'electron-log/main'
Expand Down Expand Up @@ -102,6 +103,7 @@ app.on('ready', () => {
ipcMain.handle(channels.READ_RELOAD_JSON_FILE, handleJsonReadAndReload)

// communs handlers and getters
ipcMain.handle(channels.INITIALIZATION_APP, initialization)
ipcMain.handle(channels.GET_HOMEDIR, handleHomeDir)
ipcMain.handle(channels.GET_WORKDIR, handleWorkDir)
ipcMain.handle(channels.IS_NODE_INSTALLED, handleNodeInstalled)
Expand Down
Loading

0 comments on commit 9452952

Please sign in to comment.