trim-source.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import path from 'path';
  2. import { fdir as Fdir } from 'fdir';
  3. import { readFileByLine } from './lib/fetch-text-by-line';
  4. const sourceDir = path.resolve(import.meta.dir, '../Source');
  5. (async () => {
  6. const promises: Array<Promise<unknown>> = [];
  7. const paths = await new Fdir()
  8. .withFullPaths()
  9. // .exclude((dirName, dirPath) => {
  10. // if (dirName === 'domainset' || dirName === 'ip' || dirName === 'non_ip') {
  11. // return false;
  12. // }
  13. // console.error(picocolors.red(`[build-comman] Unknown dir: ${dirPath}`));
  14. // return true;
  15. // })
  16. .filter((filepath, isDirectory) => {
  17. if (isDirectory) return true;
  18. const extname = path.extname(filepath);
  19. if (extname === '.js' || extname === '.ts') {
  20. return false;
  21. }
  22. return true;
  23. })
  24. .crawl(sourceDir)
  25. .withPromise();
  26. for (let i = 0, len = paths.length; i < len; i++) {
  27. const fullPath = paths[i];
  28. promises.push(trimFileLines(fullPath));
  29. }
  30. return Promise.all(promises);
  31. })();
  32. async function trimFileLines(file: string) {
  33. let result = '';
  34. for await (const line of readFileByLine(file)) {
  35. result += line.trim() + '\n';
  36. }
  37. return Bun.write(file, result);
  38. }