build.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // @ts-check
  2. const path = require('path');
  3. const { PathScurry } = require('path-scurry');
  4. const { readFileByLine } = require('./lib/fetch-remote-text-by-line');
  5. const { processLine } = require('./lib/process-line');
  6. const { compareAndWriteFile } = require('./lib/string-array-compare');
  7. const { withBannerArray } = require('./lib/with-banner');
  8. const { domainDeduper } = require('./lib/domain-deduper');
  9. const { surgeRulesetToClashClassicalTextRuleset, surgeDomainsetToClashDomainset } = require('./lib/clash');
  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(__dirname, '../Source');
  14. const outputSurgeDir = path.resolve(__dirname, '../List');
  15. const outputClashDir = path.resolve(__dirname, '../Clash');
  16. (async () => {
  17. /** @type {Promise<void>[]} */
  18. const promises = [];
  19. const pw = new PathScurry(sourceDir);
  20. for await (const entry of pw) {
  21. if (entry.isFile()) {
  22. if (path.extname(entry.name) === '.js') {
  23. continue;
  24. }
  25. const relativePath = entry.relative();
  26. if (relativePath.startsWith('domainset/')) {
  27. promises.push(transformDomainset(entry.fullpath(), relativePath));
  28. continue;
  29. }
  30. if (
  31. relativePath.startsWith('ip/')
  32. || relativePath.startsWith('non_ip/')
  33. ) {
  34. promises.push(transformRuleset(entry.fullpath(), relativePath));
  35. continue;
  36. }
  37. }
  38. }
  39. await Promise.all(promises);
  40. })();
  41. /**
  42. * @param {string} sourcePath
  43. */
  44. const processFile = async (sourcePath) => {
  45. /** @type {Set<string>} */
  46. const lines = new Set();
  47. let title = '';
  48. /** @type {string[]} */
  49. const descriptions = [];
  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.add(l);
  65. }
  66. }
  67. return /** @type {const} */ ([title, descriptions, lines]);
  68. };
  69. /**
  70. * @param {string} sourcePath
  71. * @param {string} relativePath
  72. */
  73. async function transformDomainset(sourcePath, relativePath) {
  74. const res = await processFile(sourcePath);
  75. if (!res) return;
  76. const [title, descriptions, lines] = res;
  77. const deduped = domainDeduper(Array.from(lines));
  78. const description = [
  79. 'License: AGPL 3.0',
  80. 'Homepage: https://ruleset.skk.moe',
  81. 'GitHub: https://github.com/SukkaW/Surge',
  82. ...(
  83. descriptions.length
  84. ? ['', ...descriptions]
  85. : []
  86. )
  87. ];
  88. await Promise.all([
  89. // Surge DOMAIN-SET
  90. compareAndWriteFile(
  91. withBannerArray(
  92. title,
  93. description,
  94. new Date(),
  95. deduped
  96. ),
  97. path.resolve(outputSurgeDir, relativePath)
  98. ),
  99. // Clash domain text
  100. compareAndWriteFile(
  101. withBannerArray(
  102. title,
  103. description,
  104. new Date(),
  105. surgeDomainsetToClashDomainset(deduped)
  106. ),
  107. // change path extname to .txt
  108. path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
  109. )
  110. ]);
  111. }
  112. /**
  113. * Output Surge RULE-SET and Clash classical text format
  114. *
  115. * @param {string} sourcePath
  116. * @param {string} relativePath
  117. */
  118. async function transformRuleset(sourcePath, relativePath) {
  119. const res = await processFile(sourcePath);
  120. if (!res) return;
  121. const [title, descriptions, set] = res;
  122. const description = [
  123. 'License: AGPL 3.0',
  124. 'Homepage: https://ruleset.skk.moe',
  125. 'GitHub: https://github.com/SukkaW/Surge',
  126. ...(
  127. descriptions.length
  128. ? ['', ...descriptions]
  129. : []
  130. )
  131. ];
  132. const lines = Array.from(set);
  133. const clashSupported = surgeRulesetToClashClassicalTextRuleset(set);
  134. await Promise.all([
  135. // Surge RULE-SET
  136. compareAndWriteFile(
  137. withBannerArray(
  138. title,
  139. description,
  140. new Date(),
  141. lines
  142. ),
  143. path.resolve(outputSurgeDir, relativePath)
  144. ),
  145. // Clash domainset
  146. compareAndWriteFile(
  147. withBannerArray(
  148. title,
  149. description,
  150. new Date(),
  151. clashSupported
  152. ),
  153. // change path extname to .txt
  154. path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
  155. )
  156. ]);
  157. }