From dc856711b2f9c671a3d512be74d81bac6a8200be Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Tue, 10 Feb 2026 23:41:30 +0800 Subject: [PATCH] update deno commons mod --- libraries/deno-commons-mod.ts | 75 ++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 23 deletions(-) diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index c121997..90dfa06 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -8,6 +8,18 @@ import {dirname, fromFileUrl} from "https://deno.land/std/path/mod.ts"; // import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64"; // import { decodeHex, encodeHex } from "jsr:@std/encoding/hex"; +export function isDeno(): boolean { + return typeof Deno !== "undefined"; +} + +export function args(): string[] { + return isDeno() ? Deno.args : process.argv.slice(2); +} + +export function osEnv(key: string): string | undefined { + return isDeno() ? Deno.env.get(key) : process.env[key]; +} + export class ProcessOutput { code: number; stdout: string; @@ -158,7 +170,7 @@ export function getEnv(envKey: string): string | null { return envValue.trim(); } } - return Deno.env.get(envKey) || null; + return osEnv(envKey) || null; } export function isEnvOn(envKey: string): boolean { @@ -259,11 +271,14 @@ export async function clearLastLine() { } export async function printLastLine(line: string) { - await Deno.stdout.write( - new TextEncoder().encode( - `\x1b[1000D${line}\x1b[K`, - ), + const encodedLineBytes = new TextEncoder().encode( + `\x1b[1000D${line}\x1b[K`, ); + if (isDeno()) { + await Deno.stdout.write(encodedLineBytes); + } else { + process.stdout.write(encodedLineBytes); + } } interface ColorToken { @@ -397,7 +412,11 @@ function parseColorTokens(message: string, renderColor: boolean): ColorToken[] { const COLOR_MAP: Record = { blink: "5", bold: "1", + b: "1", under: "4", + u: "4", + strikeout: "9", + s: "9", black: "30", red: "31", @@ -629,19 +648,19 @@ export function getHomeDirOrDie(): string { } export function getHomeDir(): string | null { - if (Deno.build.os === "windows") { - const userProfile = Deno.env.get("USERPROFILE"); - if (userProfile) { - return userProfile; - } - const homeDrive = Deno.env.get("HOMEDRIVE"); - const homePath = Deno.env.get("HOMEPATH"); - if (homeDrive && homePath) { - return homeDrive + homePath; - } - return null; - } - return Deno.env.get("HOME") || null; + // if (Deno.build.os === "windows") { + // const userProfile = Deno.env.get("USERPROFILE"); + // if (userProfile) { + // return userProfile; + // } + // const homeDrive = Deno.env.get("HOMEDRIVE"); + // const homePath = Deno.env.get("HOMEPATH"); + // if (homeDrive && homePath) { + // return homeDrive + homePath; + // } + // return null; + // } + return osEnv("HOME") || null; } export function resolveFilename(filename: string): string { @@ -669,12 +688,22 @@ export function joinPath(path1: string, ...paths: string[]): string { } export async function existsPath(path: string): Promise { - try { - const stat = await Deno.stat(path); - return stat != null; - } catch { - return false; + if (isDeno()) { + try { + return await Deno.stat(path) != null; + } catch { + return false; + } } + return new Promise((resolve) => { + fs.stat(path, (err, stats) => { + if (err) { + resolve(false); + } else { + resolve(stats != null); + } + }); + }); } export async function readFileToString(