work-rhythm 0.1.4 → 0.1.5
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.
|
@@ -24,7 +24,7 @@ when_to_use: "第一次进入工作目录、初始化还没做完(setup.status
|
|
|
24
24
|
- 定位之后,读写文件都用绝对路径 `WORKDIR/…`;命令站在 WORKDIR 里跑,或者带上 `--workdir "$WORKDIR"`。下文的路径都省略了 `WORKDIR/` 前缀。
|
|
25
25
|
- `SOURCE=pointer` 时在回复里点一句记到了哪里(「已记到 my-work」),用户在别的项目里才找得到。
|
|
26
26
|
- `WORKDIR` 为空说明这台机器上还没有登记过工作目录。这不算异常,用选择题工具问一句「用哪个工作目录」,三项,**新建**排第一、标为默认:
|
|
27
|
-
1. **新建(默认)**:先问放到哪,默认提议 `~/workspace/my-work
|
|
27
|
+
1. **新建(默认)**:先问放到哪,默认提议 `~/workspace/my-work`(目录已存在但是空的可以直接用;非空就换个名字),然后 `work-rhythm init <目录>`。它用包里带的模板建出工作目录:个人信息是空白的、数据源全关、没有记录,并登记;之后从「步骤 0」接着引导。全程不联网,不需要任何仓库的访问权。脚本报「包不完整」时,让用户重新安装一次 `work-rhythm`。
|
|
28
28
|
2. **我给一个**:用户报一个已有目录,`work-rhythm workdir register <路径>` 登记后继续。
|
|
29
29
|
3. **让 AI 找**:`work-rhythm workdir find` 列出本机带 `00-config/profile.yaml` 的目录(路径、初始化状态、称呼),把候选给用户选,选中就登记。一个都没找到就照实说,回到另外两项。
|
|
30
30
|
|
package/package.json
CHANGED
package/scripts/cmd/init.js
CHANGED
|
@@ -17,7 +17,9 @@ const USAGE = `work-rhythm init <目录> —— 新建一个工作目录:用
|
|
|
17
17
|
work-rhythm init ~/workspace/my-work
|
|
18
18
|
work-rhythm init my-work 相对路径按当前目录算;上级目录没有就一起建
|
|
19
19
|
建好之后对 AI 说「开始引导」。工作目录只是机器上的一个目录:不 git init,不同步到任何仓库。
|
|
20
|
-
|
|
20
|
+
目标目录还不存在就建(上级目录没有也一起建);已经存在但是空的(只有 .DS_Store 也算空)就直接铺进去;已经存在而且不是空的拒绝,
|
|
21
|
+
免得盖掉别人的东西——那已经是某个目录了,拿它当工作目录用 work-rhythm workdir register。
|
|
22
|
+
退出码: 0 建好;2 用法不对,或目录已存在且非空;5 包不完整或铺出来的不是空白的(会把写了一半的东西清掉:新建的目录整个删掉,原有的空目录清回空的)。`;
|
|
21
23
|
|
|
22
24
|
module.exports = {
|
|
23
25
|
usage: USAGE,
|
|
@@ -25,18 +27,29 @@ module.exports = {
|
|
|
25
27
|
const { stdout, stderr, cwd, env, packageRoot } = ctx;
|
|
26
28
|
if (args.length !== 1) throw new UsageError(USAGE);
|
|
27
29
|
const target = path.resolve(cwd, args[0]);
|
|
28
|
-
|
|
30
|
+
// 目录已经存在:空的话可以直接铺进去(用户先 mkdir 了、或者编辑器先建了目录);不是空的别碰
|
|
31
|
+
const existed = fs.existsSync(target);
|
|
32
|
+
if (existed) {
|
|
33
|
+
if (!fs.statSync(target).isDirectory()) throw new Fail(EXIT.USAGE, `${target} 已经存在,而且不是目录;换个位置`);
|
|
34
|
+
const clutter = fs.readdirSync(target).filter((name) => name !== '.DS_Store');
|
|
35
|
+
if (clutter.length) throw new Fail(EXIT.USAGE, `${target} 已经存在而且不是空的(${clutter.length} 项,比如 ${clutter[0]}),不动它。它要是本来就是工作目录,用 work-rhythm workdir register;要新建就换个位置,或者先把里面的东西清走`);
|
|
36
|
+
}
|
|
29
37
|
// 上级目录没有就一起建
|
|
30
38
|
fs.mkdirSync(target, { recursive: true });
|
|
39
|
+
// 铺到一半失败:新建的目录整个删掉;原本就存在的空目录清回空的,目录本身留着
|
|
40
|
+
const undo = () => {
|
|
41
|
+
if (!existed) fs.rmSync(target, { recursive: true, force: true });
|
|
42
|
+
else for (const name of fs.readdirSync(target)) fs.rmSync(path.join(target, name), { recursive: true, force: true });
|
|
43
|
+
};
|
|
31
44
|
let problems;
|
|
32
45
|
try {
|
|
33
46
|
problems = writeWorkdir(target, packageRoot);
|
|
34
47
|
} catch (error) {
|
|
35
|
-
|
|
48
|
+
undo();
|
|
36
49
|
throw new Fail(EXIT.FAILED, error.message);
|
|
37
50
|
}
|
|
38
51
|
if (problems.length) {
|
|
39
|
-
|
|
52
|
+
undo();
|
|
40
53
|
throw new Fail(EXIT.FAILED, `铺出来的不是空白的工作目录,已经撤掉:${problems.join(';')}`);
|
|
41
54
|
}
|
|
42
55
|
// 有些助手只认 CLAUDE.md
|
|
@@ -41,7 +41,7 @@ test('init:用包里的模板建出工作目录并登记;不 git init,不
|
|
|
41
41
|
assert.equal(read(cfg), `workdir: ${real}\n`);
|
|
42
42
|
});
|
|
43
43
|
|
|
44
|
-
test('init
|
|
44
|
+
test('init:相对路径按当前目录算,上级目录没有就一起建;目录已存在且非空、参数不对时退出码为 2', (t) => {
|
|
45
45
|
const box = makeSandbox(t);
|
|
46
46
|
const cfg = path.join(box.root, 'wr-config.yaml');
|
|
47
47
|
const env = { WORK_RHYTHM_CONFIG: cfg };
|
|
@@ -53,11 +53,49 @@ test('init:相对路径按当前目录算,上级目录没有就一起建;
|
|
|
53
53
|
assert.ok(exists(path.join(box.root, 'no', 'such', 'dir', 'x', '00-config', 'profile.yaml')));
|
|
54
54
|
const again = rawRun(['init', path.join(box.root, 'my-work')], { cwd: box.root, env });
|
|
55
55
|
assert.equal(again.code, 2);
|
|
56
|
-
assert.match(again.stderr,
|
|
56
|
+
assert.match(again.stderr, /已经存在而且不是空的(\d+ 项,比如 [^)]+),不动它。它要是本来就是工作目录,用 work-rhythm workdir register;要新建就换个位置/);
|
|
57
57
|
assert.equal(rawRun(['init'], { cwd: box.root, env }).code, 2);
|
|
58
58
|
assert.equal(rawRun(['init', 'a', 'b'], { cwd: box.root, env }).code, 2);
|
|
59
59
|
});
|
|
60
60
|
|
|
61
|
+
test('init:目录已经存在但是空的,直接铺进去;只有 .DS_Store 也算空;非空的和一个普通文件都拒绝,什么都不动', (t) => {
|
|
62
|
+
const box = makeSandbox(t);
|
|
63
|
+
const cfg = path.join(box.root, 'wr-config.yaml');
|
|
64
|
+
const env = { WORK_RHYTHM_CONFIG: cfg };
|
|
65
|
+
const empty = path.join(box.root, 'home', 'empty');
|
|
66
|
+
fs.mkdirSync(empty, { recursive: true }); // 用户先 mkdir 了
|
|
67
|
+
const r = rawRun(['init', empty], { cwd: box.root, env });
|
|
68
|
+
assert.equal(r.code, 0, r.out);
|
|
69
|
+
assert.equal(r.stdout, `WORKDIR=${fs.realpathSync(empty)}\n`);
|
|
70
|
+
for (const f of ['00-config/profile.yaml', 'AGENTS.md', 'docs/formats.md']) assert.ok(exists(path.join(empty, f)), `缺 ${f}`);
|
|
71
|
+
assert.equal(read(cfg), `workdir: ${fs.realpathSync(empty)}\n`);
|
|
72
|
+
// Finder 建过 .DS_Store 的空目录
|
|
73
|
+
const finder = path.join(box.root, 'home', 'finder');
|
|
74
|
+
fs.mkdirSync(finder, { recursive: true });
|
|
75
|
+
write(path.join(finder, '.DS_Store'), '\0\0\0');
|
|
76
|
+
assert.equal(rawRun(['init', finder], { cwd: box.root, env }).code, 0);
|
|
77
|
+
assert.ok(exists(path.join(finder, '.DS_Store')), '人家的 .DS_Store 留着');
|
|
78
|
+
// 非空:拒绝,里面的东西一样不动
|
|
79
|
+
const used = path.join(box.root, 'home', 'used');
|
|
80
|
+
write(path.join(used, 'notes', 'a.md'), '我的东西');
|
|
81
|
+
const refused = rawRun(['init', used], { cwd: box.root, env });
|
|
82
|
+
assert.equal(refused.code, 2);
|
|
83
|
+
assert.match(refused.stderr, /已经存在而且不是空的(1 项,比如 notes),不动它/);
|
|
84
|
+
assert.equal(exists(path.join(used, 'notes', 'a.md')), true);
|
|
85
|
+
assert.equal(exists(path.join(used, '00-config')), false);
|
|
86
|
+
// 是个普通文件:也拒绝
|
|
87
|
+
const file = path.join(box.root, 'home', 'a-file');
|
|
88
|
+
write(file, 'x');
|
|
89
|
+
const notDir = rawRun(['init', file], { cwd: box.root, env });
|
|
90
|
+
assert.equal(notDir.code, 2);
|
|
91
|
+
assert.match(notDir.stderr, /已经存在,而且不是目录/);
|
|
92
|
+
assert.equal(read(file), 'x');
|
|
93
|
+
// 隐藏文件不算空:只有 .DS_Store 放行
|
|
94
|
+
const hidden = path.join(box.root, 'home', 'hidden');
|
|
95
|
+
write(path.join(hidden, '.gitkeep'), '');
|
|
96
|
+
assert.equal(rawRun(['init', hidden], { cwd: box.root, env }).code, 2);
|
|
97
|
+
});
|
|
98
|
+
|
|
61
99
|
test('init:建出来的工作目录里 work-rhythm 的命令都能用:check、today、sync', (t) => {
|
|
62
100
|
const box = makeSandbox(t);
|
|
63
101
|
const cfg = path.join(box.root, 'wr-config.yaml');
|