2023-11-01 15:05:31 -04:00
|
|
|
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();
|
2023-11-01 15:25:52 -04:00
|
|
|
const suffix = '.ts';
|
2023-11-01 15:05:31 -04:00
|
|
|
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);
|
2023-11-01 15:27:31 -04:00
|
|
|
};
|