Skip to content

Commit

Permalink
Fix cli for node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Jun 15, 2024
1 parent 611e371 commit 2ede366
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.16.2

- Fix for Node.js; use `readline` instead of prompt.
-

## 0.16.1

- Fix for Node.js not treating subarrays of buffers as expected
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/kv",
"version": "0.16.1",
"version": "0.16.2",
"exports": {
".": "./mod.ts",
"./cli": "./src/cli/mod.ts"
Expand Down
3 changes: 0 additions & 3 deletions src/cli/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ export function hasParameter(p: string[], n: number): boolean {
return true;
}
}
export function userInput(t: string): string {
return prompt(t) || "";
}
export function toHexString(bytes: number): string {
// Ensure the input is a valid number
if (typeof bytes !== "number" || isNaN(bytes)) {
Expand Down
20 changes: 19 additions & 1 deletion src/cli/loop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Colors } from "@cross/utils";
import { CurrentRuntime, Runtime } from "@cross/runtime";
import { createInterface } from "node:readline";

import type { KVCliHandler, KVDBContainer } from "./common.ts";

Expand All @@ -16,7 +18,23 @@ export async function main() {
let exit = false;
while (!exit) {
// Collect user input
const command = await prompt(Colors.blue(">"));
let command;
if (CurrentRuntime === Runtime.Node) {
const rl = createInterface({
// @ts-ignore Cross-runtime
input: process.stdin,
// @ts-ignore Cross-runtime
output: process.stdout,
});
command = await new Promise((resolve) => {
rl.question(Colors.blue("> "), (cmd: unknown) => {
rl.close();
resolve(cmd);
});
});
} else {
command = await prompt(Colors.blue(">"));
}
if (command === null) {
continue;
}
Expand Down

0 comments on commit 2ede366

Please sign in to comment.