Skip to content

Commit

Permalink
feat: 新增配置项 ruleInclude 和 ruleExclude,支持仅开启或禁用部分规则
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Jan 24, 2024
1 parent aededaf commit b11921e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ w2 start -M capture

配置文件名称为 `w2.x-scripts.config.js`,默认会在当前目录和用户 Home 目录下查找。

各配置项及详细说明请参考类型定义[W2CookiesConfig]('./index.d.ts')
各配置项及详细说明请参考类型定义[W2XScriptsConfig]('./index.d.ts')

## 脚本规则的编写

Expand Down
6 changes: 3 additions & 3 deletions src/lib/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
* @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';
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',
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions src/lib/rulesManage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<RuleRunOnType | 'all', Map<string, RuleItem>>> = { all: new Map() };
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

type IncomingHttpHeaders = import('http').IncomingHttpHeaders;

export interface W2CookiesConfig {
export interface W2XScriptsConfig {
/** 是否开启调试模式。默认读取环境变量 DEBUG */
debug?: boolean;
/** 日志级别。默认为 info */
Expand All @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion w2.x-scripts.config.sample.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('@lzwme/whistle.x-scripts').W2CookiesConfig} */
/** @type {import('@lzwme/whistle.x-scripts').W2XScriptsConfig} */
const config = {
// debug: false,
// logType: 'info',
Expand Down

0 comments on commit b11921e

Please sign in to comment.