|
@@ -6,6 +6,7 @@ import { readFileByLine } from './lib/fetch-text-by-line';
|
|
|
import { processLine } from './lib/process-line';
|
|
import { processLine } from './lib/process-line';
|
|
|
import { createRuleset } from './lib/create-file';
|
|
import { createRuleset } from './lib/create-file';
|
|
|
import { domainDeduper } from './lib/domain-deduper';
|
|
import { domainDeduper } from './lib/domain-deduper';
|
|
|
|
|
+import type { Span } from './trace';
|
|
|
import { task } from './trace';
|
|
import { task } from './trace';
|
|
|
import { SHARED_DESCRIPTION } from './lib/constants';
|
|
import { SHARED_DESCRIPTION } from './lib/constants';
|
|
|
|
|
|
|
@@ -17,7 +18,7 @@ const sourceDir = path.resolve(import.meta.dir, '../Source');
|
|
|
const outputSurgeDir = path.resolve(import.meta.dir, '../List');
|
|
const outputSurgeDir = path.resolve(import.meta.dir, '../List');
|
|
|
const outputClashDir = path.resolve(import.meta.dir, '../Clash');
|
|
const outputClashDir = path.resolve(import.meta.dir, '../Clash');
|
|
|
|
|
|
|
|
-export const buildCommon = task(import.meta.path, async () => {
|
|
|
|
|
|
|
+export const buildCommon = task(import.meta.path, async (span) => {
|
|
|
const promises: Array<Promise<unknown>> = [];
|
|
const promises: Array<Promise<unknown>> = [];
|
|
|
|
|
|
|
|
const pw = new PathScurry(sourceDir);
|
|
const pw = new PathScurry(sourceDir);
|
|
@@ -33,14 +34,14 @@ export const buildCommon = task(import.meta.path, async () => {
|
|
|
|
|
|
|
|
const relativePath = entry.relative();
|
|
const relativePath = entry.relative();
|
|
|
if (relativePath.startsWith('domainset/')) {
|
|
if (relativePath.startsWith('domainset/')) {
|
|
|
- promises.push(transformDomainset(entry.fullpath(), relativePath));
|
|
|
|
|
|
|
+ promises.push(transformDomainset(span, entry.fullpath(), relativePath));
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
if (
|
|
if (
|
|
|
relativePath.startsWith('ip/')
|
|
relativePath.startsWith('ip/')
|
|
|
|| relativePath.startsWith('non_ip/')
|
|
|| relativePath.startsWith('non_ip/')
|
|
|
) {
|
|
) {
|
|
|
- promises.push(transformRuleset(entry.fullpath(), relativePath));
|
|
|
|
|
|
|
+ promises.push(transformRuleset(span, entry.fullpath(), relativePath));
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -52,46 +53,50 @@ if (import.meta.main) {
|
|
|
buildCommon();
|
|
buildCommon();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const processFile = async (sourcePath: string) => {
|
|
|
|
|
- console.log('Processing', sourcePath);
|
|
|
|
|
-
|
|
|
|
|
- const lines: string[] = [];
|
|
|
|
|
-
|
|
|
|
|
- let title = '';
|
|
|
|
|
- const descriptions: string[] = [];
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- for await (const line of readFileByLine(sourcePath)) {
|
|
|
|
|
- if (line === MAGIC_COMMAND_SKIP) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (line.startsWith(MAGIC_COMMAND_TITLE)) {
|
|
|
|
|
- title = line.slice(MAGIC_COMMAND_TITLE.length).trim();
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (line.startsWith(MAGIC_COMMAND_DESCRIPTION)) {
|
|
|
|
|
- descriptions.push(line.slice(MAGIC_COMMAND_DESCRIPTION.length).trim());
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const l = processLine(line);
|
|
|
|
|
- if (l) {
|
|
|
|
|
- lines.push(l);
|
|
|
|
|
|
|
+const processFile = (span: Span, sourcePath: string) => {
|
|
|
|
|
+ // console.log('Processing', sourcePath);
|
|
|
|
|
+ return span.traceChild(`process file: ${sourcePath}`).traceAsyncFn(async () => {
|
|
|
|
|
+ const lines: string[] = [];
|
|
|
|
|
+
|
|
|
|
|
+ let title = '';
|
|
|
|
|
+ const descriptions: string[] = [];
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ for await (const line of readFileByLine(sourcePath)) {
|
|
|
|
|
+ if (line === MAGIC_COMMAND_SKIP) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (line.startsWith(MAGIC_COMMAND_TITLE)) {
|
|
|
|
|
+ title = line.slice(MAGIC_COMMAND_TITLE.length).trim();
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (line.startsWith(MAGIC_COMMAND_DESCRIPTION)) {
|
|
|
|
|
+ descriptions.push(line.slice(MAGIC_COMMAND_DESCRIPTION.length).trim());
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const l = processLine(line);
|
|
|
|
|
+ if (l) {
|
|
|
|
|
+ lines.push(l);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.error('Error processing', sourcePath);
|
|
|
|
|
+ console.trace(e);
|
|
|
}
|
|
}
|
|
|
- } catch (e) {
|
|
|
|
|
- console.error('Error processing', sourcePath);
|
|
|
|
|
- console.trace(e);
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- return [title, descriptions, lines] as const;
|
|
|
|
|
|
|
+ return [title, descriptions, lines] as const;
|
|
|
|
|
+ });
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-async function transformDomainset(sourcePath: string, relativePath: string) {
|
|
|
|
|
- const res = await processFile(sourcePath);
|
|
|
|
|
|
|
+async function transformDomainset(parentSpan: Span, sourcePath: string, relativePath: string) {
|
|
|
|
|
+ const span = parentSpan.traceChild(`transform domainset: ${path.basename(sourcePath, path.extname(sourcePath))}`);
|
|
|
|
|
+
|
|
|
|
|
+ const res = await processFile(span, sourcePath);
|
|
|
if (!res) return;
|
|
if (!res) return;
|
|
|
|
|
+
|
|
|
const [title, descriptions, lines] = res;
|
|
const [title, descriptions, lines] = res;
|
|
|
|
|
|
|
|
const deduped = domainDeduper(lines);
|
|
const deduped = domainDeduper(lines);
|
|
@@ -104,7 +109,8 @@ async function transformDomainset(sourcePath: string, relativePath: string) {
|
|
|
)
|
|
)
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- return Promise.all(createRuleset(
|
|
|
|
|
|
|
+ return createRuleset(
|
|
|
|
|
+ span,
|
|
|
title,
|
|
title,
|
|
|
description,
|
|
description,
|
|
|
new Date(),
|
|
new Date(),
|
|
@@ -112,15 +118,18 @@ async function transformDomainset(sourcePath: string, relativePath: string) {
|
|
|
'domainset',
|
|
'domainset',
|
|
|
path.resolve(outputSurgeDir, relativePath),
|
|
path.resolve(outputSurgeDir, relativePath),
|
|
|
path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
|
|
path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
|
|
|
- ));
|
|
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Output Surge RULE-SET and Clash classical text format
|
|
* Output Surge RULE-SET and Clash classical text format
|
|
|
*/
|
|
*/
|
|
|
-async function transformRuleset(sourcePath: string, relativePath: string) {
|
|
|
|
|
- const res = await processFile(sourcePath);
|
|
|
|
|
- if (!res) return;
|
|
|
|
|
|
|
+async function transformRuleset(parentSpan: Span, sourcePath: string, relativePath: string) {
|
|
|
|
|
+ const span = parentSpan.traceChild(`transform ruleset: ${path.basename(sourcePath, path.extname(sourcePath))}`);
|
|
|
|
|
+
|
|
|
|
|
+ const res = await processFile(span, sourcePath);
|
|
|
|
|
+ if (!res) return null;
|
|
|
|
|
+
|
|
|
const [title, descriptions, lines] = res;
|
|
const [title, descriptions, lines] = res;
|
|
|
|
|
|
|
|
const description = [
|
|
const description = [
|
|
@@ -132,7 +141,8 @@ async function transformRuleset(sourcePath: string, relativePath: string) {
|
|
|
)
|
|
)
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- return Promise.all(createRuleset(
|
|
|
|
|
|
|
+ return createRuleset(
|
|
|
|
|
+ span,
|
|
|
title,
|
|
title,
|
|
|
description,
|
|
description,
|
|
|
new Date(),
|
|
new Date(),
|
|
@@ -140,5 +150,5 @@ async function transformRuleset(sourcePath: string, relativePath: string) {
|
|
|
'ruleset',
|
|
'ruleset',
|
|
|
path.resolve(outputSurgeDir, relativePath),
|
|
path.resolve(outputSurgeDir, relativePath),
|
|
|
path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
|
|
path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
|
|
|
- ));
|
|
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|