wxt
Version:
⚡ Next-gen Web Extension Framework
37 lines (36 loc) • 1.15 kB
JavaScript
import { unnormalizePath } from "./paths.mjs";
import { wxt } from "../wxt.mjs";
import { access, readFile, writeFile } from "node:fs/promises";
import { glob } from "tinyglobby";
//#region src/core/utils/fs.ts
async function pathExists(path) {
try {
await access(path);
return true;
} catch {
return false;
}
}
async function readJson(path) {
return JSON.parse(await readFile(path, "utf-8"));
}
/**
* Only write the contents to a file if it results in a change. This prevents
* unnecessary file watchers from being triggered, like WXT's dev server or the
* TS language server in editors.
*
* @param file The file to write to.
* @param newContents The new text content to write.
*/
async function writeFileIfDifferent(file, newContents) {
if (await readFile(file, "utf-8").catch(() => void 0) !== newContents) await writeFile(file, newContents);
}
async function getPublicFiles() {
if (!await pathExists(wxt.config.publicDir)) return [];
return (await glob("**/*", {
cwd: wxt.config.publicDir,
expandDirectories: false
})).map(unnormalizePath);
}
//#endregion
export { getPublicFiles, pathExists, readJson, writeFileIfDifferent };