@kzmshx/chiken 0.1.2 → 0.1.4

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/dist/cli.mjs CHANGED
@@ -12,7 +12,7 @@ import path7 from "node:path";
12
12
  import fs from "node:fs";
13
13
  import path from "node:path";
14
14
  var KIND = { study: "\u8ABF\u3079\u308B", try: "\u8A66\u3059", change: "\u5909\u3048\u308B" };
15
- var LINK = { pre: "\u524D\u63D0\u306B\u306A\u308B", ease: "\u9032\u3081\u308B\u3068\u697D\u306B\u306A\u308B", same: "\u540C\u3058\u5F62" };
15
+ var LINK = { pre: "\u524D\u63D0\u306B\u306A\u308B", ease: "\u9032\u3081\u308B\u3068\u697D\u306B\u306A\u308B", same: "\u4F3C\u305F\u69CB\u9020" };
16
16
  var ALINK = { pre: "\u524D\u306B\u8981\u308B", alt: "\u3069\u3061\u3089\u304B" };
17
17
  var ORIGIN = { self: "\u81EA\u5206\u304C\u611F\u3058\u3066\u3044\u308B", ai: "AI \u306E\u63D0\u6848" };
18
18
  var AUTHOR = { self: "\u53C2\u52A0\u8005", external: "\u767B\u58C7\u30FB\u8CC7\u6599\u30FB\u8A18\u4E8B", ai: "AI \u306E\u63D0\u6848" };
@@ -169,11 +169,51 @@ var LAYOUT = {
169
169
  sources: "sources",
170
170
  exports: "exports"
171
171
  };
172
+ var KEYS = {
173
+ config: ["name", "title", "lead", "linkBase", "rules", "exports"],
174
+ export: ["name", "title", "subtitle", "sources", "problems", "firsthand", "include", "exclude", "rules", "out", "release", "contexts", "start"],
175
+ rules: ["replace", "drop", "forbid", "lead", "areaSum", "priorityText"],
176
+ areas: ["contexts", "areas"],
177
+ context: ["id", "name", "sum"],
178
+ area: ["id", "name", "context", "sum"],
179
+ problem: ["id", "area", "origin", "before", "groups", "session", "updated"],
180
+ action: ["id", "kind", "origin", "problem", "also", "needs", "basis", "groups", "updated"],
181
+ collection: ["id", "kind", "type", "name", "dates", "days", "sessionMinutes", "photos", "summary", "url"],
182
+ source: ["id", "medium", "taken", "type", "title", "org", "who", "date", "day", "time", "url", "slides", "sum", "take", "notes", "points", "insights", "groups"],
183
+ insight: ["n", "text", "kind", "author", "who", "problems", "verify", "groups"]
184
+ };
185
+ var SENSITIVE = ["groups", "include", "exclude", "rules"];
186
+ var PASSTHROUGH = ["tags", "aliases", "summary", "date", "created", "cssclasses", "publish"];
187
+ var NOTE_KINDS = ["problem", "action", "collection", "source"];
188
+ function nearSensitive(k) {
189
+ const d = (a, b) => {
190
+ const m = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]);
191
+ for (let j = 1; j <= b.length; j++) m[0][j] = j;
192
+ for (let i = 1; i <= a.length; i++) for (let j = 1; j <= b.length; j++) m[i][j] = Math.min(m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
193
+ return m[a.length][b.length];
194
+ };
195
+ return SENSITIVE.find((s) => d(k.toLowerCase(), s) <= 2);
196
+ }
197
+ function unknownKeys(o, kind, where, issues) {
198
+ if (!o || typeof o !== "object" || Array.isArray(o)) return;
199
+ for (const k of Object.keys(o)) {
200
+ if (KEYS[kind].includes(k)) continue;
201
+ if (NOTE_KINDS.includes(kind) && PASSTHROUGH.includes(k)) continue;
202
+ const near = nearSensitive(k);
203
+ if (near || kind === "export" || kind === "rules") issues.push({ level: "error", msg: `${where}: \u77E5\u3089\u306A\u3044\u9805\u76EE ${k}${near ? `\uFF08${near} \u306E\u7DB4\u308A\u306E\u8AA4\u308A\u306A\u3089\u3001\u96A0\u3059\u3064\u3082\u308A\u306E\u3082\u306E\u304C\u51FA\u3066\u3057\u307E\u3046\uFF09` : ""}` });
204
+ else issues.push({ level: "warn", msg: `${where}: \u77E5\u3089\u306A\u3044\u9805\u76EE ${k}\uFF08chiken \u306F\u8AAD\u307E\u306A\u3044\uFF09` });
205
+ }
206
+ }
172
207
  function readMd(file) {
173
208
  const text = fs2.readFileSync(file, "utf8").replace(/\r\n/g, "\n");
174
209
  const m = /^---\n([\s\S]*?)\n---\n?/.exec(text);
175
210
  if (!m) return { fm: {}, body: text };
176
- return { fm: YAML.parse(m[1]) ?? {}, body: text.slice(m[0].length) };
211
+ try {
212
+ return { fm: YAML.parse(m[1]) ?? {}, body: text.slice(m[0].length) };
213
+ } catch (e) {
214
+ throw new Error(`${file}: frontmatter \u306E YAML \u304C\u8AAD\u3081\u306A\u3044\uFF08git \u306E\u7AF6\u5408\u306E\u5370\u304C\u6B8B\u3063\u3066\u3044\u306A\u3044\u304B\uFF09
215
+ ${e.message}`);
216
+ }
177
217
  }
178
218
  var mdFiles = (dir) => fs2.existsSync(dir) ? fs2.readdirSync(dir, { recursive: true }).map(String).filter((f) => f.endsWith(".md")).map((f) => path2.join(dir, f)) : [];
179
219
  var title = (body) => /^#\s+(.+)$/m.exec(body)?.[1].trim() ?? "";
@@ -183,8 +223,8 @@ function section(body, h) {
183
223
  return t || void 0;
184
224
  }
185
225
  var bullets = (t) => t ? t.split("\n").filter((l) => l.startsWith("- ")).map((l) => l.slice(2).trim()) : void 0;
