libs directory, default main function
This commit is contained in:
1 parent
a08c41a426
commit
80454f9a2a
3 files changed
+50
-12
No files matched your search
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
/*
|
||||
* ESM loader hook registered from 'main.js' via `module.register()`.
|
||||
* It resolves '@lib/<name>' specifiers against the project's '.gral/lib'
|
||||
* directory, supplied through the `data` option of `register()`.
|
||||
*/
|
||||
|
||||
import * as path from 'path';
|
||||
import { pathToFileURL } from 'url';
|
||||
|
||||
const LIB_PREFIX = '@lib/';
|
||||
|
||||
let libDir = null;
|
||||
|
||||
export function initialize(data) {
|
||||
libDir = data && data.libDir ? data.libDir : null;
|
||||
}
|
||||
|
||||
export async function resolve(specifier, context, nextResolve) {
|
||||
if (libDir && specifier.startsWith(LIB_PREFIX)) {
|
||||
const target = path.join(libDir, specifier.slice(LIB_PREFIX.length));
|
||||
return { url: pathToFileURL(target).href, shortCircuit: true };
|
||||
}
|
||||
return nextResolve(specifier, context);
|
||||
}
|
||||
Regular → Executable
+21
-11
@@ -19,12 +19,14 @@
|
||||
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { register } from 'node:module';
|
||||
import Horde from './horde.js';
|
||||
import run from './run.js';
|
||||
|
||||
const CONFIG_DIR = '.gral';
|
||||
const CONFIG_FILE = 'config.json';
|
||||
const FUNCTIONS_DIR = 'functions';
|
||||
const LIB_DIR = 'lib';
|
||||
|
||||
const DEFAULT_CONFIG = {
|
||||
host: 'wss://hub.hordelab.com',
|
||||
@@ -211,12 +213,12 @@ async function main(argv) {
|
||||
let config = loadConfig(root);
|
||||
Gral.path = root;
|
||||
|
||||
// Make '.gral/lib/*' importable from function modules via '@lib/<name>'.
|
||||
const libDir = path.join(root, CONFIG_DIR, LIB_DIR);
|
||||
await register('./loader.js', import.meta.url, { data: { libDir } });
|
||||
|
||||
const functions = await loadFunctions(root);
|
||||
|
||||
let horde = new Horde({host: config.host, token: config.token});
|
||||
await horde.connect();
|
||||
|
||||
Gral.horde = horde;
|
||||
Gral.horde = new Horde({host: config.host, token: config.token});
|
||||
|
||||
if (command.help) {
|
||||
console.log(HELP);
|
||||
@@ -234,15 +236,20 @@ async function main(argv) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!command.functionName) {
|
||||
console.error('Error: a function name is required.');
|
||||
console.error(HELP);
|
||||
return 1;
|
||||
let fname = command.functionName;
|
||||
if (!fname) {
|
||||
if (functions.has('main')) {
|
||||
fname = 'main';
|
||||
} else {
|
||||
console.error('Error: a function name is required.');
|
||||
console.error(HELP);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
const fn = functions.get(command.functionName);
|
||||
const fn = functions.get(fname);
|
||||
if (!fn) {
|
||||
console.error(`Error: unknown function '${command.functionName}'.`);
|
||||
console.error(`Error: unknown function '${fname}'.`);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -253,6 +260,9 @@ async function main(argv) {
|
||||
|
||||
const input = { prompt: command.prompt };
|
||||
|
||||
// Connect before executing
|
||||
await Gral.horde.connect();
|
||||
|
||||
try {
|
||||
await fn(input);
|
||||
} catch (err) {
|
||||
|
||||
Reference in new issue
Block a user