build-common.ts 3.5 KB

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