fix-dts.mts 1022 B

123456789101112131415161718192021222324252627282930313233
  1. import { readdir, readFile, writeFile, lstat } from "node:fs/promises";
  2. import { join, resolve } from "node:path";
  3. /** Adds two spaces to the end of each line in JSDoc comments to preserve the meticulous line breaks */
  4. async function addTrailingSpaces(filePath: string) {
  5. const content = String(await readFile(filePath, "utf8"));
  6. const fixedContent = content.replace(/\/\*\*[\s\S]*?\*\//g, (m) =>
  7. m.replace(/\n/g, (m, i) =>
  8. i > 3 ? ` ${m}` : m
  9. )
  10. );
  11. await writeFile(filePath, fixedContent, "utf8");
  12. }
  13. /** Recursively processes all files in the given directory */
  14. async function processRecursive(directory: string) {
  15. directory = resolve(directory);
  16. const files = await readdir(directory);
  17. for(const file of files) {
  18. const fullPath = join(directory, file);
  19. const stats = await lstat(fullPath);
  20. if(stats.isDirectory())
  21. await processRecursive(fullPath);
  22. else if(fullPath.endsWith(".d.ts"))
  23. await addTrailingSpaces(fullPath);
  24. }
  25. }
  26. processRecursive("./dist/lib");