scroll/src/common/runtime.ts

218 lines
4.5 KiB
JavaScript
Raw Normal View History

2023-11-02 13:06:48 -04:00
import { getTermios } from './termios.ts';
// ----------------------------------------------------------------------------
// Runtime adapter interfaces
// ----------------------------------------------------------------------------
export enum RunTimeType {
Bun = 'bun',
Deno = 'deno',
Unknown = 'common',
}
/**
* The native functions for getting/setting terminal settings
*/
export interface IFFI {
/**
* Get the existing termios settings (for canonical mode)
*/
tcgetattr(fd: number, termiosPtr: unknown): number;
/**
* Update the termios settings
*/
tcsetattr(fd: number, act: number, termiosPtr: unknown): number;
/**
* Update the termios pointer with raw mode settings
*/
cfmakeraw(termiosPtr: unknown): void;
/**
* Convert a TypedArray to an opaque pointer for ffi calls
*/
// deno-lint-ignore no-explicit-any
getPointer(ta: any): unknown;
}
2023-11-16 13:00:02 -05:00
/**
* The size of terminal in rows and columns
*/
export interface ITerminalSize {
rows: number;
cols: number;
}
/**
* Runtime-specific terminal functionality
*/
export interface ITerminal {
/**
* The arguments passed to the program on launch
*/
argv: string[];
/**
* The generator function returning chunks of input from the stdin stream
*/
inputLoop(): AsyncGenerator<Uint8Array, void, unknown>;
/**
* Get the size of the terminal
*/
getTerminalSize(): Promise<ITerminalSize>;
/**
* Pipe a string to stdout
*/
writeStdout(s: string): Promise<void>;
}
/**
* Runtime-specific file handling
*/
export interface IFIO {
openFile(path: string): Promise<string>;
openFileSync(path: string): string;
appendFile(path: string, contents: string): Promise<void>;
2023-11-16 13:00:02 -05:00
appendFileSync(path: string, contents: string): void;
}
/**
* The common interface for runtime adapters
*/
export interface IRuntime {
/**
* The name of the runtime
*/
name: RunTimeType;
/**
* Runtime-specific FFI
*/
ffi: IFFI;
/**
* Runtime-specific terminal functionality
*/
term: ITerminal;
/**
* Runtime-specific file system io
*/
file: IFIO;
/**
* Set up an event handler
*
* @param eventName - The event to listen for
* @param handler - The event handler
*/
2023-11-16 13:00:02 -05:00
onEvent: (
eventName: string,
handler: (e: Event | ErrorEvent) => void,
) => void;
/**
* Set a beforeExit/beforeUnload event handler for the runtime
* @param cb - The event handler
*/
onExit(cb: () => void): void;
/**
* Stop execution
*
* @param code
*/
exit(code?: number): void;
}
// ----------------------------------------------------------------------------
// Misc runtime functions
// ----------------------------------------------------------------------------
2023-11-02 13:06:48 -04:00
let scrollRuntime: IRuntime | null = null;
2023-11-01 15:05:31 -04:00
/**
* Kill program, displaying an error message
* @param s
*/
2023-11-16 13:00:02 -05:00
export function die(s: string | Error): void {
getTermios().then((t) => {
t.disableRawMode();
console.error(s);
getRuntime().then((r) => r.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
*/
export function getRuntimeType(): RunTimeType {
let runtime = RunTimeType.Unknown;
2023-11-01 15:05:31 -04:00
if ('Deno' in globalThis) {
runtime = RunTimeType.Deno;
2023-11-01 15:05:31 -04:00
}
if ('Bun' in globalThis) {
runtime = RunTimeType.Bun;
2023-11-01 15:05:31 -04:00
}
return runtime;
}
2023-11-16 13:00:02 -05:00
/**
* Get the adapter object for the current Runtime
*/
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) => {
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;
};