refactor: 重构为单向生成架构,CLI 零参数可用

- 迁移 examples/rules/ 到 src/rules/,resources/devices.json 到 src/devices.json
- 迁移 resources/fetch-devlist.js 到 src/tools/fetch-devlist.ts 并改造为 ESM
- CLI 简化为零参数:bun run pack 自动编译并输出 dist/miot_{timestamp}.bak
- 移除备份合并能力:删除 mergeCompiledRules、RuleReplaceStrategy、dry-run、replace 参数
- 编译器简化为从零生成,不再支持 baseBackup 合并
- 新增 fetch-devices 命令用于从中枢网关更新设备清单
- 新增 bn.js、elliptic 依赖
- 更新测试路径引用,移除依赖合并逻辑的测试用例
- 更新 README.md 反映新项目结构和命令
This commit is contained in:
2026-05-08 00:03:27 +08:00
parent 690ef8ce83
commit 63dc55fa5a
15 changed files with 2133 additions and 243 deletions

View File

@@ -1,10 +1,10 @@
import { describe, expect, test } from "bun:test";
import { readDeviceInventoryFile, readBackupFile } from "../src/index.ts";
import { readBackupFile } from "../src/index.ts";
import { collectRuleDefinitions, compileRule, compileWorkflow } from "../src/compiler.ts";
import examples, { conditionBeforeAction, eventTurnOn, mergedTriggers } from "../examples/rules/index.ts";
import examples, { conditionBeforeAction, eventTurnOn, mergedTriggers } from "../src/rules/index.ts";
const now = 1_760_000_000_000;
const devices = readDeviceInventoryFile("resources/devices.json");
const devices = (await import("../src/devices.json")).default;
describe("规则编译", () => {
test("单条事件触发开灯规则生成稳定 JSON", () => {
@@ -79,16 +79,8 @@ describe("规则编译", () => {
});
test("模块导出收集支持默认数组和命名规则", async () => {
const moduleExports = await import("../examples/rules/index.ts");
const moduleExports = await import("../src/rules/index.ts");
const definitions = collectRuleDefinitions(moduleExports);
expect(definitions.map((definition) => definition.name)).toEqual(examples.map((definition) => definition.name));
});
test("编译并追加到真实备份时保留原有规则", () => {
const base = readBackupFile("resources/备份2026_4_26 19_17_42.bak");
const result = compileWorkflow([eventTurnOn], base, { now, devices });
expect(result.diagnostics.filter((diagnostic) => diagnostic.severity === "error")).toEqual([]);
expect(result.backup.rules).toHaveLength(base.rules.length + 1);
expect(result.backup.rules.at(-1)?.cfg.userData.name).toBe("示例-事件触发开灯");
});
});