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 23, 2024
1 parent 32ec9b1 commit 3a0988a
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 7 deletions.
60 changes: 57 additions & 3 deletions electron-app/ecoindex-app/src/main/handlers/Initalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { initGetHomeDir } from './initHandlers/getHomeDir'
import { initGetWorkDir } from './initHandlers/getWorkDir'
import { initIsNodeInstalled } from './initHandlers/IsNodeInstalled'
import { initIsNodeNodeVersionOK } from './initHandlers/isNodeVersionOK'
import { initPluginCanInstall } from './initHandlers/plugin_canInstall'
import { initPluginGetLastVersion } from './initHandlers/plugin_getLastVersion'
import { initPluginIsIntalled } from './initHandlers/plugin_isInstalled'
import { initPuppeteerBrowserInstallation } from './initHandlers/puppeteerBrowser_installation'
import { initPuppeteerBrowserIsInstalled } from './initHandlers/puppeteerBrowser_isInstalled'
Expand All @@ -23,7 +25,9 @@ type initializedDatas = {
initGetWorkDir?: string
initSetNpmDir?: string
initPuppeteerBrowserIsInstalled?: boolean
initPluginIsIntalled?: boolean
initPluginIsIntalled?: boolean | string
initPluginGetLastVersion?: string
initPluginCanInstall?: boolean
}

const readInitalizedDatas = (value: initializedDatas): boolean => {
Expand Down Expand Up @@ -151,12 +155,62 @@ export const initialization = async (
return false
}

mainLog.log(`7. Is a Plugin installed ...`)
mainLog.log(`7.1 Is a Plugin installed on host ...`)
// #region Plugin Installed
const getPluginIsInstalledReturned = await initPluginIsIntalled(event)
initializedDatas.initPluginIsIntalled =
getPluginIsInstalledReturned.result as boolean
getPluginIsInstalledReturned.result as string
mainLog.log(getPluginIsInstalledReturned.toString())
// #region Plugin Last Version
if (initializedDatas.initPluginIsIntalled) {
// plugin installed
mainLog.log(`7.2 Plugin is Installed on host ...`)
mainLog.log(`7.2 Check plugin last version on registry ...`)
const getPluginGetLastVersionReturned =
await initPluginGetLastVersion(
event,
initializedDatas.initPluginIsIntalled
)
initializedDatas.initPluginGetLastVersion =
getPluginGetLastVersionReturned.result as string
mainLog.log(getPluginGetLastVersionReturned.toString())
if (
initializedDatas.initPluginGetLastVersion ===
initializedDatas.initPluginIsIntalled
) {
const pluginMessage = `Plugin version installed is ${initializedDatas.initPluginGetLastVersion}`
const pluginOK = new ConfigData('plugin_installed')
pluginOK.result = true
pluginOK.message = pluginMessage
getMainWindow().webContents.send(
channels.INITIALIZATION_DATAS,
pluginOK
)
const pluginVersion = new ConfigData('plugin_version')
pluginVersion.result = initializedDatas.initPluginGetLastVersion
pluginVersion.message = pluginMessage
getMainWindow().webContents.send(
channels.INITIALIZATION_DATAS,
pluginVersion
)
}
} else {
// plugin not installed
mainLog.log(`7.2 Plugin NOT installed on host ...`)
mainLog.log(`7.2 Check if electron can write in ~/.npm ...`)
const getPluginCanInstallReturned =
await initPluginCanInstall(event)
initializedDatas.initPluginCanInstall =
getPluginCanInstallReturned.result as boolean
mainLog.log(getPluginCanInstallReturned.toString())
if (initializedDatas.initPluginCanInstall) {
mainLog.log(`7.3 Electron can write in ~/.npm ...`)
mainLog.log(`7.3 Plugin installation ...`)
} else {
mainLog.log(`7.3 Electron CAN'T write in ~/.npm ...`)
mainLog.log(`7.3 Plugin SUDO installation ...`)
}
}

// #region

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const initIsNodeNodeVersionOK = async (
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}`
output.message = returned
}
mainLog.debug(output)
return new Promise<ConfigData>((resolve, reject) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { IpcMainEvent, IpcMainInvokeEvent } from 'electron'
import { accessSync, constants } from 'node:fs'

import { ConfigData } from '../../../class/ConfigData'
import { getMainLog } from '../../main'
import os from 'node:os'
import path from 'node:path'

export const initPluginCanInstall = (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_event: IpcMainEvent | IpcMainInvokeEvent
) => {
const mainLog = getMainLog().scope(
'main/initialization/initPluginGetLastVersion'
)
mainLog.debug(
`Check latest version of lighthouse-plugin-ecoindex on registry.`
)
const toReturned = new ConfigData('plugin_installed')
return new Promise<ConfigData>((resolve) => {
try {
const { homedir } = os.userInfo()
// for testing `/Users/normaluser/Public` is not writable or readable
// const npmPath = path.join(
// os.platform() === 'win32' ? `C:\\` : `/`,
// `Users`,
// `normaluser`,
// `Public`
// )
const npmPath = path.join(homedir, '.npm')
accessSync(npmPath, constants.R_OK && constants.W_OK)
toReturned.result = true
toReturned.message = `User can write`
} catch (error) {
toReturned.result = false
toReturned.message = `User CAN'T write`
}
resolve(toReturned)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { IpcMainEvent, IpcMainInvokeEvent } from 'electron'

