|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const { PathScurry } = require('path-scurry'); |
| 5 | +const { readFileByLine } = require('./lib/fetch-remote-text-by-line'); |
| 6 | +const { processLine } = require('./lib/process-line'); |
| 7 | +const { createRuleset } = require('./lib/create-file'); |
| 8 | +const { domainDeduper } = require('./lib/domain-deduper'); |
| 9 | +const { runner } = require('./lib/trace-runner'); |
| 10 | + |
| 11 | +const MAGIC_COMMAND_SKIP = '# $ custom_build_script'; |
| 12 | +const MAGIC_COMMAND_TITLE = '# $ meta_title '; |
| 13 | +const MAGIC_COMMAND_DESCRIPTION = '# $ meta_description '; |
| 14 | + |
| 15 | +const sourceDir = path.resolve(__dirname, '../Source'); |
| 16 | +const outputSurgeDir = path.resolve(__dirname, '../List'); |
| 17 | +const outputClashDir = path.resolve(__dirname, '../Clash'); |
| 18 | + |
| 19 | +const buildCommon = async () => { |
| 20 | + /** @type {Promise<void>[]} */ |
| 21 | + const promises = []; |
| 22 | + |
| 23 | + const pw = new PathScurry(sourceDir); |
| 24 | + for await (const entry of pw) { |
| 25 | + if (entry.isFile()) { |
| 26 | + if (path.extname(entry.name) === '.js') { |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + const relativePath = entry.relative(); |
| 31 | + if (relativePath.startsWith('domainset/')) { |
| 32 | + promises.push(transformDomainset(entry.fullpath(), relativePath)); |
| 33 | + continue; |
| 34 | + } |
| 35 | + if ( |
| 36 | + relativePath.startsWith('ip/') |
| 37 | + || relativePath.startsWith('non_ip/') |
| 38 | + ) { |
| 39 | + promises.push(transformRuleset(entry.fullpath(), relativePath)); |
| 40 | + continue; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return Promise.all(promises); |
| 46 | +}; |
| 47 | + |
| 48 | +module.exports.buildCommon = buildCommon; |
| 49 | + |
| 50 | +if (require.main === module) { |
| 51 | + runner(__filename, buildCommon); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @param {string} sourcePath |
| 56 | + */ |
| 57 | +const processFile = async (sourcePath) => { |
| 58 | + /** @type {string[]} */ |
| 59 | + const lines = []; |
| 60 | + |
| 61 | + let title = ''; |
| 62 | + /** @type {string[]} */ |
| 63 | + const descriptions = []; |
| 64 | + |
| 65 | + for await (const line of readFileByLine(sourcePath)) { |
| 66 | + if (line === MAGIC_COMMAND_SKIP) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (line.startsWith(MAGIC_COMMAND_TITLE)) { |
| 71 | + title = line.slice(MAGIC_COMMAND_TITLE.length).trim(); |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + if (line.startsWith(MAGIC_COMMAND_DESCRIPTION)) { |
| 76 | + descriptions.push(line.slice(MAGIC_COMMAND_DESCRIPTION.length).trim()); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + const l = processLine(line); |
| 81 | + if (l) { |
| 82 | + lines.push(l); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return /** @type {const} */ ([title, descriptions, lines]); |
| 87 | +}; |
| 88 | + |
| 89 | +/** |
| 90 | + * @param {string} sourcePath |
| 91 | + * @param {string} relativePath |
| 92 | + */ |
| 93 | +async function transformDomainset(sourcePath, relativePath) { |
| 94 | + const res = await processFile(sourcePath); |
| 95 | + if (!res) return; |
| 96 | + const [title, descriptions, lines] = res; |
| 97 | + |
| 98 | + const deduped = domainDeduper(lines); |
| 99 | + const description = [ |
| 100 | + 'License: AGPL 3.0', |
| 101 | + 'Homepage: https://ruleset.skk.moe', |
| 102 | + 'GitHub: https://github.com/SukkaW/Surge', |
| 103 | + ...( |
| 104 | + descriptions.length |
| 105 | + ? ['', ...descriptions] |
| 106 | + : [] |
| 107 | + ) |
| 108 | + ]; |
| 109 | + |
| 110 | + await Promise.all(createRuleset( |
| 111 | + title, |
| 112 | + description, |
| 113 | + new Date(), |
| 114 | + deduped, |
| 115 | + 'domainset', |
| 116 | + path.resolve(outputSurgeDir, relativePath), |
| 117 | + path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`) |
| 118 | + )); |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Output Surge RULE-SET and Clash classical text format |
| 123 | + * |
| 124 | + * @param {string} sourcePath |
| 125 | + * @param {string} relativePath |
| 126 | + */ |
| 127 | +async function transformRuleset(sourcePath, relativePath) { |
| 128 | + const res = await processFile(sourcePath); |
| 129 | + if (!res) return; |
| 130 | + const [title, descriptions, lines] = res; |
| 131 | + |
| 132 | + const description = [ |
| 133 | + 'License: AGPL 3.0', |
| 134 | + 'Homepage: https://ruleset.skk.moe', |
| 135 | + 'GitHub: https://github.com/SukkaW/Surge', |
| 136 | + ...( |
| 137 | + descriptions.length |
| 138 | + ? ['', ...descriptions] |
| 139 | + : [] |
| 140 | + ) |
| 141 | + ]; |
| 142 | + |
| 143 | + await Promise.all(createRuleset( |
| 144 | + title, |
| 145 | + description, |
| 146 | + new Date(), |
| 147 | + lines, |
| 148 | + 'ruleset', |
| 149 | + path.resolve(outputSurgeDir, relativePath), |
| 150 | + path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`) |
| 151 | + )); |
| 152 | +} |
0 commit comments