2023-11-02 13:06:48 -04:00
|
|
|
import { getTermios } from './termios.ts';
|
2023-11-10 18:22:09 -05:00
|
|
|
import { IRuntime, RunTimeType } from './types.ts';
|
2023-11-02 13:06:48 -04:00
|
|
|
|
2023-11-10 18:22:09 -05:00
|
|
|
let scrollRuntime: IRuntime | null = null;
|
2023-11-01 15:05:31 -04:00
|
|
|
|
2023-11-10 18:22:09 -05:00
|
|
|
/**
|
|
|
|
* Kill program, displaying an error message
|
|
|
|
* @param s
|
|
|
|
*/
|
|
|
|
export async function die(s: string | Error): Promise<void> {
|
|
|
|
(await getTermios()).disableRawMode();
|
2023-11-02 13:06:48 -04:00
|
|
|
console.error(s);
|
2023-11-10 18:22:09 -05:00
|
|
|
(await getRuntime()).exit();
|
2023-11-02 13:06:48 -04:00
|
|
|
}
|
|
|
|
|
2023-11-01 15:05:31 -04:00
|
|
|
/**
|
|
|
|
* Determine which Typescript runtime we are operating under
|
|
|
|
*/
|
2023-11-10 18:22:09 -05:00
|
|
|
export function getRuntimeType(): RunTimeType {
|
|
|
|
let runtime = RunTimeType.Unknown;
|
2023-11-01 15:05:31 -04:00
|
|
|
|
|
|
|
if ('Deno' in globalThis) {
|
2023-11-10 18:22:09 -05:00
|
|
|
runtime = RunTimeType.Deno;
|
2023-11-01 15:05:31 -04:00
|
|
|
}
|
|
|
|
if ('Bun' in globalThis) {
|
2023-11-10 18:22:09 -05:00
|
|
|
runtime = RunTimeType.Bun;
|
2023-11-01 15:05:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return runtime;
|
2023-11-10 18:22:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getRuntime(): Promise<IRuntime> {
|
|
|
|
if (scrollRuntime === null) {
|
|
|
|
const runtime = getRuntimeType();
|
|
|
|
const path = `../${runtime}/mod.ts`;
|
|
|
|
|
|
|
|
const pkg = await import(path);
|
|
|
|
if ('default' in pkg) {
|
|
|
|
scrollRuntime = pkg.default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(scrollRuntime!);
|
|
|
|
}
|
2023-11-01 15:05:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Import a runtime-specific module
|
|
|
|
*
|
2023-11-03 11:59:58 -04:00
|
|
|
* eg. to load "src/bun/mod.ts", if the runtime is bun,
|
2023-11-01 15:05:31 -04:00
|
|
|
* you can use like so `await importForRuntime('index')`;
|
|
|
|
*
|
|
|
|
* @param path - the path within the runtime module
|
|
|
|
*/
|
|
|
|
export const importForRuntime = async (path: string) => {
|
2023-11-10 18:22:09 -05:00
|
|
|
const runtime = getRuntimeType();
|
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
|
|
|
};
|
2023-11-03 11:59:58 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|