build-common.ts 4.2 KB

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