From d402a1c96aa2651b30bdb0e6dd7e074e84f9cd56 Mon Sep 17 00:00:00 2001 From: lisonge Date: Sun, 9 Jul 2023 23:24:35 +0800 Subject: [PATCH] feat: use selector syntax check --- .prettierignore | 1 + dist/gkd.json | 4 ++-- package.json | 22 +++++++++++++++++----- pnpm-lock.yaml | 17 ++++++++++++----- src/file.ts | 37 ++++++++++++++++++++++++++++++++++++- src/main.ts | 6 ++++-- src/selector.ts | 7 +++++++ src/types.ts | 11 +++++------ tsconfig.json | 6 +++++- 9 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 src/selector.ts diff --git a/.prettierignore b/.prettierignore index e3c58fae..8dc38e0d 100644 --- a/.prettierignore +++ b/.prettierignore @@ -3,3 +3,4 @@ pnpm-workspace.yaml LICENCE dist +src/kotlin diff --git a/dist/gkd.json b/dist/gkd.json index 3d3f654b..30daa100 100644 --- a/dist/gkd.json +++ b/dist/gkd.json @@ -1,8 +1,8 @@ { "name": "GKD官方订阅", - "version": 5, + "version": 6, "author": "lisonge", - "supportUrl": "https://github.com/lisonge/gkd-subscription", + "supportUrl": "https://github.com/gkd-kit/subscription", "apps": [ { "id": "air.tv.douyu.android", diff --git a/package.json b/package.json index 0d36c085..66e04df0 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "@gkd-kit/subscription", "type": "module", - "version": "0.0.5", - "private": false, + "version": "0.0.6", "main": "./dist/gkd.json", + "unpkg": "./dist/gkd.json", + "jsdelivr": "./dist/gkd.json", "files": [ "dist" ], @@ -11,18 +12,29 @@ "build": "tsx ./src/main.ts" }, "dependencies": { + "@gkd-kit/selector": "0.0.3", "@types/lodash": "^4.14.194", "@types/node": "^20", "@types/prettier": "2.7.3", "dayjs": "^1.11.7", - "dotenv": "16.2.0", + "dotenv": "^16.2.0", "lodash": "^4.17.21", - "prettier": "2.8.8", - "qiniu": "7.8.0", + "prettier": "^2.8.8", + "qiniu": "^7.8.0", "tsx": "^3.12.6", "typescript": "^5.0.4", "undici": "^5.22.1" }, + "author": "lisonge", + "license": "MIT", + "homepage": "https://github.com/gkd-kit/subscription#readme", + "bugs": { + "url": "https://github.com/gkd-kit/subscription/issues" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/gkd-kit/subscription.git" + }, "volta": { "node": "20.3.0" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5941aa5c..63df5e65 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,10 +1,13 @@ lockfileVersion: '6.0' settings: - autoInstallPeers: true + autoInstallPeers: false excludeLinksFromLockfile: false dependencies: + '@gkd-kit/selector': + specifier: 0.0.3 + version: 0.0.3 '@types/lodash': specifier: ^4.14.194 version: 4.14.194 @@ -18,16 +21,16 @@ dependencies: specifier: ^1.11.7 version: 1.11.7 dotenv: - specifier: 16.2.0 + specifier: ^16.2.0 version: 16.2.0 lodash: specifier: ^4.17.21 version: 4.17.21 prettier: - specifier: 2.8.8 + specifier: ^2.8.8 version: 2.8.8 qiniu: - specifier: 7.8.0 + specifier: ^7.8.0 version: 7.8.0 tsx: specifier: ^3.12.6 @@ -36,7 +39,7 @@ dependencies: specifier: ^5.0.4 version: 5.0.4 undici: - specifier: 5.22.1 + specifier: ^5.22.1 version: 5.22.1 packages: @@ -260,6 +263,10 @@ packages: dev: false optional: true + /@gkd-kit/selector@0.0.3: + resolution: {integrity: sha512-CXpUQ38F0CWtsKgbL4hzitGCPvIHeDFPCUvNjjAuyrd/55ODbJxzFe0OQ6LzLoFimklNNZ8MOe2tJHIhxnNZ0A==} + dev: false + /@tootallnate/once@1.1.2: resolution: {integrity: sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==} engines: {node: '>= 6'} diff --git a/src/file.ts b/src/file.ts index f39c526f..1159c3d5 100644 --- a/src/file.ts +++ b/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 = (array: IArray = []): T[] => { + return Array().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`, diff --git a/src/main.ts b/src/main.ts index f54c5549..07552a05 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,17 +1,19 @@ import { relativePath, walk, writeConfig } from './file'; import { AppConfig, AppConfigMudule } from './types'; +import url from 'node:url'; const apps: AppConfig[] = []; for await (const tsFp of walk(relativePath('./apps'))) { - const mod: AppConfigMudule = await import(`file://` + tsFp); + const mod: AppConfigMudule = await import(url.pathToFileURL(tsFp).href); apps.push(mod.default); } + // a,b,c,d apps.sort((a, b) => (a.id > b.id ? 1 : -1)); await writeConfig(`../dist/gkd.json`, { name: `GKD官方订阅`, author: `lisonge`, - supportUrl: `https://github.com/lisonge/gkd-subscription`, + supportUrl: `https://github.com/gkd-kit/subscription`, apps, }); diff --git a/src/selector.ts b/src/selector.ts new file mode 100644 index 00000000..0db19a1a --- /dev/null +++ b/src/selector.ts @@ -0,0 +1,7 @@ +import SelectorKit from '@gkd-kit/selector'; + +const { CommonSelector } = SelectorKit.li.songe.selector; + +export const parseSelector = (source: string) => { + return CommonSelector.Companion.parse(source); +}; diff --git a/src/types.ts b/src/types.ts index 033f6f65..f8a964c6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -type IArray = T | T[]; +export type IArray = T | T[]; export type SubscriptionConfig = { name?: string; @@ -20,7 +20,7 @@ export type SubscriptionConfig = { type NumberFilter = {}; type StringFilter = {}; -type AutoProps = { +type CommonProps = { activityIds?: IArray; excludeActivityIds?: IArray; cd?: number; @@ -42,7 +42,7 @@ type AutoProps = { export type AppConfig = { id: string; groups?: GroupConfig[]; -} & AutoProps; +} & CommonProps; export type AppConfigMudule = { default: AppConfig; @@ -53,16 +53,15 @@ type GroupConfig = { name: string; desc?: string; rules?: IArray; -} & AutoProps; +} & CommonProps; type RuleConfig = { - key?: number; name?: string; desc?: string; matches?: IArray; excludeMatches?: IArray; preKeys?: IArray; -} & AutoProps; +} & CommonProps; export const defineSubsConfig = (config: SubscriptionConfig) => { return JSON.stringify(config, undefined, 2); diff --git a/tsconfig.json b/tsconfig.json index 177758a3..c3e0a767 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,5 +13,9 @@ "strict": true, "skipLibCheck": true }, - "include": ["./src/**/*.ts", "./scripts/**/*.ts", "./types/**/*.ts"] + "include": [ + "./src/**/*.{ts,js,mjs,cjs}", + "./scripts/**/*.ts", + "./types/**/*.ts" + ] }