mirror of
https://github.com/AIsouler/GKD_subscription.git
synced 2025-12-20 00:35:17 +08:00
feat: use selector syntax check
This commit is contained in:
37
src/file.ts
37
src/file.ts
@@ -4,12 +4,17 @@ import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import url from 'node:url';
|
||||
import selfPkg from '../package.json';
|
||||
import type { SubscriptionConfig } from './types';
|
||||
import type { SubscriptionConfig, IArray } from './types';
|
||||
import { parseSelector } from './selector';
|
||||
|
||||
export const relativePath = (p: string) => {
|
||||
return url.fileURLToPath(new URL(p, import.meta.url));
|
||||
};
|
||||
|
||||
const iArrayToArray = <T>(array: IArray<T> = []): T[] => {
|
||||
return Array<T>().concat(array);
|
||||
};
|
||||
|
||||
export const writeConfig = async (fp: string, config: SubscriptionConfig) => {
|
||||
const filePath = relativePath(fp);
|
||||
const newConfig: SubscriptionConfig = { ...config };
|
||||
@@ -41,6 +46,36 @@ export const writeConfig = async (fp: string, config: SubscriptionConfig) => {
|
||||
});
|
||||
});
|
||||
|
||||
newConfig.apps?.forEach((app) => {
|
||||
app.groups?.forEach((g) => {
|
||||
if (!g.rules) return;
|
||||
const rules = iArrayToArray(g.rules).map((r) => {
|
||||
if (typeof r == 'string') {
|
||||
return { matches: r };
|
||||
}
|
||||
return r;
|
||||
});
|
||||
rules.forEach((r) => {
|
||||
[r.matches, r.excludeMatches]
|
||||
.map((m) => iArrayToArray(m))
|
||||
.flat()
|
||||
.forEach((selector) => {
|
||||
try {
|
||||
parseSelector(selector);
|
||||
} catch (e) {
|
||||
console.error({
|
||||
message: `invalid selector`,
|
||||
appId: app.id,
|
||||
groupKey: g.key,
|
||||
selector,
|
||||
});
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const sortKeys: (keyof SubscriptionConfig)[] = [
|
||||
`name`,
|
||||
`version`,
|
||||
|
||||
Reference in New Issue
Block a user