186
- var need = (v, file, key) => {
187
- if (v === void 0 || v === null || v === "") throw new Error(`${file}: frontmatter \u306B ${key} \u304C\u306A\u3044`);
226
+ var need = (v, file, key2) => {
227
+ if (v === void 0 || v === null || v === "") throw new Error(`${file}: frontmatter \u306B ${key2} \u304C\u306A\u3044`);
188
228
  return String(v);
189
229
  };
190
230
  var str = (v) => v === void 0 || v === null ? void 0 : String(v);
@@ -192,10 +232,18 @@ var arr = (v) => Array.isArray(v) ? v.map(String) : void 0;
192
232
  function readRoot(root) {
193
233
  const config = YAML.parse(fs2.readFileSync(path2.join(root, LAYOUT.config), "utf8"));
194
234
  const legacyShare = [];
235
+ const issues = [];
236
+ const rel = (f) => path2.relative(root, f);
237
+ unknownKeys(config, "config", LAYOUT.config, issues);
238
+ for (const e of config.exports ?? []) unknownKeys(e, "export", `${LAYOUT.config} \u306E exports \u306E ${e.name}`, issues);
239
+ for (const [n, r] of Object.entries(config.rules ?? {})) unknownKeys(r, "rules", `${LAYOUT.config} \u306E rules \u306E ${n}`, issues);
195
240
  const files = /* @__PURE__ */ new Map();
196
241
  const areasFm = readMd(path2.join(root, LAYOUT.areas)).fm;
197
242
  const contexts = (areasFm.contexts ?? []).map((c) => ({ id: String(c.id), name: String(c.name), ...c.sum ? { sum: String(c.sum) } : {} }));
198
243
  if (!Array.isArray(areasFm.areas)) throw new Error(`${path2.join(root, LAYOUT.areas)}: frontmatter \u306B areas \u304C\u306A\u3044`);
244
+ unknownKeys(areasFm, "areas", LAYOUT.areas, issues);
245
+ for (const c of areasFm.contexts ?? []) unknownKeys(c, "context", `${LAYOUT.areas} \u306E contexts \u306E ${c.id}`, issues);
246
+ for (const a of areasFm.areas) unknownKeys(a, "area", `${LAYOUT.areas} \u306E areas \u306E ${a.id}`, issues);
199
247
  const problemAreas = areasFm.areas.map((a) => ({ id: a.id, name: a.name, sum: a.sum, ...a.context ? { context: String(a.context) } : {} }));
200
248
  const problems = [];
201
249
  const problemLinks = [];
@@ -204,6 +252,7 @@ function readRoot(root) {
204
252
  const id = need(fm.id, f, "id");
205
253
  files.set(id, f);
206
254
  if (fm.share !== void 0) legacyShare.push(id);
255
+ unknownKeys(Object.fromEntries(Object.entries(fm).filter(([k]) => k !== "share")), "problem", rel(f), issues);
207
256
  const p = { id, area: String(fm.area), origin: fm.origin, name: title(body), now: bullets(section(body, "\u4ECA\u306E\u72B6\u614B")) ?? [] };
208
257
  const pg = arr(fm.groups);
209
258
  if (pg?.length) p.groups = pg;
@@ -236,6 +285,7 @@ function readRoot(root) {
236
285
  if (check2) a.check = check2;
237
286
  if (done) a.done = done;
238
287
  if (fm.share !== void 0) legacyShare.push(id);
288
+ unknownKeys(Object.fromEntries(Object.entries(fm).filter(([k]) => k !== "share")), "action", rel(f), issues);
239
289
  const ag = arr(fm.groups);
240
290
  if (ag?.length) a.groups = ag;
241
291
  if (detailed) a.doc = body.replace(/<!--[\s\S]*?-->/g, "").trim().replace(/^#\s.*\n+/, "").trim();
@@ -250,15 +300,28 @@ function readRoot(root) {
250
300
  const indexFiles = mdFiles(path2.join(root, LAYOUT.sources)).filter((f) => path2.basename(f) === "_index.md");
251
301
  const collectionDirs = new Map(indexFiles.map((f) => [path2.dirname(f), readMd(f).fm]));
252
302
  for (const [dir, fm] of collectionDirs) {
253
- files.set(String(fm.id), path2.join(dir, "_index.md"));
303
+ const idx = path2.join(dir, "_index.md");
304
+ if (!fm.id) {
305
+ issues.push({ level: "error", msg: `${rel(idx)}: id \u304C\u306A\u3044\uFF08\u307E\u3068\u307E\u308A\u3068\u3057\u3066\u8AAD\u307E\u306A\u3044\uFF09` });
306
+ continue;
307
+ }
308
+ if (collections.some((c) => c.id === String(fm.id))) issues.push({ level: "error", msg: `\u307E\u3068\u307E\u308A\u306E ID \u306E\u91CD\u306A\u308A: ${fm.id}\uFF08${rel(idx)}\uFF09` });
309
+ unknownKeys(fm, "collection", rel(idx), issues);
310
+ files.set(String(fm.id), idx);
254
311
  collections.push({ id: String(fm.id), kind: String(fm.kind ?? "event"), type: str(fm.type), dates: arr(fm.dates), dir: path2.relative(root, dir), name: String(fm.name), days: fm.days, sessionMinutes: fm.sessionMinutes, photos: str(fm.photos), summary: fm.summary });
255
312
  }
256
313
  const collectionOf = (f) => [...collectionDirs.entries()].find(([dir]) => f.startsWith(dir + path2.sep));
257
314
  for (const f of mdFiles(path2.join(root, LAYOUT.sources)).filter((x) => path2.basename(x) !== "_index.md").sort()) {
258
315
  const { fm } = readMd(f);
259
- if (!fm.id) continue;
316
+ if (!fm.id) {
317
+ if (Object.keys(fm).length) issues.push({ level: "error", msg: `${rel(f)}: id \u304C\u306A\u3044\uFF08\u30A4\u30F3\u30D7\u30C3\u30C8\u3068\u3057\u3066\u8AAD\u307E\u306A\u3044\uFF09` });
318
+ continue;
319
+ }
260
320
  const sid = String(fm.id);
321
+ if (fm.share !== void 0) legacyShare.push(sid);
322
+ unknownKeys(Object.fromEntries(Object.entries(fm).filter(([k]) => k !== "share")), "source", rel(f), issues);
261
323
  files.set(sid, f);
324
+ if (fm.medium === void 0) issues.push({ level: "warn", msg: `${rel(f)}: medium \u304C\u306A\u3044\uFF08article \u3068\u307F\u306A\u3057\u305F\uFF09` });
262
325
  const medium = String(fm.medium ?? "article");
263
326
  const b = collectionOf(f);
264
327
  const isSession = medium === "talk" && !!b && fm.day !== void 0;
@@ -295,6 +358,11 @@ function readRoot(root) {
295
358
  insightSources.push(src);
296
359
  const list2 = fm.insights ?? [];
297
360
  for (const i of list2) {
361
+ if (i.n === void 0 || i.n === null || !/^\d+$/.test(String(i.n))) {
362
+ issues.push({ level: "error", msg: `${rel(f)}: \u756A\u53F7 n \u306E\u306A\u3044\u77E5\u898B\u304C\u3042\u308B\uFF08${String(i.text ?? "").slice(0, 30)}\u2026\uFF09` });
363
+ continue;
364
+ }
365
+ unknownKeys(Object.fromEntries(Object.entries(i).filter(([k]) => k !== "share")), "insight", `${rel(f)} \u306E\u77E5\u898B ${i.n}`, issues);
298
366
  const ins = { id: `${sid}#${i.n}`, source: sid, text: String(i.text), kind: i.kind, author: i.author, problems: arr(i.problems) ?? [] };
299
367
  if (i.who) ins.who = String(i.who);
300
368
  if (i.verify) ins.verify = String(i.verify);
@@ -319,7 +387,7 @@ function readRoot(root) {
319
387
  actions,
320
388
  actionLinks
321
389
  };
322
- return { root, config, collections, map, legacyShare, sourceOf, files };
390
+ return { root, config, collections, map, legacyShare, issues, sourceOf, files };
323
391
  }
324
392
 
325
393
  // node/lib/cue.ts
@@ -373,21 +441,19 @@ function cue(args) {
373
441
  throw new Error(er.stderr || String(e));
374
442
  }
375
443
  }
376
- var warned = false;
377
444
  function vetMap(map) {
378
- const file = path4.join(CACHE, "map.json");
379
- fs4.mkdirSync(path4.dirname(file), { recursive: true });
445
+ fs4.mkdirSync(CACHE, { recursive: true });
446
+ const dir = fs4.mkdtempSync(path4.join(CACHE, "vet-"));
447
+ const file = path4.join(dir, "map.json");
380
448
  fs4.writeFileSync(file, JSON.stringify({ ...map, flow: void 0, actions: map.actions.map(({ doc: _d, ...a }) => a) }));
381
449
  try {
382
450
  cue(["vet", "-c", "./schema/model.cue", "./schema/check.cue", file, "-d", "#Checked"]);
383
451
  } catch (e) {
384
- if (e instanceof NoCue) {
385
- if (!warned) console.error("cue \u304C\u306A\u3044\u306E\u3067\u3001schema \u306E\u691C\u67FB\uFF08cue vet\uFF09\u306F\u98DB\u3070\u3057\u305F\u3002\u5165\u308C\u308B\u306A\u3089 https://cuelang.org/docs/introduction/installation/");
386
- warned = true;
387
- return;
388
- }
452
+ if (e instanceof NoCue) return;
389
453
  throw new Error(`\u30C7\u30FC\u30BF\u30D5\u30A9\u30EB\u30C0\u304B\u3089\u7D44\u307F\u7ACB\u3066\u305F\u5730\u56F3\u304C schema \u306B\u5408\u308F\u306A\u3044:
390
454
  ${e.message}`);
455
+ } finally {
456
+ fs4.rmSync(dir, { recursive: true, force: true });
391
457
  }
392
458
  }
393
459
 
@@ -422,6 +488,117 @@ var EVENT_TYPE = {
422
488
  };
423
489
  var collectionLabel = (kind, type) => type && EVENT_TYPE[type] || COLLECTION[kind] || kind;
424
490
 
491
+ // node/root/check.ts
492
+ function check(v) {
493
+ const f = [];
494
+ const err = (msg) => f.push({ level: "error", msg });
495
+ const warn = (msg) => f.push({ level: "warn", msg });
496
+ const m = v.map;
497
+ const dup = (xs, what) => {
498
+ const seen = /* @__PURE__ */ new Set();
499
+ for (const x of xs) {
500
+ if (seen.has(x)) err(`${what} \u306E\u91CD\u306A\u308A: ${x}`);
501
+ seen.add(x);
502
+ }
503
+ };
504
+ for (const i of v.issues) (i.level === "error" ? err : warn)(i.msg);
505
+ for (const c of m.contexts) if (!/^[a-z][a-z0-9]*$/.test(c.id)) err(`\u30B3\u30F3\u30C6\u30AD\u30B9\u30C8\u306E ID \u306E\u5F62: ${c.id}\uFF08\u82F1\u5C0F\u6587\u5B57\u3067\u59CB\u307E\u308B\u82F1\u5C0F\u6587\u5B57\u3068\u6570\u5B57\uFF09`);
506
+ for (const a of m.problemAreas) if (!/^[a-z]+$/.test(a.id)) err(`\u9818\u57DF\u306E ID \u306E\u5F62: ${a.id}\uFF08\u82F1\u5C0F\u6587\u5B57\u3060\u3051\uFF09`);
507
+ for (const p of m.problems) if (!p.now.length) err(`${p.id} \u306E\u300C\u4ECA\u306E\u72B6\u614B\u300D\u304C\u306A\u3044\uFF08\u672C\u6587\u306E ## \u4ECA\u306E\u72B6\u614B \u306B\u7B87\u6761\u3067\u66F8\u304F\uFF09`);
508
+ for (const x of m.insightSources) {
509
+ if (x.medium && !(x.medium in MEDIUM)) err(`${x.id} \u306E\u5A92\u4F53\uFF08medium\uFF09\u306E\u5024: ${x.medium}\uFF08${Object.keys(MEDIUM).join(" / ")}\uFF09`);
510
+ if (x.taken && !(x.taken in TAKEN)) err(`${x.id} \u306E\u53D7\u3051\u53D6\u308A\u65B9\uFF08taken\uFF09\u306E\u5024: ${x.taken}\uFF08${Object.keys(TAKEN).join(" / ")}\uFF09`);
511
+ if (x.noteType && !["research", "plan", "record"].includes(x.noteType)) err(`${x.id} \u306E\u30E1\u30E2\u306E\u7A2E\u5225\uFF08type\uFF09\u306E\u5024: ${x.noteType}\uFF08research / plan / record\uFF09`);
512
+ if (x.date && !(/^\d{4}-\d{2}-\d{2}$/.test(x.date) && !Number.isNaN(Date.parse(x.date)))) err(`${x.id} \u306E\u65E5\u4ED8\uFF08date\uFF09\u306E\u5F62: ${x.date}\uFF08YYYY-MM-DD\uFF09`);
513
+ }
514
+ for (const s of m.sessions) if (!/^\d{1,2}:\d{2}$/.test(s.time)) err(`${s.id} \u306E\u6642\u523B\uFF08time\uFF09\u306E\u5F62: ${s.time}\uFF08HH:MM\uFF09`);
515
+ dup(m.problems.map((p) => p.id), "\u8AB2\u984C\u306E ID");
516
+ dup(m.actions.map((a) => a.id), "\u30A2\u30AF\u30B7\u30E7\u30F3\u306E ID");
517
+ dup(m.insights.map((i) => i.id), "\u77E5\u898B\u306E\u756A\u53F7");
518
+ dup([...v.sourceOf.keys()], "\u30A4\u30F3\u30D7\u30C3\u30C8\u306E ID");
519
+ for (const p of m.problems) if (!/^P\d{2,}$/.test(p.id)) err(`\u8AB2\u984C\u306E ID \u306E\u5F62: ${p.id}`);
520
+ for (const a of m.actions) if (!/^A\d{3,}$/.test(a.id)) err(`\u30A2\u30AF\u30B7\u30E7\u30F3\u306E ID \u306E\u5F62: ${a.id}`);
521
+ const P = new Set(m.problems.map((p) => p.id));
522
+ const A = new Set(m.actions.map((a) => a.id));
523
+ const I = new Set(m.insights.map((i) => i.id));
524
+ const areas = new Set(m.problemAreas.map((a) => a.id));
525
+ const ctx = new Set(m.contexts.map((c) => c.id));
526
+ for (const a of m.problemAreas) if (a.context && !ctx.has(a.context)) err(`\u9818\u57DF ${a.id} \u306E\u30B3\u30F3\u30C6\u30AD\u30B9\u30C8\u304C\u306A\u3044: ${a.context}\uFF08areas.md \u306E contexts\uFF09`);
527
+ for (const p of m.problems) {
528
+ if (!areas.has(p.area)) err(`${p.id} \u306E\u9818\u57DF\u304C\u306A\u3044: ${p.area}`);
529
+ if (!["self", "ai"].includes(p.origin)) err(`${p.id} \u306E\u51FA\u3069\u3053\u308D\uFF08origin\uFF09\u306E\u5024: ${p.origin}\uFF08self / ai\uFF09`);
530
+ }
531
+ for (const l of m.problemLinks) {
532
+ if (!P.has(l.up)) err(`${l.down} \u306E before\uFF08\u524D\u306B\u3042\u308B\u8AB2\u984C\uFF09\u306B\u3001\u306A\u3044\u8AB2\u984C: ${l.up}`);
533
+ if (!["pre", "ease", "same"].includes(l.kind)) err(`${l.down} \u306E before \u306E\u7A2E\u985E\u306E\u5024: ${l.kind}\uFF08pre / ease / same\uFF09`);
534
+ }
535
+ for (const a of m.actions) {
536
+ if (!P.has(a.problem)) err(`${a.id} \u306E\u4E3B\u306A\u8AB2\u984C\u304C\u306A\u3044: ${a.problem}`);
537
+ for (const x of a.also ?? []) if (!P.has(x)) err(`${a.id} \u306E\u3042\u308F\u305B\u3066\u9032\u3080\u8AB2\u984C\u304C\u306A\u3044: ${x}`);
538
+ for (const b of a.basis ?? []) if (!I.has(b)) err(`${a.id} \u306E\u6839\u62E0\u306E\u77E5\u898B\u304C\u306A\u3044: ${b}`);
539
+ if (!["study", "try", "change"].includes(a.kind)) err(`${a.id} \u306E\u7A2E\u985E\uFF08kind\uFF09\u306E\u5024: ${a.kind}\uFF08study / try / change\uFF09`);
540
+ if (!a.basis?.length) warn(`${a.id} \u306B\u6839\u62E0\u306E\u77E5\u898B\u304C\u306A\u3044\uFF08chiken set ${a.id} basis <\u77E5\u898B>,\u2026\uFF09`);
541
+ if (!a.why) warn(`${a.id} \u306B\u300C\u52B9\u304D\u305D\u3046\u306A\u7406\u7531\u300D\u304C\u306A\u3044\uFF08\u672C\u6587\u306E ## \u52B9\u304D\u305D\u3046\u306A\u7406\u7531 \u306B\u66F8\u304F\uFF09`);
542
+ }
543
+ for (const l of m.actionLinks) {
544
+ if (!A.has(l.from)) err(`${l.to} \u306E needs\uFF08\u524D\u306B\u8981\u308B\u30A2\u30AF\u30B7\u30E7\u30F3\uFF09\u306B\u3001\u306A\u3044\u30A2\u30AF\u30B7\u30E7\u30F3: ${l.from}`);
545
+ if (!["pre", "alt"].includes(l.kind)) err(`${l.to} \u306E needs \u306E\u7A2E\u985E\u306E\u5024: ${l.kind}\uFF08pre / alt\uFF09`);
546
+ }
547
+ for (const i of m.insights) {
548
+ for (const p of i.problems) if (!P.has(p)) err(`${i.id} \u306E\u624B\u304C\u304B\u308A\u306B\u306A\u308B\u8AB2\u984C\u304C\u306A\u3044: ${p}`);
549
+ if (!["fact", "impression"].includes(i.kind)) err(`${i.id} \u306E\u7A2E\u985E\uFF08kind\uFF09\u306E\u5024: ${i.kind}\uFF08fact / impression\uFF09`);
550
+ if (!["self", "external", "ai"].includes(i.author)) err(`${i.id} \u306E\u66F8\u304D\u624B\uFF08author\uFF09\u306E\u5024: ${i.author}\uFF08self / external / ai\uFF09`);
551
+ }
552
+ const G = /^[a-z0-9][a-z0-9-]*$/;
553
+ const groupsOf = [...m.problems.map((p) => [p.id, p.groups]), ...m.actions.map((a) => [a.id, a.groups]), ...m.insightSources.map((x) => [x.id, x.groups]), ...m.insights.map((i) => [i.id, i.groups])];
554
+ for (const [id, gs] of groupsOf) for (const g of gs ?? []) if (!G.test(g)) err(`${id} \u306E\u30B0\u30EB\u30FC\u30D7\u306E\u540D\u524D: ${g}\uFF08\u82F1\u5C0F\u6587\u5B57\u30FB\u6570\u5B57\u30FB\u30CF\u30A4\u30D5\u30F3\uFF09`);
555
+ for (const id of v.legacyShare) err(`${id} \u306B share \u304C\u3042\u308B\u3002\u4ECA\u306F groups \u3067\u66F8\u304F\uFF08share: self \u306F groups: [private] \u306B\u3057\u3001\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8\u3067 exclude: [private]\uFF09`);
556
+ const used = new Set(groupsOf.flatMap(([, gs]) => gs ?? []));
557
+ for (const e of v.config.exports ?? []) {
558
+ if (e.rules && !v.config.rules?.[e.rules]) err(`\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8 ${e.name} \u306E rules \u304C\u306A\u3044: ${e.rules}\uFF08chiken.yaml \u306E rules\uFF09`);
559
+ for (const g of [...e.include ?? [], ...e.exclude ?? []]) if (!used.has(g)) warn(`\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8 ${e.name} \u306E\u30B0\u30EB\u30FC\u30D7 ${g} \u3092\u6301\u3064\u3082\u306E\u304C\u306A\u3044\uFF08\u7DB4\u308A\u3092\u78BA\u304B\u3081\u308B\uFF09`);
560
+ if (e.audience !== void 0) err(`\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8 ${e.name} \u306B audience \u304C\u3042\u308B\u3002\u4ECA\u306F exclude \u3068 rules \u3067\u66F8\u304F\uFF08ADR-0011\uFF09`);
561
+ }
562
+ if (v.config.org !== void 0) err("chiken.yaml \u306B org \u304C\u3042\u308B\u3002\u4ECA\u306F rules \u306B\u540D\u524D\u3092\u4ED8\u3051\u3066\u66F8\u304D\u3001\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8\u306E rules \u3067\u6307\u3059\uFF08ADR-0011\uFF09");
563
+ const cyc = (edges, what) => {
564
+ const g = /* @__PURE__ */ new Map();
565
+ for (const [a, b] of edges) g.set(a, [...g.get(a) ?? [], b]);
566
+ const state = /* @__PURE__ */ new Map();
567
+ const dfs = (n, trail) => {
568
+ state.set(n, 1);
569
+ for (const x of g.get(n) ?? []) {
570
+ if (state.get(x) === 1) err(`${what}\u306E\u8F2A: ${[...trail, n, x].join(" \u2192 ")}`);
571
+ else if (!state.has(x)) dfs(x, [...trail, n]);
572
+ }
573
+ state.set(n, 2);
574
+ };
575
+ for (const n of g.keys()) if (!state.has(n)) dfs(n, []);
576
+ };
577
+ cyc(m.problemLinks.filter((l) => l.kind !== "same").map((l) => [l.up, l.down]), "\u8AB2\u984C\u306E\u524D\u5F8C");
578
+ cyc(m.actionLinks.filter((l) => l.kind === "pre").map((l) => [l.from, l.to]), "\u30A2\u30AF\u30B7\u30E7\u30F3\u306E\u524D\u5F8C");
579
+ for (const p of m.problems) {
580
+ const acts = m.actions.some((a) => a.problem === p.id || a.also?.includes(p.id));
581
+ const links = m.problemLinks.some((l) => l.up === p.id || l.down === p.id);
582
+ const ins = m.insights.some((i) => i.problems.includes(p.id));
583
+ if (!acts && !links && !ins) warn(`${p.id} \u306F\u3064\u306A\u304C\u308A\u3082\u77E5\u898B\u3082\u30A2\u30AF\u30B7\u30E7\u30F3\u3082\u306A\u3044`);
584
+ else if (!acts) warn(`${p.id} \u306B\u30A2\u30AF\u30B7\u30E7\u30F3\u304C\u306A\u3044`);
585
+ }
586
+ const loose = m.insights.filter((i) => !i.problems.length).length;
587
+ if (loose) warn(`\u3069\u306E\u8AB2\u984C\u306B\u3082\u7D50\u3070\u308C\u3066\u3044\u306A\u3044\u77E5\u898B: ${loose} \u4EF6`);
588
+ return f;
589
+ }
590
+ function runCheck(root) {
591
+ const v = readRoot(root);
592
+ const findings = check(v);
593
+ let schema = null;
594
+ try {
595
+ vetMap(v.map);
596
+ } catch (e) {
597
+ schema = e.message;
598
+ }
599
+ return { ok: !schema && !findings.some((x) => x.level === "error"), schema, findings };
600
+ }
601
+
425
602
  // node/export/scope.ts
426
603
  function hits(w, t) {
427
604
  return w.startsWith("(?i)") ? new RegExp(w.slice(4), "i").test(t) : t.toLowerCase().includes(w.toLowerCase());
@@ -806,6 +983,10 @@ ${where.join("\n")}`);
806
983
  async function buildExports(root, only) {
807
984
  if (!fs7.existsSync(path7.join(root, "chiken.yaml"))) throw new Error(`chiken.yaml \u304C\u306A\u3044\u3002chiken \u306E\u30C7\u30FC\u30BF\u30D5\u30A9\u30EB\u30C0\u3092\u6E21\u3059: ${root}`);
808
985
  const v = readRoot(root);
986
+ const errors = check(v).filter((f) => f.level === "error");
987
+ if (errors.length) throw new Error(`\u70B9\u691C\u3067\u30A8\u30E9\u30FC\u304C\u3042\u308B\u306E\u3067\u66F8\u304D\u51FA\u3055\u306A\u3044\uFF08chiken check \u3067\u898B\u308B\uFF09:
988
+ ${errors.slice(0, 10).map((f) => ` ${f.msg}`).join("\n")}${errors.length > 10 ? `
989
+ \u307B\u304B ${errors.length - 10} \u4EF6` : ""}`);
809
990
  vetMap(v.map);
810
991
  const photoOf = await preparePhotos(v);
811
992
  const targets = v.config.exports.filter((x) => !only || x.name === only);
@@ -893,7 +1074,8 @@ function editFm(tx, v, id, fn) {
893
1074
  const file = v.files.get(id) ?? fail(`\u898B\u3064\u304B\u3089\u306A\u3044: ${id}`);
894
1075
  const { fm, body } = readMd(file);
895
1076
  fn(fm);
896
- fm.updated = today();
1077
+ const k = kindOf(v, id);
1078
+ if (k === "problem" || k === "action") fm.updated = today();
897
1079
  tx.writeMd(file, fm, body);
898
1080
  return file;
899
1081
  }
@@ -941,8 +1123,16 @@ var ops = {
941
1123
  },
942
1124
  // frontmatter の 1 項目を変える。文章は本文で直すので、ここでは扱わない
943
1125
  set(tx, v, id, field, value) {
944
- const allowed = ["kind", "area", "origin", "problem", "groups", "also", "basis", "session"];
945
- if (!allowed.includes(field)) fail(`\u5909\u3048\u3089\u308C\u308B\u9805\u76EE: ${allowed.join(", ")}\uFF08\u6587\u7AE0\u306F\u672C\u6587\u3067\u76F4\u3059\u3002\u77E5\u898B\u306F insight set\u3001\u77E5\u898B\u3068\u8AB2\u984C\u306F tag\uFF09`);
1126
+ const k = v.collections.some((c) => c.id === id) ? "collection" : kindOf(v, id);
1127
+ const ALLOWED2 = {
1128
+ problem: ["area", "origin", "groups", "session"],
1129
+ action: ["kind", "origin", "problem", "also", "basis", "groups"],
1130
+ source: ["groups"]
1131
+ };
1132
+ if (!k) fail(`\u898B\u3064\u304B\u3089\u306A\u3044: ${id}`);
1133
+ if (k === "insight") fail(`\u77E5\u898B\u306E\u9805\u76EE\u306F insight set \u3067\u5909\u3048\u308B: ${id}`);
1134
+ const allowed = ALLOWED2[k] ?? [];
1135
+ if (!allowed.includes(field)) fail(allowed.length ? `${id} \u3067\u5909\u3048\u3089\u308C\u308B\u9805\u76EE: ${allowed.join(", ")}\uFF08\u6587\u7AE0\u306F\u672C\u6587\u3067\u76F4\u3059\u3002\u77E5\u898B\u3068\u8AB2\u984C\u306F tag\uFF09` : `${id}\uFF08${k}\uFF09\u306F set \u3067\u5909\u3048\u3089\u308C\u306A\u3044\u3002\u30D5\u30A1\u30A4\u30EB\u3092\u76F4\u63A5\u76F4\u3059`);
946
1136
  editFm(tx, v, id, (fm) => {
947
1137
  fm[field] = Array.isArray(value) ? value.length ? value : void 0 : value || void 0;
948
1138
  });
@@ -1000,12 +1190,16 @@ ${a.why ?? ""}
1000
1190
  if (a.medium !== "note" && !a.taken) fail(`--taken \u304C\u8981\u308B\uFF08${Object.keys(TAKEN).join(", ")}\uFF09`);
1001
1191
  if (a.taken && !(a.taken in TAKEN)) fail(`\u53D7\u3051\u53D6\u308A\u65B9\u306E\u5024: ${Object.keys(TAKEN).join(", ")}`);
1002
1192
  if (a.day && !a.collection) fail("--day \u306F\u307E\u3068\u307E\u308A\uFF08\u30A4\u30D9\u30F3\u30C8\uFF09\u306E\u4E2D\u306E\u767B\u58C7\u3060\u3051");
1003
- if (a.date && !/^\d{4}-\d{2}-\d{2}$/.test(a.date)) fail("\u65E5\u4ED8\u306F YYYY-MM-DD");
1193
+ if (!/^[a-z0-9][a-z0-9-]*$/.test(a.id)) fail(`--id \u306F\u82F1\u5C0F\u6587\u5B57\u3068\u6570\u5B57\u3068\u30CF\u30A4\u30D5\u30F3: ${a.id}`);
1194
+ if (a.date && !(/^\d{4}-\d{2}-\d{2}$/.test(a.date) && !Number.isNaN(Date.parse(a.date)))) fail(`\u65E5\u4ED8\u306F YYYY-MM-DD: ${a.date}`);
1195
+ if (a.time && !/^([01]?\d|2[0-3]):[0-5]\d$/.test(a.time)) fail(`\u6642\u523B\u306F HH:MM: ${a.time}`);
1196
+ if (a.day && !/^[1-9]\d*$/.test(a.day)) fail(`--day \u306F 1 \u4EE5\u4E0A\u306E\u6570: ${a.day}`);
1004
1197
  let file;
1005
1198
  let id;
1006
1199
  const name = `${safeName(a.file ?? a.title)}.md`;
1007
1200
  if (a.collection) {
1008
1201
  const b = v.collections.find((x) => x.id === a.collection) ?? fail(`\u307E\u3068\u307E\u308A\u304C\u306A\u3044: ${a.collection}\uFF08\u4F7F\u3048\u308B\u307E\u3068\u307E\u308A: ${v.collections.map((x) => x.id).join(", ") || "\u306A\u3057"}\uFF09`);
1202
+ if (a.day && b.days && !(a.day in b.days)) fail(`${b.id} \u306B ${a.day} \u65E5\u76EE\u304C\u306A\u3044\uFF08_index.md \u306E days: ${Object.keys(b.days).join(", ")}\uFF09`);
1009
1203
  id = `${b.id}/${a.id}`;
1010
1204
  file = path9.join(v.root, b.dir, a.medium === "note" ? "notes" : "", name);
1011
1205
  } else {
@@ -1063,6 +1257,7 @@ ${a.why ?? ""}
1063
1257
  },
1064
1258
  // 同じインプットの中で重なる知見を 1 件にまとめる。drop の課題を keep に足し、drop を根拠にしていたアクションを keep に付け替えて、drop を消す
1065
1259
  merge(tx, v, keep, drop) {
1260
+ if (keep === drop) fail("\u6B8B\u3059\u77E5\u898B\u3068\u6D88\u3059\u77E5\u898B\u304C\u540C\u3058");
1066
1261
  if (keep.split("#")[0] !== drop.split("#")[0]) fail("\u307E\u3068\u3081\u3089\u308C\u308B\u306E\u306F\u540C\u3058\u30A4\u30F3\u30D7\u30C3\u30C8\u306E\u77E5\u898B\u3060\u3051");
1067
1262
  const d = v.map.insights.find((i) => i.id === drop) ?? fail(`\u77E5\u898B\u304C\u306A\u3044: ${drop}`);
1068
1263
  ops.tag(tx, v, keep, d.problems);
@@ -1128,108 +1323,6 @@ ${a.why ?? ""}
1128
1323
  import fs10 from "node:fs";
1129
1324
  import path10 from "node:path";
1130
1325
  import YAML2 from "yaml";
1131
-
1132
- // node/root/check.ts
1133
- function check(v) {
1134
- const f = [];
1135
- const err = (msg) => f.push({ level: "error", msg });
1136
- const warn = (msg) => f.push({ level: "warn", msg });
1137
- const m = v.map;
1138
- const dup = (xs, what) => {
1139
- const seen = /* @__PURE__ */ new Set();
1140
- for (const x of xs) {
1141
- if (seen.has(x)) err(`${what} \u306E\u91CD\u306A\u308A: ${x}`);
1142
- seen.add(x);
1143
- }
1144
- };
1145
- dup(m.problems.map((p) => p.id), "\u8AB2\u984C\u306E ID");
1146
- dup(m.actions.map((a) => a.id), "\u30A2\u30AF\u30B7\u30E7\u30F3\u306E ID");
1147
- dup(m.insights.map((i) => i.id), "\u77E5\u898B\u306E\u756A\u53F7");
1148
- dup([...v.sourceOf.keys()], "\u30A4\u30F3\u30D7\u30C3\u30C8\u306E ID");
1149
- for (const p of m.problems) if (!/^P\d{2,}$/.test(p.id)) err(`\u8AB2\u984C\u306E ID \u306E\u5F62: ${p.id}`);
1150
- for (const a of m.actions) if (!/^A\d{3,}$/.test(a.id)) err(`\u30A2\u30AF\u30B7\u30E7\u30F3\u306E ID \u306E\u5F62: ${a.id}`);
1151
- const P = new Set(m.problems.map((p) => p.id));
1152
- const A = new Set(m.actions.map((a) => a.id));
1153
- const I = new Set(m.insights.map((i) => i.id));
1154
- const areas = new Set(m.problemAreas.map((a) => a.id));
1155
- const ctx = new Set(m.contexts.map((c) => c.id));
1156
- for (const a of m.problemAreas) if (a.context && !ctx.has(a.context)) err(`\u9818\u57DF ${a.id} \u306E\u30B3\u30F3\u30C6\u30AD\u30B9\u30C8\u304C\u306A\u3044: ${a.context}\uFF08areas.md \u306E contexts\uFF09`);
1157
- for (const p of m.problems) {
1158
- if (!areas.has(p.area)) err(`${p.id} \u306E\u9818\u57DF\u304C\u306A\u3044: ${p.area}`);
1159
- if (!["self", "ai"].includes(p.origin)) err(`${p.id} \u306E\u51FA\u3069\u3053\u308D\uFF08origin\uFF09\u306E\u5024: ${p.origin}\uFF08self / ai\uFF09`);
1160
- }
1161
- for (const l of m.problemLinks) {
1162
- if (!P.has(l.up)) err(`${l.down} \u306E before\uFF08\u524D\u306B\u3042\u308B\u8AB2\u984C\uFF09\u306B\u3001\u306A\u3044\u8AB2\u984C: ${l.up}`);
1163
- if (!["pre", "ease", "same"].includes(l.kind)) err(`${l.down} \u306E before \u306E\u7A2E\u985E\u306E\u5024: ${l.kind}\uFF08pre / ease / same\uFF09`);
1164
- }
1165
- for (const a of m.actions) {
1166
- if (!P.has(a.problem)) err(`${a.id} \u306E\u4E3B\u306A\u8AB2\u984C\u304C\u306A\u3044: ${a.problem}`);
1167
- for (const x of a.also ?? []) if (!P.has(x)) err(`${a.id} \u306E\u3042\u308F\u305B\u3066\u9032\u3080\u8AB2\u984C\u304C\u306A\u3044: ${x}`);
1168
- for (const b of a.basis ?? []) if (!I.has(b)) err(`${a.id} \u306E\u6839\u62E0\u306E\u77E5\u898B\u304C\u306A\u3044: ${b}`);
1169
- if (!["study", "try", "change"].includes(a.kind)) err(`${a.id} \u306E\u7A2E\u985E\uFF08kind\uFF09\u306E\u5024: ${a.kind}\uFF08study / try / change\uFF09`);
1170
- if (!a.basis?.length) warn(`${a.id} \u306B\u6839\u62E0\u306E\u77E5\u898B\u304C\u306A\u3044\uFF08chiken set ${a.id} basis <\u77E5\u898B>,\u2026\uFF09`);
1171
- if (!a.why) warn(`${a.id} \u306B\u300C\u52B9\u304D\u305D\u3046\u306A\u7406\u7531\u300D\u304C\u306A\u3044\uFF08\u672C\u6587\u306E ## \u52B9\u304D\u305D\u3046\u306A\u7406\u7531 \u306B\u66F8\u304F\uFF09`);
1172
- }
1173
- for (const l of m.actionLinks) {
1174
- if (!A.has(l.from)) err(`${l.to} \u306E needs\uFF08\u524D\u306B\u8981\u308B\u30A2\u30AF\u30B7\u30E7\u30F3\uFF09\u306B\u3001\u306A\u3044\u30A2\u30AF\u30B7\u30E7\u30F3: ${l.from}`);
1175
- if (!["pre", "alt"].includes(l.kind)) err(`${l.to} \u306E needs \u306E\u7A2E\u985E\u306E\u5024: ${l.kind}\uFF08pre / alt\uFF09`);
1176
- }
1177
- for (const i of m.insights) {
1178
- for (const p of i.problems) if (!P.has(p)) err(`${i.id} \u306E\u624B\u304C\u304B\u308A\u306B\u306A\u308B\u8AB2\u984C\u304C\u306A\u3044: ${p}`);
1179
- if (!["fact", "impression"].includes(i.kind)) err(`${i.id} \u306E\u7A2E\u985E\uFF08kind\uFF09\u306E\u5024: ${i.kind}\uFF08fact / impression\uFF09`);
1180
- if (!["self", "external", "ai"].includes(i.author)) err(`${i.id} \u306E\u66F8\u304D\u624B\uFF08author\uFF09\u306E\u5024: ${i.author}\uFF08self / external / ai\uFF09`);
1181
- }
1182
- const G = /^[a-z0-9][a-z0-9-]*$/;
1183
- const groupsOf = [...m.problems.map((p) => [p.id, p.groups]), ...m.actions.map((a) => [a.id, a.groups]), ...m.insightSources.map((x) => [x.id, x.groups]), ...m.insights.map((i) => [i.id, i.groups])];
1184
- for (const [id, gs] of groupsOf) for (const g of gs ?? []) if (!G.test(g)) err(`${id} \u306E\u30B0\u30EB\u30FC\u30D7\u306E\u540D\u524D: ${g}\uFF08\u82F1\u5C0F\u6587\u5B57\u30FB\u6570\u5B57\u30FB\u30CF\u30A4\u30D5\u30F3\uFF09`);
1185
- for (const id of v.legacyShare) err(`${id} \u306B share \u304C\u3042\u308B\u3002\u4ECA\u306F groups \u3067\u66F8\u304F\uFF08share: self \u306F groups: [private] \u306B\u3057\u3001\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8\u3067 exclude: [private]\uFF09`);
1186
- const used = new Set(groupsOf.flatMap(([, gs]) => gs ?? []));
1187
- for (const e of v.config.exports ?? []) {
1188
- if (e.rules && !v.config.rules?.[e.rules]) err(`\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8 ${e.name} \u306E rules \u304C\u306A\u3044: ${e.rules}\uFF08chiken.yaml \u306E rules\uFF09`);
1189
- for (const g of [...e.include ?? [], ...e.exclude ?? []]) if (!used.has(g)) warn(`\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8 ${e.name} \u306E\u30B0\u30EB\u30FC\u30D7 ${g} \u3092\u6301\u3064\u3082\u306E\u304C\u306A\u3044\uFF08\u7DB4\u308A\u3092\u78BA\u304B\u3081\u308B\uFF09`);
1190
- if (e.audience !== void 0) err(`\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8 ${e.name} \u306B audience \u304C\u3042\u308B\u3002\u4ECA\u306F exclude \u3068 rules \u3067\u66F8\u304F\uFF08ADR-0011\uFF09`);
1191
- }
1192
- if (v.config.org !== void 0) err("chiken.yaml \u306B org \u304C\u3042\u308B\u3002\u4ECA\u306F rules \u306B\u540D\u524D\u3092\u4ED8\u3051\u3066\u66F8\u304D\u3001\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8\u306E rules \u3067\u6307\u3059\uFF08ADR-0011\uFF09");
1193
- const cyc = (edges, what) => {
1194
- const g = /* @__PURE__ */ new Map();
1195
- for (const [a, b] of edges) g.set(a, [...g.get(a) ?? [], b]);
1196
- const state = /* @__PURE__ */ new Map();
1197
- const dfs = (n, trail) => {
1198
- state.set(n, 1);
1199
- for (const x of g.get(n) ?? []) {
1200
- if (state.get(x) === 1) err(`${what}\u306E\u8F2A: ${[...trail, n, x].join(" \u2192 ")}`);
1201
- else if (!state.has(x)) dfs(x, [...trail, n]);
1202
- }
1203
- state.set(n, 2);
1204
- };
1205
- for (const n of g.keys()) if (!state.has(n)) dfs(n, []);
1206
- };
1207
- cyc(m.problemLinks.filter((l) => l.kind !== "same").map((l) => [l.up, l.down]), "\u8AB2\u984C\u306E\u524D\u5F8C");
1208
- cyc(m.actionLinks.filter((l) => l.kind === "pre").map((l) => [l.from, l.to]), "\u30A2\u30AF\u30B7\u30E7\u30F3\u306E\u524D\u5F8C");
1209
- for (const p of m.problems) {
1210
- const acts = m.actions.some((a) => a.problem === p.id || a.also?.includes(p.id));
1211
- const links = m.problemLinks.some((l) => l.up === p.id || l.down === p.id);
1212
- const ins = m.insights.some((i) => i.problems.includes(p.id));
1213
- if (!acts && !links && !ins) warn(`${p.id} \u306F\u3064\u306A\u304C\u308A\u3082\u77E5\u898B\u3082\u30A2\u30AF\u30B7\u30E7\u30F3\u3082\u306A\u3044`);
1214
- else if (!acts) warn(`${p.id} \u306B\u30A2\u30AF\u30B7\u30E7\u30F3\u304C\u306A\u3044`);
1215
- }
1216
- const loose = m.insights.filter((i) => !i.problems.length).length;
1217
- if (loose) warn(`\u3069\u306E\u8AB2\u984C\u306B\u3082\u7D50\u3070\u308C\u3066\u3044\u306A\u3044\u77E5\u898B: ${loose} \u4EF6`);
1218
- return f;
1219
- }
1220
- function runCheck(root) {
1221
- const v = readRoot(root);
1222
- const findings = check(v);
1223
- let schema = null;
1224
- try {
1225
- vetMap(v.map);
1226
- } catch (e) {
1227
- schema = e.message;
1228
- }
1229
- return { ok: !schema && !findings.some((x) => x.level === "error"), schema, findings };
1230
- }
1231
-
1232
- // node/root/tx.ts
1233
1326
  function toYaml(o) {
1234
1327
  const doc = new YAML2.Document(o);
1235
1328
  YAML2.visit(doc, { Seq(_, node) {
@@ -1291,26 +1384,61 @@ ${body.replace(/^\n+/, "").replace(/\s*$/, "\n")}`);
1291
1384
  for (const f of this.created) fs10.rmSync(f, { force: true });
1292
1385
  }
1293
1386
  };
1294
- function transact(root, fn, opts = {}) {
1295
- const tx = new Tx(root);
1296
- let result;
1297
- try {
1298
- result = fn(tx, readRoot(root));
1299
- } catch (e) {
1300
- tx.rollback();
1301
- throw e;
1387
+ function lock(root) {
1388
+ const dir = path10.join(root, ".chiken");
1389
+ const file = path10.join(dir, "lock");
1390
+ fs10.mkdirSync(dir, { recursive: true });
1391
+ const until = Date.now() + 3e4;
1392
+ for (; ; ) {
1393
+ try {
1394
+ fs10.writeFileSync(file, String(process.pid), { flag: "wx" });
1395
+ return () => fs10.rmSync(file, { force: true });
1396
+ } catch (e) {
1397
+ if (e.code !== "EEXIST") throw e;
1398
+ try {
1399
+ if (Date.now() - fs10.statSync(file).mtimeMs > 6e4) {
1400
+ fs10.rmSync(file, { force: true });
1401
+ continue;
1402
+ }
1403
+ } catch {
1404
+ continue;
1405
+ }
1406
+ if (Date.now() > until) throw new Error(`\u307B\u304B\u306E\u66F8\u304D\u63DB\u3048\u304C\u7D42\u308F\u3089\u306A\u3044\uFF08${path10.relative(root, file)}\uFF09\u3002\u6B62\u307E\u3063\u305F\u30D7\u30ED\u30BB\u30B9\u306E\u6B8B\u308A\u306A\u3089\u6D88\u3059`);
1407
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100);
1408
+ }
1302
1409
  }
1303
- if (!tx.touched) return { result, check: null, rolledBack: false, history: tx.historyDir };
1304
- let c;
1410
+ }
1411
+ var key = (f) => f.msg;
1412
+ function transact(root, fn, opts = {}) {
1413
+ const release = lock(root);
1305
1414
  try {
1306
- c = runCheck(root);
1307
- } catch (e) {
1308
- tx.rollback();
1309
- throw e;
1415
+ const tx = new Tx(root);
1416
+ const before = runCheck(root);
1417
+ const had = new Set(before.findings.filter((f) => f.level === "error").map(key));
1418
+ let result;
1419
+ try {
1420
+ result = fn(tx, readRoot(root));
1421
+ } catch (e) {
1422
+ tx.rollback();
1423
+ throw e;
1424
+ }
1425
+ if (!tx.touched) return { result, check: null, rolledBack: false, history: tx.historyDir, introduced: [] };
1426
+ let c;
1427
+ try {
1428
+ c = runCheck(root);
1429
+ } catch (e) {
1430
+ tx.rollback();
1431
+ throw e;
1432
+ }
1433
+ const introduced = c.findings.filter((f) => f.level === "error" && !had.has(key(f)));
1434
+ const schemaNew = !!c.schema && !before.schema;
1435
+ const bad = introduced.length > 0 || schemaNew;
1436
+ const rolledBack = bad && !opts.force;
1437
+ if (rolledBack) tx.rollback();
1438
+ return { result, check: bad ? c : null, rolledBack, history: tx.historyDir, introduced };
1439
+ } finally {
1440
+ release();
1310
1441
  }
1311
- const rolledBack = !c.ok && !opts.force;
1312
- if (rolledBack) tx.rollback();
1313
- return { result, check: c, rolledBack, history: tx.historyDir };
1314
1442
  }
1315
1443
 
1316
1444
  // node/serve.ts
@@ -1360,7 +1488,7 @@ async function serve(root, opts) {
1360
1488
  if (!ALLOWED.includes(op)) return send(res, 400, { error: `\u4F7F\u3048\u306A\u3044\u547D\u4EE4: ${op}` });
1361
1489
  const fn = ops[op];
1362
1490
  const r = transact(root, (tx, v) => fn(tx, v, ...args));
1363
- const errors = r.check ? [...r.check.schema ? [r.check.schema] : [], ...r.check.findings.filter((x) => x.level === "error").map((x) => x.msg)] : [];
1491
+ const errors = r.check ? [...r.introduced.map((x) => x.msg), ...r.check.schema && !r.introduced.length ? [r.check.schema] : []] : [];
1364
1492
  console.log(`${op} ${JSON.stringify(args)} \u2192 ${r.rolledBack ? "\u623B\u3057\u305F" : "\u66F8\u3044\u305F"}`);
1365
1493
  return send(res, r.rolledBack ? 409 : 200, { ok: !r.rolledBack, result: r.result, errors });
1366
1494
  }
@@ -1433,7 +1561,7 @@ areas:
1433
1561
 
1434
1562
  // node/cli.ts
1435
1563
  var argv = process.argv.slice(2);
1436
- var BOOL = /* @__PURE__ */ new Set(["json", "force", "edit", "help"]);
1564
+ var BOOL = /* @__PURE__ */ new Set(["json", "force", "edit", "help", "version"]);
1437
1565
  var KNOWN = /* @__PURE__ */ new Set([...BOOL, "root", "port", "export", "name", "area", "now", "why", "origin", "problem", "kind", "also", "basis", "check", "done", "title", "id", "collection", "type", "taken", "date", "day", "time", "file", "url", "org", "who", "text", "problems", "verify", "reason", "source", "author"]);
1438
1566
  var flags = /* @__PURE__ */ new Map();
1439
1567
  var pos = [];
@@ -1456,7 +1584,12 @@ var json = flags.has("json");
1456
1584
  var list = (v) => v ? v.split(",").map((x) => x.trim()).filter(Boolean) : [];
1457
1585
  function findRoot() {
1458
1586
  const given = flag("root") ?? process.env.CHIKEN_ROOT;
1459
- if (given) return path13.resolve(given);
1587
+ if (given) {
1588
+ const dir2 = path13.resolve(given);
1589
+ if (!fs13.existsSync(dir2)) throw new Error(`\u30C7\u30FC\u30BF\u30D5\u30A9\u30EB\u30C0\u304C\u306A\u3044: ${dir2}`);
1590
+ if (!fs13.existsSync(path13.join(dir2, LAYOUT.config))) throw new Error(`${LAYOUT.config} \u304C\u306A\u3044\u3002chiken \u306E\u30C7\u30FC\u30BF\u30D5\u30A9\u30EB\u30C0\u304B\u78BA\u304B\u3081\u308B\uFF08\u65B0\u3057\u304F\u4F5C\u308B\u306A\u3089 chiken init\uFF09: ${dir2}`);
1591
+ return dir2;
1592
+ }
1460
1593
  let dir = process.cwd();
1461
1594
  while (true) {
1462
1595
  if (fs13.existsSync(path13.join(dir, LAYOUT.config))) return dir;
@@ -1478,18 +1611,21 @@ function printCheck(c) {
1478
1611
  }
1479
1612
  function write(root, fn, human) {
1480
1613
  const r = transact(root, fn, { force: flags.has("force") });
1481
- if (r.check && !r.check.ok) {
1482
- printCheck(r.check);
1483
- console.error(r.rolledBack ? "\u70B9\u691C\u3067\u6B62\u307E\u3063\u305F\u306E\u3067\u3001\u66F8\u304D\u63DB\u3048\u3092\u623B\u3057\u305F\uFF08\u6B8B\u3059\u306A\u3089 --force\uFF09" : `\u70B9\u691C\u3067\u6B62\u307E\u3063\u305F\u304C --force \u306A\u306E\u3067\u6B8B\u3057\u305F\u3002\u66F8\u304D\u63DB\u3048\u308B\u524D\u306E\u30D5\u30A1\u30A4\u30EB\u306F ${r.history} \u306B\u3042\u308B`);
1484
- process.exitCode = 1;
1614
+ if (r.check) {
1615
+ const lines = r.introduced.length ? r.introduced.map((x) => `\u30A8\u30E9\u30FC ${x.msg}`) : [`\u30A8\u30E9\u30FC ${r.check.schema ?? "schema \u306E\u691C\u67FB\u3067\u6B62\u307E\u3063\u305F"}`];
1616
+ if (json) console.log(JSON.stringify({ ok: false, rolledBack: r.rolledBack, errors: r.introduced.length ? r.introduced : [{ level: "error", msg: r.check.schema }] }, null, 2));
1617
+ else console.error(lines.join("\n"));
1618
+ console.error(r.rolledBack ? "\u3053\u306E\u66F8\u304D\u63DB\u3048\u3067\u30A8\u30E9\u30FC\u304C\u5897\u3048\u305F\u306E\u3067\u3001\u66F8\u304D\u63DB\u3048\u3092\u623B\u3057\u305F\uFF08\u6B8B\u3059\u306A\u3089 --force\uFF09" : `\u30A8\u30E9\u30FC\u304C\u5897\u3048\u305F\u304C --force \u306A\u306E\u3067\u6B8B\u3057\u305F\u3002\u66F8\u304D\u63DB\u3048\u308B\u524D\u306E\u30D5\u30A1\u30A4\u30EB\u306F ${r.history} \u306B\u3042\u308B`);
1619
+ process.exitCode = r.rolledBack ? 3 : 1;
1485
1620
  return;
1486
1621
  }
1487
1622
  out(human(r.result), r.result);
1488
1623
  }
1489
1624
  var HELP = `chiken [--root <\u30C7\u30FC\u30BF\u30D5\u30A9\u30EB\u30C0>] [--json] [--force] <\u547D\u4EE4> \u2026
1625
+ chiken --version / chiken help\uFF08-h\uFF09
1490
1626
 
1491
1627
  \u30C7\u30FC\u30BF\u30D5\u30A9\u30EB\u30C0\u306F --root\u3001\u74B0\u5883\u5909\u6570 CHIKEN_ROOT\u3001\u4ECA\u3044\u308B\u30D5\u30A9\u30EB\u30C0\u304B\u3089\u4E0A\u306B chiken.yaml \u3092\u63A2\u3059\u3001\u306E\u9806\u306B\u6C7A\u3081\u308B\u3002
1492
- \u66F8\u304D\u63DB\u3048\u308B\u547D\u4EE4\u306F\u3001\u66F8\u304D\u63DB\u3048\u308B\u524D\u306E\u30D5\u30A1\u30A4\u30EB\u3092 <\u30C7\u30FC\u30BF\u30D5\u30A9\u30EB\u30C0>/.chiken/history/ \u306B\u30D0\u30C3\u30AF\u30A2\u30C3\u30D7\u3057\u3001\u70B9\u691C\u3067\u30A8\u30E9\u30FC\u304C\u3042\u308C\u3070\u623B\u3059\uFF08--force \u3067\u6B8B\u3059\uFF09\u3002
1628
+ \u66F8\u304D\u63DB\u3048\u308B\u547D\u4EE4\u306F\u3001\u66F8\u304D\u63DB\u3048\u308B\u524D\u306E\u30D5\u30A1\u30A4\u30EB\u3092 <\u30C7\u30FC\u30BF\u30D5\u30A9\u30EB\u30C0>/.chiken/history/ \u306B\u30D0\u30C3\u30AF\u30A2\u30C3\u30D7\u3057\u3001\u70B9\u691C\u3067\u3053\u306E\u66F8\u304D\u63DB\u3048\u3067\u5897\u3048\u305F\u30A8\u30E9\u30FC\u304C\u3042\u308C\u3070\u623B\u3059\uFF08--force \u3067\u6B8B\u3059\uFF09\u3002
1493
1629
 
1494
1630
  \u59CB\u3081\u308B
1495
1631
  init [<\u30D5\u30A9\u30EB\u30C0>] [--title \u2026] \u65B0\u3057\u3044\u30C7\u30FC\u30BF\u30D5\u30A9\u30EB\u30C0\u306E\u96DB\u5F62\u3092\u4F5C\u308B
@@ -1529,8 +1665,17 @@ var HELP = `chiken [--root <\u30C7\u30FC\u30BF\u30D5\u30A9\u30EB\u30C0>] [--json
1529
1665
  \u753B\u9762\u3092\u624B\u5143\u3067\u958B\u304F\uFF08--edit \u3067\u7DE8\u96C6\u30E2\u30FC\u30C9\uFF09
1530
1666
 
1531
1667
  --json \u306F\u3001\u4E00\u89A7\u30FB\u4E2D\u8EAB\u30FB\u70B9\u691C\u30FB\u66F8\u304D\u63DB\u3048\u306E\u7D50\u679C\u3092 JSON \u3067\u51FA\u3059\u3002\u8A73\u3057\u304F\u306F docs/data-layout.md \u306E\u300CCLI\u300D\u3002`;
1668
+ var MAX_ARGS = { ls: 1, show: 1, search: Infinity, new: 1, source: 2, insight: Infinity, link: 2, unlink: 2, tag: Infinity, untag: Infinity, set: Infinity, rename: Infinity, merge: 2, rm: 1, check: 0, serve: 0, build: 1, release: 1, init: 1 };
1532
1669
  function main() {
1670
+ if (pos.includes("-h")) return console.log(HELP);
1671
+ if (pos.includes("-V") || flags.has("version")) return console.log(JSON.parse(fs13.readFileSync(path13.join(REPO, "package.json"), "utf8")).version);
1533
1672
  const [cmd, ...rest] = pos;
1673
+ if (cmd && cmd !== "help" && !(cmd in MAX_ARGS)) {
1674
+ console.error(`\u77E5\u3089\u306A\u3044\u547D\u4EE4: ${cmd}\uFF08chiken help\uFF09`);
1675
+ process.exitCode = 1;
1676
+ return;
1677
+ }
1678
+ if (cmd && rest.length > (MAX_ARGS[cmd] ?? 0)) fail2(`${cmd} \u306E\u5F15\u6570\u304C\u591A\u3044: ${rest.slice(MAX_ARGS[cmd]).join(" ")}\uFF081 \u56DE\u306B 1 \u4EF6\u3002chiken help\uFF09`);
1534
1679
  const unknown = [...flags.keys()].filter((k) => !KNOWN.has(k));
1535
1680
  if (unknown.length) fail2(`\u77E5\u3089\u306A\u3044\u30AA\u30D7\u30B7\u30E7\u30F3: ${unknown.map((k) => `--${k}`).join(" ")}\uFF08chiken help\uFF09`);
1536
1681
  if (!cmd || cmd === "help" || flags.has("help")) return console.log(HELP);
@@ -1677,6 +1822,7 @@ ${i.text}${i.verify ? `
1677
1822
  return;
1678
1823
  }
1679
1824
  case "serve":
1825
+ if (flag("port") && !/^\d+$/.test(flag("port"))) fail2(`--port \u306F\u6570: ${flag("port")}`);
1680
1826
  void serve(root, { port: Number(flag("port") ?? 5178), edit: flags.has("edit"), exportName: flag("export") });
1681
1827
  return;
1682
1828
  case "build":