tazuna 0.23.0 → 0.27.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 +6 -3
- package/dist/assets/protocol.js +9 -4
- package/dist/cli.js +6 -4
- package/dist/commands/apply.js +32 -7
- package/dist/commands/commit-apply.js +65 -29
- package/dist/commands/commit.js +24 -6
- package/dist/commands/show.js +59 -7
- package/dist/commands/watch-pack.js +145 -0
- package/dist/commands/watch.js +283 -13
- package/dist/core/commit-message.js +42 -2
- package/dist/core/diff.js +16 -0
- package/dist/core/watch-keys.js +30 -0
- package/dist/infra/git.js +60 -5
- package/dist/infra/history.js +25 -11
- package/dist/infra/watch-keys.js +46 -0
- package/dist/infra/watch-lock.js +180 -0
- package/package.json +1 -1
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tazuna watch の画面からのパック (p) と、前回のパックで貼るものをもう一度コピーする (c)。
|
|
3
|
+
* 貼るものの決め方は tazuna pack と同じ (pack/handoff.ts の planHandoff。パックが貼付上限の目安以内なら
|
|
4
|
+
* パック全体、超えるなら本文用テキスト)。読むのはルートの既定の出力 (repomix-output.xml と .prompt.md) で、
|
|
5
|
+
* パックの中身は作ったときに伏せてあるものをそのまま使う (ここで新しく読むソースはない)。
|
|
6
|
+
*/
|
|
7
|
+
import { readFileSync, statSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { PACK_COMMAND } from "../brand.js";
|
|
10
|
+
import { GENERATED_OUTPUTS } from "../pack/initProject.js";
|
|
11
|
+
import { PASTE_LIMIT_CHARS } from "../pack/handoff.js";
|
|
12
|
+
import { listHistoryIds, readManifest } from "../infra/history.js";
|
|
13
|
+
const PACK_FILE = GENERATED_OUTPUTS[0] ?? "repomix-output.xml";
|
|
14
|
+
const PROMPT_FILE = GENERATED_OUTPUTS[1] ?? "repomix-output.prompt.md";
|
|
15
|
+
/**
|
|
16
|
+
* 本文用テキストを同じパックのものとみなす、パックの更新時刻との差 (ミリ秒)。tazuna pack はパックの直後に
|
|
17
|
+
* 本文用テキストを書く。--no-prompt で作ったパックの横に前の回の本文用テキストが残っていても使わない
|
|
18
|
+
* (FAT・ネットワークドライブの 2 秒単位の更新時刻でも同じ回のものを外さないよう、2 秒の幅を持たせる)
|
|
19
|
+
*/
|
|
20
|
+
const PROMPT_SAME_RUN_MS = 2_000;
|
|
21
|
+
const withoutBom = (text) => text.replace(/^/, "");
|
|
22
|
+
const pad2 = (n) => String(n).padStart(2, "0");
|
|
23
|
+
/** 履歴 ID の先頭 (YYYY-MM-DD_HHmm。infra/history.ts の createHistoryId と同じ形・ローカル時刻) */
|
|
24
|
+
function historyIdPrefix(at) {
|
|
25
|
+
return `${at.getFullYear()}-${pad2(at.getMonth() + 1)}-${pad2(at.getDate())}_${pad2(at.getHours())}${pad2(at.getMinutes())}`;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* パックを書いた後に当てた回数。manifest は非信頼入力なので、読めない・形が違うものは数えない
|
|
29
|
+
* (案内の 1 行に使うだけで、何も止めない)
|
|
30
|
+
*/
|
|
31
|
+
export function countAppliesSince(root, since) {
|
|
32
|
+
const prefix = historyIdPrefix(since);
|
|
33
|
+
let count = 0;
|
|
34
|
+
for (const id of listHistoryIds(root)) {
|
|
35
|
+
if (id < prefix)
|
|
36
|
+
continue;
|
|
37
|
+
let manifest;
|
|
38
|
+
try {
|
|
39
|
+
manifest = readManifest(root, id);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (typeof manifest !== "object" || manifest === null)
|
|
45
|
+
continue;
|
|
46
|
+
const { appliedAt, files, undoneAt } = manifest;
|
|
47
|
+
if (typeof appliedAt !== "string" || !Array.isArray(files) || undoneAt !== undefined)
|
|
48
|
+
continue;
|
|
49
|
+
const at = new Date(appliedAt);
|
|
50
|
+
if (Number.isNaN(at.getTime()) || at.getTime() <= since.getTime())
|
|
51
|
+
continue;
|
|
52
|
+
if (files.some((file) => typeof file === "object" && file !== null && file.applied === true)) {
|
|
53
|
+
count++;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return count;
|
|
57
|
+
}
|
|
58
|
+
/** ルートの既定の出力から、前回のパックを読む (無ければ null) */
|
|
59
|
+
export function readLastPack(root) {
|
|
60
|
+
const packPath = join(root, PACK_FILE);
|
|
61
|
+
const promptPath = join(root, PROMPT_FILE);
|
|
62
|
+
let packMtime;
|
|
63
|
+
let pack;
|
|
64
|
+
try {
|
|
65
|
+
packMtime = statSync(packPath).mtimeMs;
|
|
66
|
+
pack = withoutBom(readFileSync(packPath, "utf8"));
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
let prompt;
|
|
72
|
+
try {
|
|
73
|
+
if (statSync(promptPath).mtimeMs >= packMtime - PROMPT_SAME_RUN_MS) {
|
|
74
|
+
prompt = withoutBom(readFileSync(promptPath, "utf8"));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
prompt = undefined;
|
|
79
|
+
}
|
|
80
|
+
const packAt = new Date(packMtime);
|
|
81
|
+
return {
|
|
82
|
+
packName: PACK_FILE,
|
|
83
|
+
promptName: PROMPT_FILE,
|
|
84
|
+
pack,
|
|
85
|
+
prompt,
|
|
86
|
+
packAt,
|
|
87
|
+
appliedSince: countAppliesSince(root, packAt),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/** 作った時刻の表示 (同じ日なら時刻だけ) */
|
|
91
|
+
function madeAtLabel(at, now) {
|
|
92
|
+
const hm = `${pad2(at.getHours())}:${pad2(at.getMinutes())}`;
|
|
93
|
+
return at.toDateString() === now.toDateString() ? hm : `${at.getMonth() + 1}/${at.getDate()} ${hm}`;
|
|
94
|
+
}
|
|
95
|
+
const kiloChars = (text) => Math.max(1, Math.round(text.length / 1000));
|
|
96
|
+
/**
|
|
97
|
+
* c (もう一度コピー) の結果の知らせ方。純粋関数
|
|
98
|
+
* @param last 前回のパック (無ければ null)
|
|
99
|
+
* @param plan 貼るもの (planHandoff の結果。last が null なら null)
|
|
100
|
+
* @param copied クリップボードに書いた結果 (書いていなければ null)
|
|
101
|
+
*/
|
|
102
|
+
export function describeCopy(last, plan, copied, now) {
|
|
103
|
+
if (last === null || plan === null) {
|
|
104
|
+
return {
|
|
105
|
+
title: "✗ パックがありません",
|
|
106
|
+
headline: `✗ 前回のパック (${PACK_FILE}) が見つかりません`,
|
|
107
|
+
details: ["p を打って Enter でパックを作れます (貼るものはそのときクリップボードに入ります)"],
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (plan.kind === "none") {
|
|
111
|
+
return {
|
|
112
|
+
title: "✗ 本文用テキストがありません",
|
|
113
|
+
headline: `✗ パックが貼付上限の目安 (約 ${PASTE_LIMIT_CHARS / 1000}K 文字) を超えていて、本文用テキスト (${last.promptName}) がありません`,
|
|
114
|
+
details: [
|
|
115
|
+
`${last.packName} を添付してください。p を打って Enter でパックを作り直すと、本文用テキストも作ります`,
|
|
116
|
+
],
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const what = plan.kind === "paste-pack" ? last.packName : last.promptName;
|
|
120
|
+
if (copied === null || !copied.ok) {
|
|
121
|
+
return {
|
|
122
|
+
title: "✗ コピーできませんでした",
|
|
123
|
+
headline: `✗ クリップボードにコピーできませんでした${copied === null || copied.ok ? "" : ` (${copied.message})`}`,
|
|
124
|
+
details: [`貼るものは ${what} にあります (ファイルを開いて全体をコピーしてください)`],
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const made = `${madeAtLabel(last.packAt, now)} に作ったパックのものです`;
|
|
128
|
+
const stale = last.appliedSince > 0
|
|
129
|
+
? [
|
|
130
|
+
`このパックを作った後に changes.md を ${last.appliedSince} 回当てています。` +
|
|
131
|
+
`今のコードを渡すなら p を打って Enter でパックを作り直してください (${PACK_COMMAND} と同じ)`,
|
|
132
|
+
]
|
|
133
|
+
: [];
|
|
134
|
+
return plan.kind === "paste-pack"
|
|
135
|
+
? {
|
|
136
|
+
title: "✓ パック全体をコピー",
|
|
137
|
+
headline: `✓ パック全体 (約 ${kiloChars(plan.text)}K 文字) をクリップボードにコピーしました → Copilot Chat の本文に貼って送信してください (添付は不要)`,
|
|
138
|
+
details: [made, ...stale],
|
|
139
|
+
}
|
|
140
|
+
: {
|
|
141
|
+
title: "✓ 本文用テキストをコピー",
|
|
142
|
+
headline: `✓ 本文用テキスト (約 ${kiloChars(plan.text)}K 文字) をクリップボードにコピーしました → ${last.packName} を添付し、本文に貼って送信してください`,
|
|
143
|
+
details: [made, ...stale],
|
|
144
|
+
};
|
|
145
|
+
}
|
package/dist/commands/watch.js
CHANGED
|
@@ -4,26 +4,36 @@
|
|
|
4
4
|
* 二重適用の防止・成功後の履歴への移動は `tazuna` (引数なし) と同じ。Ctrl+C で終了する。
|
|
5
5
|
* 結果はターミナルのタイトルでも知らせ (--bell でベルも。既定は鳴らさない)、
|
|
6
6
|
* 当てなかった Downloads の changes.md は少し待ってから状態ディレクトリの unapplied/ へ移す。
|
|
7
|
+
* 同じダウンロードフォルダを見る watch は 1 つだけ (後から始めた watch に切り替わり、前の watch は自分で終了する。
|
|
8
|
+
* 1 つの changes.md が 2 か所に当たらないように。infra/watch-lock.ts)。
|
|
9
|
+
* 画面で p (パック)・c (前回のパックで貼るものをもう一度コピー)・b (ビルド)・h (一覧) を打って Enter で操作できる
|
|
10
|
+
* (0.27.0。パック・ビルド用の端末を別に開かなくて済むように。core/watch-keys.ts・commands/watch-pack.ts)。
|
|
7
11
|
*/
|
|
8
12
|
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
9
13
|
import { basename, relative } from "node:path";
|
|
10
14
|
import { setTimeout as delay } from "node:timers/promises";
|
|
11
15
|
import { parseArgs } from "node:util";
|
|
12
|
-
import { TOOL_NAME } from "../brand.js";
|
|
16
|
+
import { BUILD_COMMAND, PACK_COMMAND, TOOL_NAME } from "../brand.js";
|
|
13
17
|
import { hasChangesHeading } from "../core/parser.js";
|
|
14
18
|
import { initialWatchState, observeWatch } from "../core/watch.js";
|
|
19
|
+
import { writeClipboardSync } from "../infra/clipboard.js";
|
|
15
20
|
import { loadConfig } from "../infra/config.js";
|
|
16
21
|
import { listChangesFiles, resolveDownloadsDir } from "../infra/downloads.js";
|
|
17
22
|
import { findProjectRoot } from "../infra/root.js";
|
|
18
23
|
import { err, out, outEmphasis, restoreTerminalTitle, ringBell, setTerminalTitle } from "../infra/term.js";
|
|
19
24
|
import { moveToUnapplied, unappliedDir } from "../infra/unapplied.js";
|
|
25
|
+
import { listenWatchInput } from "../infra/watch-keys.js";
|
|
26
|
+
import { acquireWatchLock, checkWatchLock, releaseWatchLock, watchLockPath, } from "../infra/watch-lock.js";
|
|
27
|
+
import { PASTE_LIMIT_CHARS, planHandoff } from "../pack/handoff.js";
|
|
20
28
|
import { runApply } from "./apply.js";
|
|
29
|
+
import { describeCopy, readLastPack } from "./watch-pack.js";
|
|
21
30
|
const DEFAULT_INTERVAL_MS = 1000;
|
|
22
31
|
/**
|
|
23
|
-
* 当てなかった changes.md を移すまでの猶予。Downloads
|
|
24
|
-
* watch
|
|
25
|
-
*
|
|
26
|
-
*
|
|
32
|
+
* 当てなかった changes.md を移すまでの猶予。Downloads は複数のリポジトリで共通なので、別のリポジトリで
|
|
33
|
+
* 同じファイルを当てに行くものがあり得る (0.26.0 より前の版の watch や、手で実行した tazuna。このリポジトリにも
|
|
34
|
+
* 同じ名前のファイルがあって別のリポジトリ向けと見分けられなかったとき、すぐ移すとそちらが当てられなくなる)。
|
|
35
|
+
* 0.26.0 からの watch どうしは同じ Downloads に 1 つだけ (後から始めた方に切り替わる) なので、ここでは競わない。
|
|
36
|
+
* watch は 1 秒間隔の 2 回の観測で渡すので、15 秒あれば向こうの適用が先に終わる
|
|
27
37
|
*/
|
|
28
38
|
const DEFAULT_TIDY_DELAY_MS = 15_000;
|
|
29
39
|
const pad2 = (n) => String(n).padStart(2, "0");
|
|
@@ -41,7 +51,11 @@ export function describeWatchResult(result, file) {
|
|
|
41
51
|
return {
|
|
42
52
|
title: `✓ 適用 ${result.files} ファイル`,
|
|
43
53
|
headline: `✓ 適用しました (${result.files} ファイル)`,
|
|
44
|
-
details: [
|
|
54
|
+
details: [
|
|
55
|
+
file.buildKey === true
|
|
56
|
+
? `差分を VS Code のソース管理か ${TOOL_NAME} show で確認してから、b を打って Enter でビルドしてください`
|
|
57
|
+
: `差分を VS Code のソース管理か ${TOOL_NAME} show で確認してからビルドしてください`,
|
|
58
|
+
],
|
|
45
59
|
};
|
|
46
60
|
case "already-applied":
|
|
47
61
|
return {
|
|
@@ -124,6 +138,73 @@ export function describeTidy(kind, file) {
|
|
|
124
138
|
lines.push(forceLine(file.movedTo ?? file.path, file.rootOption));
|
|
125
139
|
return lines;
|
|
126
140
|
}
|
|
141
|
+
/** 開始時刻の表示 (同じ日なら時刻だけ) */
|
|
142
|
+
function startedLabel(iso, now) {
|
|
143
|
+
const at = new Date(iso);
|
|
144
|
+
if (Number.isNaN(at.getTime()))
|
|
145
|
+
return iso;
|
|
146
|
+
const hm = `${pad2(at.getHours())}:${pad2(at.getMinutes())}`;
|
|
147
|
+
return at.toDateString() === now.toDateString() ? hm : `${at.getMonth() + 1}/${at.getDate()} ${hm}`;
|
|
148
|
+
}
|
|
149
|
+
/** 同じルートか (Windows・macOS は大文字小文字を区別しないのが普通) */
|
|
150
|
+
function sameRoot(a, b, platform) {
|
|
151
|
+
return platform === "win32" || platform === "darwin" ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* 始めたときに、前に動いていた watch から切り替えたことを知らせる行。前の watch が動いていなければ (ウィンドウを
|
|
155
|
+
* 閉じて印だけが残っていた) 何も出さない。純粋関数
|
|
156
|
+
*/
|
|
157
|
+
export function describeHandover(previous, root, now, platform = process.platform) {
|
|
158
|
+
if (previous === null || !previous.alive)
|
|
159
|
+
return [];
|
|
160
|
+
const where = sameRoot(previous.root, root, platform) ? "このリポジトリで" : `${previous.root} で`;
|
|
161
|
+
return [
|
|
162
|
+
`${where}動いていた ${TOOL_NAME} watch (PID ${previous.pid}、${startedLabel(previous.startedAt, now)} から) から、ここに切り替えます`,
|
|
163
|
+
"同じダウンロードフォルダを見る watch は 1 つだけです (1 つの changes.md が 2 か所に当たらないように、あちらは自動で終了します)",
|
|
164
|
+
];
|
|
165
|
+
}
|
|
166
|
+
/** 後から始めた watch に切り替わったとき (この watch が終了するとき) の行。純粋関数 */
|
|
167
|
+
export function describeTakeover(by, root, platform = process.platform) {
|
|
168
|
+
return {
|
|
169
|
+
headline: sameRoot(by.root, root, platform)
|
|
170
|
+
? `このリポジトリで別の ${TOOL_NAME} watch が始まりました (PID ${by.pid})`
|
|
171
|
+
: `別の場所で ${TOOL_NAME} watch が始まりました: ${by.root}`,
|
|
172
|
+
details: [
|
|
173
|
+
"同じダウンロードフォルダを見る watch は 1 つだけにするため、ここでの監視を終了します (1 つの changes.md が 2 か所に当たらないように)",
|
|
174
|
+
`ここで続けるときは、もう一度 ${TOOL_NAME} watch を実行してください (あちらの watch が終了します)`,
|
|
175
|
+
],
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/** 画面で打てるコマンドの一覧 (開始時と h のとき)。build: b (ビルド) を案内するか。純粋関数 */
|
|
179
|
+
export function describeWatchKeys(options) {
|
|
180
|
+
return [
|
|
181
|
+
"画面で 1 文字打って Enter:",
|
|
182
|
+
` p パックを作る (この端末で ${PACK_COMMAND} と打つのと同じ。貼るものをクリップボードへ)`,
|
|
183
|
+
" c 前回のパックで貼るものを、もう一度クリップボードへ (別のものをコピーした後や、新しいチャットに貼り直すとき)",
|
|
184
|
+
...(options.build
|
|
185
|
+
? [` b ビルドする (${BUILD_COMMAND} と同じ。失敗したらエラーと周辺のコードをクリップボードへ)`]
|
|
186
|
+
: []),
|
|
187
|
+
" h この一覧を出す",
|
|
188
|
+
];
|
|
189
|
+
}
|
|
190
|
+
/** 結果の後の 1 行の案内 (待っていることと、打てるコマンド)。keys: コマンドを受け付けるか。純粋関数 */
|
|
191
|
+
export function describeWatchGuide(options) {
|
|
192
|
+
if (!options.keys)
|
|
193
|
+
return "引き続き待っています (Ctrl+C で終了)";
|
|
194
|
+
return ("引き続き待っています ― 打って Enter: p パックを作る / c 貼るものをもう一度コピー / " +
|
|
195
|
+
(options.build ? "b ビルド / " : "") +
|
|
196
|
+
"h 一覧 (Ctrl+C で終了)");
|
|
197
|
+
}
|
|
198
|
+
/** 打ったものがコマンドでなかったとき・貼り付けとみなしたときの行 (それ以外は null)。純粋関数 */
|
|
199
|
+
export function describeWatchInput(input) {
|
|
200
|
+
if (input.kind === "pasted") {
|
|
201
|
+
return `${input.lines} 行がまとめて入力されたため、貼り付けとみなして何もしていません (コマンドは 1 文字を打って Enter)`;
|
|
202
|
+
}
|
|
203
|
+
if (input.kind === "unknown") {
|
|
204
|
+
return `「${input.text.length > 20 ? `${input.text.slice(0, 20)}…` : input.text}」はコマンドではありません`;
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
127
208
|
export async function watchCommand(argv, options = {}) {
|
|
128
209
|
const { values } = parseArgs({
|
|
129
210
|
args: argv,
|
|
@@ -147,6 +228,9 @@ export async function watchCommand(argv, options = {}) {
|
|
|
147
228
|
].filter((f) => !seen.has(f.path) && (seen.add(f.path), true));
|
|
148
229
|
};
|
|
149
230
|
const stop = new AbortController();
|
|
231
|
+
// 観測の間の待ちを、打ったコマンドで早めに切り上げる (stop で止めたときも切り上げる)
|
|
232
|
+
let tick = new AbortController();
|
|
233
|
+
stop.signal.addEventListener("abort", () => tick.abort(), { once: true });
|
|
150
234
|
const onSigint = () => stop.abort();
|
|
151
235
|
process.once("SIGINT", onSigint);
|
|
152
236
|
const onAbort = () => stop.abort();
|
|
@@ -185,7 +269,7 @@ export async function watchCommand(argv, options = {}) {
|
|
|
185
269
|
pendingTidy.splice(i, 1);
|
|
186
270
|
const current = stampOf(p.path);
|
|
187
271
|
if (current === null) {
|
|
188
|
-
out(`(${clock(new Date())}) ${p.path} は移す前に無くなっていました (
|
|
272
|
+
out(`(${clock(new Date())}) ${p.path} は移す前に無くなっていました (別のリポジトリで当てたか、手で動かしたようです)`);
|
|
189
273
|
continue;
|
|
190
274
|
}
|
|
191
275
|
if (current !== p.stamp)
|
|
@@ -204,9 +288,162 @@ export async function watchCommand(argv, options = {}) {
|
|
|
204
288
|
out(` ${line}`);
|
|
205
289
|
}
|
|
206
290
|
};
|
|
291
|
+
const commands = [];
|
|
292
|
+
let listener = null;
|
|
293
|
+
const onInput = (input) => {
|
|
294
|
+
if (input.kind === "empty")
|
|
295
|
+
return;
|
|
296
|
+
if (input.kind === "command") {
|
|
297
|
+
if (commands.some((c) => c.kind === "command" && c.command === input.command))
|
|
298
|
+
return;
|
|
299
|
+
commands.push({ kind: "command", command: input.command });
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
commands.push({ kind: "note", line: describeWatchInput(input) ?? "", withKeys: input.kind === "unknown" });
|
|
303
|
+
}
|
|
304
|
+
tick.abort();
|
|
305
|
+
};
|
|
306
|
+
const keysEnabled = options.input !== undefined;
|
|
307
|
+
// b を案内するのは、tazuna build と同じ探し方で .sln / .vbproj / .csproj が見つかるときだけ
|
|
308
|
+
// (kind: "generic" の構成ではビルドのコマンドはプロジェクトのもの)
|
|
309
|
+
const findBuildTarget = async () => {
|
|
310
|
+
try {
|
|
311
|
+
const { findTargetQuietly } = await import("./pack-io.js");
|
|
312
|
+
return findTargetQuietly(values.root === undefined ? undefined : root) !== undefined;
|
|
313
|
+
}
|
|
314
|
+
catch {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
const buildAvailable = options.buildAvailable ?? (keysEnabled && (await findBuildTarget()));
|
|
319
|
+
const waitingLine = describeWatchGuide({ keys: keysEnabled, build: buildAvailable });
|
|
320
|
+
const hhmm = () => clock(new Date()).slice(0, 5);
|
|
321
|
+
// p / b: この端末で tazuna pack / tazuna build と打つのと同じ (--root 付きで始めた watch は、そのルートのもの)
|
|
322
|
+
const targetArgs = rootOption === null ? [] : [root, "--root", root];
|
|
323
|
+
const commandLabel = (command) => rootOption === null ? command : `${command} ${quoted(root)} --root ${quoted(root)}`;
|
|
324
|
+
/** パック・ビルドを行う。例外でも監視ごと止めない */
|
|
325
|
+
const runTool = async (run) => {
|
|
326
|
+
try {
|
|
327
|
+
return await run([...targetArgs]);
|
|
328
|
+
}
|
|
329
|
+
catch (e) {
|
|
330
|
+
err(`${TOOL_NAME}: ${e instanceof Error ? e.message : String(e)}`);
|
|
331
|
+
return 1;
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
const runPack = options.pack ?? (async (args) => (await import("./pack.js")).packCommand(args));
|
|
335
|
+
const packNow = async () => {
|
|
336
|
+
out("");
|
|
337
|
+
out(`━━ ${clock(new Date())} パックを作ります (${commandLabel(PACK_COMMAND)})`);
|
|
338
|
+
title("パック作成中");
|
|
339
|
+
const code = await runTool(runPack);
|
|
340
|
+
out("");
|
|
341
|
+
if (code === 0) {
|
|
342
|
+
outEmphasis("✓ パックを作りました (Copilot Chat への渡し方は上に出ています)");
|
|
343
|
+
title(`✓ パック作成 (${hhmm()})`);
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
outEmphasis("✗ パックを作れませんでした (理由は上)");
|
|
347
|
+
title(`✗ パック失敗 (${hhmm()})`);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
// b: ビルド。打ったときだけ行う (適用の後に自動でビルドはしない。差分を確かめてから動かすため)
|
|
351
|
+
const runBuild = options.build ?? (async (args) => (await import("./build.js")).buildCommand(args));
|
|
352
|
+
const buildNow = async () => {
|
|
353
|
+
out("");
|
|
354
|
+
out(`━━ ${clock(new Date())} ビルドします (${commandLabel(BUILD_COMMAND)})`);
|
|
355
|
+
title("ビルド中");
|
|
356
|
+
const code = await runTool(runBuild);
|
|
357
|
+
out("");
|
|
358
|
+
if (code === 0) {
|
|
359
|
+
outEmphasis("✓ ビルドに成功しました");
|
|
360
|
+
title(`✓ ビルド成功 (${hhmm()})`);
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
outEmphasis("✗ ビルドできませんでした (理由は上。エラーをクリップボードにコピーしたときは、そのまま AI チャットに貼ってください)");
|
|
364
|
+
title(`✗ ビルド失敗 (${hhmm()})`);
|
|
365
|
+
}
|
|
366
|
+
if (values.bell)
|
|
367
|
+
ringBell();
|
|
368
|
+
};
|
|
369
|
+
// c: 前回のパックで貼るもの (tazuna pack がクリップボードに入れるものと同じ) を、もう一度クリップボードへ
|
|
370
|
+
const writeClipboard = options.writeClipboard ?? writeClipboardSync;
|
|
371
|
+
const copyNow = () => {
|
|
372
|
+
out("");
|
|
373
|
+
out(`━━ ${clock(new Date())} 前回のパックで貼るものをコピーします`);
|
|
374
|
+
const last = readLastPack(root);
|
|
375
|
+
const plan = last === null ? null : planHandoff(last.pack, last.prompt, PASTE_LIMIT_CHARS);
|
|
376
|
+
let copied = null;
|
|
377
|
+
if (plan !== null && plan.kind !== "none") {
|
|
378
|
+
try {
|
|
379
|
+
copied = writeClipboard(plan.text);
|
|
380
|
+
}
|
|
381
|
+
catch (e) {
|
|
382
|
+
copied = { ok: false, message: e instanceof Error ? e.message : String(e) };
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
const report = describeCopy(last, plan, copied, new Date());
|
|
386
|
+
outEmphasis(report.headline);
|
|
387
|
+
for (const line of report.details)
|
|
388
|
+
out(` ${line}`);
|
|
389
|
+
title(`${report.title} (${hhmm()})`);
|
|
390
|
+
};
|
|
391
|
+
const runCommand = async (queued) => {
|
|
392
|
+
if (queued.kind === "note") {
|
|
393
|
+
out(`(${clock(new Date())}) ${queued.line}`);
|
|
394
|
+
if (queued.withKeys)
|
|
395
|
+
for (const line of describeWatchKeys({ build: buildAvailable }))
|
|
396
|
+
out(` ${line}`);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
switch (queued.command) {
|
|
400
|
+
case "help":
|
|
401
|
+
out("");
|
|
402
|
+
for (const line of describeWatchKeys({ build: buildAvailable }))
|
|
403
|
+
out(line);
|
|
404
|
+
out(" Ctrl+C 終了");
|
|
405
|
+
return;
|
|
406
|
+
case "pack":
|
|
407
|
+
await packNow();
|
|
408
|
+
break;
|
|
409
|
+
case "copy":
|
|
410
|
+
copyNow();
|
|
411
|
+
break;
|
|
412
|
+
case "build":
|
|
413
|
+
await buildNow();
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
out("");
|
|
417
|
+
out(waitingLine);
|
|
418
|
+
};
|
|
419
|
+
// 同じダウンロードフォルダを見る watch は 1 つだけにする。印を置き換え、前の watch には自分で終了してもらう
|
|
420
|
+
// (印は開始時の一覧より先に書く。前の watch はこの後に届いたものを当てる前に切り替わりに気づき、この watch は
|
|
421
|
+
// それより前からあるものを当てないので、同じ changes.md を 2 つの watch が当てることはない)
|
|
422
|
+
const me = { pid: process.pid, root, startedAt: new Date().toISOString() };
|
|
423
|
+
let lock = null;
|
|
424
|
+
let previous = null;
|
|
425
|
+
let lockError = null;
|
|
426
|
+
let takenBy = null;
|
|
427
|
+
/** 後から始めた watch に切り替わっていれば true */
|
|
428
|
+
const handedOver = () => {
|
|
429
|
+
if (takenBy === null && lock !== null) {
|
|
430
|
+
const check = checkWatchLock(lock, me);
|
|
431
|
+
if (check.state === "taken")
|
|
432
|
+
takenBy = check.by;
|
|
433
|
+
}
|
|
434
|
+
return takenBy !== null;
|
|
435
|
+
};
|
|
207
436
|
try {
|
|
437
|
+
try {
|
|
438
|
+
({ lock, previous } = acquireWatchLock(watchLockPath(downloadsDir, options.lockDir), me));
|
|
439
|
+
}
|
|
440
|
+
catch (e) {
|
|
441
|
+
lockError = e instanceof Error ? e.message : String(e);
|
|
442
|
+
}
|
|
208
443
|
// 開始時に既にあるファイルは対象外 (古い changes.md を黙って当てない)
|
|
209
444
|
const state = initialWatchState(list());
|
|
445
|
+
for (const line of describeHandover(previous, root, new Date()))
|
|
446
|
+
out(line);
|
|
210
447
|
out("changes*.md の到着を待っています。届いたら確認なしで適用します (Ctrl+C で終了)");
|
|
211
448
|
out(` Downloads: ${downloadsDir}`);
|
|
212
449
|
out(` プロジェクト直下: ${root}`);
|
|
@@ -217,20 +454,42 @@ export async function watchCommand(argv, options = {}) {
|
|
|
217
454
|
}
|
|
218
455
|
out(` 結果はこのターミナルのタイトル${values.bell ? "とベル" : ""}でも知らせます。` +
|
|
219
456
|
`当てなかった Downloads の changes.md は少し待ってから ${tidyRel} へ移します`);
|
|
457
|
+
if (lockError !== null) {
|
|
458
|
+
out(` (ほかの watch と重ならないようにする印を書けませんでした: ${lockError}。` +
|
|
459
|
+
"同じダウンロードフォルダを見る watch をほかに起動しないでください)");
|
|
460
|
+
}
|
|
461
|
+
if (options.input !== undefined) {
|
|
462
|
+
out("");
|
|
463
|
+
for (const line of describeWatchKeys({ build: buildAvailable }))
|
|
464
|
+
out(line);
|
|
465
|
+
listener = listenWatchInput(options.input, onInput);
|
|
466
|
+
}
|
|
220
467
|
title("待機中");
|
|
221
468
|
const interval = options.intervalMs ?? DEFAULT_INTERVAL_MS;
|
|
222
|
-
while (!stop.signal.aborted) {
|
|
469
|
+
while (!stop.signal.aborted && takenBy === null) {
|
|
223
470
|
try {
|
|
224
|
-
await delay(interval, undefined, { signal:
|
|
471
|
+
await delay(interval, undefined, { signal: tick.signal });
|
|
225
472
|
}
|
|
226
473
|
catch {
|
|
227
|
-
|
|
474
|
+
if (stop.signal.aborted)
|
|
475
|
+
break; // 中断
|
|
476
|
+
tick = new AbortController(); // 打ったコマンドで起こされた
|
|
228
477
|
}
|
|
478
|
+
if (handedOver())
|
|
479
|
+
break;
|
|
229
480
|
tidyDue();
|
|
481
|
+
// 打ったコマンドは、届いた changes.md より先に行う (適用の最中に打ったものはその後に)
|
|
482
|
+
while (commands.length > 0 && !stop.signal.aborted && !handedOver()) {
|
|
483
|
+
await runCommand(commands.shift());
|
|
484
|
+
tidyDue();
|
|
485
|
+
}
|
|
486
|
+
if (stop.signal.aborted || handedOver())
|
|
487
|
+
break;
|
|
230
488
|
for (const f of observeWatch(state, list())) {
|
|
231
|
-
|
|
489
|
+
// 後から始めた watch に切り替わっていたら当てない (そちらが当てる)
|
|
490
|
+
if (stop.signal.aborted || handedOver())
|
|
232
491
|
break;
|
|
233
|
-
//
|
|
492
|
+
// 別のリポジトリで先に当てて履歴へ移したもの (手で実行した tazuna や前の版の watch) は、黙って見送る
|
|
234
493
|
if (!existsSync(f.path))
|
|
235
494
|
continue;
|
|
236
495
|
// 移すのを待っていたものが書き換わって渡し直されたら、前の結果では移さない
|
|
@@ -276,6 +535,7 @@ export async function watchCommand(argv, options = {}) {
|
|
|
276
535
|
path: f.path,
|
|
277
536
|
tidy: tidy ? { dir: tidyRel, afterSec: Math.ceil(tidyDelay / 1000) } : null,
|
|
278
537
|
rootOption,
|
|
538
|
+
buildKey: keysEnabled && buildAvailable,
|
|
279
539
|
});
|
|
280
540
|
out("");
|
|
281
541
|
outEmphasis(report.headline);
|
|
@@ -286,13 +546,23 @@ export async function watchCommand(argv, options = {}) {
|
|
|
286
546
|
ringBell();
|
|
287
547
|
tidyDue();
|
|
288
548
|
out("");
|
|
289
|
-
out(
|
|
549
|
+
out(waitingLine);
|
|
290
550
|
}
|
|
291
551
|
}
|
|
552
|
+
if (takenBy !== null) {
|
|
553
|
+
const report = describeTakeover(takenBy, root);
|
|
554
|
+
out("");
|
|
555
|
+
outEmphasis(`━━ ${clock(new Date())} ${report.headline}`);
|
|
556
|
+
for (const line of report.details)
|
|
557
|
+
out(` ${line}`);
|
|
558
|
+
}
|
|
292
559
|
}
|
|
293
560
|
finally {
|
|
561
|
+
listener?.close();
|
|
294
562
|
process.removeListener("SIGINT", onSigint);
|
|
295
563
|
options.signal?.removeEventListener("abort", onAbort);
|
|
564
|
+
if (lock !== null)
|
|
565
|
+
releaseWatchLock(lock);
|
|
296
566
|
restoreTerminalTitle();
|
|
297
567
|
}
|
|
298
568
|
for (const p of pendingTidy)
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* changes.md の概要 (## CHANGES と最初の ### FILE: の間) からコミットメッセージを作る (純粋関数)。
|
|
3
3
|
* 1 回の適用 = 1 コミット (config の git.commitEachApply と tazuna commit) で使う。
|
|
4
4
|
*
|
|
5
|
-
* - 件名:
|
|
5
|
+
* - 件名: 概要の最初の内容のある行。最初の「。」の前まで、長ければ切り詰める。規約文 v6 から、概要の 1 行目は
|
|
6
|
+
* AI が件名として書く (40 文字以内の「〜を追加」の形)。「変更概要:」のような見出しは外す
|
|
6
7
|
* - 本文: 概要の全文 (件名がその行そのものなら、その次の行から)。Mermaid などのコードブロックと
|
|
7
8
|
* 「影響ファイル:」の一覧は除く (ファイルの一覧はコミット自体に残る)
|
|
8
9
|
* - 末尾: 履歴 ID のトレーラー (コミットと tazuna の履歴を突き合わせるため)
|
|
@@ -56,12 +57,19 @@ function cleanSummary(header) {
|
|
|
56
57
|
collapsed.pop();
|
|
57
58
|
return collapsed;
|
|
58
59
|
}
|
|
59
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* 概要の見出しのような書き出し (「**変更概要**」「## 概要」「変更概要: 」)。AI がよく付けるが件名にはならないので外す。
|
|
62
|
+
* 見出しだけの行は空の行として扱う
|
|
63
|
+
*/
|
|
64
|
+
const SUMMARY_LABEL = /^(?:変更の?概要|変更内容|概要|要約|summary)\s*(?:[::]\s*|$)/i;
|
|
65
|
+
/** Markdown の見出し・箇条書き・引用・強調の記号と、概要の見出しを件名から外す */
|
|
60
66
|
function plainSubject(line) {
|
|
61
67
|
return line
|
|
62
68
|
.trim()
|
|
63
69
|
.replace(/^(#{1,6}\s+|[-*+]\s+|>\s*|\d+[.)]\s+)/, "")
|
|
64
70
|
.replace(/\*\*|__/g, "")
|
|
71
|
+
.trim()
|
|
72
|
+
.replace(SUMMARY_LABEL, "")
|
|
65
73
|
.trim();
|
|
66
74
|
}
|
|
67
75
|
function truncate(text, max) {
|
|
@@ -101,3 +109,35 @@ export function buildCommitMessage(header, historyId, fileCount) {
|
|
|
101
109
|
parts.push("", `${HISTORY_TRAILER}: ${historyId}`);
|
|
102
110
|
return { subject, message: `${parts.join("\n")}\n` };
|
|
103
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* 前回のコミットの後に当てた複数の changes.md を 1 つにまとめたメッセージ (tazuna commit)。
|
|
114
|
+
* 件名は最初の回の件名、本文は各回の件名の箇条書き、末尾に各回の履歴 ID のトレーラー。1 回だけなら buildCommitMessage と同じ
|
|
115
|
+
*/
|
|
116
|
+
export function buildBatchCommitMessage(entries) {
|
|
117
|
+
const [first, ...rest] = entries;
|
|
118
|
+
if (first === undefined)
|
|
119
|
+
return buildCommitMessage("", "", 0);
|
|
120
|
+
if (rest.length === 0)
|
|
121
|
+
return buildCommitMessage(first.header, first.historyId, first.fileCount);
|
|
122
|
+
const subjects = entries.map((e) => buildCommitMessage(e.header, e.historyId, e.fileCount).subject);
|
|
123
|
+
const subject = subjects[0];
|
|
124
|
+
const lines = [
|
|
125
|
+
subject,
|
|
126
|
+
"",
|
|
127
|
+
...subjects.map((s) => `- ${s}`),
|
|
128
|
+
"",
|
|
129
|
+
...entries.map((e) => `${HISTORY_TRAILER}: ${e.historyId}`),
|
|
130
|
+
];
|
|
131
|
+
return { subject, message: `${lines.join("\n")}\n` };
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* 件名を利用者の指定 (tazuna commit -m) に替える。改行と制御文字は除く。空になれば null
|
|
135
|
+
*/
|
|
136
|
+
export function withSubject(message, subject) {
|
|
137
|
+
const cleaned = subject.replace(/[\r\n]+/g, " ").replace(UNSAFE_CHARS, "").trim();
|
|
138
|
+
if (cleaned === "")
|
|
139
|
+
return null;
|
|
140
|
+
const newline = message.message.indexOf("\n");
|
|
141
|
+
const rest = newline === -1 ? "\n" : message.message.slice(newline);
|
|
142
|
+
return { subject: cleaned, message: `${cleaned}${rest}` };
|
|
143
|
+
}
|
package/dist/core/diff.js
CHANGED
|
@@ -6,6 +6,22 @@
|
|
|
6
6
|
* (表示が粗くなるだけで内容は欠けない)。
|
|
7
7
|
*/
|
|
8
8
|
const DEFAULT_MAX_D = 2000;
|
|
9
|
+
/**
|
|
10
|
+
* 行配列 a → b で足した行と消した行の数 (適用の結果の表示用)。
|
|
11
|
+
* 差が大きく diffLines が全削除 + 全挿入に打ち切ったときは null (全行の入れ替えと見せない。行の内容ごとの数の差でも
|
|
12
|
+
* 並べ替えを 0 と数えてしまうため、数えない)
|
|
13
|
+
*/
|
|
14
|
+
export function countLineChanges(a, b) {
|
|
15
|
+
let added = 0;
|
|
16
|
+
let removed = 0;
|
|
17
|
+
for (const op of diffLines(a, b)) {
|
|
18
|
+
if (op.kind === "ins")
|
|
19
|
+
added++;
|
|
20
|
+
else if (op.kind === "del")
|
|
21
|
+
removed++;
|
|
22
|
+
}
|
|
23
|
+
return added + removed <= DEFAULT_MAX_D ? { added, removed } : null;
|
|
24
|
+
}
|
|
9
25
|
/** 行配列 a → b の差分を返す。インデックスはともに 0-based */
|
|
10
26
|
export function diffLines(a, b, maxD = DEFAULT_MAX_D) {
|
|
11
27
|
// 共通の前置き・後置きを先に確定する (パッチ適用前後の比較では大半が共通)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tazuna watch の画面で打つコマンド (1 文字 + Enter) の解釈。純粋関数。
|
|
3
|
+
* 入力の読み取りは infra/watch-keys.ts、実行は commands/watch.ts。
|
|
4
|
+
*
|
|
5
|
+
* キーを押した瞬間に反応させる方式 (raw モード) にしないのは、Ctrl+C が SIGINT ではなくキー入力になって
|
|
6
|
+
* パックやクリップボードの処理の途中で止められなくなるのと、Git Bash (mintty) では端末として読めないのと、
|
|
7
|
+
* 誤って画面に貼り付けた長い文章の中の 1 文字に反応してしまうため (Vite の開発サーバーと同じ「打って Enter」)。
|
|
8
|
+
*/
|
|
9
|
+
const COMMANDS = {
|
|
10
|
+
p: "pack",
|
|
11
|
+
c: "copy",
|
|
12
|
+
b: "build",
|
|
13
|
+
h: "help",
|
|
14
|
+
"?": "help",
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* ひとかたまりで届いた行 (呼び出し側が短い間隔で届いたものをまとめる) を解釈する。
|
|
18
|
+
* 全角の英字 (IME がオンのときの p) と大文字は同じものとして扱う (NFKC で半角・小文字にそろえる)。
|
|
19
|
+
* 内容のある行が 2 行以上なら貼り付けとみなし、どのコマンドも実行しない
|
|
20
|
+
*/
|
|
21
|
+
export function interpretWatchInput(lines) {
|
|
22
|
+
const filled = lines.map((line) => line.normalize("NFKC").trim()).filter((line) => line !== "");
|
|
23
|
+
if (filled.length === 0)
|
|
24
|
+
return { kind: "empty" };
|
|
25
|
+
if (filled.length > 1)
|
|
26
|
+
return { kind: "pasted", lines: filled.length };
|
|
27
|
+
const text = filled[0];
|
|
28
|
+
const command = COMMANDS[text.toLowerCase()];
|
|
29
|
+
return command === undefined ? { kind: "unknown", text } : { kind: "command", command };
|
|
30
|
+
}
|