shumadrid_obsidian-git-chan.../scripts/formatWithPrettier.ts
2025-03-18 16:49:51 +01:00

23 lines
860 B
TypeScript

import { getDirname } from 'obsidian-dev-utils/Path';
import { existsSync } from 'obsidian-dev-utils/ScriptUtils/NodeModules';
import {
execFromRoot,
getRootDir,
resolvePathFromRootSafe
} from 'obsidian-dev-utils/ScriptUtils/Root';
export async function formatWithPrettier(rewrite: boolean): Promise<void> {
// Modified from https://github.com/mnaoumov/obsidian-dev-utils/blob/main/src/ScriptUtils/format.ts
const prettierJsonPath = resolvePathFromRootSafe('.prettierrc.json');
if (!existsSync(prettierJsonPath)) {
const packageDirectory = getRootDir(getDirname(import.meta.url));
if (!packageDirectory) {
throw new Error('Could not find package directory.');
}
throw new Error('.prettierrc.json not found');
}
const command = rewrite ? '--write' : '--check';
await execFromRoot(['npx', 'prettier', '.', command]);
}