update deno commons mod
This commit is contained in:
@@ -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<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,12 +688,22 @@ export function joinPath(path1: string, ...paths: string[]): string {
|
||||
}
|
||||
|
||||
export async function existsPath(path: string): Promise<boolean> {
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user