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 { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64";
// import { decodeHex, encodeHex } from "jsr:@std/encoding/hex"; // 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 { export class ProcessOutput {
code: number; code: number;
stdout: string; stdout: string;
@@ -158,7 +170,7 @@ export function getEnv(envKey: string): string | null {
return envValue.trim(); return envValue.trim();
} }
} }
return Deno.env.get(envKey) || null; return osEnv(envKey) || null;
} }
export function isEnvOn(envKey: string): boolean { export function isEnvOn(envKey: string): boolean {
@@ -259,11 +271,14 @@ export async function clearLastLine() {
} }
export async function printLastLine(line: string) { export async function printLastLine(line: string) {
await Deno.stdout.write( const encodedLineBytes = new TextEncoder().encode(
new TextEncoder().encode(
`\x1b[1000D${line}\x1b[K`, `\x1b[1000D${line}\x1b[K`,
),
); );
if (isDeno()) {
await Deno.stdout.write(encodedLineBytes);
} else {
process.stdout.write(encodedLineBytes);
}
} }
interface ColorToken { interface ColorToken {
@@ -397,7 +412,11 @@ function parseColorTokens(message: string, renderColor: boolean): ColorToken[] {
const COLOR_MAP: Record<string, string> = { const COLOR_MAP: Record<string, string> = {
blink: "5", blink: "5",
bold: "1", bold: "1",
b: "1",
under: "4", under: "4",
u: "4",
strikeout: "9",
s: "9",
black: "30", black: "30",
red: "31", red: "31",
@@ -629,19 +648,19 @@ export function getHomeDirOrDie(): string {
} }
export function getHomeDir(): string | null { export function getHomeDir(): string | null {
if (Deno.build.os === "windows") { // if (Deno.build.os === "windows") {
const userProfile = Deno.env.get("USERPROFILE"); // const userProfile = Deno.env.get("USERPROFILE");
if (userProfile) { // if (userProfile) {
return userProfile; // return userProfile;
} // }
const homeDrive = Deno.env.get("HOMEDRIVE"); // const homeDrive = Deno.env.get("HOMEDRIVE");
const homePath = Deno.env.get("HOMEPATH"); // const homePath = Deno.env.get("HOMEPATH");
if (homeDrive && homePath) { // if (homeDrive && homePath) {
return homeDrive + homePath; // return homeDrive + homePath;
} // }
return null; // return null;
} // }
return Deno.env.get("HOME") || null; return osEnv("HOME") || null;
} }
export function resolveFilename(filename: string): string { 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<boolean> { export async function existsPath(path: string): Promise<boolean> {
if (isDeno()) {
try { try {
const stat = await Deno.stat(path); return await Deno.stat(path) != null;
return stat != null;
} catch { } catch {
return false; return false;
} }
}
return new Promise((resolve) => {
fs.stat(path, (err, stats) => {
if (err) {
resolve(false);
} else {
resolve(stats != null);
}
});
});
} }
export async function readFileToString( export async function readFileToString(