build-common.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 { createRuleset } = require('./lib/create-file');
  7. const { domainDeduper } = require('./lib/domain-deduper');
  8. const { runner } = require('./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. const buildCommon = async () => {
  16. /** @type {Promise<void>[]} */
  17. const promises = [];
  18. const pw = new PathScurry(sourceDir);
  19. for await (const entry of pw) {
  20. if (entry.isFile()) {
  21. if (path.extname(entry.name) === '.js') {
  22. continue;
  23. }
  24. const relativePath = entry.relative();
  25. if (relativePath.startsWith('domainset/')) {
  26. promises.push(transformDomainset(entry.fullpath(), relativePath));
  27. continue;
  28. }
  29. if (
  30. relativePath.startsWith('ip/')
  31. || relativePath.startsWith('non_ip/')
  32. ) {
  33. promises.push(transformRuleset(entry.fullpath(), relativePath));
  34. continue;
  35. }
  36. }
  37. }
  38. return Promise.all(promises);
  39. };
  40. module.exports.buildCommon = buildCommon;
  41. if (require.main === module) {
  42. runner(__filename, buildCommon);
  43. }
  44. /**
  45. * @param {string} sourcePath
  46. */
  47. const processFile = async (sourcePath) => {
  48. /** @type {string[]} */
  49. const lines = [];
  50. let title = '';
  51. /** @type {string[]} */
  52. const descriptions = [];
  53. for await (const line of readFileByLine(sourcePath)) {
  54. if (line === MAGIC_COMMAND_SKIP) {
  55. return;
  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. return /** @type {const} */ ([title, descriptions, lines]);
  71. };
  72. /**
  73. * @param {string} sourcePath
  74. * @param {string} relativePath
  75. */
  76. async function transformDomainset(sourcePath, relativePath) {
  77. const res = await processFile(sourcePath);
  78. if (!res) return;
  79. const [title, descriptions, lines] = res;
  80. const deduped = domainDeduper(lines);
  81. const description = [
  82. 'License: AGPL 3.0',
  83. 'Homepage: https://ruleset.skk.moe',
  84. 'GitHub: https://github.com/SukkaW/Surge',
  85. ...(
  86. descriptions.length
  87. ? ['', ...descriptions]
  88. : []
  89. )
  90. ];
  91. await Promise.all(createRuleset(
  92. title,
  93. description,
  94. new Date(),
  95. deduped,
  96. 'domainset',
  97. path.resolve(outputSurgeDir, relativePath),
  98. path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
  99. ));
  100. }
  101. /**
  102. * Output Surge RULE-SET and Clash classical text format
  103. *
  104. * @param {string} sourcePath
  105. * @param {string} relativePath
  106. */
  107. async function transformRuleset(sourcePath, relativePath) {
  108. const res = await processFile(sourcePath);
  109. if (!res) return;
  110. const [title, descriptions, lines] = res;
  111. const description = [
  112. 'License: AGPL 3.0',
  113. 'Homepage: https://ruleset.skk.moe',
  114. 'GitHub: https://github.com/SukkaW/Surge',
  115. ...(
  116. descriptions.length
  117. ? ['', ...descriptions]
  118. : []
  119. )
  120. ];
  121. await Promise.all(createRuleset(
  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. }