@ryusenkei/conventions 0.5.1 → 0.6.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 +3 -1
- package/dist/hook.js +19 -6
- package/dist/policy.d.ts +6 -2
- package/dist/policy.js +67 -27
- package/dist/shell.js +45 -0
- package/files/commit-msg +1 -1
- package/files/pre-push +3 -8
- package/package.json +2 -2
- package/rules.md +72 -50
package/README.md
CHANGED
|
@@ -59,7 +59,9 @@ The PreToolUse hook blocks the call and tells Claude why, so Claude can fix it a
|
|
|
59
59
|
| Secrets | `permissions.deny` blocks reading `.env` and `.dev.vars` with Claude's file tools (not Bash) |
|
|
60
60
|
| Definition of done (`--stop`) | The Stop hook runs `pnpm lint` and `pnpm test` when the working tree has changes, and sends Claude back once on failure |
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
The hook works out the target from the command itself. After `cd` or `git -C`, it checks against that repository (including its epic opt-in and `prose-lint.json`). After `git switch` or `git checkout`, it uses the new branch. Heredoc bodies are not parsed as commands.
|
|
63
|
+
|
|
64
|
+
Commit messages are checked by `.githooks/commit-msg`, and edited files by prose-lint's PostToolUse hook. `.githooks/pre-push` runs `pnpm run --if-present check`, so a repository that defines a `check` script cannot push until it passes. It applies to people as well as Claude. When [Hostslot](https://github.com/ryusenkei-inc/hostslot) (`hostslot`) is on the `PATH` and the repository has a `check` script, the check runs through `hostslot run`, so pushes from parallel agents and repositories wait for free memory instead of exhausting it.
|
|
63
65
|
|
|
64
66
|
### Epic flow
|
|
65
67
|
|
package/dist/hook.js
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
// - hook 自体の失敗は終了コード1(拒否にしない → 作業を継続)
|
|
6
6
|
import { spawnSync } from 'node:child_process';
|
|
7
7
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
8
|
-
import { tmpdir } from 'node:os';
|
|
9
|
-
import { join } from 'node:path';
|
|
8
|
+
import { homedir, tmpdir } from 'node:os';
|
|
9
|
+
import { isAbsolute, join, resolve } from 'node:path';
|
|
10
10
|
import { fileURLToPath } from 'node:url';
|
|
11
11
|
import { denyReason } from "./policy.js";
|
|
12
12
|
const input = JSON.parse(readFileSync(0, 'utf8'));
|
|
@@ -14,7 +14,7 @@ const root = process.env.CLAUDE_PROJECT_DIR ?? input.cwd ?? process.cwd();
|
|
|
14
14
|
// Windows の `pnpm` は `.cmd` → シェル経由でのみ起動可能
|
|
15
15
|
const shell = process.platform === 'win32';
|
|
16
16
|
if (input.hook_event_name === 'PreToolUse') {
|
|
17
|
-
const reason = denyReason(input, { root, titleIssues, readFile, currentBranch, prHead, prBase, prChecks });
|
|
17
|
+
const reason = denyReason(input, { root, repoRoot, resolveDir, titleIssues, readFile, currentBranch, prHead, prBase, prChecks });
|
|
18
18
|
if (reason) {
|
|
19
19
|
process.stderr.write(`${reason}\n`);
|
|
20
20
|
process.exit(2);
|
|
@@ -23,14 +23,14 @@ if (input.hook_event_name === 'PreToolUse') {
|
|
|
23
23
|
else if (input.hook_event_name === 'Stop') {
|
|
24
24
|
stop();
|
|
25
25
|
}
|
|
26
|
-
// PR のタイトル = squash のコミットのメッセージ → prose-lint
|
|
27
|
-
function titleIssues(title) {
|
|
26
|
+
// PR のタイトル = squash のコミットのメッセージ → prose-lint のコミットの規則で検査(対象のリポの `prose-lint.json` を反映)
|
|
27
|
+
function titleIssues(title, cwd) {
|
|
28
28
|
const dir = mkdtempSync(join(tmpdir(), 'conventions-'));
|
|
29
29
|
try {
|
|
30
30
|
const file = join(dir, 'title');
|
|
31
31
|
writeFileSync(file, `${title}\n`);
|
|
32
32
|
const cli = fileURLToPath(new URL('./cli.js', import.meta.resolve('@ryusenkei/prose-lint')));
|
|
33
|
-
const r = spawnSync(process.execPath, [cli, '--commit-msg', file], { cwd: root, encoding: 'utf8' });
|
|
33
|
+
const r = spawnSync(process.execPath, [cli, '--commit-msg', file], { cwd: repoRoot(cwd) ?? root, encoding: 'utf8' });
|
|
34
34
|
return r.status === 0 ? [] : [`${r.stdout}${r.stderr}`.trim()];
|
|
35
35
|
}
|
|
36
36
|
finally {
|
|
@@ -45,6 +45,19 @@ function readFile(path) {
|
|
|
45
45
|
return undefined;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
+
function repoRoot(cwd) {
|
|
49
|
+
const r = spawnSync('git', ['rev-parse', '--show-toplevel'], { cwd: cwd ?? root, encoding: 'utf8' });
|
|
50
|
+
return (r.status === 0 && r.stdout.trim()) || undefined;
|
|
51
|
+
}
|
|
52
|
+
// 変数・コマンド置換・`cd -` は解決しない
|
|
53
|
+
// Git Bash の `/c/...` は Windows のパス `C:/...` に変換
|
|
54
|
+
function resolveDir(base, dir) {
|
|
55
|
+
if (dir === '-' || /[$`]/.test(dir))
|
|
56
|
+
return undefined;
|
|
57
|
+
const home = dir === '~' || dir.startsWith('~/') ? join(homedir(), dir.slice(1)) : dir;
|
|
58
|
+
const native = process.platform === 'win32' ? home.replace(/^\/([a-zA-Z])(?=\/|$)/, '$1:') : home;
|
|
59
|
+
return isAbsolute(native) ? resolve(native) : resolve(base ?? root, native);
|
|
60
|
+
}
|
|
48
61
|
function currentBranch(cwd) {
|
|
49
62
|
const r = spawnSync('git', ['branch', '--show-current'], { cwd: cwd ?? root, encoding: 'utf8' });
|
|
50
63
|
return (r.status === 0 && r.stdout.trim()) || undefined;
|
package/dist/policy.d.ts
CHANGED
|
@@ -8,10 +8,14 @@ export type ToolUse = {
|
|
|
8
8
|
export type Checks = 'pass' | 'pending' | 'fail' | 'none' | 'unknown';
|
|
9
9
|
/** 判定に必要な外部の処理 */
|
|
10
10
|
export type Context = {
|
|
11
|
-
/**
|
|
11
|
+
/** セッションのリポのルート(作業ディレクトリの git のルートが不明なときの判定の対象) */
|
|
12
12
|
root: string;
|
|
13
|
+
/** 作業ディレクトリの git のルート(git の外・取得の失敗時は `undefined`) */
|
|
14
|
+
repoRoot: (cwd: string | undefined) => string | undefined;
|
|
15
|
+
/** `cd` の移動先(`base` からの相対・`~` などを解決。解決できなければ `undefined`) */
|
|
16
|
+
resolveDir: (base: string | undefined, dir: string) => string | undefined;
|
|
13
17
|
/** PR のタイトルの指摘(なければ空配列) */
|
|
14
|
-
titleIssues: (title: string) => string[];
|
|
18
|
+
titleIssues: (title: string, cwd: string | undefined) => string[];
|
|
15
19
|
/** ファイルの内容(読み込みの失敗時は `undefined`) */
|
|
16
20
|
readFile: (path: string) => string | undefined;
|
|
17
21
|
/** 現在のブランチ(detached HEAD・取得の失敗時は `undefined`) */
|
package/dist/policy.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// PreToolUse で拒否するツールの実行
|
|
2
2
|
// - 判定は純粋な関数 → 外部の処理(タイトルの検査・ファイルの読み込み・git・gh)は `Context` で注入
|
|
3
3
|
// - 拒否の理由は Claude に返却 → Claude が修正して再実行
|
|
4
|
+
// - 対象のリポ・ブランチはコマンドから解決: `cd`・`git -C` の後はその git のルート、ブランチの切り替えの後はその先のブランチ
|
|
4
5
|
import { isAbsolute, join } from 'node:path';
|
|
5
6
|
import { commands, option } from "./shell.js";
|
|
6
7
|
const TYPES = ['build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'style', 'test'];
|
|
@@ -43,22 +44,40 @@ export function denyReason(use, ctx) {
|
|
|
43
44
|
}
|
|
44
45
|
if (use.tool_name !== 'Bash')
|
|
45
46
|
return undefined;
|
|
46
|
-
const
|
|
47
|
+
const st = { cwd: use.cwd };
|
|
48
|
+
const reasons = commands(String(use.tool_input.command ?? '')).flatMap((words) => commandReasons(words, st, ctx));
|
|
47
49
|
return reasons.length ? reasons.join('\n') : undefined;
|
|
48
50
|
}
|
|
49
|
-
|
|
51
|
+
// 判定の後に、`cd`・ブランチの切り替えを状態に反映
|
|
52
|
+
function commandReasons(words, st, ctx) {
|
|
50
53
|
const [cmd, ...args] = words;
|
|
54
|
+
if (cmd === 'cd' || cmd === 'pushd') {
|
|
55
|
+
const dir = args.find((a) => !a.startsWith('-'));
|
|
56
|
+
const next = dir === undefined ? undefined : ctx.resolveDir(st.cwd, dir);
|
|
57
|
+
if (next !== undefined && next !== st.cwd)
|
|
58
|
+
Object.assign(st, { cwd: next, branch: undefined });
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
51
61
|
if (cmd === 'yarn' || (cmd === 'npm' && NPM_CHANGES.includes(args[0] ?? '')))
|
|
52
62
|
return ['パッケージ管理は pnpm のみ(`pnpm add`・`pnpm install` など)'];
|
|
53
|
-
if (cmd === 'git')
|
|
54
|
-
|
|
63
|
+
if (cmd === 'git') {
|
|
64
|
+
const { dir, rest } = gitArgs(args);
|
|
65
|
+
// `git -C <path>` はその場所の git で判定し、切り替えは状態に反映しない
|
|
66
|
+
const target = dir === undefined ? st : { cwd: ctx.resolveDir(st.cwd, dir) ?? st.cwd };
|
|
67
|
+
const reasons = gitReasons(rest, target, ctx);
|
|
68
|
+
const branch = dir === undefined ? switchedBranch(rest[0], rest.slice(1)) : undefined;
|
|
69
|
+
if (branch !== undefined)
|
|
70
|
+
st.branch = branch;
|
|
71
|
+
return reasons;
|
|
72
|
+
}
|
|
55
73
|
if (cmd === 'gh' && args[0] === 'pr')
|
|
56
|
-
return prReasons(args[1], args.slice(2),
|
|
74
|
+
return prReasons(args[1], args.slice(2), st, ctx);
|
|
57
75
|
return [];
|
|
58
76
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
77
|
+
const branchOf = (st, ctx) => st.branch ?? ctx.currentBranch(st.cwd);
|
|
78
|
+
// epic のフローの opt-in: 対象のリポのルートの `package.json` の `"ryusenkei": { "flow": "epic" }`
|
|
79
|
+
function epicFlow(st, ctx) {
|
|
80
|
+
const raw = ctx.readFile(join(ctx.repoRoot(st.cwd) ?? ctx.root, 'package.json'));
|
|
62
81
|
if (raw === undefined)
|
|
63
82
|
return false;
|
|
64
83
|
try {
|
|
@@ -69,11 +88,30 @@ function epicFlow(ctx) {
|
|
|
69
88
|
}
|
|
70
89
|
}
|
|
71
90
|
// git の全体のオプション(`-C <path>`・`-c <name=value>` など)を除去
|
|
91
|
+
// - `dir`: `-C` の場所(複数あれば git と同じく前から順に連結)
|
|
72
92
|
function gitArgs(args) {
|
|
73
93
|
let i = 0;
|
|
74
|
-
|
|
94
|
+
let dir;
|
|
95
|
+
while (i < args.length && args[i].startsWith('-')) {
|
|
96
|
+
const value = args[i + 1];
|
|
97
|
+
if (args[i] === '-C' && value !== undefined)
|
|
98
|
+
dir = dir === undefined || isAbsolute(value) ? value : join(dir, value);
|
|
75
99
|
i += args[i] === '-C' || args[i] === '-c' ? 2 : 1;
|
|
76
|
-
|
|
100
|
+
}
|
|
101
|
+
return { dir, rest: args.slice(i) };
|
|
102
|
+
}
|
|
103
|
+
// 切り替え先のブランチ(`git switch`・`git checkout`)
|
|
104
|
+
// - `git checkout <path>` と区別できない形(`--` を含む・位置引数が2つ以上)は対象外
|
|
105
|
+
function switchedBranch(sub, args) {
|
|
106
|
+
if (sub !== 'switch' && sub !== 'checkout')
|
|
107
|
+
return undefined;
|
|
108
|
+
const created = sub === 'switch' ? option(args, ['-c', '-C', '--create', '--force-create']) : option(args, ['-b', '-B']);
|
|
109
|
+
if (created !== undefined)
|
|
110
|
+
return created;
|
|
111
|
+
if (args.includes('--') || args.includes('--detach') || args.includes('-d'))
|
|
112
|
+
return undefined;
|
|
113
|
+
const names = args.filter((a) => !a.startsWith('-'));
|
|
114
|
+
return names.length === 1 && names[0] !== '-' ? names[0] : undefined;
|
|
77
115
|
}
|
|
78
116
|
// 位置引数(`--` 以降はすべて位置引数)
|
|
79
117
|
function positionals(args, valued) {
|
|
@@ -110,7 +148,7 @@ function hasShort(args, flag, valued = '') {
|
|
|
110
148
|
}
|
|
111
149
|
return false;
|
|
112
150
|
}
|
|
113
|
-
function gitReasons([sub, ...args],
|
|
151
|
+
function gitReasons([sub, ...args], st, ctx) {
|
|
114
152
|
if ((sub === 'commit' || sub === 'push') && args.includes('--no-verify'))
|
|
115
153
|
return ['フックの省略(`--no-verify`)は禁止'];
|
|
116
154
|
// `git commit -n` = `--no-verify`(`git push -n` は dry run)
|
|
@@ -121,11 +159,11 @@ function gitReasons([sub, ...args], cwd, ctx) {
|
|
|
121
159
|
if (sub === 'add' && bulkAdd(args))
|
|
122
160
|
return [BULK_STAGING];
|
|
123
161
|
if (sub === 'push')
|
|
124
|
-
return pushReasons(args,
|
|
162
|
+
return pushReasons(args, st, ctx);
|
|
125
163
|
const name = newBranch(sub, args);
|
|
126
164
|
if (name === undefined || BRANCH.test(name))
|
|
127
165
|
return [];
|
|
128
|
-
if (!epicFlow(ctx))
|
|
166
|
+
if (!epicFlow(st, ctx))
|
|
129
167
|
return [`ブランチ名は ${BRANCH_RULE}: \`${name}\``];
|
|
130
168
|
if (DEV_BRANCH.test(name))
|
|
131
169
|
return [];
|
|
@@ -139,21 +177,21 @@ function bulkAdd(args) {
|
|
|
139
177
|
return positionals(args, ADD_VALUE).some((p) => ALL_PATHS.includes(p));
|
|
140
178
|
}
|
|
141
179
|
// main・master への force push を拒否
|
|
142
|
-
function pushReasons(args,
|
|
180
|
+
function pushReasons(args, st, ctx) {
|
|
143
181
|
const forceAll = args.some((a) => a === '--force' || a.startsWith('--force-with-lease')) || hasShort(args, 'f', 'o');
|
|
144
182
|
if (args.includes('--all') || args.includes('--mirror'))
|
|
145
183
|
return forceAll ? [`\`--all\`・\`--mirror\` の force push は禁止(${PROTECTED.join('・')} を含む)。${ASK_USER}`] : [];
|
|
146
184
|
const [, ...refspecs] = positionals(args, PUSH_VALUE);
|
|
147
185
|
const targets = refspecs.length
|
|
148
|
-
? refspecs.map((r) => ({ force: forceAll || r.startsWith('+'), branch: pushTarget(r.replace(/^\+/, ''),
|
|
149
|
-
: [{ force: forceAll, branch: ctx
|
|
186
|
+
? refspecs.map((r) => ({ force: forceAll || r.startsWith('+'), branch: pushTarget(r.replace(/^\+/, ''), st, ctx) }))
|
|
187
|
+
: [{ force: forceAll, branch: branchOf(st, ctx) }];
|
|
150
188
|
const hit = targets.find((t) => t.force && t.branch !== undefined && PROTECTED.includes(t.branch));
|
|
151
189
|
return hit ? [`\`${hit.branch}\` への force push は禁止。${ASK_USER}`] : [];
|
|
152
190
|
}
|
|
153
|
-
function pushTarget(refspec,
|
|
191
|
+
function pushTarget(refspec, st, ctx) {
|
|
154
192
|
const dst = refspec.includes(':') ? refspec.slice(refspec.indexOf(':') + 1) : refspec;
|
|
155
193
|
const name = dst.replace(/^refs\/heads\//, '');
|
|
156
|
-
return name === 'HEAD' ? ctx
|
|
194
|
+
return name === 'HEAD' ? branchOf(st, ctx) : name;
|
|
157
195
|
}
|
|
158
196
|
// 作成・リネームするブランチの名前
|
|
159
197
|
function newBranch(sub, args) {
|
|
@@ -175,9 +213,10 @@ function newBranch(sub, args) {
|
|
|
175
213
|
return undefined;
|
|
176
214
|
}
|
|
177
215
|
}
|
|
178
|
-
function prReasons(sub, args,
|
|
216
|
+
function prReasons(sub, args, st, ctx) {
|
|
217
|
+
const { cwd } = st;
|
|
179
218
|
if (sub === 'merge')
|
|
180
|
-
return mergeReasons(args,
|
|
219
|
+
return mergeReasons(args, st, ctx);
|
|
181
220
|
if (sub !== 'create' && sub !== 'edit')
|
|
182
221
|
return [];
|
|
183
222
|
const reasons = [];
|
|
@@ -185,25 +224,26 @@ function prReasons(sub, args, cwd, ctx) {
|
|
|
185
224
|
if (title === undefined && sub === 'create')
|
|
186
225
|
reasons.push('PR のタイトルを `--title` で指定(`main` のコミットのメッセージになるため検査)');
|
|
187
226
|
if (title !== undefined)
|
|
188
|
-
reasons.push(...ctx.titleIssues(title).map((i) => `PR のタイトル: ${i}`));
|
|
227
|
+
reasons.push(...ctx.titleIssues(title, cwd).map((i) => `PR のタイトル: ${i}`));
|
|
189
228
|
const file = option(args, ['--body-file', '-F']);
|
|
190
229
|
const body = [option(args, ['--body', '-b']), file && file !== '-' ? ctx.readFile(isAbsolute(file) || !cwd ? file : join(cwd, file)) : undefined].join('\n');
|
|
191
230
|
if (AI_TRAILER.test(body) || AI_NOTE.test(body))
|
|
192
231
|
reasons.push('PR の本文に AI の署名を付けない');
|
|
193
|
-
if (epicFlow(ctx))
|
|
194
|
-
reasons.push(...epicReasons(sub, args,
|
|
232
|
+
if (epicFlow(st, ctx))
|
|
233
|
+
reasons.push(...epicReasons(sub, args, st, ctx));
|
|
195
234
|
return reasons;
|
|
196
235
|
}
|
|
197
236
|
// `--auto` も対象(必須のチェックがないリポでは即時にマージされる)
|
|
198
237
|
// - epic のフローの dev/* への PR: チェックなしを許可(CI を実行せず pre-push の `pnpm check` が関門)
|
|
199
|
-
function mergeReasons(args,
|
|
238
|
+
function mergeReasons(args, st, ctx) {
|
|
239
|
+
const { cwd } = st;
|
|
200
240
|
if (args.some((a) => a === '--merge' || a === '--rebase' || a === '-m' || a === '-r'))
|
|
201
241
|
return ['マージは squash のみ(`--squash`)'];
|
|
202
242
|
const pr = positionals(args, MERGE_VALUE)[0];
|
|
203
243
|
const checks = ctx.prChecks(pr, cwd);
|
|
204
244
|
if (checks === 'pass')
|
|
205
245
|
return [];
|
|
206
|
-
if (checks === 'none' && epicFlow(ctx)) {
|
|
246
|
+
if (checks === 'none' && epicFlow(st, ctx)) {
|
|
207
247
|
const base = ctx.prBase(pr, cwd);
|
|
208
248
|
if (base !== undefined && DEV_BRANCH.test(base))
|
|
209
249
|
return [];
|
|
@@ -213,12 +253,12 @@ function mergeReasons(args, cwd, ctx) {
|
|
|
213
253
|
// epic のフロー: main への PR は dev/* から、それ以外のブランチの PR は dev/* へ
|
|
214
254
|
// - `gh pr create` の base の省略 = main(既定のブランチ)
|
|
215
255
|
// - `gh pr edit` は base を変更する場合のみ
|
|
216
|
-
function epicReasons(sub, args,
|
|
256
|
+
function epicReasons(sub, args, st, ctx) {
|
|
217
257
|
const explicit = option(args, ['--base', '-B']);
|
|
218
258
|
if (sub === 'edit' && explicit === undefined)
|
|
219
259
|
return [];
|
|
220
260
|
const base = explicit ?? 'main';
|
|
221
|
-
const head = sub === 'create' ? (option(args, ['--head', '-H'])?.replace(/^.*:/, '') ?? ctx
|
|
261
|
+
const head = sub === 'create' ? (option(args, ['--head', '-H'])?.replace(/^.*:/, '') ?? branchOf(st, ctx)) : ctx.prHead(positionals(args, EDIT_VALUE)[0], st.cwd);
|
|
222
262
|
if (head !== undefined && DEV_BRANCH.test(head))
|
|
223
263
|
return [];
|
|
224
264
|
if (PROTECTED.includes(base))
|
package/dist/shell.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// - 区切り: `&&`・`||`・`;`・`|`・`&`・改行 → コマンドごとの語の配列
|
|
3
3
|
// - 解釈: 引用符(`'`・`"`)とバックスラッシュ → 変数・コマンド置換は展開しない
|
|
4
4
|
// - 先頭の `NAME=value`(環境変数の指定)は除去
|
|
5
|
+
// - heredoc の本文はコマンドとして分割しない。`"$(cat <<'EOF' ... EOF)"` のように引用符の中なら語の一部、外なら除去
|
|
6
|
+
const HEREDOC = /^<<(-?)[ \t]*(['"]?)([A-Za-z0-9_]+)\2/;
|
|
5
7
|
/** コマンド文字列をコマンドごとの語の配列に分解 */
|
|
6
8
|
export function commands(line) {
|
|
7
9
|
const out = [];
|
|
@@ -10,6 +12,30 @@ export function commands(line) {
|
|
|
10
12
|
// 空の引用符(`""`)も1語
|
|
11
13
|
let has = false;
|
|
12
14
|
let quote;
|
|
15
|
+
// 次の改行の後に本文を読む heredoc の終端の語(`<<-` は行頭のタブを除いて照合)
|
|
16
|
+
let heredocs = [];
|
|
17
|
+
// 本文を読み、終端の行の後の改行の位置を返す
|
|
18
|
+
const readHeredocs = (from) => {
|
|
19
|
+
let at = from;
|
|
20
|
+
const bodies = [];
|
|
21
|
+
for (const { end, tabs } of heredocs) {
|
|
22
|
+
const body = [];
|
|
23
|
+
while (at < line.length) {
|
|
24
|
+
const next = line.indexOf('\n', at);
|
|
25
|
+
const stop = next === -1 ? line.length : next;
|
|
26
|
+
const text = line.slice(at, stop);
|
|
27
|
+
at = stop + 1;
|
|
28
|
+
if ((tabs ? text.replace(/^\t+/, '') : text) === end)
|
|
29
|
+
break;
|
|
30
|
+
body.push(text);
|
|
31
|
+
}
|
|
32
|
+
bodies.push(body.join('\n'));
|
|
33
|
+
}
|
|
34
|
+
if (quote)
|
|
35
|
+
word += bodies.join('\n');
|
|
36
|
+
heredocs = [];
|
|
37
|
+
return at - 1;
|
|
38
|
+
};
|
|
13
39
|
const endWord = () => {
|
|
14
40
|
if (has)
|
|
15
41
|
words.push(word);
|
|
@@ -25,6 +51,25 @@ export function commands(line) {
|
|
|
25
51
|
};
|
|
26
52
|
for (let i = 0; i < line.length; i++) {
|
|
27
53
|
const c = line[i];
|
|
54
|
+
const heredoc = quote !== "'" && line.startsWith('<<', i) && line[i - 1] !== '<' && line[i + 2] !== '<' ? line.slice(i).match(HEREDOC) : null;
|
|
55
|
+
if (heredoc) {
|
|
56
|
+
heredocs.push({ end: heredoc[3], tabs: heredoc[1] === '-' });
|
|
57
|
+
if (quote)
|
|
58
|
+
word += heredoc[0];
|
|
59
|
+
else
|
|
60
|
+
endWord();
|
|
61
|
+
i += heredoc[0].length - 1;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (c === '\n' && heredocs.length) {
|
|
65
|
+
if (quote)
|
|
66
|
+
word += c;
|
|
67
|
+
else
|
|
68
|
+
endCommand();
|
|
69
|
+
// 終端の行の後の改行は通常の区切りとして次の周回で処理
|
|
70
|
+
i = readHeredocs(i + 1) - 1;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
28
73
|
if (quote) {
|
|
29
74
|
if (c === quote)
|
|
30
75
|
quote = undefined;
|
package/files/commit-msg
CHANGED
package/files/pre-push
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
#!/bin/sh
|
|
2
2
|
# push の前の検査(`check` のスクリプトを持つリポのみ。CI の失敗・Actions の消費を手元で防止)
|
|
3
|
-
#
|
|
3
|
+
# hostslot があれば資源の空きを待って実行(並行するエージェント・リポの重い検査によるメモリの枯渇を防止)
|
|
4
4
|
# `check` のないリポは枠を待たない
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
for gate in loadslot jobgate; do
|
|
8
|
-
if command -v "$gate" >/dev/null 2>&1; then
|
|
9
|
-
exec "$gate" run --weight 2 -- pnpm run check
|
|
10
|
-
fi
|
|
11
|
-
done
|
|
5
|
+
if grep -q '"check":' package.json 2>/dev/null && command -v hostslot >/dev/null 2>&1; then
|
|
6
|
+
exec hostslot run --weight 2 -- pnpm run check
|
|
12
7
|
fi
|
|
13
8
|
exec pnpm run --if-present check
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ryusenkei/conventions",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "RYUSENKEI のモノリポの共通の規約(Claude Code の規約・hook、共通ファイルの同期)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "RYUSENKEI",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"provenance": true
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@ryusenkei/prose-lint": "^0.
|
|
37
|
+
"@ryusenkei/prose-lint": "^0.2.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/node": "^26.6.1",
|
package/rules.md
CHANGED
|
@@ -1,58 +1,80 @@
|
|
|
1
1
|
## コード
|
|
2
2
|
|
|
3
|
-
-
|
|
4
|
-
-
|
|
5
|
-
-
|
|
6
|
-
- TypeScript
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
|
|
11
|
-
##
|
|
12
|
-
|
|
13
|
-
-
|
|
14
|
-
- `*.tfvars
|
|
15
|
-
- `.env.example
|
|
16
|
-
-
|
|
17
|
-
- YAML
|
|
18
|
-
- 共通ファイル(`.editorconfig
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
3
|
+
- パッケージの責務と依存は、README の構成の表に従う
|
|
4
|
+
- リポ内で共有するコードは `packages/*` に置く。リポをまたぐものは toolkit(npm の `@ryusenkei/*`)に置き、変更も toolkit で行う
|
|
5
|
+
- エントリポイントはパッケージごとの `src/index.ts`。export するのは外部で使うものだけで、他パッケージの再 export はしない
|
|
6
|
+
- TypeScript はビルドせず、Node で直接実行する(`node scripts/x.ts`)
|
|
7
|
+
- テストは Vitest で書き、対象と同じディレクトリに `*.test.ts` を置く
|
|
8
|
+
- 子プロセスは `scripts/lib/exec.ts` の `exec` で起動する。Windows の `.cmd` に対応するため
|
|
9
|
+
- 依存の更新は Dependabot の PR で行う。月1回で自動の rebase はしないので、マージの前に手で rebase する
|
|
10
|
+
|
|
11
|
+
## 設定と秘密値
|
|
12
|
+
|
|
13
|
+
- 秘密値はルートの `.env` にまとめ、配信は `pnpm env:push` だけで行う
|
|
14
|
+
- `*.tfvars` と Terraform の state はコミットしない
|
|
15
|
+
- `.env.example` に変数を足したら、`scripts/env.ts` の `TARGETS` にも定義する
|
|
16
|
+
- 環境変数を付けて実行するときは `node scripts/env.ts run NAME=value <cmd>` を使う。`NAME=value cmd` は Windows で動かない
|
|
17
|
+
- YAML の拡張子は `.yaml` にする(pnpm のファイルに合わせる)
|
|
18
|
+
- 共通ファイル(`.editorconfig`、`.github/dependabot.yaml`、`.claude/settings.json` など)は `@ryusenkei/conventions` から同期するので、直接編集しない。変えるときは toolkit で直し、`ryusenkei-conventions sync` で反映する
|
|
19
|
+
|
|
20
|
+
## 文章を書く場所
|
|
21
|
+
|
|
22
|
+
| 場所 | 性格 | 書くこと |
|
|
23
|
+
| --- | --- | --- |
|
|
24
|
+
| コード | 常に最新 | 意図は名前、型、関数の分け方で表す。コメントはその場所に欠かせない制約だけ |
|
|
25
|
+
| README | 変更と一緒に直す | 今の構成と設計の方針 |
|
|
26
|
+
| PR、issue | その時点の記録 | 経緯、検証、判断、採用しなかった案 |
|
|
27
|
+
|
|
28
|
+
- 決定の記録のファイル(ADR など)は置かない
|
|
29
|
+
- 個人名は書かない。コード、コメント、ドキュメント、設定値のすべてが対象
|
|
30
|
+
|
|
31
|
+
## 書く量
|
|
32
|
+
|
|
33
|
+
| 種類 | 書く | 書かない |
|
|
34
|
+
| --- | --- | --- |
|
|
35
|
+
| コードのコメント | 既定は書かない。書くのは、読み手が直そうとしてしまう一見おかしなコードの理由、コードから読み取れない外部の事実(仕様、プラットフォームの癖)、型で表せない単位や形式だけ | コードがしていることの説明、経緯、採用しなかった案、issue に結びつかない TODO |
|
|
36
|
+
| export の JSDoc | 名前と型以上の情報があるときだけ(単位、副作用、エラーコード、前提条件) | 名前を言い換えるだけの JSDoc |
|
|
37
|
+
| README | 何のリポか、動かし方、構成、今の設計の方針 | 経緯、判断の理由の長文、機能の宣伝 |
|
|
38
|
+
| PR | 背景(1〜3行)、レビューに要る粒度での変更、実際に行った検証 | 差分をなぞる説明、ファイルごとの実況 |
|
|
39
|
+
| issue | 問題、決めた範囲、未決の点。決まったら決定を書き足す | 調査の過程の全文 |
|
|
40
|
+
| コミットメッセージ | 1行で変更を言い切る | 本文での経緯の説明 |
|
|
41
|
+
|
|
42
|
+
説明が要ると感じたら、先にコードや構成を直せないか考える
|
|
43
|
+
|
|
44
|
+
## 書き方
|
|
45
|
+
|
|
46
|
+
- 語彙は慣例に合わせる。公式の文書の日本語訳や、業界でよく使う言い方を基準にし、安易に平易化や漢語化をしない
|
|
47
|
+
- 例: データの操作は「作成」「取得」「更新」「削除」(UI と API の慣例)。ソフトウェアの版は「バージョン」
|
|
48
|
+
- 常体の短い文で書く。体言止めや、記号(`→`、`・`)で語をつなぐ略記にしない
|
|
49
|
+
- 表は比較や対応表のように、中身が表の形のものに使う。飾りとして表や、太字の見出しを付けた箇条書きにしない
|
|
50
|
+
- エラー、ログ、標準出力はコード(識別子)で表す。人が読む文言は表示のマスタ(辞書)から引き、その場に日本語を書かない
|
|
51
|
+
- コード上の値と環境変数はバッククォートで囲む。ドメインの概念、説明、表示の文言は日本語で書く
|
|
52
|
+
- 文章を見直すときは、日本語の文書・issue・PR に yomiyasu、英語の README に humanizer のスキルを使う。出てくるのは見直しの候補で、点数を合格の基準にしない
|
|
53
|
+
- prose-lint が止めるのは機械的な誤り(空白、句読点、表記の揺れ)だけ。ドメインの用語が表記の規則に当たるときは、`prose-lint.json` の `allow` に足す
|
|
54
|
+
|
|
55
|
+
## Git と PR
|
|
56
|
+
|
|
57
|
+
- add は変更・追加したファイルをパスで指定する。一括の add はフックで止まる
|
|
58
|
+
- push の前に `pnpm check` を通す。`check` を持つリポでは pre-push で強制される
|
|
59
|
+
- PR の本文は背景、変更、検証の順に書く
|
|
60
|
+
- マージは CI の全チェックの成功を確かめてから行う(フックで強制)
|
|
61
|
+
- epic のフロー(有効にしたリポだけ)
|
|
62
|
+
- epic ごとに `dev/<issue の番号>-<内容>` を作り、sub-issue の PR は dev のブランチに向ける
|
|
63
|
+
- dev のブランチへのマージに CI は使わず、pre-push の `pnpm check` を関門にする。チェックがある場合は成功が必要
|
|
64
|
+
- `main` へのマージは、デプロイのときに dev のブランチからだけ行う
|
|
44
65
|
|
|
45
66
|
## CI(GitHub Actions)
|
|
46
67
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
68
|
+
非公開のリポでは GitHub Free の時間を節約する(上限は組織の合計で月2,000分、ジョブごとに分単位で切り上げて計上)
|
|
69
|
+
|
|
70
|
+
- 実行するのは `main` 行きの PR だけ。マージの結果の ref は PR で検査済みなので、`main` への push では実行しない
|
|
71
|
+
- 検査は1つのジョブに順に並べる。並列に分けると、切り上げと準備の時間が増える
|
|
72
|
+
- 同じ PR で新しい実行が始まったら、古い実行を中止する(`concurrency` の `cancel-in-progress`)
|
|
73
|
+
- PR のタイトルの変更(`edited`)は、タイトルだけを検査する別のワークフローで扱う。依存は入れない
|
|
74
|
+
- キャッシュのキーにコミットの ID(`github.sha`)を含めない。毎回保存が走り、復元が効かなくなる
|
|
75
|
+
- Windows と macOS のランナーは公開のリポだけで使う。非公開では2倍と10倍で計上される
|
|
54
76
|
|
|
55
77
|
## 完了の条件
|
|
56
78
|
|
|
57
|
-
- `pnpm lint
|
|
58
|
-
- README
|
|
79
|
+
- `pnpm lint`(型検査、prose-lint、共通ファイルの検査)と `pnpm test` が通る
|
|
80
|
+
- README の構成とコマンドの記述が変更と合っている
|