update deno commons mod

This commit is contained in:
2026-02-10 23:41:30 +08:00
parent 2c92cbc682
commit dc856711b2

View File

@@ -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(
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<string, string> = {
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,13 +688,23 @@ export function joinPath(path1: string, ...paths: string[]): string {
}
export async function existsPath(path: string): Promise<boolean> {
if (isDeno()) {
try {
const stat = await Deno.stat(path);
return stat != null;
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(
filename: string,