feat: use selector syntax check

This commit is contained in:
lisonge
2023-07-09 23:24:35 +08:00
parent 0f2206bbe4
commit d402a1c96a
9 changed files with 89 additions and 22 deletions

View File

@@ -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`,