update deno common smod

This commit is contained in:
2026-02-11 01:50:54 +08:00
parent 6ee6f0e2e6
commit b4ec83b4c3

View File

@@ -4,6 +4,7 @@
import {decodeBase64, encodeBase64} from "jsr:@std/encoding/base64"; import {decodeBase64, encodeBase64} from "jsr:@std/encoding/base64";
import {dirname, fromFileUrl} from "https://deno.land/std/path/mod.ts"; import {dirname, fromFileUrl} from "https://deno.land/std/path/mod.ts";
import {spawn, SpawnOptionsWithoutStdio} from "node:child_process"; import {spawn, SpawnOptionsWithoutStdio} from "node:child_process";
import { readFile,readFileSync } from 'node:fs';
// reference: https://docs.deno.com/examples/hex_base64_encoding/ // reference: https://docs.deno.com/examples/hex_base64_encoding/
// import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64"; // import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64";
@@ -773,25 +774,39 @@ export async function existsPath(path: string): Promise<boolean> {
export async function readFileToString( export async function readFileToString(
filename: string, filename: string,
): Promise<string | null> { ): Promise<string | null> {
try { if (isDeno()) {
return await Deno.readTextFile(resolveFilename(filename)); try {
} catch (e) { return await Deno.readTextFile(resolveFilename(filename));
if (e instanceof Error && e.name == "NotFound") { } catch (e) {
return null; if (e instanceof Error && e.name == "NotFound") {
return null;
}
throw e;
} }
throw e;
} }
return Promise((resolve, reject) => {
readFile(resolveFilename(filename), 'utf8', (err, buffer) => {
if (err) {
reject(err);
} else {
resolve(buffer);
}
});
});
} }
export function readFileToStringSync(filename: string): string | null { export function readFileToStringSync(filename: string): string | null {
try { if (isDeno()) {
return Deno.readTextFileSync(resolveFilename(filename)); try {
} catch (e) { return Deno.readTextFileSync(resolveFilename(filename));
if (e instanceof Error && e.name == "NotFound") { } catch (e) {
return null; if (e instanceof Error && e.name == "NotFound") {
return null;
}
throw e;
} }
throw e;
} }
return readFileSync(resolveFilename(filename), 'utf8');
} }
export async function writeStringToFile( export async function writeStringToFile(