This commit is contained in:
2026-02-11 23:02:11 +08:00
parent f40eb40d6b
commit 6a08be3f50
10 changed files with 108 additions and 82 deletions

View File

@@ -2,7 +2,8 @@
// - https://docs.deno.com/runtime/fundamentals/testing/
import {decodeBase64, encodeBase64} from "jsr:@std/encoding/base64";
import {dirname, fromFileUrl} from "https://deno.land/std/path/mod.ts";
import {dirname, fromFileUrl} from "jsr:@std/path";
import {toArrayBuffer} from "jsr:@std/streams";
import {spawn, SpawnOptionsWithoutStdio} from "node:child_process";
import {mkdir, readFile, readFileSync, rm, writeFile} from "node:fs";
@@ -22,6 +23,36 @@ export function osEnv(key: string): string | undefined {
return isDeno() ? Deno.env.get(key) : process.env[key];
}
export function stdin(): ReadableStream {
return isDeno() ? Deno.stdin.readable : process.stdin;
}
function streamToArrayBuffer(stream) {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on("error", (err) => reject(err));
stream.on("data", (chunk) => {
chunks.push(chunk);
});
stream.on("end", () => {
const buffer = Buffer.concat(chunks);
resolve(
buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength,
),
);
});
});
}
export function stdinToArrayBuffer(): Promise<ArrayBuffer> {
if (isDeno()) {
return toArrayBuffer(Deno.stdin.readable);
}
return streamToArrayBuffer(process.stdin);
}
export function exit(code?: number): never {
isDeno() ? Deno.exit(code) : process.exit(code);
}
@@ -222,7 +253,7 @@ export function parseIntVal(val: any, defaultVal: number): number {
}
export function getEnv(envKey: string): string | null {
const homeDir = homeDir();
const homeDir = getHomeDir();
if ((homeDir !== null) && envKey) {
const envValue = readFileToStringSync(
`${homeDir}/.config/envs/${envKey}`,
@@ -705,18 +736,18 @@ export function getHomeDirOrDie(): string {
}
export function homeDirOrDie(): string {
const homeDir = homeDir();
const homeDir = getHomeDir();
if (homeDir === null) {
throw new Error("Cannot find home dir");
}
return homeDir;
}
export function getHomeDir(): string | null {
return homeDir();
export function homeDir(): string | null {
return getHomeDir();
}
export function homeDir(): string | null {
export function getHomeDir(): string | null {
// if (Deno.build.os === "windows") {
// const userProfile = osEnv("USERPROFILE");
// if (userProfile) {
@@ -734,7 +765,7 @@ export function homeDir(): string | null {
export function resolveFilename(filename: string): string {
if (filename.startsWith("~/")) {
return homeDir() + filename.substring(1);
return getHomeDir() + filename.substring(1);
}
return filename;
}