@pecot/workflow-mcp 0.0.0-stage → 5.0.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.
Files changed (244) hide show
  1. package/.claude-plugin/plugin.json +8 -0
  2. package/LICENSE +21 -0
  3. package/README.md +126 -2
  4. package/bin/pecot-workflow-mcp.mjs +10 -0
  5. package/cli/dist/cli.d.ts +26 -0
  6. package/cli/dist/cli.js +222 -0
  7. package/cli/dist/commands/cell-table.d.ts +3 -0
  8. package/cli/dist/commands/cell-table.js +495 -0
  9. package/cli/dist/commands/command-binding.d.ts +26 -0
  10. package/cli/dist/commands/command-binding.js +266 -0
  11. package/cli/dist/commands/command-definition.d.ts +136 -0
  12. package/cli/dist/commands/command-definition.js +101 -0
  13. package/cli/dist/commands/command-help.d.ts +8 -0
  14. package/cli/dist/commands/command-help.js +173 -0
  15. package/cli/dist/commands/command-stop.d.ts +19 -0
  16. package/cli/dist/commands/command-stop.js +30 -0
  17. package/cli/dist/commands/dependabot.d.ts +3 -0
  18. package/cli/dist/commands/dependabot.js +165 -0
  19. package/cli/dist/commands/directory-option.d.ts +7 -0
  20. package/cli/dist/commands/directory-option.js +48 -0
  21. package/cli/dist/commands/failure.d.ts +13 -0
  22. package/cli/dist/commands/failure.js +33 -0
  23. package/cli/dist/commands/fuzzy.d.ts +7 -0
  24. package/cli/dist/commands/fuzzy.js +79 -0
  25. package/cli/dist/commands/github-client.d.ts +12 -0
  26. package/cli/dist/commands/github-client.js +23 -0
  27. package/cli/dist/commands/global-options.d.ts +25 -0
  28. package/cli/dist/commands/global-options.js +83 -0
  29. package/cli/dist/commands/help-layout.d.ts +40 -0
  30. package/cli/dist/commands/help-layout.js +117 -0
  31. package/cli/dist/commands/help.d.ts +8 -0
  32. package/cli/dist/commands/help.js +55 -0
  33. package/cli/dist/commands/input-schema.d.ts +14 -0
  34. package/cli/dist/commands/input-schema.js +56 -0
  35. package/cli/dist/commands/issue-board.d.ts +38 -0
  36. package/cli/dist/commands/issue-board.js +62 -0
  37. package/cli/dist/commands/issue-comment.d.ts +3 -0
  38. package/cli/dist/commands/issue-comment.js +196 -0
  39. package/cli/dist/commands/issue-create.d.ts +3 -0
  40. package/cli/dist/commands/issue-create.js +329 -0
  41. package/cli/dist/commands/issue-project.d.ts +3 -0
  42. package/cli/dist/commands/issue-project.js +176 -0
  43. package/cli/dist/commands/issue-quick.d.ts +3 -0
  44. package/cli/dist/commands/issue-quick.js +126 -0
  45. package/cli/dist/commands/issue-read.d.ts +3 -0
  46. package/cli/dist/commands/issue-read.js +80 -0
  47. package/cli/dist/commands/issue-shared.d.ts +159 -0
  48. package/cli/dist/commands/issue-shared.js +419 -0
  49. package/cli/dist/commands/issue.d.ts +4 -0
  50. package/cli/dist/commands/issue.js +15 -0
  51. package/cli/dist/commands/names.d.ts +9 -0
  52. package/cli/dist/commands/names.js +16 -0
  53. package/cli/dist/commands/output.d.ts +78 -0
  54. package/cli/dist/commands/output.js +187 -0
  55. package/cli/dist/commands/pr.d.ts +3 -0
  56. package/cli/dist/commands/pr.js +933 -0
  57. package/cli/dist/commands/project-info.d.ts +48 -0
  58. package/cli/dist/commands/project-info.js +181 -0
  59. package/cli/dist/commands/project-sync.d.ts +91 -0
  60. package/cli/dist/commands/project-sync.js +273 -0
  61. package/cli/dist/commands/project.d.ts +5 -0
  62. package/cli/dist/commands/project.js +677 -0
  63. package/cli/dist/commands/registry.d.ts +6 -0
  64. package/cli/dist/commands/registry.js +27 -0
  65. package/cli/dist/commands/release.d.ts +10 -0
  66. package/cli/dist/commands/release.js +414 -0
  67. package/cli/dist/commands/repository-display.d.ts +3 -0
  68. package/cli/dist/commands/repository-display.js +7 -0
  69. package/cli/dist/commands/repository-match.d.ts +14 -0
  70. package/cli/dist/commands/repository-match.js +33 -0
  71. package/cli/dist/commands/repository.d.ts +5 -0
  72. package/cli/dist/commands/repository.js +265 -0
  73. package/cli/dist/commands/rich-panel.d.ts +22 -0
  74. package/cli/dist/commands/rich-panel.js +295 -0
  75. package/cli/dist/commands/security.d.ts +3 -0
  76. package/cli/dist/commands/security.js +164 -0
  77. package/cli/dist/commands/session.d.ts +3 -0
  78. package/cli/dist/commands/session.js +161 -0
  79. package/cli/dist/commands/tracker-sync.d.ts +58 -0
  80. package/cli/dist/commands/tracker-sync.js +53 -0
  81. package/cli/dist/commands/usage-error.d.ts +47 -0
  82. package/cli/dist/commands/usage-error.js +72 -0
  83. package/cli/dist/commands/value-types.d.ts +46 -0
  84. package/cli/dist/commands/value-types.js +94 -0
  85. package/cli/dist/commands/work-target.d.ts +13 -0
  86. package/cli/dist/commands/work-target.js +41 -0
  87. package/cli/dist/commands/worktree.d.ts +3 -0
  88. package/cli/dist/commands/worktree.js +335 -0
  89. package/cli/dist/domain/api-error-detail.d.ts +10 -0
  90. package/cli/dist/domain/api-error-detail.js +25 -0
  91. package/cli/dist/domain/branch-name.d.ts +5 -0
  92. package/cli/dist/domain/branch-name.js +10 -0
  93. package/cli/dist/domain/changelog.d.ts +43 -0
  94. package/cli/dist/domain/changelog.js +107 -0
  95. package/cli/dist/domain/config.d.ts +215 -0
  96. package/cli/dist/domain/config.js +300 -0
  97. package/cli/dist/domain/dependabot-pull-requests.d.ts +82 -0
  98. package/cli/dist/domain/dependabot-pull-requests.js +338 -0
  99. package/cli/dist/domain/dependabot-source.d.ts +51 -0
  100. package/cli/dist/domain/dependabot-source.js +4 -0
  101. package/cli/dist/domain/dependabot.d.ts +47 -0
  102. package/cli/dist/domain/dependabot.js +88 -0
  103. package/cli/dist/domain/errors.d.ts +19 -0
  104. package/cli/dist/domain/errors.js +31 -0
  105. package/cli/dist/domain/field-options.d.ts +31 -0
  106. package/cli/dist/domain/field-options.js +44 -0
  107. package/cli/dist/domain/format.d.ts +47 -0
  108. package/cli/dist/domain/format.js +168 -0
  109. package/cli/dist/domain/frontmatter.d.ts +122 -0
  110. package/cli/dist/domain/frontmatter.js +264 -0
  111. package/cli/dist/domain/git-runner.d.ts +10 -0
  112. package/cli/dist/domain/git-runner.js +4 -0
  113. package/cli/dist/domain/git-url.d.ts +17 -0
  114. package/cli/dist/domain/git-url.js +30 -0
  115. package/cli/dist/domain/issue-kind.d.ts +16 -0
  116. package/cli/dist/domain/issue-kind.js +16 -0
  117. package/cli/dist/domain/issue-readout.d.ts +107 -0
  118. package/cli/dist/domain/issue-readout.js +4 -0
  119. package/cli/dist/domain/json-null.d.ts +2 -0
  120. package/cli/dist/domain/json-null.js +5 -0
  121. package/cli/dist/domain/json.d.ts +21 -0
  122. package/cli/dist/domain/json.js +47 -0
  123. package/cli/dist/domain/prerequisites.d.ts +32 -0
  124. package/cli/dist/domain/prerequisites.js +192 -0
  125. package/cli/dist/domain/pull-request.d.ts +134 -0
  126. package/cli/dist/domain/pull-request.js +232 -0
  127. package/cli/dist/domain/python-format.d.ts +105 -0
  128. package/cli/dist/domain/python-format.js +304 -0
  129. package/cli/dist/domain/readiness.d.ts +172 -0
  130. package/cli/dist/domain/readiness.js +280 -0
  131. package/cli/dist/domain/reference-parser.d.ts +49 -0
  132. package/cli/dist/domain/reference-parser.js +119 -0
  133. package/cli/dist/domain/release.d.ts +49 -0
  134. package/cli/dist/domain/release.js +72 -0
  135. package/cli/dist/domain/repository-policy.d.ts +32 -0
  136. package/cli/dist/domain/repository-policy.js +65 -0
  137. package/cli/dist/domain/repository-resolver.d.ts +57 -0
  138. package/cli/dist/domain/repository-resolver.js +119 -0
  139. package/cli/dist/domain/sections.d.ts +69 -0
  140. package/cli/dist/domain/sections.js +514 -0
  141. package/cli/dist/domain/security-alerts-source.d.ts +29 -0
  142. package/cli/dist/domain/security-alerts-source.js +4 -0
  143. package/cli/dist/domain/security-alerts.d.ts +52 -0
  144. package/cli/dist/domain/security-alerts.js +107 -0
  145. package/cli/dist/domain/security-tracker.d.ts +115 -0
  146. package/cli/dist/domain/security-tracker.js +353 -0
  147. package/cli/dist/domain/state.d.ts +29 -0
  148. package/cli/dist/domain/state.js +56 -0
  149. package/cli/dist/domain/tracker-body.d.ts +70 -0
  150. package/cli/dist/domain/tracker-body.js +65 -0
  151. package/cli/dist/domain/yaml-document.d.ts +5 -0
  152. package/cli/dist/domain/yaml-document.js +206 -0
  153. package/cli/dist/infra/authored-notes.d.ts +37 -0
  154. package/cli/dist/infra/authored-notes.js +66 -0
  155. package/cli/dist/infra/changelog-file.d.ts +19 -0
  156. package/cli/dist/infra/changelog-file.js +43 -0
  157. package/cli/dist/infra/child-stdin.d.ts +4 -0
  158. package/cli/dist/infra/child-stdin.js +8 -0
  159. package/cli/dist/infra/config-store.d.ts +49 -0
  160. package/cli/dist/infra/config-store.js +121 -0
  161. package/cli/dist/infra/diagnostics.d.ts +9 -0
  162. package/cli/dist/infra/diagnostics.js +2 -0
  163. package/cli/dist/infra/file-kind.d.ts +9 -0
  164. package/cli/dist/infra/file-kind.js +26 -0
  165. package/cli/dist/infra/frontmatter-file.d.ts +14 -0
  166. package/cli/dist/infra/frontmatter-file.js +30 -0
  167. package/cli/dist/infra/git.d.ts +74 -0
  168. package/cli/dist/infra/git.js +133 -0
  169. package/cli/dist/infra/github/auth.d.ts +60 -0
  170. package/cli/dist/infra/github/auth.js +158 -0
  171. package/cli/dist/infra/github/client.d.ts +173 -0
  172. package/cli/dist/infra/github/client.js +576 -0
  173. package/cli/dist/infra/github/dependabot-source.d.ts +13 -0
  174. package/cli/dist/infra/github/dependabot-source.js +33 -0
  175. package/cli/dist/infra/github/errors.d.ts +47 -0
  176. package/cli/dist/infra/github/errors.js +42 -0
  177. package/cli/dist/infra/github/index.d.ts +13 -0
  178. package/cli/dist/infra/github/index.js +19 -0
  179. package/cli/dist/infra/github/issues.d.ts +129 -0
  180. package/cli/dist/infra/github/issues.js +273 -0
  181. package/cli/dist/infra/github/payload.d.ts +44 -0
  182. package/cli/dist/infra/github/payload.js +80 -0
  183. package/cli/dist/infra/github/projects.d.ts +108 -0
  184. package/cli/dist/infra/github/projects.js +303 -0
  185. package/cli/dist/infra/github/pulls.d.ts +80 -0
  186. package/cli/dist/infra/github/pulls.js +161 -0
  187. package/cli/dist/infra/github/queries.d.ts +36 -0
  188. package/cli/dist/infra/github/queries.js +883 -0
  189. package/cli/dist/infra/github/readout.d.ts +18 -0
  190. package/cli/dist/infra/github/readout.js +156 -0
  191. package/cli/dist/infra/github/repos.d.ts +68 -0
  192. package/cli/dist/infra/github/repos.js +137 -0
  193. package/cli/dist/infra/github/rest-path.d.ts +14 -0
  194. package/cli/dist/infra/github/rest-path.js +31 -0
  195. package/cli/dist/infra/github/security-alerts-source.d.ts +15 -0
  196. package/cli/dist/infra/github/security-alerts-source.js +73 -0
  197. package/cli/dist/infra/gitignore-spec.d.ts +36 -0
  198. package/cli/dist/infra/gitignore-spec.js +235 -0
  199. package/cli/dist/infra/nx-scope.d.ts +51 -0
  200. package/cli/dist/infra/nx-scope.js +182 -0
  201. package/cli/dist/infra/os-error.d.ts +24 -0
  202. package/cli/dist/infra/os-error.js +38 -0
  203. package/cli/dist/infra/paths.d.ts +88 -0
  204. package/cli/dist/infra/paths.js +234 -0
  205. package/cli/dist/infra/state-store.d.ts +45 -0
  206. package/cli/dist/infra/state-store.js +118 -0
  207. package/cli/dist/infra/template-store.d.ts +15 -0
  208. package/cli/dist/infra/template-store.js +34 -0
  209. package/cli/dist/infra/text-file.d.ts +12 -0
  210. package/cli/dist/infra/text-file.js +33 -0
  211. package/cli/dist/infra/worktree-copy.d.ts +24 -0
  212. package/cli/dist/infra/worktree-copy.js +95 -0
  213. package/cli/dist/infra/worktree-links.d.ts +139 -0
  214. package/cli/dist/infra/worktree-links.js +433 -0
  215. package/cli/dist/infra/worktree-setup.d.ts +21 -0
  216. package/cli/dist/infra/worktree-setup.js +41 -0
  217. package/cli/dist/infra/worktree.d.ts +174 -0
  218. package/cli/dist/infra/worktree.js +567 -0
  219. package/cli/dist/mcp/result.d.ts +33 -0
  220. package/cli/dist/mcp/result.js +52 -0
  221. package/cli/dist/mcp/serve.d.ts +51 -0
  222. package/cli/dist/mcp/serve.js +183 -0
  223. package/cli/dist/mcp/server.d.ts +21 -0
  224. package/cli/dist/mcp/server.js +128 -0
  225. package/cli/dist/mcp/session-directory.d.ts +26 -0
  226. package/cli/dist/mcp/session-directory.js +84 -0
  227. package/cli/dist/package.json +1 -0
  228. package/cli/dist/version.d.ts +17 -0
  229. package/cli/dist/version.js +42 -0
  230. package/cli/templates/comment/merge-diff.md +34 -0
  231. package/cli/templates/comment/plan.md +181 -0
  232. package/cli/templates/comment/precede-notify.md +33 -0
  233. package/cli/templates/comment/review-findings.md +42 -0
  234. package/cli/templates/comment/review-resolution.md +48 -0
  235. package/cli/templates/issue/bug.md +63 -0
  236. package/cli/templates/issue/epic.md +54 -0
  237. package/cli/templates/issue/task-implement.md +67 -0
  238. package/cli/templates/issue/task.md +48 -0
  239. package/cli/templates/issue/tracker.md +48 -0
  240. package/cli/templates/pr/create.md +123 -0
  241. package/cli/templates/pr/release.md +56 -0
  242. package/hooks/lib/core/pecot-gitignore-ensure.sh +48 -0
  243. package/npm-shrinkwrap.json +83 -0
  244. package/package.json +22 -4
