chore: add prettier formatting scripts

This commit is contained in:
shumadrid 2025-03-18 16:49:51 +01:00
parent c96e65f94e
commit 633a8f5f82
3 changed files with 35 additions and 0 deletions

7
scripts/format-check.ts Normal file
View file

@ -0,0 +1,7 @@
/* eslint-disable unicorn/filename-case */
import { formatWithPrettier } from './formatWithPrettier.ts';
export async function invoke(): Promise<void> {
await formatWithPrettier(false);
}

5
scripts/format.ts Normal file
View file

@ -0,0 +1,5 @@
import { formatWithPrettier } from './formatWithPrettier.ts';
export async function invoke(): Promise<void> {
await formatWithPrettier(true);
}

View file

@ -0,0 +1,23 @@
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]);
}