Refactor: simplify build infra

This commit is contained in:
SukkaW
2023-09-14 22:34:25 +08:00
parent 2448cbe39a
commit 573c0f5274
22 changed files with 127 additions and 132 deletions

View File

@@ -1,6 +1,5 @@
// @ts-check
const fs = require('fs');
const fse = require('fs-extra');
const { readFileByLine } = require('./fetch-remote-text-by-line');
const { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } = require('./clash');
@@ -9,26 +8,33 @@ const { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset
* @param {string} filePath
*/
async function compareAndWriteFile(linesA, filePath) {
await fse.ensureFile(filePath);
let isEqual = true;
let index = 0;
if (!fs.existsSync(filePath)) {
console.log(`${filePath} does not exists, writing...`);
isEqual = false;
} else {
let index = 0;
for await (const lineB of readFileByLine(filePath)) {
const lineA = linesA[index];
index++;
for await (const lineB of readFileByLine(filePath)) {
const lineA = linesA[index];
index++;
if (lineA[0] === '#' && lineB[0] === '#') {
continue;
if (lineA[0] === '#' && lineB[0] === '#') {
continue;
}
if (lineA !== lineB) {
isEqual = false;
break;
}
}
if (lineA !== lineB) {
if (index !== linesA.length) {
isEqual = false;
break;
}
}
if (!isEqual || index !== linesA.length) {
if (!isEqual) {
const stream = fs.createWriteStream(filePath, { encoding: 'utf-8' });
for (let i = 0, len = linesA.length; i < len; i++) {

View File

@@ -1,14 +1,5 @@
// @ts-check
const tldts = require('./cached-tld-parse');
/**
* @param {string} domain
*/
module.exports.isDomainLoose = (domain) => {
const { isIcann, isPrivate, isIp } = tldts.parse(domain);
return !!(!isIp && (isIcann || isPrivate));
};
/**
* @param {string | null | undefined} domain
*/

View File

@@ -1,5 +1,10 @@
const { fetchRemoteTextAndCreateReadlineInterface } = require('./fetch-remote-text-by-line');
const { isDomainLoose } = require('./is-domain-loose');
const tldts = require('tldts');
const isDomainLoose = (domain) => {
const { isIcann, isPrivate, isIp } = tldts.parse(domain);
return !!(!isIp && (isIcann || isPrivate));
};
/**
* @param {string | URL} url

View File

@@ -93,9 +93,6 @@ async function processHosts(hostsUrl, includeAllSubDomain = false) {
return domainSets;
}
const R_KNOWN_NOT_NETWORK_FILTER_PATTERN = /[#&%~=]/;
const R_KNOWN_NOT_NETWORK_FILTER_PATTERN_2 = /(\$popup|\$removeparam|\$popunder)/;
/**
* @param {string | URL} filterRulesUrl
* @param {readonly (string | URL)[] | undefined} [fallbackUrls]
@@ -197,8 +194,7 @@ async function processFilterRules(filterRulesUrl, fallbackUrls, includeThirdPart
downloadTime = performance.now() - downloadStart;
for (let i = 0, len = filterRules.length; i < len; i++) {
const line = filterRules[i].trim();
lineCb(line);
lineCb(filterRules[i].trim());
}
}
@@ -212,6 +208,9 @@ async function processFilterRules(filterRulesUrl, fallbackUrls, includeThirdPart
};
}
const R_KNOWN_NOT_NETWORK_FILTER_PATTERN = /[#&%~=]/;
const R_KNOWN_NOT_NETWORK_FILTER_PATTERN_2 = /(\$popup|\$removeparam|\$popunder)/;
/**
* @param {string} $line
* @param {boolean} includeThirdParties

View File

@@ -42,13 +42,11 @@ const compare = (a, b) => {
* @param {import('gorhill-publicsuffixlist').default | null} [gorhill]
*/
const createDomainSorter = (gorhill = null) => {
const cached = require('./cached-tld-parse');
if (gorhill) {
/**
* @param {string} input
*/
const getDomain = cached.createCachedGorhillGetDomain(gorhill);
const getDomain = require('./cached-tld-parse').createCachedGorhillGetDomain(gorhill);
/**
* @param {string} a
@@ -66,7 +64,7 @@ const createDomainSorter = (gorhill = null) => {
};
}
const tldts = cached;
const tldts = require('./cached-tld-parse');
/**
* @param {string} a
* @param {string} b

View File

@@ -36,19 +36,10 @@ module.exports.traceAsync = traceAsync;
* @template T
* @param {string} __filename
* @param {() => Promise<T>} fn
* @returns {Promise<T>}
* @param {string | null} [customname]
*/
module.exports.runner = async (__filename, fn) => {
return traceAsync(`⌛ [${path.basename(__filename, path.extname(__filename))}]`, fn);
};
/**
* @template T
* @param {string} __filename
* @param {() => Promise<T>} fn
*/
module.exports.task = (__filename, fn) => {
const taskName = path.basename(__filename, path.extname(__filename));
module.exports.task = (__filename, fn, customname = null) => {
const taskName = customname ?? path.basename(__filename, path.extname(__filename));
return () => {
console.log(`🏃 [${taskName}] Start executing`);
return traceAsync(`✅ [${taskName}] Executed successfully`, fn);