From b11921edcee4c0d270b94bdd66bb1dffb40a1035 Mon Sep 17 00:00:00 2001 From: renxia Date: Wed, 24 Jan 2024 21:37:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9=20ruleInclude=20=E5=92=8C=20ruleExclude=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BB=85=E5=BC=80=E5=90=AF=E6=88=96=E7=A6=81?= =?UTF-8?q?=E7=94=A8=E9=83=A8=E5=88=86=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/lib/getConfig.ts | 6 +++--- src/lib/rulesManage.ts | 8 ++++++-- typings/index.d.ts | 6 +++++- w2.x-scripts.config.sample.js | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1c76118..763fab2 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ w2 start -M capture 配置文件名称为 `w2.x-scripts.config.js`,默认会在当前目录和用户 Home 目录下查找。 -各配置项及详细说明请参考类型定义[W2CookiesConfig]('./index.d.ts')。 +各配置项及详细说明请参考类型定义[W2XScriptsConfig]('./index.d.ts')。 ## 脚本规则的编写 diff --git a/src/lib/getConfig.ts b/src/lib/getConfig.ts index 4d782d4..b4d27ee 100644 --- a/src/lib/getConfig.ts +++ b/src/lib/getConfig.ts @@ -5,7 +5,7 @@ * @LastEditTime: 2024-01-22 14:30:54 * @Description: */ -import type { W2CookiesConfig } from '../../typings'; +import type { W2XScriptsConfig } from '../../typings'; import { existsSync, readdirSync, statSync } from 'node:fs'; import { basename, resolve } from 'node:path'; import { homedir } from 'node:os'; @@ -13,7 +13,7 @@ import { assign, color } from '@lzwme/fe-utils'; import { logger } from './helper'; import { rulesManage } from './rulesManage'; -const config: W2CookiesConfig = { +const config: W2XScriptsConfig = { debug: Boolean(process.env.DEBUG), logType: process.env.LOG_TYPE as never, qlHost: process.env.QL_HOST || 'http://127.0.0.1:5700', @@ -68,7 +68,7 @@ export function getConfig(useCache = true) { if (Array.isArray(d)) d.forEach(d => allRules.add(d)); else allRules.add(d); }); - rulesManage.classifyRules([...allRules], !isLoaded); + rulesManage.classifyRules([...allRules], config, !isLoaded); isLoaded = true; } diff --git a/src/lib/rulesManage.ts b/src/lib/rulesManage.ts index ef097bf..9eecea7 100644 --- a/src/lib/rulesManage.ts +++ b/src/lib/rulesManage.ts @@ -2,7 +2,7 @@ import { existsSync, readdirSync, statSync } from 'node:fs'; import { resolve } from 'node:path'; import { color } from '@lzwme/fe-utils'; import { logger } from './helper'; -import type { RuleRunOnType, RuleItem } from '../../typings'; +import type { RuleRunOnType, RuleItem, W2XScriptsConfig } from '../../typings'; const { green, cyan, magenta, magentaBright, greenBright } = color; const RulesCache: Partial>> = { all: new Map() }; @@ -34,13 +34,17 @@ function ruleFormat(rule: RuleItem) { return rule; } -function classifyRules(rules: RuleItem[], isInit = false) { +function classifyRules(rules: RuleItem[], config: W2XScriptsConfig, isInit = false) { if (isInit) Object.values(RulesCache).forEach(cache => cache.clear()); + const { ruleInclude = [], ruleExclude = [] } = config; rules.forEach((rule, idx) => { if (!rule || !ruleFormat(rule)) return; RulesCache.all.set(rule.ruleId, rule); + if (ruleExclude.includes(rule.ruleId)) return; + if (ruleInclude.length && !ruleInclude.includes(rule.ruleId)) return; + if (rule.disabled) { logger.log(`规则已禁用: [${rule.on}][${rule.ruleId}][${rule.desc}]`); if (RulesCache[rule.on]?.has(rule.ruleId)) RulesCache[rule.on]!.delete(rule.ruleId); diff --git a/typings/index.d.ts b/typings/index.d.ts index ad85329..12998dc 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -9,7 +9,7 @@ type IncomingHttpHeaders = import('http').IncomingHttpHeaders; -export interface W2CookiesConfig { +export interface W2XScriptsConfig { /** 是否开启调试模式。默认读取环境变量 DEBUG */ debug?: boolean; /** 日志级别。默认为 info */ @@ -28,6 +28,10 @@ export interface W2CookiesConfig { rules?: RuleItem[]; /** 指定规则集文件路径或所在目录,尝试从该列表加载自定义的规则集 */ ruleDirs?: string[]; + /** 启用的 ruleId。若设置,则仅在该列表中的 ruleId 会启用 */ + ruleInclude?: string[]; + /** 排除/禁用的 ruleId。若设置,则在该列表中的 ruleId 会被过滤 */ + ruleExclude?: string[]; } // export type RuleType = 'saveCookie' | 'mock' | 'modify'; diff --git a/w2.x-scripts.config.sample.js b/w2.x-scripts.config.sample.js index 3151b09..77e487b 100644 --- a/w2.x-scripts.config.sample.js +++ b/w2.x-scripts.config.sample.js @@ -1,4 +1,4 @@ -/** @type {import('@lzwme/whistle.x-scripts').W2CookiesConfig} */ +/** @type {import('@lzwme/whistle.x-scripts').W2XScriptsConfig} */ const config = { // debug: false, // logType: 'info',