export function read_file_bytes(path) {
try {
return Deno.readFileSync(path);
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return undefined;
} else {
throw err;
}
}
}
export function canonicalize_path(path) {
try {
return Deno.realPathSync(path);
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return undefined;
} else {
throw err;
}
}
}
export function create_dir_all(path) {
Deno.mkdirSync(path, { recursive: true });
}
export function atomic_write_file(path, bytes) {
function parentPath(path) {
const lastSlashIndex = path.lastIndexOf("/") ?? path.lastIndexOf("\\");
return path.slice(0, lastSlashIndex);
}
const cachePerm = 0o644;
const tempName = path + "." + randomHex();
try {
Deno.writeFileSync(tempName, bytes, { mode: cachePerm });
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
try {
Deno.mkdirSync(parentPath(path), { recursive: true });
} catch {
}
Deno.writeFileSync(tempName, bytes, { mode: cachePerm });
} else {
throw err;
}
}
try {
Deno.renameSync(tempName, path);
} catch (err) {
try {
Deno.removeSync(tempName);
} catch {
}
throw err;
}
function randomHex() {
const arr = new Uint8Array(2);
crypto.getRandomValues(arr);
return Array.from(arr, (dec) => dec.toString(16).padStart(2, "0")).join("");
}
}
export function modified_time(path) {
try {
const stat = Deno.lstatSync(path);
return msToS(stat.mtime.getTime());
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return undefined;
} else {
throw err;
}
}
}
export function is_file(path) {
try {
const stat = Deno.lstatSync(path);
return stat.isFile;
} catch {
return false;
}
}
export function time_now() {
return msToS(Date.now());
}
function msToS(ms) {
return Math.round(ms / 1000);
}