Refactor: reduce path manipulation

This commit is contained in:
SukkaW 2024-11-20 22:48:39 +08:00
parent 9f43a765ca
commit 484afab42c

View File

@ -45,7 +45,7 @@ export const buildCommon = task(require.main === module, __filename)(async (span
const fullPath = SOURCE_DIR + path.sep + relativePath; const fullPath = SOURCE_DIR + path.sep + relativePath;
if (relativePath.startsWith(domainsetSrcFolder)) { if (relativePath.startsWith(domainsetSrcFolder)) {
promises.push(transformDomainset(span, fullPath, relativePath)); promises.push(transformDomainset(span, fullPath));
continue; continue;
} }
// if ( // if (
@ -107,15 +107,17 @@ function processFile(span: Span, sourcePath: string) {
}); });
} }
function transformDomainset(parentSpan: Span, sourcePath: string, relativePath: string) { function transformDomainset(parentSpan: Span, sourcePath: string) {
const extname = path.extname(sourcePath);
const basename = path.basename(sourcePath, extname);
return parentSpan return parentSpan
.traceChildAsync( .traceChildAsync(
`transform domainset: ${path.basename(sourcePath, path.extname(sourcePath))}`, `transform domainset: ${basename}`,
async (span) => { async (span) => {
const res = await processFile(span, sourcePath); const res = await processFile(span, sourcePath);
if (res === $skip) return; if (res === $skip) return;
const id = path.basename(relativePath).slice(0, -path.extname(relativePath).length); const id = basename;
const [title, descriptions, lines] = res; const [title, descriptions, lines] = res;
let description: string[]; let description: string[];
@ -140,13 +142,20 @@ function transformDomainset(parentSpan: Span, sourcePath: string, relativePath:
* Output Surge RULE-SET and Clash classical text format * Output Surge RULE-SET and Clash classical text format
*/ */
async function transformRuleset(parentSpan: Span, sourcePath: string, relativePath: string) { async function transformRuleset(parentSpan: Span, sourcePath: string, relativePath: string) {
const extname = path.extname(sourcePath);
const basename = path.basename(sourcePath, extname);
return parentSpan return parentSpan
.traceChild(`transform ruleset: ${path.basename(sourcePath, path.extname(sourcePath))}`) .traceChild(`transform ruleset: ${basename}`)
.traceAsyncFn(async (span) => { .traceAsyncFn(async (span) => {
const res = await processFile(span, sourcePath); const res = await processFile(span, sourcePath);
if (res === $skip) return; if (res === $skip) return;
const [type, id] = relativePath.slice(0, -path.extname(relativePath).length).split(path.sep); const id = basename;
const [type] = relativePath.slice(0, -extname.length).split(path.sep);
console.log({ relativePath, basename, id, type });
if (type !== 'ip' && type !== 'non_ip') { if (type !== 'ip' && type !== 'non_ip') {
throw new TypeError(`Invalid type: ${type}`); throw new TypeError(`Invalid type: ${type}`);
} }