@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
48 lines (45 loc) • 1.33 kB
JavaScript
import { access } from 'node:fs/promises';
/**
* Returns whether a path exists on disk. Used to decide whether an
* auto-generated sibling file (e.g. `client.ts`, `page.ts`) already exists so
* the validate passes never overwrite developer-authored files.
*/
export async function fileExists(filePath) {
try {
await access(filePath);
return true;
} catch {
return false;
}
}
/**
* Cached prettier module reference. Resolved lazily on first use so the
* dependency stays optional — if the consumer doesn't have prettier installed,
* formatting is skipped and the unformatted content is returned.
*/
let prettierModulePromise;
async function loadPrettier() {
if (!prettierModulePromise) {
prettierModulePromise = import('prettier').catch(() => null);
}
return prettierModulePromise;
}
/**
* Formats `content` with the project's prettier configuration for `filePath`.
* Falls back to the original content when prettier is not installed or fails.
*/
export async function formatWithPrettier(content, filePath) {
const prettier = await loadPrettier();
if (!prettier) {
return content;
}
try {
const config = await prettier.resolveConfig(filePath);
return await prettier.format(content, {
...config,
filepath: filePath
});
} catch {
return content;
}
}