-
-
Notifications
You must be signed in to change notification settings - Fork 655
Expand file tree
/
Copy pathsession-loaders.mjs
More file actions
44 lines (39 loc) · 1.56 KB
/
session-loaders.mjs
File metadata and controls
44 lines (39 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Session module loaders — bundle-first with build/ fallback.
*
* All session modules are loaded from esbuild bundles (hooks/session-*.bundle.mjs).
* Bundles are built by CI (bundle.yml) and shipped with every release.
* Fallback: if bundles are missing (marketplace installs), try build/session/*.js.
*/
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import { existsSync } from "node:fs";
export function createSessionLoaders(hookDir) {
// Auto-detect bundle directory: bundles live in hooks/ root, not platform subdirs.
// If hookDir itself has bundles, use it; otherwise go up one level.
const bundleDir = existsSync(join(hookDir, "session-db.bundle.mjs"))
? hookDir
: join(hookDir, "..");
// Fallback: if bundles missing, try build/session/*.js (marketplace installs)
const pluginRoot = join(bundleDir, "..");
const buildSession = join(pluginRoot, "build", "session");
async function loadModule(bundleName, buildName) {
const bundlePath = join(bundleDir, bundleName);
if (existsSync(bundlePath)) {
return await import(pathToFileURL(bundlePath).href);
}
const buildPath = join(buildSession, buildName);
return await import(pathToFileURL(buildPath).href);
}
return {
async loadSessionDB() {
return await loadModule("session-db.bundle.mjs", "db.js");
},
async loadExtract() {
return await loadModule("session-extract.bundle.mjs", "extract.js");
},
async loadSnapshot() {
return await loadModule("session-snapshot.bundle.mjs", "snapshot.js");
},
};
}