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, conf: cnfDir,
logs: logDir, logs: logDir,
cache: path.join(basDir,"data","cache"), cache: path.join(basDir,"data","cache"),
electron: true,
debug debug
}); });

27
app.js
View file

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

View file

@ -2,18 +2,26 @@ const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const server = require('@gridspace/app-server'); const server = require('@gridspace/app-server');
// deref src and web for windows // deref src, web, mod for windows
const srcTmp = path.join('tmp','src'); const srcTmp = path.join('tmp','src');
fs.copySync("src", srcTmp, { dereference: true }); fs.copySync("src", srcTmp, { dereference: true });
const webTmp = path.join('tmp','web'); const webTmp = path.join('tmp','web');
fs.copySync("web", webTmp, { dereference: true }); fs.copySync("web", webTmp, { dereference: true });
// const modTmp = path.join('tmp','mod'); const modTmp = path.join('tmp','mod');
// fs.copySync("mod", modTmp, { dereference: true }); 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({ server({
electron: true,
dryrun: true, dryrun: true,
single: true single: true
}); });

View file

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

View file

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