export enum RunTime { Bun = 'bun', Deno = 'deno', Unknown = 'common', } /** * Determine which Typescript runtime we are operating under */ export const getRuntime = (): RunTime => { let runtime = RunTime.Unknown; if ('Deno' in globalThis) { runtime = RunTime.Deno; } if ('Bun' in globalThis) { runtime = RunTime.Bun; } return runtime; }; /** * Import a runtime-specific module * * eg. to load "src/bun/index.ts", if the runtime is bun, * you can use like so `await importForRuntime('index')`; * * @param path - the path within the runtime module */ export const importForRuntime = async (path: string) => { const runtime = getRuntime(); const suffix = '.ts'; const base = `../${runtime}/`; const pathParts = path.split('/') .filter((part) => part !== '' && part !== '.' && part !== suffix) .map((part) => part.replace(suffix, '')); const cleanedPath = pathParts.join('/'); const importPath = base + cleanedPath + suffix; return await import(importPath); }