qiyu-cli 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +467 -2
- package/dist/cli.js +248 -0
- package/dist/commands/api.js +770 -0
- package/dist/commands/auth.js +349 -0
- package/dist/commands/credentials-source.js +70 -0
- package/dist/commands/db.js +503 -0
- package/dist/commands/local-fallback-wiring.js +67 -0
- package/dist/commands/local-fallback.js +34 -0
- package/dist/commands/query.js +564 -0
- package/dist/commands/skill-specs.js +64 -0
- package/dist/commands/skill.js +908 -0
- package/dist/commands/spec-types.js +145 -0
- package/dist/commands/stats.js +299 -0
- package/dist/commands/store-flags.js +56 -0
- package/dist/commands/store.js +258 -0
- package/dist/commands/sync-run.js +479 -0
- package/dist/commands/sync.js +757 -0
- package/dist/commands/tree.js +27 -0
- package/dist/core/config.js +259 -0
- package/dist/core/credentials.js +71 -0
- package/dist/core/edition.js +28 -0
- package/dist/core/envelope.js +201 -0
- package/dist/core/errors.js +356 -0
- package/dist/core/http.js +348 -0
- package/dist/core/ratelimit.js +126 -0
- package/dist/core/registry/index.js +212 -0
- package/dist/core/registry/params/ai.knowledge.addKnowledge.json +88 -0
- package/dist/core/registry/params/ai.knowledge.deleteKnowledges.json +46 -0
- package/dist/core/registry/params/ai.knowledge.editChannelAnswer.json +60 -0
- package/dist/core/registry/params/ai.knowledge.editKnowledge.json +95 -0
- package/dist/core/registry/params/ai.knowledge.editKnowledgeNoSimilars.json +102 -0
- package/dist/core/registry/params/ai.knowledge.getCategoryTree.json +39 -0
- package/dist/core/registry/params/ai.knowledge.getDetail.json +46 -0
- package/dist/core/registry/params/ai.knowledge.search.json +67 -0
- package/dist/core/registry/params/ai.statistic.transferHuman.json +60 -0
- package/dist/core/registry/params/category.list.json +39 -0
- package/dist/core/registry/params/chat.queue.group.query.json +39 -0
- package/dist/core/registry/params/crm.modifyOrder.json +48 -0
- package/dist/core/registry/params/crm.modifyUser.json +48 -0
- package/dist/core/registry/params/crm.syncCrmInfo.json +46 -0
- package/dist/core/registry/params/crm.tagChange.json +74 -0
- package/dist/core/registry/params/data.overview.ipcc.json +32 -0
- package/dist/core/registry/params/data.overview.session.json +53 -0
- package/dist/core/registry/params/evaluation.setting.get.json +39 -0
- package/dist/core/registry/params/event.applyStaff.json +116 -0
- package/dist/core/registry/params/event.evaluate.json +74 -0
- package/dist/core/registry/params/event.inviteEvaluate.json +81 -0
- package/dist/core/registry/params/event.qcResult.json +166 -0
- package/dist/core/registry/params/event.queryQueueStatus.json +39 -0
- package/dist/core/registry/params/event.queueEnter.json +40 -0
- package/dist/core/registry/params/event.queueLeave.json +46 -0
- package/dist/core/registry/params/event.quitQueue.json +39 -0
- package/dist/core/registry/params/event.sessionChange.json +95 -0
- package/dist/core/registry/params/event.sessionData.json +452 -0
- package/dist/core/registry/params/event.sessionEnd.json +117 -0
- package/dist/core/registry/params/event.sessionStart.json +109 -0
- package/dist/core/registry/params/event.updateUInfo.json +46 -0
- package/dist/core/registry/params/export.session.check.json +7 -0
- package/dist/core/registry/params/export.session.json +7 -0
- package/dist/core/registry/params/export.session.one.json +7 -0
- package/dist/core/registry/params/export.session.one.message.json +7 -0
- package/dist/core/registry/params/get_call_order.json +50 -0
- package/dist/core/registry/params/get_call_user_info.json +36 -0
- package/dist/core/registry/params/get_order.json +50 -0
- package/dist/core/registry/params/get_token.json +22 -0
- package/dist/core/registry/params/get_user_info.json +36 -0
- package/dist/core/registry/params/icoutcall.callIntercept.list.json +32 -0
- package/dist/core/registry/params/icoutcall.dialogFlow.fuzzySearch.json +53 -0
- package/dist/core/registry/params/icoutcall.dialogFlow.getDialogFlowList.json +32 -0
- package/dist/core/registry/params/icoutcall.get.customer.json +39 -0
- package/dist/core/registry/params/icoutcall.get.jobdetail.json +39 -0
- package/dist/core/registry/params/icoutcall.get.jobproperties.json +39 -0
- package/dist/core/registry/params/icoutcall.history.seat.json +67 -0
- package/dist/core/registry/params/icoutcall.history.team.json +67 -0
- package/dist/core/registry/params/icoutcall.job.create.json +53 -0
- package/dist/core/registry/params/icoutcall.job.importcustomer.json +46 -0
- package/dist/core/registry/params/icoutcall.job.pause.json +39 -0
- package/dist/core/registry/params/icoutcall.job.readdcustomer.json +109 -0
- package/dist/core/registry/params/icoutcall.job.start.json +39 -0
- package/dist/core/registry/params/icoutcall.job.terminate.json +39 -0
- package/dist/core/registry/params/icoutcall.phone.getLineList.json +32 -0
- package/dist/core/registry/params/ipcc.calltask.add.auto.json +117 -0
- package/dist/core/registry/params/ipcc.calltask.add.byCustom.json +96 -0
- package/dist/core/registry/params/ipcc.calltask.add.byRelation.json +82 -0
- package/dist/core/registry/params/ipcc.calltask.add.toGroup.json +89 -0
- package/dist/core/registry/params/ipcc.calltask.add.toStaff.json +89 -0
- package/dist/core/registry/params/ipcc.calltask.get.json +47 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.add.json +54 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.delete.json +40 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.get.json +54 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.update.json +40 -0
- package/dist/core/registry/params/ipcc.customfield.template.get.json +47 -0
- package/dist/core/registry/params/ipcc.login.json +47 -0
- package/dist/core/registry/params/kefu.add.json +130 -0
- package/dist/core/registry/params/kefu.delete.json +39 -0
- package/dist/core/registry/params/kefu.update.json +137 -0
- package/dist/core/registry/params/kefu.v2.query.json +39 -0
- package/dist/core/registry/params/kefuGroup.add.json +39 -0
- package/dist/core/registry/params/kefuGroup.delete.json +39 -0
- package/dist/core/registry/params/kefuGroup.update.json +46 -0
- package/dist/core/registry/params/kefuGroup.updateKefus.json +46 -0
- package/dist/core/registry/params/merchant.account.login.json +53 -0
- package/dist/core/registry/params/merchant.changePassword.json +46 -0
- package/dist/core/registry/params/merchant.create.json +81 -0
- package/dist/core/registry/params/merchant.disable.json +39 -0
- package/dist/core/registry/params/merchant.enable.json +39 -0
- package/dist/core/registry/params/merchant.list.json +46 -0
- package/dist/core/registry/params/merchant.query.json +46 -0
- package/dist/core/registry/params/merchant.update.json +74 -0
- package/dist/core/registry/params/message.send.image.json +53 -0
- package/dist/core/registry/params/message.send.json +53 -0
- package/dist/core/registry/params/message.send.voice.json +53 -0
- package/dist/core/registry/params/message.sendFile.json +39 -0
- package/dist/core/registry/params/message.uploadFile.json +39 -0
- package/dist/core/registry/params/online.session.category.list.json +39 -0
- package/dist/core/registry/params/online.session.customfield.template.json +39 -0
- package/dist/core/registry/params/online.session.customfield.update.json +74 -0
- package/dist/core/registry/params/order.upload.json +40 -0
- package/dist/core/registry/params/push.msg.image.json +74 -0
- package/dist/core/registry/params/push.msg.richText.json +74 -0
- package/dist/core/registry/params/push.msg.text.json +74 -0
- package/dist/core/registry/params/receive.message.json +89 -0
- package/dist/core/registry/params/robot.data.getQuestionsByCategoryName.json +67 -0
- package/dist/core/registry/params/robot.data.knowledge.json +46 -0
- package/dist/core/registry/params/robot.data.kpoint.json +46 -0
- package/dist/core/registry/params/robot.data.queryStandardByEffectiveQuestion.json +46 -0
- package/dist/core/registry/params/robot.data.queryStandardQuestionList.json +46 -0
- package/dist/core/registry/params/robot.evaluation.comment.json +67 -0
- package/dist/core/registry/params/robot.evaluation.setting.json +46 -0
- package/dist/core/registry/params/robot.session.assist.json +74 -0
- package/dist/core/registry/params/robot.session.chat.json +74 -0
- package/dist/core/registry/params/robot.session.close.json +53 -0
- package/dist/core/registry/params/robot.session.commonQuestions.json +74 -0
- package/dist/core/registry/params/robot.session.create.json +81 -0
- package/dist/core/registry/params/robot.session.evaluation.json +67 -0
- package/dist/core/registry/params/robot.session.msgAssociate.json +46 -0
- package/dist/core/registry/params/robot.session.msgEvaluation.json +53 -0
- package/dist/core/registry/params/robot.session.msgPredict.json +74 -0
- package/dist/core/registry/params/robot.session.transfer.json +36 -0
- package/dist/core/registry/params/robot.session.uploadFile.json +32 -0
- package/dist/core/registry/params/robot.statistic.summary.json +53 -0
- package/dist/core/registry/params/robot.transfer.config.json +15 -0
- package/dist/core/registry/params/session.user.profile.json +39 -0
- package/dist/core/registry/params/settings.wxTemplates.json +46 -0
- package/dist/core/registry/params/smstask.create.json +53 -0
- package/dist/core/registry/params/smstask.status.json +39 -0
- package/dist/core/registry/params/staff.group.list.new.json +39 -0
- package/dist/core/registry/params/staff.group.members.new.json +46 -0
- package/dist/core/registry/params/staff.list.new.json +46 -0
- package/dist/core/registry/params/staff.login.json +47 -0
- package/dist/core/registry/params/statistic.overview.json +61 -0
- package/dist/core/registry/params/statistic.performance.json +82 -0
- package/dist/core/registry/params/statistic.salaryDetail.json +54 -0
- package/dist/core/registry/params/statistic.satisfaction.report.json +68 -0
- package/dist/core/registry/params/statistic.staffAttendance.json +75 -0
- package/dist/core/registry/params/statistic.staffquality.json +75 -0
- package/dist/core/registry/params/statistic.staffworklod.json +68 -0
- package/dist/core/registry/params/tag.all.json +32 -0
- package/dist/core/registry/params/tag.batchUpdate.json +32 -0
- package/dist/core/registry/params/tag.update.json +60 -0
- package/dist/core/registry/params/ticket.apply.json +46 -0
- package/dist/core/registry/params/ticket.batch.modify.json +53 -0
- package/dist/core/registry/params/ticket.create.json +151 -0
- package/dist/core/registry/params/ticket.filter.count.json +46 -0
- package/dist/core/registry/params/ticket.filter.list.json +46 -0
- package/dist/core/registry/params/ticket.finish.json +60 -0
- package/dist/core/registry/params/ticket.followers.update.json +60 -0
- package/dist/core/registry/params/ticket.list.json +74 -0
- package/dist/core/registry/params/ticket.log.json +39 -0
- package/dist/core/registry/params/ticket.modify.json +74 -0
- package/dist/core/registry/params/ticket.new.detail.json +39 -0
- package/dist/core/registry/params/ticket.reminder.json +60 -0
- package/dist/core/registry/params/ticket.reopen.json +67 -0
- package/dist/core/registry/params/ticket.reply.json +60 -0
- package/dist/core/registry/params/ticket.search.json +116 -0
- package/dist/core/registry/params/ticket.template.fields.json +39 -0
- package/dist/core/registry/params/ticket.template.list.json +39 -0
- package/dist/core/registry/params/ticket.transfer.json +74 -0
- package/dist/core/registry/params/track.event.set.json +32 -0
- package/dist/core/registry/params/wecom.robot.switch.json +43 -0
- package/dist/core/registry/params.js +84 -0
- package/dist/core/registry/specs.json +2172 -0
- package/dist/core/registry/validate.json +11 -0
- package/dist/core/registry/validation-overrides.js +98 -0
- package/dist/core/sanitize.js +24 -0
- package/dist/core/sign.js +21 -0
- package/dist/core/time.js +194 -0
- package/dist/core/validate.js +140 -0
- package/dist/output.js +263 -0
- package/dist/postinstall.js +178 -0
- package/dist/query/errors.js +395 -0
- package/dist/query/mask.js +368 -0
- package/dist/query/metric-catalog.js +85 -0
- package/dist/query/sandbox.js +544 -0
- package/dist/query/table-query.js +297 -0
- package/dist/query/time-shape.js +44 -0
- package/dist/schedule/install.js +377 -0
- package/dist/skill/check.js +429 -0
- package/dist/skill/detect.js +371 -0
- package/dist/skill/install.js +1202 -0
- package/dist/skill/paths.js +343 -0
- package/dist/skill/qiyu/SKILL.md +92 -0
- package/dist/skill/qiyu/errors.md +55 -0
- package/dist/skill/qiyu/forbidden.md +52 -0
- package/dist/skill/qiyu/metrics.md +65 -0
- package/dist/skill/qiyu/protocols.md +55 -0
- package/dist/skill/qiyu/setup.md +50 -0
- package/dist/skill/refs.js +379 -0
- package/dist/store/config.js +137 -0
- package/dist/store/duckdb.js +321 -0
- package/dist/store/index.js +18 -0
- package/dist/store/retry.js +36 -0
- package/dist/store/schema.js +419 -0
- package/dist/store/types.js +4 -0
- package/dist/sync/day.js +8 -0
- package/dist/sync/engine.js +818 -0
- package/dist/sync/rate-gate.js +95 -0
- package/dist/sync/registry.js +22 -0
- package/dist/sync/state.js +114 -0
- package/dist/sync/tasks/dim.js +186 -0
- package/dist/sync/tasks/knowledge.js +252 -0
- package/dist/sync/tasks/pace.js +66 -0
- package/dist/sync/tasks/pipeline.js +321 -0
- package/dist/sync/tasks/reports.js +448 -0
- package/dist/sync/tasks/sessions.js +188 -0
- package/dist/sync/tasks/tickets.js +219 -0
- package/dist/sync/types.js +1 -0
- package/dist/sync/windowing.js +56 -0
- package/package.json +52 -4
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
// src/schedule/install.ts —— D-T14:系统调度条目生成(spec §7.6 / 设计稿 §7.6)。
|
|
2
|
+
// 平台条目:darwin=launchd plist / linux=systemd unit+timer(crontab 兜底行在 notes)/
|
|
3
|
+
// win32=schtasks 命令行。**平台检测与外部命令执行是注入点**(__setPlatform/__setRun,
|
|
4
|
+
// 沿 reports.ts __setSleep 的测试注入惯例)——命令层永远经 currentPlatform()/
|
|
5
|
+
// runScheduleCommand() 取用,不直接触 process.platform / child_process。
|
|
6
|
+
//
|
|
7
|
+
// cron 换算的三条诚实性约束:
|
|
8
|
+
// ① 解析不出的 cron 显式抛 usage(绝不静默装一个错误时刻的计划);
|
|
9
|
+
// ② cron 的「日 + 周」同时受限是 OR 语义 —— launchd 用 array of dicts、
|
|
10
|
+
// systemd 用两条 OnCalendar 表达(单 dict/单行是 AND,会静默收窄触发面);
|
|
11
|
+
// ③ win32 的 schtasks 只能表达一个子集(每 N 分 / 每 N 时 / 每日 / 每周 / 每月),
|
|
12
|
+
// 子集外显式失败并指引手工创建。
|
|
13
|
+
import { execFileSync } from 'node:child_process';
|
|
14
|
+
import { existsSync } from 'node:fs';
|
|
15
|
+
/** 平台检测的显式失败形态(usage):未知平台不猜测、不静默回退。 */
|
|
16
|
+
export function detectPlatform() {
|
|
17
|
+
const p = process.platform;
|
|
18
|
+
if (p === 'darwin' || p === 'linux' || p === 'win32')
|
|
19
|
+
return p;
|
|
20
|
+
throw usageError(`当前平台(${p})不支持 sync install/uninstall:支持 darwin / linux / win32`);
|
|
21
|
+
}
|
|
22
|
+
const defaultRun = (argv) => {
|
|
23
|
+
// 子进程 stdout 不透传(调度器命令的输出对 Agent 无价值);stderr 保留便于排障。
|
|
24
|
+
execFileSync(argv[0], argv.slice(1), { stdio: ['ignore', 'ignore', 'inherit'] });
|
|
25
|
+
};
|
|
26
|
+
let platformFn = detectPlatform;
|
|
27
|
+
let runFn = defaultRun;
|
|
28
|
+
/** 仅测试用:替换平台检测(命令层经 currentPlatform() 间接取用)。 */
|
|
29
|
+
export function __setPlatform(fn) { platformFn = fn; }
|
|
30
|
+
/** 仅测试用:替换外部命令执行(默认 execFileSync)。 */
|
|
31
|
+
export function __setRun(fn) { runFn = fn; }
|
|
32
|
+
export function __resetScheduleDeps() { platformFn = detectPlatform; runFn = defaultRun; }
|
|
33
|
+
export function currentPlatform() { return platformFn(); }
|
|
34
|
+
export function runScheduleCommand(argv) { runFn(argv); }
|
|
35
|
+
function usageError(msg) {
|
|
36
|
+
const e = new Error(msg);
|
|
37
|
+
e.kind = 'usage';
|
|
38
|
+
return e;
|
|
39
|
+
}
|
|
40
|
+
const FIELDS = [
|
|
41
|
+
{ name: 'minute', min: 0, max: 59 },
|
|
42
|
+
{ name: 'hour', min: 0, max: 23 },
|
|
43
|
+
{ name: 'day', min: 1, max: 31 },
|
|
44
|
+
{ name: 'month', min: 1, max: 12 },
|
|
45
|
+
// cron 的周字段:0=周日;7 亦是周日(归一到 0)
|
|
46
|
+
{ name: 'weekday', min: 0, max: 6 },
|
|
47
|
+
];
|
|
48
|
+
const WILDCARD = (min, max) => ({ values: fullRange(min, max), wildcard: true });
|
|
49
|
+
function fullRange(min, max) {
|
|
50
|
+
const out = [];
|
|
51
|
+
for (let i = min; i <= max; i++)
|
|
52
|
+
out.push(i);
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
function parseField(src, def) {
|
|
56
|
+
const values = new Set();
|
|
57
|
+
for (const item of src.split(',')) {
|
|
58
|
+
if (item === '*')
|
|
59
|
+
return WILDCARD(def.min, def.max);
|
|
60
|
+
let m = /^\*\/(\d+)$/.exec(item);
|
|
61
|
+
if (m) {
|
|
62
|
+
const step = Number(m[1]);
|
|
63
|
+
if (step < 1)
|
|
64
|
+
throw usageError(`无法解析的 cron 字段(${def.name}):步进必须 ≥ 1(${item})`);
|
|
65
|
+
for (let v = def.min; v <= def.max; v += step)
|
|
66
|
+
values.add(v);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
m = /^(\d+)-(\d+)(?:\/(\d+))?$/.exec(item);
|
|
70
|
+
if (m) {
|
|
71
|
+
const a = Number(m[1]);
|
|
72
|
+
const b = Number(m[2]);
|
|
73
|
+
const step = m[3] === undefined ? 1 : Number(m[3]);
|
|
74
|
+
if (step < 1)
|
|
75
|
+
throw usageError(`无法解析的 cron 字段(${def.name}):步进必须 ≥ 1(${item})`);
|
|
76
|
+
if (a > b)
|
|
77
|
+
throw usageError(`无法解析的 cron 字段(${def.name}):范围逆序(${item})`);
|
|
78
|
+
if (a < def.min || b > def.max) {
|
|
79
|
+
throw usageError(`无法解析的 cron 字段(${def.name}):${item} 超出 ${def.min}-${def.max}(weekday 的 7 表示周日,请写 0)`);
|
|
80
|
+
}
|
|
81
|
+
for (let v = a; v <= b; v += step)
|
|
82
|
+
values.add(v);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (/^\d+$/.test(item)) {
|
|
86
|
+
const v = Number(item);
|
|
87
|
+
// weekday 允许 7(周日)并归一到 0 —— 常见书写习惯
|
|
88
|
+
const norm = def.name === 'weekday' && v === 7 ? 0 : v;
|
|
89
|
+
if (norm < def.min || norm > def.max) {
|
|
90
|
+
throw usageError(`无法解析的 cron 字段(${def.name}):${item} 超出 ${def.min}-${def.max}(weekday 的 7 表示周日,请写 0)`);
|
|
91
|
+
}
|
|
92
|
+
values.add(norm);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
throw usageError(`无法解析的 cron 字段(${def.name}):${item}(支持 * / 数字 / a-b / a-b/k / */k / 逗号列表;不支持月份与星期名称)`);
|
|
96
|
+
}
|
|
97
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
98
|
+
// 全值域 = 通配(语义等价,输出不展开成 60 个值)
|
|
99
|
+
const wildcard = sorted.length === def.max - def.min + 1;
|
|
100
|
+
return { values: sorted, wildcard };
|
|
101
|
+
}
|
|
102
|
+
/** 解析 5 段 cron;解析失败抛 usage(契约诚实性:绝不静默装错时刻的计划)。 */
|
|
103
|
+
export function parseCron(expr) {
|
|
104
|
+
const parts = expr.trim().split(/\s+/);
|
|
105
|
+
if (parts.length !== 5) {
|
|
106
|
+
throw usageError(`无法解析的 cron 表达式:必须是 5 段(分 时 日 月 周),实际 ${parts.length} 段:${expr}`);
|
|
107
|
+
}
|
|
108
|
+
const [minute, hour, day, month, weekday] = parts.map((p, i) => parseField(p, FIELDS[i]));
|
|
109
|
+
return { minute: minute, hour: hour, day: day, month: month, weekday: weekday };
|
|
110
|
+
}
|
|
111
|
+
const WEEKDAY_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
112
|
+
const pad2 = (n) => String(n).padStart(2, '0');
|
|
113
|
+
const pad2List = (f) => f.values.map(pad2).join(',');
|
|
114
|
+
function xmlEscape(s) {
|
|
115
|
+
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
116
|
+
}
|
|
117
|
+
function shellQuote(s) {
|
|
118
|
+
return /[\s"]/.test(s) ? `"${s.replace(/"/g, '\\"')}"` : s;
|
|
119
|
+
}
|
|
120
|
+
export const LAUNCHD_LABEL = 'com.qiyu-cli.sync';
|
|
121
|
+
export const SYSTEMD_NAMES = { service: 'qiyu-sync.service', timer: 'qiyu-sync.timer' };
|
|
122
|
+
export const SCHTASKS_TASK_NAME = 'qiyu-cli-sync';
|
|
123
|
+
/**
|
|
124
|
+
* 调度条目要运行的命令(sync run + 用户显式给出的存储定位参数)。
|
|
125
|
+
* ⚠️ 修复批 4 #4:`sync install --db /x.duckdb` 的前置探测用的是 /x.duckdb,若调度的
|
|
126
|
+
* 命令不带同一组参数,装出来的计划会跑**默认库** —— 探测的目标与调度执行的目标
|
|
127
|
+
* 必须是同一个库。只透传用户**显式给出**的存储参数(不给默认值,避免默认值漂移)。
|
|
128
|
+
*/
|
|
129
|
+
function runArgs(exec, storeArgs = []) {
|
|
130
|
+
return [...exec, 'sync', 'run', ...storeArgs];
|
|
131
|
+
}
|
|
132
|
+
/** launchd 单键的 XML:单值 <integer>,多值 <array>。 */
|
|
133
|
+
function launchdKey(key, f) {
|
|
134
|
+
const value = f.values.length === 1
|
|
135
|
+
? `<integer>${f.values[0]}</integer>`
|
|
136
|
+
: `<array>${f.values.map((v) => `<integer>${v}</integer>`).join('')}</array>`;
|
|
137
|
+
return `<key>${key}</key>${value}`;
|
|
138
|
+
}
|
|
139
|
+
function launchdCalendarXml(spec) {
|
|
140
|
+
const base = (extra) => {
|
|
141
|
+
let inner = '';
|
|
142
|
+
if (!spec.minute.wildcard)
|
|
143
|
+
inner += launchdKey('Minute', spec.minute);
|
|
144
|
+
if (!spec.hour.wildcard)
|
|
145
|
+
inner += launchdKey('Hour', spec.hour);
|
|
146
|
+
if (!spec.month.wildcard)
|
|
147
|
+
inner += launchdKey('Month', spec.month);
|
|
148
|
+
return `<dict>${inner}${extra}</dict>`;
|
|
149
|
+
};
|
|
150
|
+
// ⚠️ launchd 单 dict 内多键是 AND(同键数组是键内 OR);cron 日+周是 OR ——
|
|
151
|
+
// 同时受限拆成 array of two dicts。月/时/分的键内 OR 与 cron 一致,数组直通。
|
|
152
|
+
const dayR = !spec.day.wildcard;
|
|
153
|
+
const wdR = !spec.weekday.wildcard;
|
|
154
|
+
const dicts = [];
|
|
155
|
+
if (dayR)
|
|
156
|
+
dicts.push(base(launchdKey('Day', spec.day)));
|
|
157
|
+
if (wdR) {
|
|
158
|
+
// cron 0=周日 → launchd Weekday 1=周日 … 7=周六;多值 → 键内数组(launchd 键内 OR)
|
|
159
|
+
const wd = { values: spec.weekday.values.map((v) => v + 1), wildcard: false };
|
|
160
|
+
dicts.push(base(launchdKey('Weekday', wd)));
|
|
161
|
+
}
|
|
162
|
+
if (dicts.length === 0)
|
|
163
|
+
return base('');
|
|
164
|
+
if (dicts.length === 1)
|
|
165
|
+
return dicts[0];
|
|
166
|
+
return `<array>${dicts.join('')}</array>`;
|
|
167
|
+
}
|
|
168
|
+
function systemdOnCalendar(spec) {
|
|
169
|
+
const dow = spec.weekday.wildcard
|
|
170
|
+
? null
|
|
171
|
+
: spec.weekday.values.map((n) => WEEKDAY_NAMES[n]).join(',');
|
|
172
|
+
const month = spec.month.wildcard ? '*' : pad2List(spec.month);
|
|
173
|
+
const hour = spec.hour.wildcard ? '*' : pad2List(spec.hour);
|
|
174
|
+
const minute = spec.minute.wildcard ? '*' : pad2List(spec.minute);
|
|
175
|
+
const time = `${hour}:${minute}:00`;
|
|
176
|
+
const dateOf = (day) => `*-${month}-${day.wildcard ? '*' : pad2List(day)}`;
|
|
177
|
+
// ⚠️ systemd 单条 OnCalendar 内 DOW 与日期是 AND;cron 日+周是 OR —— 多行表达 OR。
|
|
178
|
+
const dayR = !spec.day.wildcard;
|
|
179
|
+
const wdR = !spec.weekday.wildcard;
|
|
180
|
+
if (dayR && wdR) {
|
|
181
|
+
return [`${dow} ${dateOf(WILDCARD(1, 31))} ${time}`, `${dateOf(spec.day)} ${time}`];
|
|
182
|
+
}
|
|
183
|
+
if (wdR)
|
|
184
|
+
return [`${dow} ${dateOf(spec.day)} ${time}`];
|
|
185
|
+
return [`${dateOf(spec.day)} ${time}`];
|
|
186
|
+
}
|
|
187
|
+
/** 值列是否为「从 0 起的等差数列」(cron 步进语义,可映射 schtasks /MO)。
|
|
188
|
+
* 修复批 4 #7:schtasks `/MO N` **不在每小时/每天边界重置**(cron 的步进写法在
|
|
189
|
+
* 每小时重置)——「每 7 分钟」在 cron 里每小时重置、在 schtasks 里不重置,装出来的
|
|
190
|
+
* 计划会漂移(第 2 小时从 07 分起、第 3 小时从 14 分起……),违反「绝不静默装出
|
|
191
|
+
* 错误时间的计划」。判据:`(fullMax + 1) % step !== 0` → 落 unmappable。
|
|
192
|
+
* 例:每 30 分钟 → 值列 [0,30],60 % 30 === 0 → 可映射;每 7 分钟 → [0,7,…,56],
|
|
193
|
+
* 60 % 7 !== 0 → 不可映射。小时侧同理(24 % step)。 */
|
|
194
|
+
function progressionStep(values, fullMax) {
|
|
195
|
+
if (values.length < 2)
|
|
196
|
+
return null;
|
|
197
|
+
if (values[0] !== 0)
|
|
198
|
+
return null;
|
|
199
|
+
const step = values[1] - values[0];
|
|
200
|
+
if (step < 2)
|
|
201
|
+
return null;
|
|
202
|
+
for (let i = 1; i < values.length; i++) {
|
|
203
|
+
if (values[i] - values[i - 1] !== step)
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
// 覆盖到值域末端(*/30 → [0,30] 收尾于 59 语义下不算,cron 步进覆盖 0..max-1)
|
|
207
|
+
if (values[values.length - 1] + step <= fullMax)
|
|
208
|
+
return null;
|
|
209
|
+
// 修复批 4 #7:步进必须整除值域宽度(schtasks 不重置 → 不整除即漂移)
|
|
210
|
+
if ((fullMax + 1) % step !== 0)
|
|
211
|
+
return null;
|
|
212
|
+
return step;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* cron → schtasks 计划形态(子集映射;子集外显式失败)。
|
|
216
|
+
* 支持:每 N 分钟 / 每 N 小时(步进或单值)、每日定时、每周定时、每月定时、全通配(每分钟)。
|
|
217
|
+
*/
|
|
218
|
+
function schtasksScheduleArgs(spec) {
|
|
219
|
+
const time = `${pad2(spec.hour.values[0])}:${pad2(spec.minute.values[0])}`;
|
|
220
|
+
const dateRestricted = !spec.day.wildcard || !spec.month.wildcard;
|
|
221
|
+
if (dateRestricted && !spec.day.wildcard && spec.month.wildcard
|
|
222
|
+
&& spec.weekday.wildcard && spec.minute.values.length === 1 && spec.hour.values.length === 1) {
|
|
223
|
+
return ['/SC', 'MONTHLY', '/D', spec.day.values.join(','), '/ST', time];
|
|
224
|
+
}
|
|
225
|
+
if (!spec.weekday.wildcard && spec.day.wildcard && spec.month.wildcard
|
|
226
|
+
&& spec.minute.values.length === 1 && spec.hour.values.length === 1) {
|
|
227
|
+
return ['/SC', 'WEEKLY', '/D', spec.weekday.values.map((n) => WEEKDAY_NAMES[n].toUpperCase()).join(','), '/ST', time];
|
|
228
|
+
}
|
|
229
|
+
if (spec.day.wildcard && spec.month.wildcard && spec.weekday.wildcard) {
|
|
230
|
+
if (spec.minute.wildcard && spec.hour.wildcard) {
|
|
231
|
+
return ['/SC', 'MINUTE', '/MO', '1'];
|
|
232
|
+
}
|
|
233
|
+
// 修复批 4 #8:`/SC HOURLY /MO N` 的判据是「固定分钟 + 小时步进」——
|
|
234
|
+
// `* */2 * * *`(每分钟、每 2 小时)不能映射:schtasks 没有「每分钟但只在
|
|
235
|
+
// 第 2 小时」的形态,强制映射会把语义收窄成「每 2 小时跑一次」(见下 unmappable)。
|
|
236
|
+
// `0 */2 * * *`(每 2 小时的第 0 分)→ `/SC HOURLY /MO 2 /ST 00:00`。
|
|
237
|
+
if (!spec.minute.wildcard && !spec.hour.wildcard && spec.minute.values.length === 1) {
|
|
238
|
+
const step = progressionStep(spec.hour.values, 23);
|
|
239
|
+
if (step !== null) {
|
|
240
|
+
return ['/SC', 'HOURLY', '/MO', String(step), '/ST', `00:${pad2(spec.minute.values[0])}`];
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// 每分钟 + 小时受限 → schtasks 无可等价形态(语义会收窄),显式拒绝
|
|
244
|
+
if (spec.minute.wildcard && !spec.hour.wildcard)
|
|
245
|
+
throw unmappable();
|
|
246
|
+
if (!spec.minute.wildcard && spec.hour.wildcard) {
|
|
247
|
+
const step = progressionStep(spec.minute.values, 59);
|
|
248
|
+
if (step !== null)
|
|
249
|
+
return ['/SC', 'MINUTE', '/MO', String(step)];
|
|
250
|
+
throw unmappable();
|
|
251
|
+
}
|
|
252
|
+
if (!spec.minute.wildcard && !spec.hour.wildcard
|
|
253
|
+
&& spec.minute.values.length === 1 && spec.hour.values.length === 1) {
|
|
254
|
+
return ['/SC', 'DAILY', '/ST', time];
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
throw unmappable();
|
|
258
|
+
}
|
|
259
|
+
function unmappable() {
|
|
260
|
+
return usageError('无法把该 cron 映射为 schtasks 计划(支持:每 N 分钟 / 每 N 小时 / 每日 / 每周 / 每月的固定时刻)。'
|
|
261
|
+
+ '请在 Windows 任务计划程序中手工创建,或换一个可映射的时刻');
|
|
262
|
+
}
|
|
263
|
+
function crontabLine(schedule, exec, storeArgs = []) {
|
|
264
|
+
return `${schedule} ${runArgs(exec, storeArgs).map(shellQuote).join(' ')}`;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* 生成安装计划(纯函数,不写 fs)。
|
|
268
|
+
* `storeArgs`(修复批 4 #4):命令层把用户显式给出的存储定位参数(--db/--profile/
|
|
269
|
+
* --backend)原样透传进来,进 runArgs —— 调度命令与前置探测同一库。缺省空数组 =
|
|
270
|
+
* 用户未给存储参数,调度命令不追加任何存储参数(默认值解析留给运行时,绝不在此
|
|
271
|
+
* 硬编码一个默认路径 —— 那会引入默认值漂移:命令层默认变了而调度条目不变)。
|
|
272
|
+
*/
|
|
273
|
+
export function planInstall(platform, schedule, opts) {
|
|
274
|
+
const spec = parseCron(schedule);
|
|
275
|
+
const run = runArgs(opts.exec, opts.storeArgs ?? []);
|
|
276
|
+
if (platform === 'darwin') {
|
|
277
|
+
const dir = `${opts.home}/Library/LaunchAgents`;
|
|
278
|
+
const path = `${dir}/${LAUNCHD_LABEL}.plist`;
|
|
279
|
+
const logsDir = `${opts.home}/Library/Logs`;
|
|
280
|
+
const argsXml = run.map((a) => `<string>${xmlEscape(a)}</string>`).join('');
|
|
281
|
+
const content = `<?xml version="1.0" encoding="UTF-8"?>
|
|
282
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
283
|
+
<plist version="1.0">
|
|
284
|
+
<dict>
|
|
285
|
+
<key>Label</key><string>${LAUNCHD_LABEL}</string>
|
|
286
|
+
<key>ProgramArguments</key>
|
|
287
|
+
<array>${argsXml}</array>
|
|
288
|
+
<key>StartCalendarInterval</key>${launchdCalendarXml(spec)}
|
|
289
|
+
<key>StandardOutPath</key><string>${xmlEscape(logsDir)}/qiyu-sync.log</string>
|
|
290
|
+
<key>StandardErrorPath</key><string>${xmlEscape(logsDir)}/qiyu-sync.err.log</string>
|
|
291
|
+
</dict>
|
|
292
|
+
</plist>
|
|
293
|
+
`;
|
|
294
|
+
return {
|
|
295
|
+
platform, schedule,
|
|
296
|
+
entries: [{ path, content }],
|
|
297
|
+
commands: [['launchctl', 'load', path]],
|
|
298
|
+
notes: ['launchd 在下次登录时自动加载 ~/Library/LaunchAgents/ 下的条目;立即生效请执行上面的 launchctl load 命令。',
|
|
299
|
+
'若已装过旧版本,先 launchctl unload 再覆盖。'],
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
if (platform === 'linux') {
|
|
303
|
+
const dir = `${opts.home}/.config/systemd/user`;
|
|
304
|
+
const servicePath = `${dir}/${SYSTEMD_NAMES.service}`;
|
|
305
|
+
const timerPath = `${dir}/${SYSTEMD_NAMES.timer}`;
|
|
306
|
+
const execLine = run.map(shellQuote).join(' ');
|
|
307
|
+
const onCalendar = systemdOnCalendar(spec);
|
|
308
|
+
const service = `[Unit]
|
|
309
|
+
Description=七鱼数据同步(qiyu sync run)
|
|
310
|
+
|
|
311
|
+
[Service]
|
|
312
|
+
Type=oneshot
|
|
313
|
+
ExecStart=${execLine}
|
|
314
|
+
`;
|
|
315
|
+
const timer = `[Unit]
|
|
316
|
+
Description=七鱼数据同步定时器
|
|
317
|
+
|
|
318
|
+
[Timer]
|
|
319
|
+
${onCalendar.map((c) => `OnCalendar=${c}`).join('\n')}
|
|
320
|
+
Persistent=true
|
|
321
|
+
|
|
322
|
+
[Install]
|
|
323
|
+
WantedBy=timers.target
|
|
324
|
+
`;
|
|
325
|
+
return {
|
|
326
|
+
platform, schedule,
|
|
327
|
+
entries: [
|
|
328
|
+
{ path: servicePath, content: service },
|
|
329
|
+
{ path: timerPath, content: timer },
|
|
330
|
+
],
|
|
331
|
+
commands: [
|
|
332
|
+
['systemctl', '--user', 'daemon-reload'],
|
|
333
|
+
['systemctl', '--user', 'enable', '--now', SYSTEMD_NAMES.timer],
|
|
334
|
+
],
|
|
335
|
+
notes: [`crontab 兜底(无 systemd 用户会话时手工可用):${crontabLine(schedule, opts.exec, opts.storeArgs ?? [])}`,
|
|
336
|
+
'安装后需执行上面的 systemctl 命令(daemon-reload + enable --now)定时器才会运行。'],
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
// win32:条目就是命令本身
|
|
340
|
+
const cmd = ['schtasks', '/Create', '/TN', SCHTASKS_TASK_NAME,
|
|
341
|
+
'/TR', run.map(shellQuote).join(' '), ...schtasksScheduleArgs(spec), '/F'];
|
|
342
|
+
return {
|
|
343
|
+
platform, schedule,
|
|
344
|
+
entries: [],
|
|
345
|
+
commands: [cmd],
|
|
346
|
+
notes: ['Windows 无文件条目:--yes 时直接执行 schtasks 命令;dry-run 仅打印。'],
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
/** 生成卸载计划(读 fs 只为报告条目存在性,dry-run 展示用;exists 可注入)。 */
|
|
350
|
+
export function planUninstall(platform, opts) {
|
|
351
|
+
const exists = opts.exists ?? ((p) => existsSync(p));
|
|
352
|
+
if (platform === 'darwin') {
|
|
353
|
+
const path = `${opts.home}/Library/LaunchAgents/${LAUNCHD_LABEL}.plist`;
|
|
354
|
+
return {
|
|
355
|
+
platform, remove: [{ path, exists: exists(path) }],
|
|
356
|
+
commands: [['launchctl', 'unload', path]],
|
|
357
|
+
notes: ['卸载次序:先 launchctl unload(停止已加载的代理),文件随后删除。'],
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
if (platform === 'linux') {
|
|
361
|
+
const dir = `${opts.home}/.config/systemd/user`;
|
|
362
|
+
const paths = [`${dir}/${SYSTEMD_NAMES.service}`, `${dir}/${SYSTEMD_NAMES.timer}`];
|
|
363
|
+
return {
|
|
364
|
+
platform, remove: paths.map((path) => ({ path, exists: exists(path) })),
|
|
365
|
+
commands: [
|
|
366
|
+
['systemctl', '--user', 'disable', '--now', SYSTEMD_NAMES.timer],
|
|
367
|
+
['systemctl', '--user', 'daemon-reload'],
|
|
368
|
+
],
|
|
369
|
+
notes: ['卸载次序:先 disable --now(停定时器),再删除 unit 文件并 daemon-reload。'],
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
platform, remove: [],
|
|
374
|
+
commands: [['schtasks', '/Delete', '/TN', SCHTASKS_TASK_NAME, '/F']],
|
|
375
|
+
notes: ['Windows:卸载即执行 schtasks /Delete(--yes 时执行)。'],
|
|
376
|
+
};
|
|
377
|
+
}
|