42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { readdir, readFile, writeFile, stat } from "node:fs/promises";
|
|
import { join, extname } from "node:path";
|
|
import {
|
|
formatSource,
|
|
type FormatLanguage,
|
|
type FormatOptions,
|
|
} from "./format-source.ts";
|
|
|
|
export async function format(
|
|
root: string,
|
|
options: FormatOptions & { check?: boolean } = {},
|
|
): Promise<string[]> {
|
|
const changed: string[] = [];
|
|
if ((await stat(root)).isFile()) {
|
|
const language = extname(root).slice(1) as FormatLanguage;
|
|
if (!["spug", "stpug", "stug", "stcss"].includes(language)) return changed;
|
|
const source = await readFile(root, "utf8");
|
|
const next = await formatSource(source, language, options);
|
|
if (next !== source) {
|
|
changed.push(root);
|
|
if (!options.check) await writeFile(root, next, "utf8");
|
|
}
|
|
return changed;
|
|
}
|
|
for (const entry of await readdir(root, { withFileTypes: true })) {
|
|
if (entry.isSymbolicLink()) continue;
|
|
if (
|
|
entry.name.startsWith(".") ||
|
|
[
|
|
"node_modules",
|
|
"dist",
|
|
"dist.previous",
|
|
"shadcn-svelte",
|
|
"_muddle",
|
|
].includes(entry.name)
|
|
)
|
|
continue;
|
|
const path = join(root, entry.name);
|
|
changed.push(...(await format(path, options)));
|
|
}
|
|
return changed;
|
|
}
|