feat: make CLI node compatible (#4347)

This commit is contained in:
Ruben Fiszel
2024-09-07 02:31:26 +02:00
committed by GitHub
parent d11284feb2
commit fd18f42e0f
19 changed files with 1466 additions and 247 deletions

View File

@@ -8,7 +8,7 @@ import {
Command,
setClient,
} from "./deps.ts";
import { DelimiterStream, Input, colors, log } from "./deps.ts";
import { Input, colors, log } from "./deps.ts";
import { loginInteractive } from "./login.ts";
import { getRootStore } from "./store.ts";
import { push, pull } from "./sync.ts";
@@ -43,46 +43,20 @@ export interface Instance {
prefix: string;
}
function makeInstanceStream(
readable: ReadableStream<Uint8Array>
): ReadableStream<Instance> {
return readable
.pipeThrough(new DelimiterStream(new TextEncoder().encode("\n")))
.pipeThrough(new TextDecoderStream())
.pipeThrough(
new TransformStream({
transform(line, controller) {
try {
if (line.length <= 2) {
return;
}
const instance = JSON.parse(line) as Instance;
controller.enqueue(instance);
} catch {
/* ignore */
}
},
})
);
}
async function getInstanceStream() {
const file = await Deno.open((await getRootStore()) + "instances.ndjson", {
write: false,
read: true,
});
return makeInstanceStream(file.readable);
}
export async function allInstances(): Promise<Instance[]> {
try {
const instanceStream = await getInstanceStream();
const instances: Instance[] = [];
for await (const instance of instanceStream) {
instances.push(instance);
}
return instances;
const file = (await getRootStore()) + "instances.ndjson";
const txt = await Deno.readTextFile(file);
return txt
.split("\n")
.map((line) => {
if (line.length <= 2) {
return;
}
const instance = JSON.parse(line) as Instance;
return instance;
})
.filter(Boolean) as Instance[];
} catch (_) {
return [];
}