import { getTermios } from './termios.ts'; export enum RunTime { Bun = 'bun', Deno = 'deno', Unknown = 'common', } export function die(s: string): void { getTermios().then((t) => t.disableRawMode()); console.error(s); } /** * 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/mod.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); }; /** * Import the default export for a runtime-specific module * (this is just a simple wrapper of `importForRuntime`) * * @param path - the path within the runtime module */ export const importDefaultForRuntime = async (path: string) => { const pkg = await importForRuntime(path); if ('default' in pkg) { return pkg.default; } return null; };