@tamai/hub-cli 0.1.0
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/README.md +145 -0
- package/bin/hub.mjs +14 -0
- package/commands/project-create.mjs +18 -0
- package/commands/project-list.mjs +18 -0
- package/commands/project-move.mjs +26 -0
- package/commands/project-show.mjs +12 -0
- package/commands/project-sync-registry.mjs +17 -0
- package/commands/project-update.mjs +101 -0
- package/commands/task-add.mjs +22 -0
- package/commands/task-audit.mjs +42 -0
- package/commands/task-claim.mjs +57 -0
- package/commands/task-list.mjs +25 -0
- package/commands/task-new.mjs +81 -0
- package/commands/task-next.mjs +23 -0
- package/commands/task-note.mjs +33 -0
- package/commands/task-report.mjs +22 -0
- package/commands/task-show.mjs +12 -0
- package/lib/api.mjs +220 -0
- package/lib/args.mjs +167 -0
- package/lib/cli.mjs +195 -0
- package/lib/format.mjs +156 -0
- package/lib/git.mjs +138 -0
- package/lib/records.mjs +197 -0
- package/package.json +30 -0
package/lib/format.mjs
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
const OPEN_TASK_STATUSES = ["inbox", "ready", "doing", "review"];
|
|
2
|
+
|
|
3
|
+
export function taskLines(task) {
|
|
4
|
+
const lines = [`${task.id} [${task.status}] ${task.title}`];
|
|
5
|
+
if (task.project) lines.push(`project: ${task.project}`);
|
|
6
|
+
if (task.due) lines.push(`due: ${task.due}`);
|
|
7
|
+
if (task.owner) lines.push(`owner: ${task.owner}`);
|
|
8
|
+
if (task.context_ref) lines.push(`context_ref: ${task.context_ref}`);
|
|
9
|
+
if (task.body) lines.push("", task.body);
|
|
10
|
+
return lines;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function printTaskResult(payload, json, stdout, kind) {
|
|
14
|
+
if (json) {
|
|
15
|
+
stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (kind === "list") {
|
|
19
|
+
const items = payload?.items ?? [];
|
|
20
|
+
if (items.length === 0) {
|
|
21
|
+
stdout.write("タスクはありません。\n");
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
stdout.write(`${items.map((item) => taskLines(item).join("\n")).join("\n\n")}\n`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (kind === "next-empty") {
|
|
28
|
+
stdout.write("readyのタスクはありません。\n");
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const lines = taskLines(payload.item);
|
|
32
|
+
if (kind === "show" && Array.isArray(payload.events) && payload.events.length > 0) {
|
|
33
|
+
lines.push("", "events:");
|
|
34
|
+
for (const event of payload.events) {
|
|
35
|
+
const note = event.note ? ` — ${event.note}` : "";
|
|
36
|
+
lines.push(`- ${event.at} ${event.kind} (${event.actor})${note}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
stdout.write(`${lines.join("\n")}\n`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function projectLines(project, { includeLinks = false } = {}) {
|
|
43
|
+
const counts = project.task_counts ?? {};
|
|
44
|
+
const lines = [`${project.slug} [${project.status}] ${project.name}`];
|
|
45
|
+
const open = OPEN_TASK_STATUSES
|
|
46
|
+
.filter((status) => typeof counts[status] === "number" && counts[status] > 0)
|
|
47
|
+
.map((status) => `${status}:${counts[status]}`);
|
|
48
|
+
if (open.length > 0) lines.push(`tasks: ${open.join(" ")}`);
|
|
49
|
+
const owners = Array.isArray(project.active_owners) ? project.active_owners : [];
|
|
50
|
+
if (owners.length > 0) lines.push(`agents: ${owners.join(", ")}`);
|
|
51
|
+
else if (project.default_agent) lines.push(`agents: -(既定 ${project.default_agent})`);
|
|
52
|
+
if (project.last_activity_at) lines.push(`last_activity: ${project.last_activity_at}`);
|
|
53
|
+
if (project.kind && project.kind !== "work") lines.push(`kind: ${project.kind}`);
|
|
54
|
+
if (project.repo) lines.push(`repo: ${project.repo}`);
|
|
55
|
+
if (project.memory_home) lines.push(`memory: ${project.memory_home}`);
|
|
56
|
+
if (project.auto_policy && project.auto_policy !== "off") lines.push(`auto: ${project.auto_policy}`);
|
|
57
|
+
if (project.note) lines.push(`note: ${project.note}`);
|
|
58
|
+
if (includeLinks && Array.isArray(project.links) && project.links.length > 0) {
|
|
59
|
+
lines.push("links:");
|
|
60
|
+
for (const link of project.links) {
|
|
61
|
+
const label = String(link?.label ?? "").replace(/\s+/g, " ").trim();
|
|
62
|
+
const url = String(link?.url ?? "").trim();
|
|
63
|
+
lines.push(`- ${label}: ${url}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return lines;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function printProjectResult(payload, json, stdout, kind) {
|
|
70
|
+
if (json) {
|
|
71
|
+
stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (kind === "list") {
|
|
75
|
+
const items = payload?.items ?? [];
|
|
76
|
+
if (items.length === 0) {
|
|
77
|
+
stdout.write("プロジェクトはありません。\n");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
stdout.write(`${items.map((item) => projectLines(item).join("\n")).join("\n\n")}\n`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const lines = projectLines(payload.item, { includeLinks: kind === "show" });
|
|
84
|
+
if (kind === "update") lines.push(`updated_tasks: ${payload.updated_tasks}`);
|
|
85
|
+
const activity = Array.isArray(payload.activity) ? payload.activity : [];
|
|
86
|
+
if (activity.length > 0) {
|
|
87
|
+
lines.push("", "activity:");
|
|
88
|
+
for (const event of activity) {
|
|
89
|
+
const isProject = event.source === "project";
|
|
90
|
+
const mark = isProject ? "[project] " : "";
|
|
91
|
+
const summary = isProject ? projectEventSummary(event) : event.task_title;
|
|
92
|
+
const body = summary ? ` ${summary}` : "";
|
|
93
|
+
const note = event.note ? ` — ${event.note}` : "";
|
|
94
|
+
lines.push(`- ${event.at} ${mark}${event.kind} (${event.actor})${body}${note}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
stdout.write(`${lines.join("\n")}\n`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function projectEventSummary(event) {
|
|
101
|
+
const meta = event.meta;
|
|
102
|
+
if (meta === null || typeof meta !== "object" || Array.isArray(meta)) return "";
|
|
103
|
+
if (event.kind === "created") {
|
|
104
|
+
const to = meta.to ?? {};
|
|
105
|
+
return to.name || to.slug ? `作成: ${to.name ?? ""}(${to.slug ?? ""})` : "作成";
|
|
106
|
+
}
|
|
107
|
+
if (event.kind === "moved") {
|
|
108
|
+
return meta.after_slug ? `並び順: ${meta.after_slug} の後ろへ` : "並び順: 先頭へ";
|
|
109
|
+
}
|
|
110
|
+
const fields = Array.isArray(meta.fields) ? meta.fields.filter((field) => typeof field === "string") : [];
|
|
111
|
+
const omitted = new Set(Array.isArray(meta.omitted) ? meta.omitted : []);
|
|
112
|
+
const from = meta.from ?? {};
|
|
113
|
+
const to = meta.to ?? {};
|
|
114
|
+
return fields
|
|
115
|
+
.map((field) => (omitted.has(field)
|
|
116
|
+
? `${field}: 変更あり`
|
|
117
|
+
: `${field}: ${formatEventValue(from[field])} → ${formatEventValue(to[field])}`))
|
|
118
|
+
.join(" / ");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function formatEventValue(value) {
|
|
122
|
+
if (value === null || value === undefined) return "なし";
|
|
123
|
+
if (typeof value === "boolean") return value ? "オン" : "オフ";
|
|
124
|
+
return String(value) || "なし";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const REGISTRY_STATUS = { active: "active", paused: "dormant", closed: "archived" };
|
|
128
|
+
const REGISTRY_HEADER = `# このファイルは hub の D1 \`projects\` から \`hub project sync-registry\` で生成した派生物。
|
|
129
|
+
# 正本はD1(指示書v4)。手で編集せず、hub側を直してから再生成すること。
|
|
130
|
+
#
|
|
131
|
+
# 注意: D1が持たない項目(client / ローカルrepoパス / obsidian / drive / aliases)は
|
|
132
|
+
# ここには出力されない。既存の project-registry.yml へ貼るときは、これらの行を消さずに
|
|
133
|
+
# slug単位でマージすること(全文置換は情報を落とす)。
|
|
134
|
+
`;
|
|
135
|
+
|
|
136
|
+
function yamlScalar(value) {
|
|
137
|
+
if (value === null || value === undefined || value === "") return "null";
|
|
138
|
+
const text = String(value);
|
|
139
|
+
return /^[\w./@-]+$/.test(text) ? text : JSON.stringify(text);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function toRegistryYaml(items) {
|
|
143
|
+
const blocks = [...items]
|
|
144
|
+
.sort((a, b) => String(a.slug).localeCompare(String(b.slug)))
|
|
145
|
+
.map((item) => {
|
|
146
|
+
const lines = [
|
|
147
|
+
`- slug: ${yamlScalar(item.slug)}`,
|
|
148
|
+
` name: ${yamlScalar(item.name)}`,
|
|
149
|
+
` status: ${REGISTRY_STATUS[item.status] ?? item.status}`,
|
|
150
|
+
` github: ${yamlScalar(item.repo)}`,
|
|
151
|
+
];
|
|
152
|
+
if (item.note) lines.push(` note: ${yamlScalar(item.note)}`);
|
|
153
|
+
return lines.join("\n");
|
|
154
|
+
});
|
|
155
|
+
return `${REGISTRY_HEADER}\n${blocks.join("\n\n")}\n`;
|
|
156
|
+
}
|
package/lib/git.mjs
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
|
|
4
|
+
const execFileAsync = promisify(execFile);
|
|
5
|
+
const GIT_QUOTE_OFF = ["-c", "core.quotepath=false"];
|
|
6
|
+
|
|
7
|
+
export async function defaultRunGit(args, { cwd }) {
|
|
8
|
+
try {
|
|
9
|
+
const { stdout } = await execFileAsync("git", args, { cwd, maxBuffer: 32 * 1024 * 1024 });
|
|
10
|
+
return { ok: true, stdout };
|
|
11
|
+
} catch {
|
|
12
|
+
return { ok: false, stdout: "" };
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function splitNul(output) {
|
|
17
|
+
return String(output).split("\0").filter((value) => value !== "");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function parseGitStatusPaths(output) {
|
|
21
|
+
const fields = splitNul(output);
|
|
22
|
+
const paths = new Set();
|
|
23
|
+
for (let index = 0; index < fields.length; index += 1) {
|
|
24
|
+
const field = fields[index];
|
|
25
|
+
if (field.length < 4) continue;
|
|
26
|
+
paths.add(field.slice(3));
|
|
27
|
+
if (field[0] === "R" || field[0] === "C") {
|
|
28
|
+
const origin = fields[index + 1];
|
|
29
|
+
if (origin !== undefined) {
|
|
30
|
+
paths.add(origin);
|
|
31
|
+
index += 1;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return paths;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function pathMatches(paths, ref) {
|
|
39
|
+
if (paths.has(ref)) return true;
|
|
40
|
+
for (const entry of paths) {
|
|
41
|
+
if (entry.endsWith("/") && ref.startsWith(entry)) return true;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function readGitState(root, runGitImpl) {
|
|
47
|
+
const run = (args) => runGitImpl(args, { cwd: root });
|
|
48
|
+
const status = await run([...GIT_QUOTE_OFF, "status", "--porcelain", "-z"]);
|
|
49
|
+
const tracked = await run([...GIT_QUOTE_OFF, "ls-files", "-z"]);
|
|
50
|
+
const hasOriginMain = await run(["rev-parse", "--verify", "--quiet", "origin/main"]);
|
|
51
|
+
const ahead = hasOriginMain.ok
|
|
52
|
+
&& (await run(["merge-base", "--is-ancestor", "origin/main", "HEAD"])).ok;
|
|
53
|
+
const behind = hasOriginMain.ok
|
|
54
|
+
&& (await run(["merge-base", "--is-ancestor", "HEAD", "origin/main"])).ok;
|
|
55
|
+
|
|
56
|
+
let unpushed = null;
|
|
57
|
+
let unpushedSkipped = null;
|
|
58
|
+
if (!hasOriginMain.ok) {
|
|
59
|
+
unpushedSkipped = "no-origin";
|
|
60
|
+
} else if (!ahead && !behind) {
|
|
61
|
+
unpushedSkipped = "diverged";
|
|
62
|
+
} else {
|
|
63
|
+
const diffArgs = [...GIT_QUOTE_OFF, "diff", "--no-renames", "--name-only", "-z"];
|
|
64
|
+
const localChanges = await run([...diffArgs, "origin/main...HEAD"]);
|
|
65
|
+
const treeDifference = await run([...diffArgs, "origin/main", "HEAD"]);
|
|
66
|
+
if (localChanges.ok && treeDifference.ok) {
|
|
67
|
+
const localPaths = new Set(splitNul(localChanges.stdout));
|
|
68
|
+
const treePaths = new Set(splitNul(treeDifference.stdout));
|
|
69
|
+
unpushed = new Set([...localPaths].filter((ref) => treePaths.has(ref)));
|
|
70
|
+
} else {
|
|
71
|
+
unpushedSkipped = "git-failed";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
dirty: status.ok ? parseGitStatusPaths(status.stdout) : null,
|
|
77
|
+
tracked: tracked.ok ? new Set(splitNul(tracked.stdout)) : null,
|
|
78
|
+
unpushed,
|
|
79
|
+
unpushedSkipped,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function auditDoneTasks(items, gitState) {
|
|
84
|
+
const suspicious = [];
|
|
85
|
+
let checked = 0;
|
|
86
|
+
let withoutRef = 0;
|
|
87
|
+
let otherRepo = 0;
|
|
88
|
+
|
|
89
|
+
for (const item of items) {
|
|
90
|
+
const ref = typeof item?.context_ref === "string" ? item.context_ref.trim() : "";
|
|
91
|
+
if (ref === "") {
|
|
92
|
+
withoutRef += 1;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const dirty = gitState.dirty !== null && pathMatches(gitState.dirty, ref);
|
|
96
|
+
const unpushed = gitState.unpushed !== null && pathMatches(gitState.unpushed, ref);
|
|
97
|
+
const known = dirty || unpushed || (gitState.tracked !== null && gitState.tracked.has(ref));
|
|
98
|
+
if (!known) {
|
|
99
|
+
otherRepo += 1;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
checked += 1;
|
|
103
|
+
const reasons = [];
|
|
104
|
+
if (dirty) reasons.push("uncommitted");
|
|
105
|
+
if (unpushed) reasons.push("unpushed");
|
|
106
|
+
if (reasons.length > 0) suspicious.push({ id: item.id, title: item.title ?? null, context_ref: ref, reasons });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return { checked, skipped_no_context_ref: withoutRef, skipped_other_repo: otherRepo, suspicious };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export const UNPUSHED_SKIP_REASONS = {
|
|
113
|
+
"no-origin": "origin/main を参照できず(git fetch が要るかもしれません)",
|
|
114
|
+
diverged: "origin/main と履歴が分岐しており(双方にコミットがある)、パスだけでは判定できないため",
|
|
115
|
+
"git-failed": "git diff を実行できず",
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const AUDIT_REASON_LABELS = {
|
|
119
|
+
uncommitted: "記録が未コミット(未追跡、または変更が残っている)",
|
|
120
|
+
unpushed: "記録の変更が origin/main に届いていない(ローカルコミット止まり)",
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export function formatAuditReport(report) {
|
|
124
|
+
const skipped = `context_ref なし ${report.skipped_no_context_ref}件`
|
|
125
|
+
+ `・このrepo外の記録 ${report.skipped_other_repo}件は対象外`;
|
|
126
|
+
if (report.suspicious.length === 0) {
|
|
127
|
+
return `done ${report.checked}件の記録を確認しました(${skipped})。問題は見つかりませんでした。\n`;
|
|
128
|
+
}
|
|
129
|
+
const lines = [`done ${report.checked}件のうち ${report.suspicious.length}件が疑わしい(${skipped}):`, ""];
|
|
130
|
+
for (const entry of report.suspicious) {
|
|
131
|
+
lines.push(`${entry.id} ${entry.title ?? ""}`.trimEnd());
|
|
132
|
+
lines.push(` ${entry.context_ref}`);
|
|
133
|
+
for (const reason of entry.reasons) lines.push(` - ${AUDIT_REASON_LABELS[reason] ?? reason}`);
|
|
134
|
+
lines.push("");
|
|
135
|
+
}
|
|
136
|
+
lines.push("doneは「本番反映と検証まで終えた」状態です。手元にしか無い変更が残っていないか確認してください。");
|
|
137
|
+
return `${lines.join("\n")}\n`;
|
|
138
|
+
}
|
package/lib/records.mjs
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import { cliError } from "./args.mjs";
|
|
4
|
+
import { describeFailure } from "./api.mjs";
|
|
5
|
+
|
|
6
|
+
export const DEFAULT_RECORD_DIR = "docs/records";
|
|
7
|
+
export const NOTE_CHUNK_LIMIT = 2_000;
|
|
8
|
+
const NOTE_MARKER_RESERVE = 16;
|
|
9
|
+
const RECORD_NUMBER_ATTEMPTS = 5;
|
|
10
|
+
|
|
11
|
+
export function slugify(title) {
|
|
12
|
+
return String(title)
|
|
13
|
+
.toLowerCase()
|
|
14
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
15
|
+
.replace(/^-+|-+$/g, "")
|
|
16
|
+
.slice(0, 60);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function nextRecordNumber(entries) {
|
|
20
|
+
const numbers = entries
|
|
21
|
+
.map((name) => /^(\d+)-/.exec(name)?.[1])
|
|
22
|
+
.filter((value) => value !== undefined)
|
|
23
|
+
.map((value) => Number.parseInt(value, 10))
|
|
24
|
+
.filter((value) => Number.isSafeInteger(value));
|
|
25
|
+
const next = numbers.length === 0 ? 1 : Math.max(...numbers) + 1;
|
|
26
|
+
return formatRecordNumber(next);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function formatRecordNumber(number) {
|
|
30
|
+
return String(number).padStart(2, "0");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function recordTemplate({ taskId, title }) {
|
|
34
|
+
return `---
|
|
35
|
+
task_id: ${taskId}
|
|
36
|
+
title: ${title}
|
|
37
|
+
related_commits: []
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
# ${title}
|
|
41
|
+
|
|
42
|
+
> 状態(status / owner / 優先度)の正本はhubの看板。ここには書かない。
|
|
43
|
+
> この記録は「なぜそうしたか」を残す場所で、完了時の検収ログが結びになる。
|
|
44
|
+
|
|
45
|
+
## 概要
|
|
46
|
+
|
|
47
|
+
## 背景
|
|
48
|
+
|
|
49
|
+
## 対応方針
|
|
50
|
+
|
|
51
|
+
## 受け入れ条件
|
|
52
|
+
|
|
53
|
+
## 作業ログ
|
|
54
|
+
`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function splitNote(text, limit = NOTE_CHUNK_LIMIT) {
|
|
58
|
+
const body = String(text).replace(/\s+$/, "");
|
|
59
|
+
if (body === "") throw cliError("noteが空です");
|
|
60
|
+
const room = limit - NOTE_MARKER_RESERVE;
|
|
61
|
+
const chunks = [];
|
|
62
|
+
let current = "";
|
|
63
|
+
|
|
64
|
+
const push = () => {
|
|
65
|
+
if (current !== "") chunks.push(current);
|
|
66
|
+
current = "";
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
for (const rawLine of body.split("\n")) {
|
|
70
|
+
let line = rawLine;
|
|
71
|
+
while (line.length > room) {
|
|
72
|
+
push();
|
|
73
|
+
chunks.push(line.slice(0, room));
|
|
74
|
+
line = line.slice(room);
|
|
75
|
+
}
|
|
76
|
+
const candidate = current === "" ? line : `${current}\n${line}`;
|
|
77
|
+
if (candidate.length > room) {
|
|
78
|
+
push();
|
|
79
|
+
current = line;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
current = candidate;
|
|
83
|
+
}
|
|
84
|
+
push();
|
|
85
|
+
|
|
86
|
+
if (chunks.length <= 1) return [body];
|
|
87
|
+
return chunks.map((chunk, index) => `(${index + 1}/${chunks.length})\n${chunk}`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function findRepoRoot(startDir, statImpl) {
|
|
91
|
+
let dir = path.resolve(startDir);
|
|
92
|
+
for (;;) {
|
|
93
|
+
try {
|
|
94
|
+
await statImpl(path.join(dir, ".git"));
|
|
95
|
+
return dir;
|
|
96
|
+
} catch {
|
|
97
|
+
const parent = path.dirname(dir);
|
|
98
|
+
if (parent === dir) return null;
|
|
99
|
+
dir = parent;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function resolveRecordTarget(dir, dependencies) {
|
|
105
|
+
if (path.isAbsolute(dir)) {
|
|
106
|
+
throw cliError("--repo-card にはリポジトリ相対のディレクトリを指定してください(絶対パスは不可)");
|
|
107
|
+
}
|
|
108
|
+
const root = await findRepoRoot(dependencies.cwd(), dependencies.statImpl);
|
|
109
|
+
if (root === null) {
|
|
110
|
+
throw cliError("gitリポジトリの中で実行してください(.gitが見つかりません)");
|
|
111
|
+
}
|
|
112
|
+
const absoluteDir = path.resolve(dependencies.cwd(), dir);
|
|
113
|
+
const relativeDir = path.relative(root, absoluteDir).split(path.sep).join("/");
|
|
114
|
+
if (relativeDir.startsWith("../")) {
|
|
115
|
+
throw cliError(`記録の出力先がリポジトリの外です: ${relativeDir}`);
|
|
116
|
+
}
|
|
117
|
+
return { root, absoluteDir };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export async function writeRepoRecord({ target, slug, item, dependencies }) {
|
|
121
|
+
const { root, absoluteDir } = target;
|
|
122
|
+
await dependencies.mkdirImpl(absoluteDir, { recursive: true });
|
|
123
|
+
|
|
124
|
+
let entries = [];
|
|
125
|
+
try {
|
|
126
|
+
entries = await dependencies.readdirImpl(absoluteDir);
|
|
127
|
+
} catch {
|
|
128
|
+
entries = [];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const name = slugify(slug ?? "") || slugify(item.title) || `task-${item.id.slice(-6).toLowerCase()}`;
|
|
132
|
+
const content = recordTemplate({ taskId: item.id, title: item.title });
|
|
133
|
+
const first = Number.parseInt(nextRecordNumber(entries), 10);
|
|
134
|
+
let number = first;
|
|
135
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
136
|
+
const absolutePath = path.join(absoluteDir, `${formatRecordNumber(number)}-${name}.md`);
|
|
137
|
+
const relative = path.relative(root, absolutePath).split(path.sep).join("/");
|
|
138
|
+
if (relative === "" || relative.startsWith("../")) {
|
|
139
|
+
throw cliError(`記録の出力先がリポジトリの外です: ${relative}`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
await dependencies.writeFileImpl(absolutePath, content, { encoding: "utf8", flag: "wx" });
|
|
144
|
+
return { path: relative, absolutePath };
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (error?.code !== "EEXIST") throw error;
|
|
147
|
+
if (attempt >= RECORD_NUMBER_ATTEMPTS) {
|
|
148
|
+
throw cliError(
|
|
149
|
+
`${describeFailure(error)}(連番 ${formatRecordNumber(first)}〜${formatRecordNumber(number)} を${attempt}回試しました)`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
dependencies.stderr.write(`記録 ${relative} は既にあるため、連番を ${formatRecordNumber(number + 1)} にずらします。\n`);
|
|
153
|
+
number += 1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function resolveTaskProject(project, { force, allowUnknown = false, api, stderr }) {
|
|
159
|
+
if (!project) return undefined;
|
|
160
|
+
|
|
161
|
+
let items;
|
|
162
|
+
try {
|
|
163
|
+
const payload = await api("/api/projects");
|
|
164
|
+
if (!Array.isArray(payload?.items)) throw cliError("projects一覧のAPI応答が不正です");
|
|
165
|
+
items = payload.items;
|
|
166
|
+
} catch (error) {
|
|
167
|
+
stderr.write(`警告: projects一覧を取得できないためproject検証を省略します: ${describeFailure(error)}\n`);
|
|
168
|
+
return project;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const exact = items.find((item) => item?.name === project);
|
|
172
|
+
if (exact) return project;
|
|
173
|
+
const normalizedSlug = project.toLowerCase();
|
|
174
|
+
const slugMatch = items.find(
|
|
175
|
+
(item) => typeof item?.slug === "string"
|
|
176
|
+
&& item.slug.toLowerCase() === normalizedSlug
|
|
177
|
+
&& typeof item.name === "string",
|
|
178
|
+
);
|
|
179
|
+
if (slugMatch) {
|
|
180
|
+
stderr.write(`project slug ${project} → ${slugMatch.name}に解決しました。\n`);
|
|
181
|
+
return slugMatch.name;
|
|
182
|
+
}
|
|
183
|
+
if (force) {
|
|
184
|
+
stderr.write(`警告: 未登録project「${project}」を--forceによりそのまま起票します。\n`);
|
|
185
|
+
return project;
|
|
186
|
+
}
|
|
187
|
+
if (allowUnknown) return project;
|
|
188
|
+
|
|
189
|
+
const candidates = items
|
|
190
|
+
.filter((item) => typeof item?.name === "string" && typeof item?.slug === "string")
|
|
191
|
+
.map((item) => `${item.slug}(${item.name})`)
|
|
192
|
+
.join("、");
|
|
193
|
+
throw cliError(
|
|
194
|
+
`未登録のprojectです: ${project}\n候補: ${candidates || "登録済みプロジェクトはありません"}`
|
|
195
|
+
+ "\n未登録名のまま起票する場合は --force を指定してください",
|
|
196
|
+
);
|
|
197
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tamai/hub-cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "CLI for the hub personal dashboard: D1-backed task board and project registry.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"bin": {
|
|
8
|
+
"hub": "bin/hub.mjs"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"bin",
|
|
12
|
+
"lib",
|
|
13
|
+
"commands",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=22"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/tamaitakamasa/hub.git",
|
|
22
|
+
"directory": "packages/hub-cli"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "node --test"
|
|
29
|
+
}
|
|
30
|
+
}
|