work-rhythm 0.0.0-stage → 0.1.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/.agents/notes/AGENTS.md +86 -0
- package/.agents/notes/decisions/2026-11-28-long-term-goals-ride-existing-tasks.md +36 -0
- package/.agents/notes/decisions/2026-11-29-building-a-workdir-means-cloning-the-template.md +25 -0
- package/.agents/notes/decisions/2026-11-29-find-workdir-cwd-first-then-registered.md +32 -0
- package/.agents/notes/decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md +48 -0
- package/.agents/notes/decisions/2026-11-30-cost-entry-writes-into-empty-cells-directly.md +25 -0
- package/.agents/notes/decisions/2026-11-30-dependencies-are-allowed-user-installs-missing-ones.md +30 -0
- package/.agents/notes/decisions/2026-11-30-markdown-is-the-record-html-is-only-a-view.md +25 -0
- package/.agents/notes/decisions/2026-11-30-message-channels-are-preset-in-the-skill.md +34 -0
- package/.agents/notes/decisions/2026-11-30-no-workdir-offers-to-create-one-by-default.md +35 -0
- package/.agents/notes/decisions/2026-11-30-paging-requires-coverage-only-when-enabled.md +27 -0
- package/.agents/notes/decisions/2026-11-30-pending-decisions-asked-in-a-chat-and-logged-briefly.md +39 -0
- package/.agents/notes/decisions/2026-11-30-push-via-personal-robot-webhook.md +33 -0
- package/.agents/notes/decisions/2026-11-30-scheduling-moves-to-a-local-service.md +37 -0
- package/.agents/notes/decisions/2026-11-30-skill-ships-with-its-template-in-a-zip.md +30 -0
- package/.agents/notes/decisions/2026-11-30-skill-stays-generic-user-provides-cli-or-mcp.md +25 -0
- package/.agents/notes/decisions/2026-12-01-everything-is-js-behind-work-rhythm.md +46 -0
- package/.agents/notes/decisions/2026-12-01-repo-is-not-a-workdir-templates-hold-workdir-files.md +29 -0
- package/.agents/notes/decisions/2026-12-01-repo-stays-generic-personal-data-lives-in-workdirs.md +32 -0
- package/.agents/notes/decisions/2026-12-01-workdir-is-not-a-git-repository.md +25 -0
- package/.agents/notes/pitfalls/2026-11-29-do-not-hand-roll-git-log.md +22 -0
- package/.agents/notes/pitfalls/2026-11-29-empty-or-partial-collector-output.md +23 -0
- package/.agents/notes/pitfalls/2026-11-29-skill-description-is-truncated.md +21 -0
- package/.agents/notes/pitfalls/2026-11-29-tool-shell-is-zsh.md +24 -0
- package/.agents/notes/pitfalls/2026-11-30-compare-timestamps-as-digits.md +22 -0
- package/.agents/notes/pitfalls/2026-11-30-message-search-lags-after-sending.md +18 -0
- package/.agents/notes/pitfalls/2026-11-30-rewriting-a-file-with-empty-output-after-tool-failure.md +20 -0
- package/.agents/notes/pitfalls/2026-11-30-secret-scan-false-positive-on-hyphenated-file-names.md +18 -0
- package/.agents/notes/pitfalls/2026-11-30-selftest-must-not-inherit-local-config.md +29 -0
- package/.agents/notes/pitfalls/2026-11-30-yzj-cli-list-limit-is-20.md +25 -0
- package/.agents/notes/pitfalls/2026-11-30-zsh-no-word-splitting-special-names.md +23 -0
- package/.agents/notes/pitfalls/2026-12-01-node-test-hooks-cannot-register-after-callbacks.md +22 -0
- package/.agents/notes/pitfalls/2026-12-01-spawning-node-test-inside-a-test-runs-nothing.md +18 -0
- package/.agents/skills/work-rhythm/SKILL.md +278 -0
- package/AGENTS.md +19 -0
- package/README.md +69 -2
- package/collectors/approval-yzj.js +114 -0
- package/collectors/calendar-yzj.js +48 -0
- package/collectors/file-activity.js +131 -0
- package/collectors/git-activity.js +105 -0
- package/collectors/im-yzj.js +151 -0
- package/collectors/linky-issues.js +88 -0
- package/collectors/local.env.example +52 -0
- package/docs/channels.md +95 -0
- package/docs/formats.md +285 -0
- package/docs/im-adapters.md +79 -0
- package/docs/scheduled-tasks.md +115 -0
- package/docs/sources.md +104 -0
- package/package.json +35 -4
- package/scripts/cli.js +101 -0
- package/scripts/cmd/check.js +330 -0
- package/scripts/cmd/collect.js +25 -0
- package/scripts/cmd/decisions.js +236 -0
- package/scripts/cmd/goals.js +145 -0
- package/scripts/cmd/init.js +49 -0
- package/scripts/cmd/listen.js +91 -0
- package/scripts/cmd/notify.js +42 -0
- package/scripts/cmd/pack.js +104 -0
- package/scripts/cmd/rhythm.js +102 -0
- package/scripts/cmd/selftest.js +23 -0
- package/scripts/cmd/sync.js +73 -0
- package/scripts/cmd/today.js +29 -0
- package/scripts/cmd/workdir.js +81 -0
- package/scripts/lib/agent.js +270 -0
- package/scripts/lib/bot.js +376 -0
- package/scripts/lib/collector.js +74 -0
- package/scripts/lib/config.js +250 -0
- package/scripts/lib/launchd.js +82 -0
- package/scripts/lib/log.js +22 -0
- package/scripts/lib/markdown.js +208 -0
- package/scripts/lib/notify.js +43 -0
- package/scripts/lib/rhythm.js +508 -0
- package/scripts/lib/robot.js +64 -0
- package/scripts/lib/run.js +151 -0
- package/scripts/lib/template.js +149 -0
- package/scripts/lib/workdir.js +98 -0
- package/scripts/test/check.test.js +279 -0
- package/scripts/test/collectors.test.js +574 -0
- package/scripts/test/config.test.js +118 -0
- package/scripts/test/decisions.test.js +129 -0
- package/scripts/test/fake-webhook.js +31 -0
- package/scripts/test/goals.test.js +109 -0
- package/scripts/test/harness.js +277 -0
- package/scripts/test/init.test.js +123 -0
- package/scripts/test/listen.test.js +362 -0
- package/scripts/test/notify.test.js +176 -0
- package/scripts/test/pack.test.js +141 -0
- package/scripts/test/rhythm.test.js +333 -0
- package/scripts/test/stubs.js +140 -0
- package/scripts/test/today.test.js +73 -0
- package/scripts/test/workdir.test.js +152 -0
- package/scripts/work-rhythm.js +18 -0
- package/tasks/alerts-morning.md +24 -0
- package/tasks/carryover.md +16 -0
- package/tasks/daily-plan.md +29 -0
- package/tasks/evening-review.md +27 -0
- package/tasks/linky-cost-check.md +13 -0
- package/tasks/linky-cost-fill.md +20 -0
- package/tasks/noon-checkin.md +15 -0
- package/tasks/weekly-rhythm.md +18 -0
- package/templates/00-config/profile.example.yaml +54 -0
- package/templates/00-config/sources.yaml +115 -0
- package/templates/03-carryover/CARRYOVER.md +14 -0
- package/templates/03-carryover/DECISIONS.md +10 -0
- package/templates/04-patterns/WORK_RHYTHM.md +16 -0
- package/templates/AGENTS.md +31 -0
- package/templates/README.md +73 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// 各处都要用的小工具:起子进程、日期、退出码、读写文件。只用 node 自带的能力。
|
|
2
|
+
'use strict';
|
|
3
|
+
const { spawnSync } = require('node:child_process');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
|
|
7
|
+
// 所有命令统一的退出码
|
|
8
|
+
const EXIT = { OK: 0, USAGE: 2, NOT_CONFIGURED: 3, MISSING_DEP: 4, FAILED: 5 };
|
|
9
|
+
|
|
10
|
+
// 命令没跑成:带着退出码抛出,入口统一把消息打给用户、按这个退出码结束
|
|
11
|
+
class Fail extends Error {
|
|
12
|
+
constructor(code, message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = 'Fail';
|
|
15
|
+
this.code = code;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 用法不对、参数不合法:消息打给用户,退出码 2
|
|
20
|
+
class UsageError extends Fail {
|
|
21
|
+
constructor(message) {
|
|
22
|
+
super(EXIT.USAGE, message);
|
|
23
|
+
this.name = 'UsageError';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 本地时间某一天的正午。用正午是为了算星期和相隔天数时不受夏令时影响
|
|
28
|
+
function noon(date) {
|
|
29
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(String(date || '').trim());
|
|
30
|
+
if (!m) return null;
|
|
31
|
+
const d = new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3]), 12);
|
|
32
|
+
// 2026-13-40 这种会被 Date 自动进位,要挡回去
|
|
33
|
+
if (d.getFullYear() !== Number(m[1]) || d.getMonth() !== Number(m[2]) - 1 || d.getDate() !== Number(m[3])) return null;
|
|
34
|
+
return d;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
38
|
+
const fmtDate = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
|
39
|
+
const fmtStamp = (d = new Date()) => `${fmtDate(d)} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
|
40
|
+
const today = () => fmtDate(new Date());
|
|
41
|
+
|
|
42
|
+
function parseDate(text, what = '日期') {
|
|
43
|
+
const d = noon(text);
|
|
44
|
+
if (!d) throw new UsageError(`无法识别${what}「${text}」(格式应为 YYYY-MM-DD)`);
|
|
45
|
+
return d;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function addDays(date, n) {
|
|
49
|
+
const d = noon(date);
|
|
50
|
+
d.setDate(d.getDate() + n);
|
|
51
|
+
return fmtDate(d);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// to 比 from 晚几天
|
|
55
|
+
function daysBetween(from, to) {
|
|
56
|
+
return Math.round((noon(to) - noon(from)) / 86400000);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const WEEKDAY_CN = ['日', '一', '二', '三', '四', '五', '六'];
|
|
60
|
+
const WEEKDAY_EN = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
61
|
+
const weekdayCn = (date) => WEEKDAY_CN[noon(date).getDay()];
|
|
62
|
+
const weekdayAbbr = (date) => WEEKDAY_EN[noon(date).getDay()];
|
|
63
|
+
|
|
64
|
+
// 起一个子进程并等它结束。不经过 shell:参数原样传过去,不会被空格、引号拆开
|
|
65
|
+
function run(command, args, { cwd, env = process.env, timeout, input, maxBuffer } = {}) {
|
|
66
|
+
let r;
|
|
67
|
+
try {
|
|
68
|
+
r = spawnSync(command, args, {
|
|
69
|
+
cwd, env, encoding: 'utf8', input, timeout: timeout ? timeout * 1000 : undefined,
|
|
70
|
+
killSignal: 'SIGTERM', maxBuffer: maxBuffer || 64 * 1024 * 1024,
|
|
71
|
+
});
|
|
72
|
+
} catch (error) {
|
|
73
|
+
return { code: null, error, stdout: '', stderr: '', timedOut: false };
|
|
74
|
+
}
|
|
75
|
+
if (r.error) return { code: null, error: r.error, stdout: r.stdout || '', stderr: r.stderr || '', timedOut: r.error.code === 'ETIMEDOUT' };
|
|
76
|
+
return { code: r.status, signal: r.signal, error: null, stdout: r.stdout || '', stderr: r.stderr || '', timedOut: false };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function isExecutable(file) {
|
|
80
|
+
try {
|
|
81
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
82
|
+
return fs.statSync(file).isFile();
|
|
83
|
+
} catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 命令在不在 PATH 上:在就返回它的完整路径,不在返回空串
|
|
89
|
+
function which(name, env = process.env) {
|
|
90
|
+
if (name.includes('/')) return isExecutable(name) ? path.resolve(name) : '';
|
|
91
|
+
for (const dir of (env.PATH || '').split(path.delimiter)) {
|
|
92
|
+
if (dir && isExecutable(path.join(dir, name))) return path.join(dir, name);
|
|
93
|
+
}
|
|
94
|
+
return '';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function readText(file) {
|
|
98
|
+
try {
|
|
99
|
+
return fs.readFileSync(file, 'utf8');
|
|
100
|
+
} catch {
|
|
101
|
+
return '';
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 先写到同目录的临时文件再改名:写一半出错也不会留下半份
|
|
106
|
+
function writeFileAtomic(file, content) {
|
|
107
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
108
|
+
const tmp = `${file}.tmp`;
|
|
109
|
+
fs.writeFileSync(tmp, content);
|
|
110
|
+
fs.renameSync(tmp, file);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function tail(text, max = 200) {
|
|
114
|
+
const s = String(text || '').trim();
|
|
115
|
+
return s.length > max ? s.slice(-max) : s;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 本地时间某一天 0 点的时间戳
|
|
119
|
+
function startOfDay(date) {
|
|
120
|
+
const d = noon(date);
|
|
121
|
+
d.setHours(0, 0, 0, 0);
|
|
122
|
+
return d.getTime();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 逐层扫目录,返回指定时间之后改过的普通文件(不跟符号链接)。名字在 prune 里的目录和文件跳过
|
|
126
|
+
function walkFiles(root, prune, sinceMs, out = []) {
|
|
127
|
+
let entries;
|
|
128
|
+
try {
|
|
129
|
+
entries = fs.readdirSync(root, { withFileTypes: true });
|
|
130
|
+
} catch {
|
|
131
|
+
return out;
|
|
132
|
+
}
|
|
133
|
+
for (const entry of entries) {
|
|
134
|
+
if (prune.has(entry.name)) continue;
|
|
135
|
+
const full = path.join(root, entry.name);
|
|
136
|
+
let st;
|
|
137
|
+
try {
|
|
138
|
+
st = fs.lstatSync(full);
|
|
139
|
+
} catch {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (st.isDirectory()) walkFiles(full, prune, sinceMs, out);
|
|
143
|
+
else if (st.isFile() && st.mtimeMs >= sinceMs) out.push(full);
|
|
144
|
+
}
|
|
145
|
+
return out;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
module.exports = {
|
|
149
|
+
EXIT, Fail, UsageError, noon, pad, fmtDate, fmtStamp, today, parseDate, addDays, daysBetween,
|
|
150
|
+
weekdayCn, weekdayAbbr, run, which, isExecutable, readText, writeFileAtomic, tail, startOfDay, walkFiles,
|
|
151
|
+
};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// 新工作目录里的东西从哪来,以及怎么把一个用过的目录清成空白的。
|
|
2
|
+
// 这个仓库不是工作目录:工作目录里独有的文件都在 templates/ 下,分两种:
|
|
3
|
+
// 共用的(SHARED):文档、任务指令、给 agent 的笔记和 skill、local.env 的样例——仓库里就有,工作目录和包里是同一份;
|
|
4
|
+
// 模板(TEMPLATE_FILES,在 templates/ 下):profile 的空白模板、sources.yaml 的清单、空白的遗留 / 决定 / 规律画像、工作目录的 AGENTS.md 和 README.md;
|
|
5
|
+
// 现生成的:profile.yaml(就是 profile.example.yaml)、sources.yaml(清单照搬,全部关着,引导时逐个确认再打开)、两个空目录 02-daily/ 和 05-goals/。
|
|
6
|
+
// 脚本和内置采集器不进工作目录:它们在包里,sources.yaml 用 id 引用。升级就是升级这个包
|
|
7
|
+
'use strict';
|
|
8
|
+
const fs = require('node:fs');
|
|
9
|
+
const path = require('node:path');
|
|
10
|
+
const { readText, walkFiles } = require('./run');
|
|
11
|
+
|
|
12
|
+
const SHARED = ['docs', 'tasks', '.agents', 'collectors/local.env.example'];
|
|
13
|
+
const TEMPLATE_FILES = ['00-config/profile.example.yaml', '00-config/sources.yaml', '03-carryover/CARRYOVER.md', '03-carryover/DECISIONS.md', '04-patterns/WORK_RHYTHM.md', 'AGENTS.md', 'README.md'];
|
|
14
|
+
const BLANK = ['03-carryover/CARRYOVER.md', '03-carryover/DECISIONS.md', '04-patterns/WORK_RHYTHM.md'];
|
|
15
|
+
// 空目录:init 现建(以前靠 .gitkeep 占位;工作目录不是 git 仓库,不用了)
|
|
16
|
+
const GENERATED_DIRS = ['02-daily', '05-goals'];
|
|
17
|
+
|
|
18
|
+
// 表里以日期开头的行是记录,表头和分隔线留着
|
|
19
|
+
const blankCarryover = (text) => text.split('\n').filter((line) => !/^\| \d/.test(line)).join('\n');
|
|
20
|
+
|
|
21
|
+
// 规律画像只留标题、引用的说明行、「数据范围」一行和各小节的标题,小节里的内容清掉。
|
|
22
|
+
// 已经是空白的文件经过它原样不变(开发时用它核对 templates/ 没走样)
|
|
23
|
+
function blankRhythm(text) {
|
|
24
|
+
const out = [];
|
|
25
|
+
const blank = () => {
|
|
26
|
+
if (out.length && out[out.length - 1] !== '') out.push('');
|
|
27
|
+
};
|
|
28
|
+
for (const line of text.split('\n')) {
|
|
29
|
+
if (line.startsWith('## ')) {
|
|
30
|
+
blank();
|
|
31
|
+
out.push(line);
|
|
32
|
+
} else if (!line.trim()) blank();
|
|
33
|
+
else if (line.startsWith('# ')) out.push(line);
|
|
34
|
+
else if (line.startsWith('> ')) out.push(line);
|
|
35
|
+
else if (line.startsWith('数据范围')) {
|
|
36
|
+
blank();
|
|
37
|
+
out.push('数据范围:尚未开始(0 个带事件的工作日)');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return `${out.join('\n').replace(/\n+$/, '')}\n`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 数据源全部关掉(引导到第 4 步时逐个确认再打开)
|
|
44
|
+
const blankSources = (text) => text.replace(/^(\s*enabled:\s*)true\b.*$/gm, '$1false');
|
|
45
|
+
|
|
46
|
+
function blankText(rel, text) {
|
|
47
|
+
if (rel.endsWith('CARRYOVER.md') || rel.endsWith('DECISIONS.md')) return blankCarryover(text);
|
|
48
|
+
if (rel === '04-patterns/WORK_RHYTHM.md') return blankRhythm(text);
|
|
49
|
+
if (rel === '00-config/sources.yaml') return blankSources(text);
|
|
50
|
+
return text;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 一个目录是不是空白的工作目录。返回问题的列表,空数组就是空白。
|
|
54
|
+
// 打包、init 之后、开发时的自检都用它核对:任何一项不对都不该往外发
|
|
55
|
+
function blankProblems(dir) {
|
|
56
|
+
const problems = [];
|
|
57
|
+
const text = (rel) => readText(path.join(dir, rel));
|
|
58
|
+
if (!fs.existsSync(path.join(dir, '00-config', 'profile.example.yaml'))) problems.push('没有 00-config/profile.example.yaml');
|
|
59
|
+
if (!fs.existsSync(path.join(dir, '00-config', 'profile.yaml'))) problems.push('没有 00-config/profile.yaml');
|
|
60
|
+
else if (!/^ status: new\b/m.test(text('00-config/profile.yaml'))) problems.push('profile.yaml 不是空白状态(setup.status 应为 new)');
|
|
61
|
+
if (!fs.existsSync(path.join(dir, '00-config', 'sources.yaml'))) problems.push('没有 00-config/sources.yaml');
|
|
62
|
+
else if (/^\s*enabled:\s*true\b/m.test(text('00-config/sources.yaml'))) problems.push('数据源没有全部关掉');
|
|
63
|
+
for (const dirName of ['02-daily', '05-goals']) {
|
|
64
|
+
const files = walkFiles(path.join(dir, dirName), new Set(), 0).filter((f) => f.endsWith('.md'));
|
|
65
|
+
if (files.length) problems.push(`${dirName} 里带了 ${files.length} 个记录文件`);
|
|
66
|
+
}
|
|
67
|
+
for (const rel of BLANK) {
|
|
68
|
+
if (/^\| \d/m.test(text(rel))) problems.push(`${rel} 里带了记录`);
|
|
69
|
+
}
|
|
70
|
+
for (const bad of ['.git', 'collectors/local.env', '01-inbox']) {
|
|
71
|
+
if (fs.existsSync(path.join(dir, bad))) problems.push(`带了不该带的 ${bad}`);
|
|
72
|
+
}
|
|
73
|
+
return problems;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 包里的模板对不对:每份都在、profile 是空白状态、数据源全关、记录里没有行、规律画像没开始。
|
|
77
|
+
// 开发时 check 查它,writeWorkdir 铺新工作目录之前也靠它兜底。返回问题的列表
|
|
78
|
+
function templateProblems(packageRoot) {
|
|
79
|
+
const problems = [];
|
|
80
|
+
const text = (rel) => readText(path.join(packageRoot, 'templates', rel));
|
|
81
|
+
for (const rel of TEMPLATE_FILES) {
|
|
82
|
+
if (!text(rel)) problems.push(`templates/${rel} 不存在`);
|
|
83
|
+
}
|
|
84
|
+
if (!/^ status: new\b/m.test(text('00-config/profile.example.yaml'))) problems.push('templates/00-config/profile.example.yaml 不是空白状态(setup.status 应为 new)');
|
|
85
|
+
if (/^\s*enabled:\s*true\b/m.test(text('00-config/sources.yaml'))) problems.push('templates/00-config/sources.yaml 里有打开的源:模板里数据源一律关着,引导时再逐个打开');
|
|
86
|
+
for (const rel of BLANK) {
|
|
87
|
+
if (/^\| \d/m.test(text(rel))) problems.push(`templates/${rel} 里带了记录`);
|
|
88
|
+
}
|
|
89
|
+
if (!text('04-patterns/WORK_RHYTHM.md').includes('数据范围:尚未开始')) problems.push('templates/04-patterns/WORK_RHYTHM.md 的「数据范围」不是「尚未开始」');
|
|
90
|
+
return problems;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 把一个目录清成空白的工作目录:个人信息换成空白模板、数据源全关、记录不留、本机配置和原始数据删掉。
|
|
94
|
+
// 只处理目录里已有的文件,缺的不补。返回退出码
|
|
95
|
+
function blank(dir) {
|
|
96
|
+
const profileExample = path.join(dir, '00-config', 'profile.example.yaml');
|
|
97
|
+
const sources = path.join(dir, '00-config', 'sources.yaml');
|
|
98
|
+
if (!fs.existsSync(profileExample)) throw new Error(`${dir} 里没有 00-config/profile.example.yaml,清不了个人信息`);
|
|
99
|
+
if (!fs.existsSync(sources)) throw new Error(`${dir} 里没有 00-config/sources.yaml`);
|
|
100
|
+
fs.writeFileSync(path.join(dir, '00-config', 'profile.yaml'), fs.readFileSync(profileExample));
|
|
101
|
+
for (const rel of ['00-config/sources.yaml', ...BLANK]) {
|
|
102
|
+
const file = path.join(dir, rel);
|
|
103
|
+
if (fs.existsSync(file)) fs.writeFileSync(file, blankText(rel, fs.readFileSync(file, 'utf8')));
|
|
104
|
+
}
|
|
105
|
+
for (const dirName of ['02-daily', '05-goals']) {
|
|
106
|
+
const base = path.join(dir, dirName);
|
|
107
|
+
for (const file of walkFiles(base, new Set(), 0).filter((f) => f.endsWith('.md'))) fs.rmSync(file, { force: true });
|
|
108
|
+
// 删空的月份目录也收掉,只留顶层
|
|
109
|
+
for (const month of listDir(base)) {
|
|
110
|
+
if (fs.readdirSync(path.join(base, month)).length === 0) fs.rmdirSync(path.join(base, month));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
for (const bad of ['03-carryover/archive', 'collectors/local.env', '01-inbox']) fs.rmSync(path.join(dir, bad), { recursive: true, force: true });
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function listDir(dir) {
|
|
118
|
+
try {
|
|
119
|
+
return fs.readdirSync(dir, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name);
|
|
120
|
+
} catch {
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function copyEntry(from, to) {
|
|
126
|
+
const stat = fs.statSync(from);
|
|
127
|
+
if (stat.isDirectory()) {
|
|
128
|
+
fs.mkdirSync(to, { recursive: true });
|
|
129
|
+
for (const entry of fs.readdirSync(from, { withFileTypes: true })) copyEntry(path.join(from, entry.name), path.join(to, entry.name));
|
|
130
|
+
} else if (stat.isFile()) {
|
|
131
|
+
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
132
|
+
fs.copyFileSync(from, to);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 用包里的模板铺出一个空白的工作目录(目录本身要先建好)。返回空白核对没通过的问题;空数组就是铺好了
|
|
137
|
+
function writeWorkdir(dir, packageRoot) {
|
|
138
|
+
const missing = templateProblems(packageRoot).filter((p) => p.endsWith(' 不存在'));
|
|
139
|
+
if (missing.length) throw new Error(`这个包不完整:${missing.join(';')}。重新安装一次 work-rhythm`);
|
|
140
|
+
for (const rel of SHARED) copyEntry(path.join(packageRoot, rel), path.join(dir, rel));
|
|
141
|
+
for (const rel of TEMPLATE_FILES) copyEntry(path.join(packageRoot, 'templates', rel), path.join(dir, rel));
|
|
142
|
+
for (const name of GENERATED_DIRS) fs.mkdirSync(path.join(dir, name), { recursive: true });
|
|
143
|
+
fs.writeFileSync(path.join(dir, '00-config', 'profile.yaml'), fs.readFileSync(path.join(dir, '00-config', 'profile.example.yaml')));
|
|
144
|
+
const sources = path.join(dir, '00-config', 'sources.yaml');
|
|
145
|
+
fs.writeFileSync(sources, blankSources(fs.readFileSync(sources, 'utf8')));
|
|
146
|
+
return blankProblems(dir);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
module.exports = { SHARED, TEMPLATE_FILES, BLANK, GENERATED_DIRS, blankCarryover, blankRhythm, blankSources, blankText, blankProblems, templateProblems, blank, writeWorkdir };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// 工作目录在哪:从当前目录往上找带 00-config/profile.yaml 的目录,找不到看用户级的登记;登记、扫描也在这里。
|
|
2
|
+
// 用户级配置默认在 ~/.config/work-rhythm/config.yaml,可用环境变量 WORK_RHYTHM_CONFIG 改位置。
|
|
3
|
+
// work-rhythm 的每个命令都靠它定作用在哪个目录上(cli.js);skill 的「找工作目录」也是它
|
|
4
|
+
'use strict';
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
const { readText, writeFileAtomic } = require('./run');
|
|
8
|
+
|
|
9
|
+
const MARKER = '00-config/profile.yaml';
|
|
10
|
+
const isWorkdir = (dir) => !!dir && fs.existsSync(path.join(dir, MARKER));
|
|
11
|
+
|
|
12
|
+
const configPath = (env = process.env) => env.WORK_RHYTHM_CONFIG || path.join(env.XDG_CONFIG_HOME || path.join(env.HOME || '', '.config'), 'work-rhythm', 'config.yaml');
|
|
13
|
+
|
|
14
|
+
// 登记的路径(~ 开头的换成 HOME);没登记返回空串
|
|
15
|
+
function registered(env = process.env) {
|
|
16
|
+
const m = readText(configPath(env)).split('\n').map((l) => /^\s*workdir:\s*(.*)$/.exec(l)).find(Boolean);
|
|
17
|
+
if (!m) return '';
|
|
18
|
+
const value = m[1].replace(/\s#.*$/, '').trim().replace(/^["']|["']$/g, '');
|
|
19
|
+
return value.replace(/^~(?=\/|$)/, env.HOME || '');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 登记为工作目录:只改配置里的 workdir 那一行,别的内容原样保留。返回绝对路径
|
|
23
|
+
function register(dir, env = process.env) {
|
|
24
|
+
const file = configPath(env);
|
|
25
|
+
const lines = readText(file).split('\n');
|
|
26
|
+
const at = lines.findIndex((l) => /^\s*workdir:/.test(l));
|
|
27
|
+
if (at >= 0) lines[at] = `${/^\s*/.exec(lines[at])[0]}workdir: ${dir}`;
|
|
28
|
+
else {
|
|
29
|
+
if (lines.length > 1 || lines[0]) lines.push(`workdir: ${dir}`);
|
|
30
|
+
else lines[0] = `workdir: ${dir}`;
|
|
31
|
+
}
|
|
32
|
+
writeFileAtomic(file, `${lines.join('\n').replace(/\n*$/, '')}\n`);
|
|
33
|
+
return dir;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 从某个目录往上找第一个工作目录;找不到返回空串
|
|
37
|
+
function findUp(start) {
|
|
38
|
+
let dir = path.resolve(start);
|
|
39
|
+
for (;;) {
|
|
40
|
+
if (isWorkdir(dir)) return dir;
|
|
41
|
+
const parent = path.dirname(dir);
|
|
42
|
+
if (parent === dir) return '';
|
|
43
|
+
dir = parent;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 站在 work-rhythm 自己的仓库里(packageRoot 就是它):仓库不是工作目录,但 check 要能查它
|
|
48
|
+
function insidePackage(cwd, packageRoot) {
|
|
49
|
+
if (!packageRoot) return false;
|
|
50
|
+
const rel = path.relative(path.resolve(packageRoot), path.resolve(cwd));
|
|
51
|
+
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 命令作用在哪个目录:--workdir 指定的优先,其次从当前目录往上找;站在 work-rhythm 自己的仓库里就是仓库(source=package,
|
|
55
|
+
// 只有 check 用得上,别的命令会报「不是工作目录」);最后看登记。
|
|
56
|
+
// 返回 { root, source: 'option' | 'cwd' | 'package' | 'pointer' | 'none', problem };登记了但用不了时 problem 说明原因
|
|
57
|
+
function resolveRoot({ cwd = process.cwd(), option = '', env = process.env, packageRoot = '' } = {}) {
|
|
58
|
+
if (option) return { root: path.resolve(option), source: 'option', problem: '' };
|
|
59
|
+
const up = findUp(cwd);
|
|
60
|
+
if (up) return { root: up, source: 'cwd', problem: '' };
|
|
61
|
+
if (insidePackage(cwd, packageRoot)) return { root: path.resolve(packageRoot), source: 'package', problem: '' };
|
|
62
|
+
const reg = registered(env);
|
|
63
|
+
if (reg && isWorkdir(reg)) return { root: reg, source: 'pointer', problem: '' };
|
|
64
|
+
return {
|
|
65
|
+
root: '',
|
|
66
|
+
source: 'none',
|
|
67
|
+
problem: reg ? `登记的工作目录不可用: ${reg}(重新登记: work-rhythm workdir register <路径>)` : '',
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 扫描几个目录,列出里面的工作目录(带 00-config/profile.yaml 的)。跳过系统目录、依赖目录和版本库,最多往下 6 层。
|
|
72
|
+
// 每个是 { dir, status, name }:status 是 setup.status,name 是 identity.name
|
|
73
|
+
function findAll(starts, home) {
|
|
74
|
+
const SKIP = new Set(['Library', '.Trash', 'node_modules', '.git', '.cache', '.npm']);
|
|
75
|
+
const found = new Map();
|
|
76
|
+
const walk = (dir, depth) => {
|
|
77
|
+
let entries;
|
|
78
|
+
try {
|
|
79
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
80
|
+
} catch {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
for (const entry of entries) {
|
|
84
|
+
const full = path.join(dir, entry.name);
|
|
85
|
+
if (entry.isDirectory()) {
|
|
86
|
+
if (!SKIP.has(entry.name) && depth < 6) walk(full, depth + 1);
|
|
87
|
+
} else if (entry.name === 'profile.yaml' && path.basename(dir) === '00-config' && entry.isFile()) {
|
|
88
|
+
const work = path.dirname(dir);
|
|
89
|
+
const profile = require('./config').loadProfile(work);
|
|
90
|
+
if (!found.has(work)) found.set(work, { dir: work, status: profile.setup && profile.setup.status || '', name: profile.identity && profile.identity.name || '' });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
for (const start of starts.length ? starts : [home]) walk(path.resolve(start), 1);
|
|
95
|
+
return [...found.values()].sort((a, b) => (a.dir < b.dir ? -1 : 1));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
module.exports = { MARKER, isWorkdir, configPath, registered, register, findUp, insidePackage, resolveRoot, findAll };
|