tools-dedupe-src.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { fdir as Fdir } from 'fdir';
  2. import path from 'node:path';
  3. import fsp from 'node:fs/promises';
  4. import { SOURCE_DIR } from './constants/dir';
  5. import { readFileByLine } from './lib/fetch-text-by-line';
  6. (async () => {
  7. const files = await new Fdir()
  8. .withFullPaths()
  9. .filter((filepath, isDirectory) => {
  10. if (isDirectory) return true;
  11. const extname = path.extname(filepath);
  12. return extname !== '.js' && extname !== '.ts';
  13. })
  14. .crawl(SOURCE_DIR)
  15. .withPromise();
  16. await Promise.all(files.map(dedupeFile));
  17. })();
  18. async function dedupeFile(file: string) {
  19. const set = new Set<string>();
  20. const result: string[] = [];
  21. for await (const line of readFileByLine(file)) {
  22. if (line.length === 0) {
  23. result.push(line);
  24. continue;
  25. }
  26. if (line[0] === '#') {
  27. result.push(line);
  28. continue;
  29. }
  30. if (set.has(line)) {
  31. // do nothing
  32. } else {
  33. set.add(line);
  34. result.push(line);
  35. }
  36. }
  37. return fsp.writeFile(file, result.join('\n') + '\n');
  38. }