telefunc
Version:
Remote functions. Instead of API.
38 lines (37 loc) • 2.05 kB
JavaScript
export { loadTelefuncFilesFromConfig };
import { assert, assertPosixPath, assertUsage, isTelefuncFilePath } from '../../utils.js';
import { import_ } from '@brillout/import';
import pc from '@brillout/picocolors';
import { getServerConfig } from '../serverConfig.js';
async function loadTelefuncFilesFromConfig(runContext) {
const { root, telefuncFiles } = getServerConfig();
assertUsage(root, `You need to set ${pc.cyan('config.root')} to be able to use ${pc.cyan('config.telefuncFiles')}, see https://telefunc.com/root`);
assert(telefuncFiles);
assertPosixPath(root);
const telefuncFilesLoaded = {};
const telefuncFilesAll = [];
await Promise.all(telefuncFiles.map(async (telefuncFilePathAbsolute) => {
const telefuncFilePath = resolveTelefuncFilePath(telefuncFilePathAbsolute, root);
telefuncFilesAll.push(telefuncFilePath);
assert(isTelefuncFilePath(runContext.telefuncFilePath));
assert(isTelefuncFilePath(telefuncFilePath));
if (telefuncFilePath !== runContext.telefuncFilePath) {
return;
}
const telefunctions = await import_(telefuncFilePathAbsolute);
telefuncFilesLoaded[telefuncFilePath] = telefunctions;
}));
return { telefuncFilesLoaded, telefuncFilesAll };
}
function resolveTelefuncFilePath(telefuncFilePathAbsolute, appRootDir) {
assertPosixPath(telefuncFilePathAbsolute);
assertUsage(telefuncFilePathAbsolute.startsWith(appRootDir), `The telefunc file ${telefuncFilePathAbsolute} doesn't live inside the root directory ${appRootDir} of your project. Either move the telefunc file inside the root directory, or change ${pc.cyan('config.root')} (https://telefunc.com/root).`);
let path = telefuncFilePathAbsolute.slice(appRootDir.length);
if (path.startsWith('/'))
path = path.slice(1);
assert(!path.startsWith('/') && !path.startsWith('.'));
assertPosixPath(path);
const telefuncFilePath = '/' + path;
assert(isTelefuncFilePath(telefuncFilePathAbsolute));
return telefuncFilePath;
}