shumadrid_obsidian-git-chan.../scripts/formatWithPrettier.ts
2025-04-08 03:39:44 +02:00

23 lines
872 B
TypeScript

import { getFolderName } from 'obsidian-dev-utils/Path';
import { existsSync } from 'obsidian-dev-utils/ScriptUtils/NodeModules';
import {
execFromRoot,
getRootFolder,
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 = getRootFolder(getFolderName(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]);
}