1
0

fix-dts.mts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { readdir, readFile, writeFile, lstat } from "node:fs/promises";
  2. import { join, resolve } from "node:path";
  3. import k from "kleur";
  4. const dtsPath = resolve("./dist/lib/");
  5. /** Adds two spaces to the end of each line in JSDoc comments to preserve the meticulous line breaks */
  6. async function addTrailingSpaces(filePath: string): Promise<void> {
  7. const content = String(await readFile(filePath, "utf8"));
  8. const fixedContent = content.replace(/\/\*\*[\s\S]*?\*\//g, (m) =>
  9. m.replace(/\n/g, (m, i) =>
  10. i > 3 ? ` ${m}` : m
  11. )
  12. );
  13. await writeFile(filePath, fixedContent, "utf8");
  14. }
  15. /** Recursively processes all files in the given directory */
  16. async function processRecursive(directory: string): Promise<void> {
  17. directory = resolve(directory);
  18. const files = await readdir(directory);
  19. for(const file of files) {
  20. const fullPath = join(directory, file);
  21. const stats = await lstat(fullPath);
  22. if(stats.isDirectory())
  23. await processRecursive(fullPath);
  24. else if(fullPath.endsWith(".d.ts"))
  25. await addTrailingSpaces(fullPath);
  26. }
  27. }
  28. try {
  29. await processRecursive(dtsPath);
  30. console.log(k.green(`Fixed all .d.ts files in ${k.reset(`'${dtsPath}'`)}`));
  31. }
  32. catch(err) {
  33. console.error(k.red(`Encountered error while fixing .d.ts files in ${k.reset(`'${dtsPath}'`)}:\n`), err);
  34. }