add support for electron modules

This commit is contained in:
Stewart Allen 2024-06-16 21:48:41 -04:00
commit 4d81932f38
5 changed files with 52 additions and 19 deletions

View file

@ -27,6 +27,7 @@ server({
conf: cnfDir,
logs: logDir,
cache: path.join(basDir,"data","cache"),
electron: true,
debug
});

27
app.js
View file

@ -64,23 +64,25 @@ function addonce(array, v) {
};
function init(mod) {
const ENV = mod.env;
startTime = time();
lastmod = mod.util.lastmod;
logger = mod.log;
debug = mod.env.debug || mod.meta.debug;
oversion = mod.env.over || mod.meta.over;
crossOrigin = mod.env.xorigin || mod.meta.xorigin || false;
serviceWorker = (mod.env.service || mod.meta.service) !== false;
debug = ENV.debug || mod.meta.debug;
oversion = ENV.over || mod.meta.over;
crossOrigin = ENV.xorigin || mod.meta.xorigin || false;
serviceWorker = (ENV.service || mod.meta.service) !== false;
http = mod.http;
util = mod.util;
dir = mod.dir;
log = mod.log;
if (mod.env.single) console.log({ cwd: process.cwd(), env: mod.env });
if (ENV.single) console.log({ cwd: process.cwd(), env: ENV });
dversion = debug ? `_${version}` : version;
cacheDir = mod.env.cache || mod.util.datadir("cache");
if (mod.env.single) logger.log({ cacheDir });
forceUseCache = mod.env.cache ? true : false;
cacheDir = ENV.cache || mod.util.datadir("cache");
if (ENV.single) logger.log({ cacheDir });
forceUseCache = ENV.cache ? true : false;
const approot = PATH.join("main","gapp");
const refcache = {};
@ -283,7 +285,14 @@ function init(mod) {
if (dir.charAt(0) === '.') return;
const stats = fs.lstatSync(`${mod.dir}/${modpath}`);
if (!(stats.isDirectory() || stats.isSymbolicLink())) return;
loadModule(mod, modpath);
const isElectronMod = util.isfile(PATH.join(mod.dir,modpath,".electron"));
if (ENV.electron && !isElectronMod) return;
if (!ENV.electron && isElectronMod) return;
try {
loadModule(mod, modpath);
} catch (error) {
console.log({ module: dir, error });
}
});
// run loads injected by modules

View file

@ -2,18 +2,26 @@ const fs = require('fs-extra');
const path = require('path');
const server = require('@gridspace/app-server');
// deref src and web for windows
// deref src, web, mod for windows
const srcTmp = path.join('tmp','src');
fs.copySync("src", srcTmp, { dereference: true });
const webTmp = path.join('tmp','web');
fs.copySync("web", webTmp, { dereference: true });
// const modTmp = path.join('tmp','mod');
// fs.copySync("mod", modTmp, { dereference: true });
const modTmp = path.join('tmp','mod');
fs.copySync("mod", modTmp, { dereference: true, filter:(src,dst) => {
const ok =
src === 'mod' ||
src.indexOf('mod/standalone') === 0 ||
src.indexOf('mod/node_modules') === 0;
// console.log(ok, src);
return ok;
} });
// pre-build asset cache
// create minified asset cache
server({
electron: true,
dryrun: true,
single: true
});

View file

@ -52,10 +52,10 @@
"node-fetch": "^3.3.2"
},
"scripts": {
"start": "electron .",
"start-dev": "electron . --devel",
"start-dbg": "electron . --debugg",
"start-ddb": "electron . --devel --debugg",
"start": "npm run prebuild && electron .",
"start-dev": "npm run prebuild && electron . --devel",
"start-dbg": "npm run prebuild && electron . --debugg",
"start-ddb": "npm run prebuild && electron . --devel --debugg",
"build": "npm run prebuild && electron-builder",
"build-debug": "npm run prebuild && DEBUG=electron-builder electron-builder",
"build-linux": "npm run build -- --linux --x64",
@ -87,6 +87,13 @@
"**/*"
]
},
{
"from": "tmp/mod",
"to": "mod",
"filter": [
"**/*"
]
},
"bin/*",
"conf/**/*",
"data/**/*",
@ -119,6 +126,7 @@
"entitlements": "bin/electron-entitlements.plist",
"entitlementsInherit": "bin/electron-entitlements.plist",
"target": [
"dmg",
"zip"
]
},

View file

@ -21,10 +21,17 @@ gapp.main("main.kiri", [], (root) => {
modfns.push(fn);
},
load_exec(api) {
const saferun = (fn) => {
try {
fn(api || kiri.api);
} catch (error) {
console.log({ module_error: error });
}
};
// complete module loading
modfns.forEach(modfn => { modfn(api || kiri.api)} );
modfns.forEach(modfn => saferun(modfn));
// rewrite load() to be immediate post-finalize
kiri.load = (modfn) => { modfn(api || kiri.api) };
kiri.load = (modfn) => saferun(modfn);
}
};