From cf99d8a68e07cffbf1fd9bf725aeac42fab77642 Mon Sep 17 00:00:00 2001 From: Renaud Heluin Date: Sun, 25 Aug 2024 19:05:43 +0200 Subject: [PATCH] feat: continue Nouvelle initialisation de l'application #47 --- .../ecoindex-app/src/class/ConfigData.ts | 17 ++++++++++- electron-app/ecoindex-app/src/interface.d.ts | 2 +- .../src/main/handlers/Initalization.ts | 29 +++++++++++++++++-- .../src/renderer/MainWindow/App.tsx | 12 +++++--- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/electron-app/ecoindex-app/src/class/ConfigData.ts b/electron-app/ecoindex-app/src/class/ConfigData.ts index a9c2bba..f9df850 100644 --- a/electron-app/ecoindex-app/src/class/ConfigData.ts +++ b/electron-app/ecoindex-app/src/class/ConfigData.ts @@ -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. */ @@ -31,6 +34,8 @@ export class ConfigData { */ message?: string + readonly errorType?: string + /** * Constructor * @param type string @@ -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 diff --git a/electron-app/ecoindex-app/src/interface.d.ts b/electron-app/ecoindex-app/src/interface.d.ts index f488012..b9a291c 100644 --- a/electron-app/ecoindex-app/src/interface.d.ts +++ b/electron-app/ecoindex-app/src/interface.d.ts @@ -41,7 +41,7 @@ export interface IElectronAPI { export interface IInitalization { // Front → Main - initializeApplication: () => Promise + initializeApplication: (forceInitialisation: boolean) => Promise // Main → Front sendConfigDatasToFront: (callback) => ConfigData } diff --git a/electron-app/ecoindex-app/src/main/handlers/Initalization.ts b/electron-app/ecoindex-app/src/main/handlers/Initalization.ts index 69f2437..d3a3ecd 100644 --- a/electron-app/ecoindex-app/src/main/handlers/Initalization.ts +++ b/electron-app/ecoindex-app/src/main/handlers/Initalization.ts @@ -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) @@ -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) diff --git a/electron-app/ecoindex-app/src/renderer/MainWindow/App.tsx b/electron-app/ecoindex-app/src/renderer/MainWindow/App.tsx index a390e54..acf86e8 100644 --- a/electron-app/ecoindex-app/src/renderer/MainWindow/App.tsx +++ b/electron-app/ecoindex-app/src/renderer/MainWindow/App.tsx @@ -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) => {