import { ConfigData } from '../../../class/ConfigData'
import { exec } from 'child_process'
import { getMainLog } from '../../main'

export const initPluginGetLastVersion = (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_event: IpcMainEvent | IpcMainInvokeEvent,
currentInstalledVersion: string
) => {
const mainLog = getMainLog().scope(
'main/initialization/initPluginGetLastVersion'
)
mainLog.debug(
`Check latest version of lighthouse-plugin-ecoindex on registry.`
)
/**
* Dernière version sur le registry.
*/
let latestVersion = ''
const toReturned = new ConfigData('plugin_installed')
return new Promise<ConfigData>((resolve) => {
const cmd = `npm view lighthouse-plugin-ecoindex version`
exec(cmd, (error, stdout, stderr) => {
if (error) {
mainLog.error(`exec error: ${error}`)
toReturned.error =
toReturned.message = `lighthouse-plugin-ecoindex can't check on registery`
// resolve(toReturned)
}
if (stdout) {
mainLog.debug(`latest version: ${stdout.trim()}`)
// if (stderr) mainLog.error(`stderr: ${stderr}`)

latestVersion = stdout.replace(`\n`, ``).trim()
if (
currentInstalledVersion.trim() !==
stdout.replace(`\n`, ``).trim()
) {
toReturned.result = latestVersion.trim()
toReturned.message = `Update from version:${currentInstalledVersion.trim()} to latest version:${latestVersion.trim()} needed`
mainLog.debug(toReturned.message)
return resolve(toReturned)
} else {
toReturned.result = latestVersion.trim()
toReturned.message = `lighthouse-plugin-ecoindex is up to date`
mainLog.debug(toReturned.message)
return resolve(toReturned)
}
}
})
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export const initPluginIsIntalled = (
.filter((version) => version !== null)
currentVersion = versions[0].replace(`\n`, ``).trim()
mainLog.debug(`version installed: ${currentVersion}`)
toReturned.result = true
toReturned.message = `${currentVersion}`
toReturned.message = `plugin allready installed`
toReturned.result = `${currentVersion}`
resolve(toReturned)
} catch (error) {
throw new Error(error)
Expand Down
8 changes: 7 additions & 1 deletion electron-app/ecoindex-app/src/renderer/MainWindow/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ function TheApp() {
const [isNodeVersionOK, setIsNodeVersionOK] = useState(false)
const [isPuppeteerBrowserInstalled, setIsPuppeteerBrowserInstalled] =
useState(false)
const [puppeteerBrowserInstalled, setPuppeteerBrowserInstalled] =
useState('loading...')
const [
isLighthouseEcoindexPluginInstalled,
setIsLighthouseEcoindexPluginInstalled,
Expand Down Expand Up @@ -576,6 +578,7 @@ function TheApp() {
break
case ConfigData.NODE_VERSION_IS_OK:
setIsNodeVersionOK(configData.result as boolean)
setNodeVersion(configData.message)
increment()
break
case ConfigData.PLUGIN_INSTALLED:
Expand All @@ -592,6 +595,9 @@ function TheApp() {
setIsPuppeteerBrowserInstalled(
configData.result !== null
)
setPuppeteerBrowserInstalled(
configData.result as string
)
increment()
break
case ConfigData.APP_READY:
Expand Down Expand Up @@ -703,7 +709,7 @@ function TheApp() {
<div>workDir: {workDir}</div>
<div>homeDir: {homeDir}</div>
<div>
PuppeteerBrowser: {isPuppeteerBrowserInstalled}
PuppeteerBrowser: {puppeteerBrowserInstalled}
</div>
</>
)}
Expand Down

0 comments on commit 3a0988a

Please sign in to comment.