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 25, 2024
1 parent f3d3a6f commit cf99d8a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
17 changes: 16 additions & 1 deletion electron-app/ecoindex-app/src/class/ConfigData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class ConfigData {
static PUPPETEER_BROWSER_INSTALLED = 'puppeteer_browser_installed'
static PUPPETEER_BROWSER_INSTALLATION = 'puppeteer_browser_installation'
static APP_CAN_NOT_BE_LAUNCHED = 'app_can_not_be_launched'
static ERROR_TYPE_NO_NODE = 'error_type_no_node'
static ERROR_TYPE_NO_WRITE_ACCESS = 'error_type_no_write_access'
static ERROR_TYPE_FIRST_INSTALL = 'error_type_first_install'
/**
* The type of the content.
*/
Expand All @@ -31,6 +34,8 @@ export class ConfigData {
*/
message?: string

readonly errorType?: string

/**
* Constructor
* @param type string
Expand All @@ -47,9 +52,19 @@ export class ConfigData {
| 'node_version_is_ok'
| 'puppeteer_browser_installed'
| 'puppeteer_browser_installation'
| 'app_can_not_be_launched'
| 'app_can_not_be_launched',
errorType?:
| 'error_type_no_node'
| 'error_type_no_write_access'
| 'error_type_first_install'
) {
this.type = type
this.errorType = errorType
if (errorType && type !== 'app_can_not_be_launched') {
throw new Error(
"`errorType `can't be used outside of `type` of `app_can_not_be_launched`."
)
}
}
/**
* Return a string representation of the object
Expand Down
2 changes: 1 addition & 1 deletion electron-app/ecoindex-app/src/interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface IElectronAPI {

export interface IInitalization {
// Front → Main
initializeApplication: () => Promise<boolean>
initializeApplication: (forceInitialisation: boolean) => Promise<boolean>
// Main → Front
sendConfigDatasToFront: (callback) => ConfigData
}
Expand Down
29 changes: 26 additions & 3 deletions electron-app/ecoindex-app/src/main/handlers/Initalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,35 @@ const readInitalizedDatas = (value: initializedDatas): boolean => {
return value.initIsNodeInstalled && value.initIsNodeNodeVersionOK
}

const APP_INSTALLED_ONCE = `app_installed_done_once`

export const initialization = async (
event: IpcMainEvent | IpcMainInvokeEvent
event: IpcMainEvent | IpcMainInvokeEvent,
forceInitialisation = false
) => {
const mainLog = getMainLog().scope('main/initialization')
const initializedDatas: initializedDatas = {}
try {
mainLog.log(`Initialization start...`)
// #region Check First launch
if (!forceInitialisation) {
mainLog.log(`0. Is first launch?`)
const hasBeenInstalledOnce = store.get(APP_INSTALLED_ONCE, false)
if (!hasBeenInstalledOnce) {
const firstLaunchDetected = new ConfigData(
'app_can_not_be_launched',
'error_type_first_install'
)
getMainWindow().webContents.send(
channels.INITIALIZATION_DATAS,
firstLaunchDetected
)
return false
}
} else {
mainLog.info(`Installation asked manually for 1st installation.`)
mainLog.debug(`forced mode started from button`)
}
mainLog.log(`1. Node installed start...`)
// #region Node installed
const isNodeReturned = await initIsNodeInstalled(event)
Expand Down Expand Up @@ -252,8 +274,9 @@ export const initialization = async (
}
}

// #region

// #region END
store.set(APP_INSTALLED_ONCE, true)
// TODO
return readInitalizedDatas(initializedDatas)
} catch (error) {
mainLog.error(error)
Expand Down
12 changes: 8 additions & 4 deletions electron-app/ecoindex-app/src/renderer/MainWindow/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,17 @@ function TheApp() {

// #region initialisationAPI
useEffect(() => {
const initalization = async () => {
const initalization = async (forceInitialisation: boolean) => {
frontLog.debug(`initializeApplication start 🚀`)
const result =
await window.initialisationAPI.initializeApplication()
frontLog.debug(`initializeApplication ended '${result}' 👍`)
await window.initialisationAPI.initializeApplication(
forceInitialisation
)
frontLog.debug(
`initializeApplication ended with ${result ? 'OK 👍' : 'KO 🚫'} status.`
)
}
initalization()
initalization(false)

window.initialisationAPI.sendConfigDatasToFront(
(configData: ConfigData) => {
Expand Down

0 comments on commit cf99d8a

Please sign in to comment.