UNPKG

ucbuilder

Version:

:Shree Ganeshay Namah: new way app design

110 lines 4.78 kB
import path, { dirname, resolve } from "node:path"; import url, { fileURLToPath, pathToFileURL } from "node:url"; import fs from "node:fs"; import { PathBridge } from "../../build/pathBridge.js"; import { configManage } from "./configManage.js"; import { correctpath, getCloneableObject, IPC_API_KEY, IPC_GET_KEY } from "../../common/ipc/enumAndMore.js"; import { createImportMap, scanAllProjects } from "./importMapGenerator.js"; export function IpcMainGroup(regKey) { if (typeof window !== "undefined") return; PathBridge.path = path; PathBridge.url = url; let urlObj = regKey; // GetRootPathByUrl_M(urlpath, configManage.filler.ucConfigList); let rtrn = { On: (key, callback) => { return IpcMainHelper.On(key, callback, urlObj); }, Handle: (key, callback) => { return IpcMainHelper.Handle(key, callback, urlObj); }, Send: (key, evt, ...args) => { IpcMainHelper.Send(key, evt, args, urlObj); //evt.sender.send(key, callback, urlPath); }, Reply: (key, evt, ...args) => { IpcMainHelper.Reply(key, evt, args /*[...args]*/, urlObj); //evt.sender.send(key, callback, urlPath); } }; return rtrn; } export class IpcMainHelper { static Send(actionKey, evt, args, importMetaUrl = "") { args = getCloneableObject(args); //args.forEach(s => s = getCloneableObject(s)); evt.sender.send(IPC_API_KEY, IPC_GET_KEY(actionKey, importMetaUrl), ...args); } static Reply(actionKey, evt, args, importMetaUrl = "") { //console.log(...args); args = getCloneableObject(args); //args.forEach(s => s = getCloneableObject(s)); evt.reply(IPC_API_KEY, IPC_GET_KEY(actionKey, importMetaUrl), ...args); } static IPC_ON = new Map(); //{ [actionKey: string]: IpcMainCallBack } = {}; static IPC_HANDLE = new Map(); // { [actionKey: string]: IpcMainInvokeCallBack } = {}; static On(actionKey, callback, importMetaUrl = "") { actionKey = IPC_GET_KEY(actionKey, importMetaUrl); if (!this.IPC_ON.has(actionKey)) this.IPC_ON.set(actionKey, callback); } static Handle(actionKey, callback, importMetaUrl = "") { actionKey = IPC_GET_KEY(actionKey, importMetaUrl); if (!this.IPC_HANDLE.has(actionKey)) this.IPC_HANDLE.set(actionKey, callback); } static async init(_ipcMain) { _ipcMain.on(IPC_API_KEY, (event, ...args) => { let actionKey = args.shift(); if (this.IPC_ON.has(actionKey)) this.IPC_ON.get(actionKey)(event, ...args); else { //configManage.filler.savePreLoadFilePath(actionKey); console.log(`!!! no 'ON EVENT' found [${actionKey}]`); } }); _ipcMain.handle(IPC_API_KEY, async (event, ...args) => { let actionKey = args.shift(); if (this.IPC_HANDLE.has(actionKey)) return await this.IPC_HANDLE.get(actionKey)(event, ...args); else { console.log(`no 'HANDLE EVENT' found [${actionKey}]`); return undefined; } }); await configManage.init(); } static INITIAL_SCRIPT = ""; static async loadURL(_path, win, options) { let htmlUrl, htmlPath; if (_path.startsWith('file:///')) { htmlUrl = _path; htmlPath = fileURLToPath(_path); } else { htmlUrl = pathToFileURL(_path).href; htmlPath = _path; } const baseURLForDataURL = options?.baseURLForDataURL ?? htmlUrl; let html = fs.readFileSync(htmlPath, "utf-8"); let projectDirList = await scanAllProjects(); const importMap = createImportMap(_path, projectDirList, dirname(baseURLForDataURL)); let mapStr = JSON.stringify(importMap); const modulePath = correctpath(resolve(dirname(fileURLToPath(import.meta.url)), '../../renderer/ipc/ShubhLabh.js')); const importMapScript = `<script type="importmap">${mapStr}</script> <script type="module" src="${modulePath}"></script>`; const headRegex = /<head\b[^>]*>/i; const htmlRegex = /<html\b[^>]*>/i; if (headRegex.test(html)) { html = html.replace(headRegex, match => match + importMapScript); } else if (htmlRegex.test(html)) { html = html.replace(htmlRegex, match => `${match}\n<head>${importMapScript}</head>`); } else { html = `${importMapScript}\n${html}`; } win.loadURL("data:text/html;charset=utf-8," + encodeURIComponent(html), options); } } //# sourceMappingURL=IpcMainHelper.js.map