run-against-source-file.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { never } from 'foxts/guard';
  2. import { readFileByLine } from './fetch-text-by-line';
  3. import { processLine } from './process-line';
  4. export default async function runAgainstSourceFile(
  5. filePath: string,
  6. callback: (domain: string, includeAllSubDomain: boolean) => void,
  7. type?: 'ruleset' | 'domainset'
  8. ) {
  9. for await (const line of readFileByLine(filePath)) {
  10. const l = processLine(line);
  11. if (!l) {
  12. continue;
  13. }
  14. if (type == null) {
  15. if (l.includes(',')) {
  16. type = 'ruleset';
  17. } else {
  18. type = 'domainset';
  19. }
  20. }
  21. if (type === 'ruleset') {
  22. const [ruleType, domain] = l.split(',', 3);
  23. if (ruleType === 'DOMAIN') {
  24. callback(domain, false);
  25. } else if (ruleType === 'DOMAIN-SUFFIX') {
  26. callback(domain, true);
  27. }
  28. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- exhaus options
  29. } else if (type === 'domainset') {
  30. if (l[0] === '.') {
  31. callback(l.slice(1), true);
  32. } else {
  33. callback(l, false);
  34. }
  35. } else {
  36. never(type);
  37. }
  38. }
  39. }