feat: 全局规则自动禁用

This commit is contained in:
lisonge
2023-12-25 17:39:27 +08:00
parent 795102ad68
commit 2985909726
4 changed files with 88 additions and 64 deletions

View File

@@ -1,33 +1,7 @@
import path from 'node:path';
import url from 'node:url';
import picocolors from 'picocolors';
import { walk } from './file';
import type { RawApp, RawSubscription } from './types';
import _ from 'lodash';
import { pinyin } from 'pinyin-pro';
import globalGroups from './globalGroups';
import categories from './categories';
const apps: RawApp[] = [];
for await (const tsFp of walk(process.cwd() + '/src/apps')) {
const mod: { default: RawApp } = await import(url.pathToFileURL(tsFp).href);
const appConfig = mod.default;
if (path.basename(tsFp, '.ts') != appConfig.id) {
throw new Error(
`${picocolors.blue(
tsFp,
)} file basename is not equal to its app id ${picocolors.blue(
appConfig.id,
)} `,
);
}
appConfig.groups?.forEach((g) => {
if (!g.name.startsWith('开屏广告')) {
g.enable = false;
}
});
apps.push(appConfig);
}
import globalGroups from './globalGroups';
import apps from './rawApps';
import type { RawSubscription } from './types';
const subsConfig: RawSubscription = {
id: 0,
@@ -39,15 +13,7 @@ const subsConfig: RawSubscription = {
'https://registry.npmmirror.com/@gkd-kit/subscription/latest/files/dist/gkd.version.json',
globalGroups,
categories,
apps: _.sortBy(apps, (a) => {
const showName = a.name || a.id;
const pyName = pinyin(showName, {
separator: '',
toneType: 'none',
});
if (pyName === showName) return showName;
return '\uFFFF' + pyName; // 让带拼音的全排在后面
}),
apps,
};
export default subsConfig;

View File

@@ -135,8 +135,31 @@ export const validSnapshotUrl = (s: string) => {
};
export const checkConfig = (newConfig: RawSubscription) => {
const globalGroups = newConfig.globalGroups || [];
globalGroups.forEach((g) => {
// check rules selector syntax
g.rules.forEach((r) => {
[r.matches, r.excludeMatches]
.map((m) => iArrayToArray(m))
.flat()
.forEach((selector) => {
try {
parseSelector(selector);
} catch (e) {
console.error({
message: 'invalid selector syntax',
groupKey: g.key,
selector,
});
throw e;
}
});
});
});
// check duplicated group key
newConfig.apps?.forEach((app) => {
const apps = newConfig.apps || [];
apps.forEach((app) => {
const deprecatedKeys = app.deprecatedKeys || [];
const keys = new Set<number>();
app.groups?.forEach((g) => {
@@ -177,13 +200,8 @@ export const checkConfig = (newConfig: RawSubscription) => {
ruleKeys.add(r.key);
}
});
});
});
// check slector syntax
newConfig.apps?.forEach((app) => {
app.groups?.forEach((g) => {
if (!g.rules) return;
// check rules selector syntax
const rules = iArrayToArray(g.rules).map((r) => {
if (typeof r == 'string') {
return { matches: r };
@@ -208,12 +226,8 @@ export const checkConfig = (newConfig: RawSubscription) => {
}
});
});
});
});
// check snapshotUrls
newConfig.apps?.forEach((app) => {
app.groups?.forEach((g) => {
// check snapshotUrls
iArrayToArray(g.snapshotUrls).forEach((u) => {
if (!validSnapshotUrl(u)) {
console.error({
@@ -243,6 +257,7 @@ export const checkConfig = (newConfig: RawSubscription) => {
});
});
});
const newKeys = Object.keys(newConfig) as (keyof RawSubscription)[];
if (newKeys.some((s) => !sortKeys.includes(s))) {
console.log({

View File

@@ -1,5 +1,22 @@
import apps from './rawApps';
import type { RawGlobalGroup } from './types';
const diabledAppIds = [
'com.android.systemui',
'com.miui.aod',
'com.miui.home',
'com.android.launcher.Launcher',
'com.bbk.launcher2.Launcher',
'com.huawei.android.launcher.unihome.UniHomeLauncher',
];
// 如果应用规则已有开屏广告一类的规则, 则在全局规则禁用此应用
diabledAppIds.push(
...apps
.filter((a) => a.groups.some((g) => g.name.startsWith('开屏广告')))
.map((a) => a.id),
);
const globalGroups: RawGlobalGroup[] = [
{
key: 0,
@@ -23,20 +40,7 @@ const globalGroups: RawGlobalGroup[] = [
action: 'clickCenter',
},
],
apps: [
{
id: 'com.android.systemui',
enable: false,
},
{
id: 'com.miui.aod',
enable: false,
},
{
id: 'com.miui.home',
enable: false,
},
],
apps: diabledAppIds.map((id) => ({ id, enable: false })),
},
];
export default globalGroups;

39
src/rawApps.ts Normal file
View File

@@ -0,0 +1,39 @@
import _ from 'lodash';
import path from 'node:path';
import url from 'node:url';
import picocolors from 'picocolors';
import { pinyin } from 'pinyin-pro';
import { walk } from './file';
import type { RawApp } from './types';
const rawApps: RawApp[] = [];
for await (const tsFp of walk(process.cwd() + '/src/apps')) {
const mod: { default: RawApp } = await import(url.pathToFileURL(tsFp).href);
const appConfig = mod.default;
if (path.basename(tsFp, '.ts') != appConfig.id) {
throw new Error(
`${picocolors.blue(
tsFp,
)} file basename is not equal to its app id ${picocolors.blue(
appConfig.id,
)} `,
);
}
appConfig.groups?.forEach((g) => {
if (!g.name.startsWith('开屏广告')) {
g.enable = false;
}
});
rawApps.push(appConfig);
}
const apps = _.sortBy(rawApps, (a) => {
const showName = a.name || a.id;
const pyName = pinyin(showName, {
separator: '',
toneType: 'none',
});
if (pyName === showName) return showName;
return '\uFFFF' + pyName; // 让带拼音的全排在后面
});
export default apps;