From 8c54ceb1047d19672c8b26b5b4b5a92a0a03e943 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 12 Jul 2024 15:33:03 -0400 Subject: [PATCH] Add shell scripts to run without just, fix the issue with bun and tsx failing to exit --- README.md | 5 ++++- bin/bun.sh | 5 +++++ bin/deno.sh | 5 +++++ bin/tsx.sh | 6 ++++++ package.json | 10 ++++++---- src/common/editor.ts | 12 ------------ src/common/main.ts | 7 ++++++- src/common/runtime/helpers.ts | 27 ++++++++++++++++++--------- src/common/runtime/mod.ts | 3 +++ src/common/runtime/node.ts | 8 +++++--- src/common/runtime/runtime.ts | 2 +- src/common/runtime/terminal_io.ts | 3 ++- tsconfig.json | 5 +++-- 13 files changed, 64 insertions(+), 34 deletions(-) create mode 100755 bin/bun.sh create mode 100755 bin/deno.sh create mode 100755 bin/tsx.sh diff --git a/README.md b/README.md index c7b4e8f..f6f7e37 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,10 @@ To simplify running, I'm using [Just](https://github.com/casey/just). - Bun: `just bun-run [filename]` - Deno: `just deno-run [filename]` -- TSX: `just tsx-run [filename` +- TSX: `just tsx-run [filename]` + +Alternatively, there are shell scripts for each runtime in the `bin` folder. +So you can run the editor by calling `./bin/deno.sh [filename]` without installing Just. Deno is generally used for dev tools, but each runtime should be functionally equivalent running the text editor. diff --git a/bin/bun.sh b/bin/bun.sh new file mode 100755 index 0000000..83e27a0 --- /dev/null +++ b/bin/bun.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -euo pipefail +PARENT_DIR="$(dirname "$(realpath "$0")")" +SCROLL="$(realpath "${PARENT_DIR}/../src/scroll.ts")" +bun run "${SCROLL}" "$@" diff --git a/bin/deno.sh b/bin/deno.sh new file mode 100755 index 0000000..7d8927e --- /dev/null +++ b/bin/deno.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -euo pipefail +PARENT_DIR="$(dirname "$(realpath "$0")")" +SCROLL="$(realpath "${PARENT_DIR}/../src/scroll.ts")" +deno run --allow-all --deny-hrtime "${SCROLL}" "$@" \ No newline at end of file diff --git a/bin/tsx.sh b/bin/tsx.sh new file mode 100755 index 0000000..6679bba --- /dev/null +++ b/bin/tsx.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euo pipefail +PARENT_DIR="$(dirname "$(realpath "$0")")" +TSX="$(realpath "${PARENT_DIR}/../node_modules/.bin/tsx")" +SCROLL="$(realpath "${PARENT_DIR}/../src/scroll.ts")" +"${TSX}" "${SCROLL}" "$@"; diff --git a/package.json b/package.json index 94eb310..595de32 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,10 @@ { "dependencies": {}, "devDependencies": { - "bun-types": "^1.0.11", - "typescript": "^5.5" + "@types/node": "*", + "bun-types": "*", + "typescript": "^5.5", + "tsx": "*" }, "scripts": { "bun-check": "bunx tsc", @@ -15,8 +17,8 @@ "deno-run": "deno run --allow-all --deny-hrtime ./src/scroll.ts", "deno-test": "deno test --allow-all", "tsx-check": "npx tsc", - "tsx-run": "npx tsx ./src/scroll.ts", - "tsx-test": "npx tsx --test './src/common/all_test.ts'" + "tsx-run": "tsx ./src/scroll.ts", + "tsx-test": "tsx --test './src/common/all_test.ts'" }, "type": "module" } diff --git a/src/common/editor.ts b/src/common/editor.ts index 29bba4d..f8deef0 100644 --- a/src/common/editor.ts +++ b/src/common/editor.ts @@ -419,12 +419,6 @@ export default class Editor { ? this.document.row(this.cursor.y).unwrap().cxToRx(this.cursor.x) : 0; - logDebug('Editor.scroll - start', { - cursor: this.cursor, - renderX: this.renderX, - offset: this.offset, - }); - const { y } = this.cursor; const offset = this.offset; const width = this.screen.cols; @@ -441,12 +435,6 @@ export default class Editor { } else if (this.renderX >= offset.x + width) { offset.x = this.renderX - width + 1; } - - logDebug('Editor.scroll - end', { - cursor: this.cursor, - renderX: this.renderX, - offset: this.offset, - }); } // -------------------------------------------------------------------------- diff --git a/src/common/main.ts b/src/common/main.ts index 147c6db..41ba834 100644 --- a/src/common/main.ts +++ b/src/common/main.ts @@ -1,5 +1,10 @@ import { readKey } from './fns.ts'; -import { getRuntime, logError, logWarning, process } from './runtime/mod.ts'; +import { + getRuntime, + logError, + logWarning, + node_process as process, +} from './runtime/mod.ts'; import Editor from './editor.ts'; export async function main() { diff --git a/src/common/runtime/helpers.ts b/src/common/runtime/helpers.ts index 6e12845..8e559ff 100644 --- a/src/common/runtime/helpers.ts +++ b/src/common/runtime/helpers.ts @@ -1,8 +1,8 @@ /** * Functions/Methods that depend on the current runtime to function */ -import { logError } from './log.ts'; -import { process } from './node.ts'; +import { logError, logWarning } from './log.ts'; +import { node_path as path, node_process as process } from './node.ts'; import { RunTimeType } from './runtime.ts'; import { IRuntime, ITestBase } from '../types.ts'; @@ -23,15 +23,24 @@ export function die(s: string | Error): void { * Determine which Typescript runtime we are operating under */ export function runtimeType(): RunTimeType { - let runtime = RunTimeType.Tsx; + const cmd = path.basename(process.argv[0]); + switch (cmd) { + case 'deno': + return RunTimeType.Deno; - if ('Deno' in globalThis) { - runtime = RunTimeType.Deno; - } else if ('Bun' in globalThis) { - runtime = RunTimeType.Bun; + case 'bun': + return RunTimeType.Bun; + + case 'node': + return RunTimeType.Tsx; + + default: + logWarning('Fallback runtime detection', { cmd }); + if ('bun' in globalThis) { + return RunTimeType.Bun; + } + return RunTimeType.Tsx; } - - return runtime; } /** diff --git a/src/common/runtime/mod.ts b/src/common/runtime/mod.ts index 55e89bf..7a80efe 100644 --- a/src/common/runtime/mod.ts +++ b/src/common/runtime/mod.ts @@ -5,3 +5,6 @@ export * from './node.ts'; export * from './runtime.ts'; export * from './terminal_io.ts'; export * from './test_base.ts'; + +import { CommonRuntime } from './runtime.ts'; +export default CommonRuntime; diff --git a/src/common/runtime/node.ts b/src/common/runtime/node.ts index 0d10331..dbf8a0e 100644 --- a/src/common/runtime/node.ts +++ b/src/common/runtime/node.ts @@ -1,7 +1,9 @@ /** * Re-export of node apis shared by runtimes */ -import * as assert from 'node:assert'; -import * as process from 'node:process'; +import node_assert from 'node:assert'; +import node_path from 'node:path'; +import node_process from 'node:process'; +import node_tty from 'node:tty'; -export { assert, process }; +export { node_assert, node_path, node_process, node_tty }; diff --git a/src/common/runtime/runtime.ts b/src/common/runtime/runtime.ts index 060e714..5adc69d 100644 --- a/src/common/runtime/runtime.ts +++ b/src/common/runtime/runtime.ts @@ -1,4 +1,4 @@ -import { process } from './node.ts'; +import { node_process as process } from './node.ts'; import CommonFileIO from './file_io.ts'; import CommonTerminalIO from './terminal_io.ts'; diff --git a/src/common/runtime/terminal_io.ts b/src/common/runtime/terminal_io.ts index 3804cfe..9804e61 100644 --- a/src/common/runtime/terminal_io.ts +++ b/src/common/runtime/terminal_io.ts @@ -1,4 +1,4 @@ -import { process } from './node.ts'; +import { node_process as process } from './node.ts'; import { readKey } from '../fns.ts'; import { ITerminal, ITerminalSize } from '../types.ts'; @@ -26,6 +26,7 @@ export const CommonTerminalIO: ITerminal = { process.stdin.resume().once( 'data', (buffer: Uint8Array) => { + process.stdin.pause(); resolve(buffer); }, ); diff --git a/tsconfig.json b/tsconfig.json index 392800a..587bec5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,8 +13,9 @@ "skipLibCheck": true, "composite": true, "downlevelIteration": true, - "esModuleInterop": false, - "allowSyntheticDefaultImports": false + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "isolatedModules": true }, "exclude": ["src/deno"] }