build-common.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // @ts-check
  2. import * as path from 'path';
  3. import { PathScurry } from 'path-scurry';
  4. import { readFileByLine } from './lib/fetch-text-by-line';
  5. import { processLine } from './lib/process-line';
  6. import { createRuleset } from './lib/create-file';
  7. import { domainDeduper } from './lib/domain-deduper';
  8. import { task } from './lib/trace-runner';
  9. import { SHARED_DESCRIPTION } from './lib/constants';
  10. const MAGIC_COMMAND_SKIP = '# $ custom_build_script';
  11. const MAGIC_COMMAND_TITLE = '# $ meta_title ';
  12. const MAGIC_COMMAND_DESCRIPTION = '# $ meta_description ';
  13. const sourceDir = path.resolve(import.meta.dir, '../Source');
  14. const outputSurgeDir = path.resolve(import.meta.dir, '../List');
  15. const outputClashDir = path.resolve(import.meta.dir, '../Clash');
  16. export const buildCommon = task(import.meta.path, async () => {
  17. const promises: Array<Promise<unknown>> = [];
  18. const pw = new PathScurry(sourceDir);
  19. for await (const entry of pw) {
  20. if (!entry.isFile()) {
  21. continue;
  22. }
  23. if (path.extname(entry.name) === '.js') {
  24. continue;
  25. }
  26. const relativePath = entry.relative();
  27. if (relativePath.startsWith('domainset/')) {
  28. promises.push(transformDomainset(entry.fullpath(), relativePath));
  29. continue;
  30. }
  31. if (
  32. relativePath.startsWith('ip/')
  33. || relativePath.startsWith('non_ip/')
  34. ) {
  35. promises.push(transformRuleset(entry.fullpath(), relativePath));
  36. continue;
  37. }
  38. }
  39. return Promise.all(promises);
  40. });
  41. if (import.meta.main) {
  42. buildCommon();
  43. }
  44. const processFile = async (sourcePath: string) => {
  45. console.log('Processing', sourcePath);
  46. const lines: string[] = [];
  47. let title = '';
  48. const descriptions: string[] = [];
  49. try {
  50. for await (const line of readFileByLine(sourcePath)) {
  51. if (line === MAGIC_COMMAND_SKIP) {
  52. return;
  53. }
  54. if (line.startsWith(MAGIC_COMMAND_TITLE)) {
  55. title = line.slice(MAGIC_COMMAND_TITLE.length).trim();
  56. continue;
  57. }
  58. if (line.startsWith(MAGIC_COMMAND_DESCRIPTION)) {
  59. descriptions.push(line.slice(MAGIC_COMMAND_DESCRIPTION.length).trim());
  60. continue;
  61. }
  62. const l = processLine(line);
  63. if (l) {
  64. lines.push(l);
  65. }
  66. }
  67. } catch (e) {
  68. console.error('Error processing', sourcePath);
  69. console.trace(e);
  70. }
  71. return [title, descriptions, lines] as const;
  72. };
  73. async function transformDomainset(sourcePath: string, relativePath: string) {
  74. const res = await processFile(sourcePath);
  75. if (!res) return;
  76. const [title, descriptions, lines] = res;
  77. const deduped = domainDeduper(lines);
  78. const description = [
  79. ...SHARED_DESCRIPTION,
  80. ...(
  81. descriptions.length
  82. ? ['', ...descriptions]
  83. : []
  84. )
  85. ];
  86. return Promise.all(createRuleset(
  87. title,
  88. description,
  89. new Date(),
  90. deduped,
  91. 'domainset',
  92. path.resolve(outputSurgeDir, relativePath),
  93. path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
  94. ));
  95. }
  96. /**
  97. * Output Surge RULE-SET and Clash classical text format
  98. */
  99. async function transformRuleset(sourcePath: string, relativePath: string) {
  100. const res = await processFile(sourcePath);
  101. if (!res) return;
  102. const [title, descriptions, lines] = res;
  103. const description = [
  104. ...SHARED_DESCRIPTION,
  105. ...(
  106. descriptions.length
  107. ? ['', ...descriptions]
  108. : []
  109. )
  110. ];
  111. return Promise.all(createRuleset(
  112. title,
  113. description,
  114. new Date(),
  115. lines,
  116. 'ruleset',
  117. path.resolve(outputSurgeDir, relativePath),
  118. path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
  119. ));
  120. }