@@ -0,0 +1,266 @@
1
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types -- zod の型は readonly にできない */
2
+ // 定義したコマンドを commander につなぎ、argv から実行する。
3
+ // commander の既定(process.exit の直呼び・英語の誤り文)は使わず、click と同じ使い方の誤りに直す。
4
+ import { Command, CommanderError, Option } from 'commander';
5
+ import { z } from 'zod';
6
+ import { closeMatches } from './fuzzy.js';
7
+ import { flagOf } from './names.js';
8
+ import { invalidValueMessage, missingArgumentMessage, missingOptionMessage, noSuchOptionMessage, optionRequiresArgumentMessage, optionTakesNoValueMessage, UsageError, } from './usage-error.js';
9
+ import { convertValue, flagKindOf, valueTypeOf } from './value-types.js';
10
+ function fieldSchemaOf(definition, name) {
11
+ const field = Reflect.get(definition.input.shape, name);
12
+ return field instanceof z.ZodType ? field : undefined;
13
+ }
14
+ function isRequired(schema) {
15
+ return !schema.safeParse(undefined).success;
16
+ }
17
+ /**
18
+ typer の Missing argument と Invalid value に出す名前。引数の名前のまま(小文字)
19
+ */
20
+ function argumentLabel(name) {
21
+ return name;
22
+ }
23
+ function discard() {
24
+ // commander の出力は使わない。誤りは UsageError にして、呼び出し側が書く
25
+ }
26
+ function isListField(definition, name) {
27
+ const schema = fieldSchemaOf(definition, name);
28
+ return schema !== undefined && flagKindOf(schema) === 'list';
29
+ }
30
+ /**
31
+ コマンド名が決まった後の argv に、`--` より前の `--help` があるか。
32
+ あれば、ほかの引数は検証せずに help を出す。`--help -1` と `--base --help` が、その例。`--` の後ろの `--help` は位置引数
33
+ */
34
+ export function isHelpRequested(argv) {
35
+ const end = argv.indexOf('--');
36
+ return (end === -1 ? argv : argv.slice(0, end)).includes('--help');
37
+ }
38
+ /**
39
+ typer の Usage 行に出す書式。
40
+ 位置引数は名前のまま並べ、必須なら `{issue_ref}`、任意なら `[version]` にする。複数なら `...` を付ける
41
+ */
42
+ export function synopsisOf(definition) {
43
+ const names = definition.positional.map((name) => {
44
+ const schema = fieldSchemaOf(definition, name);
45
+ const label = argumentLabel(name);
46
+ const suffix = isListField(definition, name) ? '...' : '';
47
+ return schema === undefined || isRequired(schema) ? `{${label}}${suffix}` : `[${label}]${suffix}`;
48
+ });
49
+ return ['[OPTIONS]', ...names].join(' ');
50
+ }
51
+ export function usageOf(definition) {
52
+ return { commandPath: definition.path, synopsis: synopsisOf(definition) };
53
+ }
54
+ function bindOption(definition, field) {
55
+ const schema = fieldSchemaOf(definition, field);
56
+ if (schema === undefined)
57
+ return undefined;
58
+ const long = flagOf(field);
59
+ const short = definition.shortFlags.get(field);
60
+ const kind = flagKindOf(schema);
61
+ const valueType = valueTypeOf(schema);
62
+ const alias = definition.aliases.get(field);
63
+ // commander は 2 つ並べた名前の後ろを長い形とみなし、属性名にする。別名を先に書いて、属性名を元の名前にそろえる
64
+ const flags = `${short === undefined ? '' : `${short}, `}${alias === undefined ? '' : `${alias}, `}${long}${kind === 'boolean' ? '' : ' <value>'}`;
65
+ const option = new Option(flags);
66
+ // `--no-copy` のような名前の入力そのものを、commander に否定形(既定が真)と読ませない
67
+ option.negate = false;
68
+ if (kind === 'list')
69
+ option.argParser((value, previous) => [...previous ?? [], value]);
70
+ return {
71
+ field,
72
+ option,
73
+ isRequired: isRequired(schema),
74
+ kind,
75
+ valueType,
76
+ names: [long, ...short === undefined ? [] : [short], ...alias === undefined ? [] : [alias]],
77
+ negation: kind === 'boolean' ? definition.negations.get(field) : undefined,
78
+ };
79
+ }
80
+ function bindOptions(definition) {
81
+ return Object.keys(definition.input.shape)
82
+ .filter(name => (name !== 'repo' || definition.hasOwnRepo) && !definition.positional.includes(name))
83
+ .map(name => bindOption(definition, name))
84
+ .filter(bound => bound !== undefined);
85
+ }
86
+ /**
87
+ click は `-1` のような負数に見える語を位置引数にせず、未定義の短いオプションとして断る(`-12` も `-1`)。
88
+ その語の位置を返す。`--` より後ろと、値を取るオプションの値の位置にある語は対象にしない
89
+ */
90
+ function negativeNumberIndex(argv, bound) {
91
+ const valueTaking = new Set(bound.filter(entry => entry.kind !== 'boolean').flatMap(entry => entry.names));
92
+ let isValueNext = false;
93
+ for (const [index, argument] of argv.entries()) {
94
+ if (isValueNext) {
95
+ isValueNext = false;
96
+ continue;
97
+ }
98
+ if (argument === '--')
99
+ return undefined;
100
+ if (/^-\d/u.test(argument))
101
+ return index;
102
+ isValueNext = valueTaking.has(argument);
103
+ }
104
+ return undefined;
105
+ }
106
+ function toUsageError(error, argv, bound, usage) {
107
+ // commander の文は `error: unknown option '--x'` の形で、引用符の中に名前が入る
108
+ const quoted = error.message.replace(/^[^']*'([^']*)'.*$/su, '$1');
109
+ switch (error.code) {
110
+ case 'commander.unknownOption': {
111
+ // click は `--name=値` の値を落として名前だけで案内する。真偽のオプションなら、値は取らない旨にして Usage 行を付けない
112
+ const typedName = quoted.startsWith('--') ? quoted.replace(/=.*$/su, '') : quoted;
113
+ const booleanNames = ['--help', ...bound.flatMap(entry => entry.kind === 'boolean' ? [entry.names[0], entry.negation] : [])];
114
+ if (typedName !== quoted && booleanNames.includes(typedName))
115
+ return new UsageError(optionTakesNoValueMessage(typedName));
116
+ const longNames = ['--help', ...bound.flatMap(entry => [...entry.names.filter(name => name.startsWith('--')), ...entry.negation === undefined ? [] : [entry.negation]])];
117
+ const possibilities = typedName.startsWith('--') ? closeMatches(typedName, longNames) : [];
118
+ return new UsageError(noSuchOptionMessage(typedName, possibilities), usage);
119
+ }
120
+ case 'commander.optionMissingArgument': {
121
+ // `-c, --comment-limit <value>` のうち、利用者が打った方の名前を出す
122
+ const names = quoted.split(/[ ,]+/u).filter(part => part.startsWith('-'));
123
+ const typed = argv.findLast(argument => names.includes(argument)) ?? names.at(-1) ?? quoted;
124
+ // click はこの誤りに Usage 行・Try 行を付けない
125
+ return new UsageError(optionRequiresArgumentMessage(typed));
126
+ }
127
+ // 上の 2 つ以外の commander の誤りは、このバインドでは起きない(引数は任意、argParser は使わない)。
128
+ // makeOptionMandatory・choices・InvalidArgumentError などを使い始めたら、この行を外して誤りごとに文とテストを足す
129
+ /* v8 ignore next */
130
+ default: {
131
+ return new UsageError(error.message.replace(/^error: /u, ''), usage);
132
+ }
133
+ }
134
+ }
135
+ export function extraArgumentsMessage(extras) {
136
+ // click は個数によらず `argument(s)` と書く
137
+ return `Got unexpected extra argument(s) (${extras.join(' ')})`;
138
+ }
139
+ function convertOrFail(text, valueType, names, usage) {
140
+ const conversion = convertValue(text, valueType);
141
+ if (!conversion.isValid)
142
+ throw new UsageError(invalidValueMessage(names, conversion.message), usage);
143
+ return conversion.value;
144
+ }
145
+ function convertField(value, kind, valueType, names, usage) {
146
+ if (typeof value === 'string')
147
+ return convertOrFail(value, valueType, names, usage);
148
+ if (kind === 'list' && Array.isArray(value)) {
149
+ return value.map((item) => typeof item === 'string' ? convertOrFail(item, valueType, names, usage) : item);
150
+ }
151
+ return value;
152
+ }
153
+ function positionalInput(definition, command, usage) {
154
+ const processed = command.processedArgs;
155
+ const entries = definition.positional.flatMap((name, index) => {
156
+ const value = processed.at(index);
157
+ const schema = fieldSchemaOf(definition, name);
158
+ if (Array.isArray(value) ? value.length > 0 : value !== undefined) {
159
+ const kind = schema === undefined ? 'value' : flagKindOf(schema);
160
+ const valueType = schema === undefined ? { type: 'string' } : valueTypeOf(schema);
161
+ return [[name, convertField(value, kind, valueType, argumentLabel(name), usage)]];
162
+ }
163
+ if (schema !== undefined && isRequired(schema))
164
+ throw new UsageError(missingArgumentMessage(argumentLabel(name)), usage);
165
+ return [];
166
+ });
167
+ const lastName = definition.positional.at(-1);
168
+ const extras = lastName !== undefined && isListField(definition, lastName) ? [] : command.args.slice(definition.positional.length);
169
+ if (extras.length > 0)
170
+ throw new UsageError(extraArgumentsMessage(extras), usage);
171
+ return entries;
172
+ }
173
+ function optionInput(command, bound, usage) {
174
+ return bound.flatMap(({ field, option, isRequired: required, kind, valueType, names }) => {
175
+ const value = command.getOptionValue(option.attributeName());
176
+ if (value !== undefined)
177
+ return [[field, convertField(value, kind, valueType, names, usage)]];
178
+ if (required && kind === 'boolean')
179
+ return [[field, false]];
180
+ if (required)
181
+ throw new UsageError(missingOptionMessage(names), usage);
182
+ return [];
183
+ });
184
+ }
185
+ /**
186
+ 検証の誤り(ZodError)を使い方の誤りにする。入力名に当たるオプションか位置引数の名前で示す
187
+ */
188
+ function issueToUsageError(definition, error, bound, usage) {
189
+ const [issue] = error.issues;
190
+ const field = issue?.path.at(0);
191
+ const names = definition.positional.includes(String(field))
192
+ ? argumentLabel(String(field))
193
+ : bound.find(entry => entry.field === field)?.names ?? 'input';
194
+ return new UsageError(invalidValueMessage(names, issue?.message ?? error.message), usage);
195
+ }
196
+ /**
197
+ run が投げた context 無しの UsageError(BadParameter)に、このコマンドの使い方を付ける。ほかの例外はそのまま返す
198
+ */
199
+ export function withUsage(error, usage) {
200
+ return error instanceof UsageError && error.context === undefined ? new UsageError(error.message, usage) : error;
201
+ }
202
+ /**
203
+ グローバルの --repo を入力 repo にする。自分の `--repo` を持つコマンドは、サブコマンドの値を先に使い、無ければグローバルの値を使う。
204
+ ignoresGlobalRepo のコマンドには、グローバルの値を渡さない
205
+ */
206
+ function withRepository(definition, input, repository) {
207
+ if (repository === undefined || definition.ignoresGlobalRepo)
208
+ return input;
209
+ return definition.hasOwnRepo ? { repo: repository, ...input } : { ...input, repo: repository };
210
+ }
211
+ /**
212
+ 定義したコマンドを argv(コマンド名より後ろ)で実行して、終了コードを返す。
213
+ グローバルの --repo は repo で渡し、入力 repo にする。
214
+ 使い方の誤りは UsageError を投げる。--help は扱わない(help は cli.ts の execute が isHelpRequested で先に出す)ので、ここでは未知のオプション。
215
+ UsageError にするのは解析と入力の検証(parse)の誤りだけで、本体(run)の例外はそのまま伝える
216
+ */
217
+ export async function runDefinition(definition, argv, io, repository) {
218
+ const usage = usageOf(definition);
219
+ const bound = bindOptions(definition);
220
+ const command = new Command(definition.path.join(' '));
221
+ command
222
+ .description(definition.description)
223
+ .helpOption(false)
224
+ .allowExcessArguments(true)
225
+ .exitOverride()
226
+ .configureOutput({ writeOut: discard, writeErr: discard, outputError: discard });
227
+ for (const name of definition.positional) {
228
+ command.argument(isListField(definition, name) ? `[${argumentLabel(name)}...]` : `[${argumentLabel(name)}]`);
229
+ }
230
+ for (const { option, negation } of bound) {
231
+ command.addOption(option);
232
+ if (negation !== undefined)
233
+ command.addOption(new Option(negation));
234
+ }
235
+ // 負数に見える語があれば、その手前までを解析する。手前の誤りを先に出し、その語を断る
236
+ const negativeIndex = negativeNumberIndex(argv, bound);
237
+ try {
238
+ await command.parseAsync(negativeIndex === undefined ? [...argv] : argv.slice(0, negativeIndex), { from: 'user' });
239
+ }
240
+ catch (error) {
241
+ if (!(error instanceof CommanderError))
242
+ throw error;
243
+ throw toUsageError(error, argv, bound, usage);
244
+ }
245
+ const negativeWord = negativeIndex === undefined ? undefined : argv.at(negativeIndex);
246
+ if (negativeWord !== undefined)
247
+ throw new UsageError(noSuchOptionMessage(negativeWord.slice(0, 2)), usage);
248
+ const entries = [...positionalInput(definition, command, usage), ...optionInput(command, bound, usage)];
249
+ const input = Object.fromEntries(entries);
250
+ let prepared;
251
+ try {
252
+ prepared = definition.parse(withRepository(definition, input, repository));
253
+ }
254
+ catch (error) {
255
+ if (error instanceof z.ZodError)
256
+ throw issueToUsageError(definition, error, bound, usage);
257
+ throw error;
258
+ }
259
+ try {
260
+ return await prepared.run(io);
261
+ }
262
+ catch (error) {
263
+ throw withUsage(error, usage);
264
+ }
265
+ }
266
+ //# sourceMappingURL=command-binding.js.map
@@ -0,0 +1,136 @@
1
+ import { z } from 'zod';
2
+ import { BaseError } from '../domain/errors.js';
3
+ /**
4
+ コマンドが出力を書く先。CLI は process の stdout・stderr、MCP は結果の文字列に集める
5
+ */
6
+ export interface CommandIo {
7
+ readonly stdout: (text: string) => void;
8
+ readonly stderr: (text: string) => void;
9
+ /**
10
+ 標準入力から 1 行読む(末尾の改行は含めない)。終端は undefined。渡されない io(MCP)は、入力の終端と同じ扱い
11
+ */
12
+ readonly readLine?: () => string | undefined;
13
+ }
14
+ /**
15
+ コマンドの途中で、決めた終了コードで止めるための例外。defineCommand が包む run の 1 か所で捕まえて、戻り値の終了コードにする。
16
+ Python の `typer.Exit(n)` に当たる。`error:` の行や使い方の枠は出さない(出す行は投げる前に書く)ので、PecotError・InvalidValueError・OsError・UsageError は継がない
17
+ */
18
+ export declare class CommandExit extends BaseError {
19
+ readonly exitCode: number;
20
+ constructor(exitCode: number);
21
+ }
22
+ declare const GLOBAL_SCHEMA: z.ZodObject<{
23
+ repo: z.ZodOptional<z.ZodString>;
24
+ }, z.core.$strip>;
25
+ export type GlobalInput = z.output<typeof GLOBAL_SCHEMA>;
26
+ export interface CommandSpec<Input extends z.ZodObject> {
27
+ /**
28
+ `['issue', 'create']`、`['add-to-project']`
29
+ */
30
+ readonly path: readonly string[];
31
+ /**
32
+ typer の help の 1 行目と同じ文
33
+ */
34
+ readonly description: string;
35
+ /**
36
+ 引数とオプション。名前は CLI のオプション名の `-` を `_` にしたもの。
37
+ `repo` はグローバルの --repo 用の予約名。hasOwnRepo が真のコマンドだけ、自分の `--repo` として持てる
38
+ */
39
+ readonly input: Input;
40
+ /**
41
+ 自分の `--repo` を持つ印。真なら input に `repo` を書ける(説明は `.describe()`、形の検証は input の refine)。
42
+ 実行時の `repo` は、サブコマンドの値を先に使い、無ければグローバルの値になる
43
+ */
44
+ readonly hasOwnRepo?: boolean;
45
+ /**
46
+ 真なら、グローバルの --repo を使わない。`repo` は自分のオプションの値だけになる(hasOwnRepo のコマンド用)
47
+ */
48
+ readonly ignoresGlobalRepo?: boolean;
49
+ /**
50
+ input のうち CLI で位置引数になるもの(順序つき)
51
+ */
52
+ readonly positional: readonly (keyof Input['shape'] & string)[];
53
+ /**
54
+ CLI の短いオプション。キーは input の名前、値は `-D` の形
55
+ */
56
+ readonly shortFlags?: Readonly<Partial<Record<keyof Input['shape'] & string, string>>>;
57
+ /**
58
+ CLI の別名の長いオプション。キーは input の名前、値は `--force` の形。短いオプションとは併用できない
59
+ */
60
+ readonly aliases?: Readonly<Partial<Record<keyof Input['shape'] & string, string>>>;
61
+ /**
62
+ CLI の否定形のオプション。キーは input の名前、値は `--no-checkout` の形。
63
+ `--checkout/--no-checkout` の形で、既定値が真の真偽値を偽にできる
64
+ */
65
+ readonly negations?: Readonly<Partial<Record<keyof Input['shape'] & string, string>>>;
66
+ /**
67
+ 実行して終了コードを返す。出力は io に書く
68
+ */
69
+ readonly run: (input: z.output<Input> & GlobalInput, io: CommandIo) => Promise<number>;
70
+ /**
71
+ CLI でだけ利用者に問うか
72
+ */
73
+ readonly interactive: boolean;
74
+ }
75
+ /**
76
+ 検証を終えた入力で、本体を呼べる状態のコマンド
77
+ */
78
+ export interface PreparedCommand {
79
+ readonly run: (io: CommandIo) => Promise<number>;
80
+ }
81
+ /**
82
+ 入力の型を消した定義。一覧の要素の型
83
+ */
84
+ export interface CommandDefinition {
85
+ readonly path: readonly string[];
86
+ readonly toolName: string;
87
+ readonly description: string;
88
+ /**
89
+ グローバルの repo を足した入力
90
+ */
91
+ readonly input: z.ZodObject;
92
+ readonly positional: readonly string[];
93
+ /**
94
+ 自分の `--repo` を持つか。真のとき、input の repo はコマンド自身の引数
95
+ */
96
+ readonly hasOwnRepo: boolean;
97
+ /**
98
+ グローバルの --repo を入力に渡さないか
99
+ */
100
+ readonly ignoresGlobalRepo: boolean;
101
+ /**
102
+ input の名前から短いオプションへ
103
+ */
104
+ readonly shortFlags: ReadonlyMap<string, string>;
105
+ /**
106
+ input の名前から別名の長いオプションへ
107
+ */
108
+ readonly aliases: ReadonlyMap<string, string>;
109
+ /**
110
+ input の名前から否定形のオプションへ
111
+ */
112
+ readonly negations: ReadonlyMap<string, string>;
113
+ readonly interactive: boolean;
114
+ /**
115
+ 検証の段。入力が不正なら ZodError を投げる。本体は呼ばない
116
+ */
117
+ readonly parse: (input: Readonly<Record<string, unknown>>) => PreparedCommand;
118
+ /**
119
+ parse してから本体を呼ぶ。本体が投げた ZodError は、検証の誤りと区別できる
120
+ */
121
+ readonly run: (input: Readonly<Record<string, unknown>>, io: CommandIo) => Promise<number>;
122
+ }
123
+ /**
124
+ path から MCP のツール名を作る。`['issue', 'create']` は issue_create、`['add-to-project']` は add_to_project
125
+ */
126
+ export declare function toolNameOf(path: readonly string[]): string;
127
+ /**
128
+ path から定義書(interface/)の項目の ID を作る。`['create-branch']` は PW-CREATE-BRANCH、`['issue', 'create']` は PW-ISSUE-CREATE
129
+ */
130
+ export declare function interfaceIdOf(path: readonly string[]): string;
131
+ /**
132
+ 型付きの仕様から、型を消した定義を作る。parse が input で検証してから本体を呼ぶので、型を消しても安全に呼べる
133
+ */
134
+ export declare function defineCommand<Input extends z.ZodObject>(spec: Readonly<CommandSpec<Input>>): CommandDefinition;
135
+ export {};
136
+ //# sourceMappingURL=command-definition.d.ts.map
@@ -0,0 +1,101 @@
1
+ // コマンドの定義の型と defineCommand()。コマンドのファイルが import する。
2
+ // registry.ts が COMMANDS を持つために各コマンドのファイルを import するので、循環を避けて定義をここに分ける。
3
+ // グローバルの --repo は、全コマンドの入力 repo として合成する。
4
+ import { z } from 'zod';
5
+ import { BaseError } from '../domain/errors.js';
6
+ import { flagOf } from './names.js';
7
+ /**
8
+ コマンドの途中で、決めた終了コードで止めるための例外。defineCommand が包む run の 1 か所で捕まえて、戻り値の終了コードにする。
9
+ Python の `typer.Exit(n)` に当たる。`error:` の行や使い方の枠は出さない(出す行は投げる前に書く)ので、PecotError・InvalidValueError・OsError・UsageError は継がない
10
+ */
11
+ export class CommandExit extends BaseError {
12
+ exitCode;
13
+ constructor(exitCode) {
14
+ super(`exit ${String(exitCode)}`);
15
+ this.exitCode = exitCode;
16
+ }
17
+ }
18
+ /**
19
+ グローバルの --repo を入力にしたもの。全コマンドの入力に足される
20
+ */
21
+ const GLOBAL_SHAPE = { repo: z.string().optional() };
22
+ const GLOBAL_SCHEMA = z.object(GLOBAL_SHAPE);
23
+ /**
24
+ path から MCP のツール名を作る。`['issue', 'create']` は issue_create、`['add-to-project']` は add_to_project
25
+ */
26
+ export function toolNameOf(path) {
27
+ return path.join('_').replaceAll('-', '_');
28
+ }
29
+ /**
30
+ path から定義書(interface/)の項目の ID を作る。`['create-branch']` は PW-CREATE-BRANCH、`['issue', 'create']` は PW-ISSUE-CREATE
31
+ */
32
+ export function interfaceIdOf(path) {
33
+ return `PW-${path.join(' ').toUpperCase().replaceAll(/[ _]/g, '-')}`;
34
+ }
35
+ function flagMap(flags) {
36
+ return new Map(Object.entries(flags ?? {}).filter((entry) => typeof entry[1] === 'string'));
37
+ }
38
+ // 入力 no_x の `--no-x` と、入力 x の否定形 `--no-x` のように、同じ長いオプションになる組を断る
39
+ function assertDistinctFlags(fields, positional, negations) {
40
+ const flags = [
41
+ ...fields.filter(name => !positional.includes(name)).map(name => flagOf(name)),
42
+ ...negations.values(),
43
+ ];
44
+ const duplicate = flags.find((flag, index) => flags.indexOf(flag) !== index);
45
+ if (duplicate !== undefined)
46
+ throw new TypeError(`オプション ${duplicate} が重複しています`);
47
+ }
48
+ async function runToExitCode(run, input, io) {
49
+ try {
50
+ return await run(input, io);
51
+ }
52
+ catch (error) {
53
+ if (error instanceof CommandExit)
54
+ return error.exitCode;
55
+ throw error;
56
+ }
57
+ }
58
+ /**
59
+ 型付きの仕様から、型を消した定義を作る。parse が input で検証してから本体を呼ぶので、型を消しても安全に呼べる
60
+ */
61
+ export function defineCommand(spec) {
62
+ if (spec.path.length === 0)
63
+ throw new TypeError('コマンドの path が空です');
64
+ const hasOwnRepository = spec.hasOwnRepo === true;
65
+ const hasRepositoryField = Object.hasOwn(spec.input.shape, 'repo');
66
+ if (hasRepositoryField && !hasOwnRepository)
67
+ throw new TypeError('input の repo はグローバルの --repo 用の予約名です');
68
+ if (hasOwnRepository && !hasRepositoryField)
69
+ throw new TypeError('hasOwnRepo のコマンドは input に repo が要ります');
70
+ assertDistinctFlags(Object.keys(spec.input.shape), spec.positional, flagMap(spec.negations));
71
+ const aliased = Object.keys(spec.aliases ?? {});
72
+ for (const name of aliased) {
73
+ if (spec.shortFlags !== undefined && Object.hasOwn(spec.shortFlags, name))
74
+ throw new TypeError(`${name}: 短いオプションと別名は併用できません`);
75
+ }
76
+ const input = hasOwnRepository ? spec.input : spec.input.safeExtend(GLOBAL_SHAPE);
77
+ const parse = (raw) => {
78
+ // 検証は spec.input で行う(全体の refine を保つ)。グローバルの repo は strictObject が拒むので外し、別に検証する。
79
+ // 自分の repo を持つコマンドは、repo も spec.input が検証する
80
+ const parsed = hasOwnRepository
81
+ ? spec.input.parse(raw)
82
+ : { ...spec.input.parse(Object.fromEntries(Object.entries(raw).filter(([name]) => name !== 'repo'))), ...GLOBAL_SCHEMA.parse(raw) };
83
+ return { run: async (io) => await runToExitCode(spec.run, parsed, io) };
84
+ };
85
+ return {
86
+ path: spec.path,
87
+ toolName: toolNameOf(spec.path),
88
+ description: spec.description,
89
+ input,
90
+ positional: spec.positional,
91
+ hasOwnRepo: hasOwnRepository,
92
+ ignoresGlobalRepo: spec.ignoresGlobalRepo === true,
93
+ shortFlags: flagMap(spec.shortFlags),
94
+ aliases: flagMap(spec.aliases),
95
+ negations: flagMap(spec.negations),
96
+ interactive: spec.interactive,
97
+ parse,
98
+ run: async (raw, io) => await parse(raw).run(io),
99
+ };
100
+ }
101
+ //# sourceMappingURL=command-definition.js.map
@@ -0,0 +1,8 @@
1
+ import type { AuthoredNotes } from '../infra/authored-notes.js';
2
+ import type { CommandDefinition } from './registry.js';
3
+ /**
4
+ コマンドの help。notes が undefined(定義書の手書きの側が読めない)なら Constraints・Details・Examples と初期値の補足を出さない。
5
+ Examples の打つ形の行は折り返さないので、幅を超えることがある。幅は 40 を下限にする
6
+ */
7
+ export declare function commandHelpText(definition: Readonly<CommandDefinition>, notes: Readonly<AuthoredNotes> | undefined, columns: number): string;
8
+ //# sourceMappingURL=command-help.d.ts.map