@contember/cli-common 2.1.0-rc.3 → 2.2.0-alpha.10

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 (278) hide show
  1. package/dist/development/index.cjs +20 -0
  2. package/dist/development/index.cjs.map +1 -1
  3. package/dist/development/index.js +22 -2
  4. package/dist/development/index.js.map +1 -1
  5. package/dist/development/src/application/Application.cjs +116 -59
  6. package/dist/development/src/application/Application.cjs.map +1 -1
  7. package/dist/development/src/application/Application.js +116 -59
  8. package/dist/development/src/application/Application.js.map +1 -1
  9. package/dist/development/src/application/CliError.cjs +104 -0
  10. package/dist/development/src/application/CliError.cjs.map +1 -0
  11. package/dist/development/src/application/CliError.js +104 -0
  12. package/dist/development/src/application/CliError.js.map +1 -0
  13. package/dist/development/src/application/Command.cjs +18 -5
  14. package/dist/development/src/application/Command.cjs.map +1 -1
  15. package/dist/development/src/application/Command.js +18 -5
  16. package/dist/development/src/application/Command.js.map +1 -1
  17. package/dist/development/src/application/CommandConfiguration.cjs +21 -3
  18. package/dist/development/src/application/CommandConfiguration.cjs.map +1 -1
  19. package/dist/development/src/application/CommandConfiguration.js +23 -5
  20. package/dist/development/src/application/CommandConfiguration.js.map +1 -1
  21. package/dist/development/src/application/CommandManager.cjs +139 -21
  22. package/dist/development/src/application/CommandManager.cjs.map +1 -1
  23. package/dist/development/src/application/CommandManager.js +139 -21
  24. package/dist/development/src/application/CommandManager.js.map +1 -1
  25. package/dist/development/src/application/ErrorRenderer.cjs +38 -0
  26. package/dist/development/src/application/ErrorRenderer.cjs.map +1 -0
  27. package/dist/development/src/application/ErrorRenderer.js +38 -0
  28. package/dist/development/src/application/ErrorRenderer.js.map +1 -0
  29. package/dist/development/src/application/GlobalOptions.cjs +54 -0
  30. package/dist/development/src/application/GlobalOptions.cjs.map +1 -0
  31. package/dist/development/src/application/GlobalOptions.js +54 -0
  32. package/dist/development/src/application/GlobalOptions.js.map +1 -0
  33. package/dist/development/src/application/Input.cjs +5 -0
  34. package/dist/development/src/application/Input.cjs.map +1 -1
  35. package/dist/development/src/application/Input.js +5 -0
  36. package/dist/development/src/application/Input.js.map +1 -1
  37. package/dist/development/src/application/InputParser.cjs +7 -3
  38. package/dist/development/src/application/InputParser.cjs.map +1 -1
  39. package/dist/development/src/application/InputParser.js +7 -3
  40. package/dist/development/src/application/InputParser.js.map +1 -1
  41. package/dist/development/src/application/Option.cjs +11 -0
  42. package/dist/development/src/application/Option.cjs.map +1 -1
  43. package/dist/development/src/application/Option.js +12 -1
  44. package/dist/development/src/application/Option.js.map +1 -1
  45. package/dist/development/src/application/Output.cjs +242 -0
  46. package/dist/development/src/application/Output.cjs.map +1 -0
  47. package/dist/development/src/application/Output.js +242 -0
  48. package/dist/development/src/application/Output.js.map +1 -0
  49. package/dist/development/src/application/UsageFormatter.cjs.map +1 -1
  50. package/dist/development/src/application/UsageFormatter.js.map +1 -1
  51. package/dist/development/src/application/exitProcess.cjs +7 -0
  52. package/dist/development/src/application/exitProcess.cjs.map +1 -0
  53. package/dist/development/src/application/exitProcess.js +7 -0
  54. package/dist/development/src/application/exitProcess.js.map +1 -0
  55. package/dist/development/src/npm/CommandRunner.cjs +126 -24
  56. package/dist/development/src/npm/CommandRunner.cjs.map +1 -1
  57. package/dist/development/src/npm/CommandRunner.js +126 -24
  58. package/dist/development/src/npm/CommandRunner.js.map +1 -1
  59. package/dist/development/src/npm/Package.cjs.map +1 -1
  60. package/dist/development/src/npm/Package.js.map +1 -1
  61. package/dist/development/src/npm/PackageWorkspace.cjs +3 -3
  62. package/dist/development/src/npm/PackageWorkspace.cjs.map +1 -1
  63. package/dist/development/src/npm/PackageWorkspace.js.map +1 -1
  64. package/dist/development/src/npm/PackageWorkspaceResolver.cjs +5 -5
  65. package/dist/development/src/npm/PackageWorkspaceResolver.cjs.map +1 -1
  66. package/dist/development/src/npm/PackageWorkspaceResolver.js.map +1 -1
  67. package/dist/development/src/npm/packageManagers/Bun.cjs +3 -4
  68. package/dist/development/src/npm/packageManagers/Bun.cjs.map +1 -1
  69. package/dist/development/src/npm/packageManagers/Bun.js +1 -2
  70. package/dist/development/src/npm/packageManagers/Bun.js.map +1 -1
  71. package/dist/development/src/npm/packageManagers/Npm.cjs +3 -4
  72. package/dist/development/src/npm/packageManagers/Npm.cjs.map +1 -1
  73. package/dist/development/src/npm/packageManagers/Npm.js +1 -2
  74. package/dist/development/src/npm/packageManagers/Npm.js.map +1 -1
  75. package/dist/development/src/npm/packageManagers/PackageManagerHelpers.cjs +3 -3
  76. package/dist/development/src/npm/packageManagers/PackageManagerHelpers.cjs.map +1 -1
  77. package/dist/development/src/npm/packageManagers/PackageManagerHelpers.js.map +1 -1
  78. package/dist/development/src/npm/packageManagers/Pnpm.cjs +5 -6
  79. package/dist/development/src/npm/packageManagers/Pnpm.cjs.map +1 -1
  80. package/dist/development/src/npm/packageManagers/Pnpm.js +1 -2
  81. package/dist/development/src/npm/packageManagers/Pnpm.js.map +1 -1
  82. package/dist/development/src/npm/packageManagers/Yarn.cjs +3 -4
  83. package/dist/development/src/npm/packageManagers/Yarn.cjs.map +1 -1
  84. package/dist/development/src/npm/packageManagers/Yarn.js +1 -2
  85. package/dist/development/src/npm/packageManagers/Yarn.js.map +1 -1
  86. package/dist/development/src/npm/packageManagers/YarnClassic.cjs +3 -4
  87. package/dist/development/src/npm/packageManagers/YarnClassic.cjs.map +1 -1
  88. package/dist/development/src/npm/packageManagers/YarnClassic.js +1 -2
  89. package/dist/development/src/npm/packageManagers/YarnClassic.js.map +1 -1
  90. package/dist/development/src/utils/findPackageRoot.cjs +3 -3
  91. package/dist/development/src/utils/findPackageRoot.cjs.map +1 -1
  92. package/dist/production/index.cjs +20 -0
  93. package/dist/production/index.cjs.map +1 -1
  94. package/dist/production/index.js +22 -2
  95. package/dist/production/index.js.map +1 -1
  96. package/dist/production/src/application/Application.cjs +116 -59
  97. package/dist/production/src/application/Application.cjs.map +1 -1
  98. package/dist/production/src/application/Application.js +116 -59
  99. package/dist/production/src/application/Application.js.map +1 -1
  100. package/dist/production/src/application/CliError.cjs +104 -0
  101. package/dist/production/src/application/CliError.cjs.map +1 -0
  102. package/dist/production/src/application/CliError.js +104 -0
  103. package/dist/production/src/application/CliError.js.map +1 -0
  104. package/dist/production/src/application/Command.cjs +18 -5
  105. package/dist/production/src/application/Command.cjs.map +1 -1
  106. package/dist/production/src/application/Command.js +18 -5
  107. package/dist/production/src/application/Command.js.map +1 -1
  108. package/dist/production/src/application/CommandConfiguration.cjs +21 -3
  109. package/dist/production/src/application/CommandConfiguration.cjs.map +1 -1
  110. package/dist/production/src/application/CommandConfiguration.js +23 -5
  111. package/dist/production/src/application/CommandConfiguration.js.map +1 -1
  112. package/dist/production/src/application/CommandManager.cjs +139 -21
  113. package/dist/production/src/application/CommandManager.cjs.map +1 -1
  114. package/dist/production/src/application/CommandManager.js +139 -21
  115. package/dist/production/src/application/CommandManager.js.map +1 -1
  116. package/dist/production/src/application/ErrorRenderer.cjs +38 -0
  117. package/dist/production/src/application/ErrorRenderer.cjs.map +1 -0
  118. package/dist/production/src/application/ErrorRenderer.js +38 -0
  119. package/dist/production/src/application/ErrorRenderer.js.map +1 -0
  120. package/dist/production/src/application/GlobalOptions.cjs +54 -0
  121. package/dist/production/src/application/GlobalOptions.cjs.map +1 -0
  122. package/dist/production/src/application/GlobalOptions.js +54 -0
  123. package/dist/production/src/application/GlobalOptions.js.map +1 -0
  124. package/dist/production/src/application/Input.cjs +5 -0
  125. package/dist/production/src/application/Input.cjs.map +1 -1
  126. package/dist/production/src/application/Input.js +5 -0
  127. package/dist/production/src/application/Input.js.map +1 -1
  128. package/dist/production/src/application/InputParser.cjs +7 -3
  129. package/dist/production/src/application/InputParser.cjs.map +1 -1
  130. package/dist/production/src/application/InputParser.js +7 -3
  131. package/dist/production/src/application/InputParser.js.map +1 -1
  132. package/dist/production/src/application/Option.cjs +11 -0
  133. package/dist/production/src/application/Option.cjs.map +1 -1
  134. package/dist/production/src/application/Option.js +12 -1
  135. package/dist/production/src/application/Option.js.map +1 -1
  136. package/dist/production/src/application/Output.cjs +242 -0
  137. package/dist/production/src/application/Output.cjs.map +1 -0
  138. package/dist/production/src/application/Output.js +242 -0
  139. package/dist/production/src/application/Output.js.map +1 -0
  140. package/dist/production/src/application/UsageFormatter.cjs.map +1 -1
  141. package/dist/production/src/application/UsageFormatter.js.map +1 -1
  142. package/dist/production/src/application/exitProcess.cjs +7 -0
  143. package/dist/production/src/application/exitProcess.cjs.map +1 -0
  144. package/dist/production/src/application/exitProcess.js +7 -0
  145. package/dist/production/src/application/exitProcess.js.map +1 -0
  146. package/dist/production/src/npm/CommandRunner.cjs +126 -24
  147. package/dist/production/src/npm/CommandRunner.cjs.map +1 -1
  148. package/dist/production/src/npm/CommandRunner.js +126 -24
  149. package/dist/production/src/npm/CommandRunner.js.map +1 -1
  150. package/dist/production/src/npm/Package.cjs.map +1 -1
  151. package/dist/production/src/npm/Package.js.map +1 -1
  152. package/dist/production/src/npm/PackageWorkspace.cjs +3 -3
  153. package/dist/production/src/npm/PackageWorkspace.cjs.map +1 -1
  154. package/dist/production/src/npm/PackageWorkspace.js.map +1 -1
  155. package/dist/production/src/npm/PackageWorkspaceResolver.cjs +5 -5
  156. package/dist/production/src/npm/PackageWorkspaceResolver.cjs.map +1 -1
  157. package/dist/production/src/npm/PackageWorkspaceResolver.js.map +1 -1
  158. package/dist/production/src/npm/packageManagers/Bun.cjs +3 -4
  159. package/dist/production/src/npm/packageManagers/Bun.cjs.map +1 -1
  160. package/dist/production/src/npm/packageManagers/Bun.js +1 -2
  161. package/dist/production/src/npm/packageManagers/Bun.js.map +1 -1
  162. package/dist/production/src/npm/packageManagers/Npm.cjs +3 -4
  163. package/dist/production/src/npm/packageManagers/Npm.cjs.map +1 -1
  164. package/dist/production/src/npm/packageManagers/Npm.js +1 -2
  165. package/dist/production/src/npm/packageManagers/Npm.js.map +1 -1
  166. package/dist/production/src/npm/packageManagers/PackageManagerHelpers.cjs +3 -3
  167. package/dist/production/src/npm/packageManagers/PackageManagerHelpers.cjs.map +1 -1
  168. package/dist/production/src/npm/packageManagers/PackageManagerHelpers.js.map +1 -1
  169. package/dist/production/src/npm/packageManagers/Pnpm.cjs +5 -6
  170. package/dist/production/src/npm/packageManagers/Pnpm.cjs.map +1 -1
  171. package/dist/production/src/npm/packageManagers/Pnpm.js +1 -2
  172. package/dist/production/src/npm/packageManagers/Pnpm.js.map +1 -1
  173. package/dist/production/src/npm/packageManagers/Yarn.cjs +3 -4
  174. package/dist/production/src/npm/packageManagers/Yarn.cjs.map +1 -1
  175. package/dist/production/src/npm/packageManagers/Yarn.js +1 -2
  176. package/dist/production/src/npm/packageManagers/Yarn.js.map +1 -1
  177. package/dist/production/src/npm/packageManagers/YarnClassic.cjs +3 -4
  178. package/dist/production/src/npm/packageManagers/YarnClassic.cjs.map +1 -1
  179. package/dist/production/src/npm/packageManagers/YarnClassic.js +1 -2
  180. package/dist/production/src/npm/packageManagers/YarnClassic.js.map +1 -1
  181. package/dist/production/src/utils/findPackageRoot.cjs +3 -3
  182. package/dist/production/src/utils/findPackageRoot.cjs.map +1 -1
  183. package/dist/types/application/Application.d.ts +22 -11
  184. package/dist/types/application/Application.d.ts.map +1 -1
  185. package/dist/types/application/Argument.d.ts.map +1 -1
  186. package/dist/types/application/CliError.d.ts +40 -0
  187. package/dist/types/application/CliError.d.ts.map +1 -0
  188. package/dist/types/application/Command.d.ts +14 -4
  189. package/dist/types/application/Command.d.ts.map +1 -1
  190. package/dist/types/application/CommandConfiguration.d.ts +10 -7
  191. package/dist/types/application/CommandConfiguration.d.ts.map +1 -1
  192. package/dist/types/application/CommandManager.d.ts +41 -3
  193. package/dist/types/application/CommandManager.d.ts.map +1 -1
  194. package/dist/types/application/ErrorRenderer.d.ts +8 -0
  195. package/dist/types/application/ErrorRenderer.d.ts.map +1 -0
  196. package/dist/types/application/GlobalOptions.d.ts +28 -0
  197. package/dist/types/application/GlobalOptions.d.ts.map +1 -0
  198. package/dist/types/application/Input.d.ts +2 -0
  199. package/dist/types/application/Input.d.ts.map +1 -1
  200. package/dist/types/application/InputParser.d.ts +8 -4
  201. package/dist/types/application/InputParser.d.ts.map +1 -1
  202. package/dist/types/application/Option.d.ts +2 -0
  203. package/dist/types/application/Option.d.ts.map +1 -1
  204. package/dist/types/application/Output.d.ts +81 -0
  205. package/dist/types/application/Output.d.ts.map +1 -0
  206. package/dist/types/application/UsageFormatter.d.ts +2 -2
  207. package/dist/types/application/UsageFormatter.d.ts.map +1 -1
  208. package/dist/types/application/exitProcess.d.ts +11 -0
  209. package/dist/types/application/exitProcess.d.ts.map +1 -0
  210. package/dist/types/application/index.d.ts +15 -10
  211. package/dist/types/application/index.d.ts.map +1 -1
  212. package/dist/types/index.d.ts +4 -4
  213. package/dist/types/index.d.ts.map +1 -1
  214. package/dist/types/npm/CommandRunner.d.ts +13 -0
  215. package/dist/types/npm/CommandRunner.d.ts.map +1 -1
  216. package/dist/types/npm/Package.d.ts +1 -1
  217. package/dist/types/npm/Package.d.ts.map +1 -1
  218. package/dist/types/npm/PackageWorkspace.d.ts +3 -3
  219. package/dist/types/npm/PackageWorkspace.d.ts.map +1 -1
  220. package/dist/types/npm/PackageWorkspaceResolver.d.ts +3 -3
  221. package/dist/types/npm/PackageWorkspaceResolver.d.ts.map +1 -1
  222. package/dist/types/npm/index.d.ts +8 -8
  223. package/dist/types/npm/index.d.ts.map +1 -1
  224. package/dist/types/npm/packageManagers/Bun.d.ts +5 -5
  225. package/dist/types/npm/packageManagers/Bun.d.ts.map +1 -1
  226. package/dist/types/npm/packageManagers/Npm.d.ts +5 -5
  227. package/dist/types/npm/packageManagers/Npm.d.ts.map +1 -1
  228. package/dist/types/npm/packageManagers/PackageManager.d.ts +2 -2
  229. package/dist/types/npm/packageManagers/PackageManager.d.ts.map +1 -1
  230. package/dist/types/npm/packageManagers/PackageManagerHelpers.d.ts +3 -3
  231. package/dist/types/npm/packageManagers/PackageManagerHelpers.d.ts.map +1 -1
  232. package/dist/types/npm/packageManagers/Pnpm.d.ts +5 -5
  233. package/dist/types/npm/packageManagers/Pnpm.d.ts.map +1 -1
  234. package/dist/types/npm/packageManagers/Yarn.d.ts +5 -5
  235. package/dist/types/npm/packageManagers/Yarn.d.ts.map +1 -1
  236. package/dist/types/npm/packageManagers/YarnClassic.d.ts +5 -5
  237. package/dist/types/npm/packageManagers/YarnClassic.d.ts.map +1 -1
  238. package/dist/types/tsconfig.tsbuildinfo +1 -1
  239. package/dist/types/utils/findPackageRoot.d.ts.map +1 -1
  240. package/dist/types/utils/index.d.ts +1 -1
  241. package/dist/types/utils/index.d.ts.map +1 -1
  242. package/package.json +3 -2
  243. package/src/application/Application.ts +138 -76
  244. package/src/application/CliError.ts +117 -0
  245. package/src/application/Command.ts +29 -8
  246. package/src/application/CommandConfiguration.ts +26 -8
  247. package/src/application/CommandManager.ts +173 -23
  248. package/src/application/ErrorRenderer.ts +40 -0
  249. package/src/application/GlobalOptions.ts +76 -0
  250. package/src/application/Input.ts +6 -0
  251. package/src/application/InputParser.ts +16 -7
  252. package/src/application/Option.ts +12 -0
  253. package/src/application/Output.ts +329 -0
  254. package/src/application/UsageFormatter.ts +3 -3
  255. package/src/application/exitProcess.ts +12 -0
  256. package/src/application/index.ts +15 -10
  257. package/src/index.ts +42 -4
  258. package/src/npm/CommandRunner.ts +166 -23
  259. package/src/npm/Package.ts +1 -1
  260. package/src/npm/PackageWorkspace.ts +4 -4
  261. package/src/npm/PackageWorkspaceResolver.ts +9 -9
  262. package/src/npm/index.ts +8 -8
  263. package/src/npm/packageManagers/Bun.ts +7 -8
  264. package/src/npm/packageManagers/Npm.ts +7 -8
  265. package/src/npm/packageManagers/PackageManager.ts +2 -2
  266. package/src/npm/packageManagers/PackageManagerHelpers.ts +3 -3
  267. package/src/npm/packageManagers/Pnpm.ts +7 -8
  268. package/src/npm/packageManagers/Yarn.ts +7 -8
  269. package/src/npm/packageManagers/YarnClassic.ts +7 -8
  270. package/src/utils/index.ts +1 -1
  271. package/tests/cases/unit/application.test.ts +426 -0
  272. package/tests/cases/unit/cliError.test.ts +127 -0
  273. package/tests/cases/unit/commandManager.test.ts +270 -0
  274. package/tests/cases/unit/commandRunner.test.ts +192 -0
  275. package/tests/cases/unit/inputParser.test.ts +21 -0
  276. package/tests/cases/unit/output.test.ts +393 -0
  277. package/tests/lib/testOutput.ts +35 -0
  278. package/types/chalk-table/index.d.ts +9 -0
@@ -1 +1 @@
1
- {"version":3,"file":"CommandConfiguration.cjs","sources":["../../../../../packages/cli-common/src/application/CommandConfiguration.ts"],"sourcesContent":["import { Argument, ArgumentConfiguration } from './Argument'\nimport { Option, OptionConfiguration, OptionMode } from './Option'\nimport { InputParser } from './InputParser'\nimport { UsageFormat, UsageFormatter } from './UsageFormatter'\nimport { Arguments, Options } from './Input'\n\nexport class CommandConfiguration<Args extends Arguments, TOptions extends Options> {\n\tprivate descriptionValue: string = ''\n\n\tprivate arguments: Argument[] = []\n\tprivate options: Option[] = []\n\n\tpublic description(description: string) {\n\t\tthis.descriptionValue = description\n\t}\n\n\tpublic argument(name: Extract<keyof Args, string>): ArgumentConfiguration {\n\t\tconst options = { name, optional: false, variadic: false }\n\t\tthis.arguments.push(options)\n\t\treturn new ArgumentConfiguration(options)\n\t}\n\n\tpublic option(name: Extract<keyof TOptions, string>): OptionConfiguration {\n\t\tconst option: Option = { name, required: false, mode: OptionMode.VALUE_NONE, deprecated: false }\n\t\tthis.options.push(option)\n\t\treturn new OptionConfiguration(option)\n\t}\n\n\tpublic validate() {\n\t\tlet hasVariadic = false\n\t\tlet hasOptional = false\n\t\tfor (let argument of this.arguments) {\n\t\t\tif (hasOptional && !argument.optional) {\n\t\t\t\tthrow new InvalidConfigurationError(`Required argument ${argument.name} cannot follow an optional `)\n\t\t\t}\n\t\t\tif (hasVariadic) {\n\t\t\t\tthrow new InvalidConfigurationError(`A variadic argument must be the last`)\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic getDescription(): string {\n\t\treturn this.descriptionValue\n\t}\n\n\tpublic createParser(): InputParser {\n\t\treturn new InputParser(this.arguments, this.options)\n\t}\n\n\tpublic getUsage(args: { format?: UsageFormat; indent?: string } = {}): string {\n\t\treturn UsageFormatter.format(this.arguments, this.options, args)\n\t}\n}\n\nexport class InvalidConfigurationError extends Error {}\n"],"names":["ArgumentConfiguration","OptionMode","OptionConfiguration","InputParser","UsageFormatter"],"mappings":";;;;;;;;;AAMO,MAAM,qBAAuE;AAAA,EAA7E,cAAA;AACN,kBAAA,MAAQ,oBAA2B,EAAA;AAEnC,kBAAA,MAAQ,aAAwB,EAAC;AACjC,kBAAA,MAAQ,WAAoB,EAAC;AAAA,EAAA;AAAA,EAEtB,YAAY,aAAqB;AACvC,SAAK,mBAAmB;AAAA,EACzB;AAAA,EAEO,SAAS,MAA0D;AACzE,UAAM,UAAU,EAAE,MAAM,UAAU,OAAO,UAAU,MAAA;AACnD,SAAK,UAAU,KAAK,OAAO;AAC3B,WAAO,IAAIA,SAAAA,sBAAsB,OAAO;AAAA,EACzC;AAAA,EAEO,OAAO,MAA4D;AACzE,UAAM,SAAiB,EAAE,MAAM,UAAU,OAAO,MAAMC,kBAAW,YAAY,YAAY,MAAA;AACzF,SAAK,QAAQ,KAAK,MAAM;AACxB,WAAO,IAAIC,OAAAA,oBAAoB,MAAM;AAAA,EACtC;AAAA,EAEO,WAAW;AAGjB,aAAS,YAAY,KAAK,WAAW;AAAA,IAOrC;AAAA,EACD;AAAA,EAEO,iBAAyB;AAC/B,WAAO,KAAK;AAAA,EACb;AAAA,EAEO,eAA4B;AAClC,WAAO,IAAIC,YAAAA,YAAY,KAAK,WAAW,KAAK,OAAO;AAAA,EACpD;AAAA,EAEO,SAAS,OAAkD,IAAY;AAC7E,WAAOC,eAAAA,eAAe,OAAO,KAAK,WAAW,KAAK,SAAS,IAAI;AAAA,EAChE;AACD;;"}
1
+ {"version":3,"file":"CommandConfiguration.cjs","sources":["../../../../../packages/cli-common/src/application/CommandConfiguration.ts"],"sourcesContent":["import { Argument, ArgumentConfiguration } from './Argument.js'\nimport { mergeOptions, Option, OptionConfiguration, OptionMode } from './Option.js'\nimport { InputParser } from './InputParser.js'\nimport { UsageFormat, UsageFormatter } from './UsageFormatter.js'\nimport { Arguments, Options } from './Input.js'\n\nexport class CommandConfiguration<Args extends Arguments, TOptions extends Options> {\n\tprivate descriptionValue: string = ''\n\n\tprivate arguments: Argument[] = []\n\tprivate options: Option[] = []\n\n\tpublic description(description: string) {\n\t\tthis.descriptionValue = description\n\t}\n\n\tpublic argument(name: Extract<keyof Args, string>): ArgumentConfiguration {\n\t\tconst options = { name, optional: false, variadic: false }\n\t\tthis.arguments.push(options)\n\t\treturn new ArgumentConfiguration(options)\n\t}\n\n\tpublic option(name: Extract<keyof TOptions, string>): OptionConfiguration {\n\t\tconst option: Option = { name, required: false, mode: OptionMode.VALUE_NONE, deprecated: false }\n\t\tthis.options.push(option)\n\t\treturn new OptionConfiguration(option)\n\t}\n\n\tpublic validate(reservedOptions: readonly Option[] = []) {\n\t\tlet hasVariadic = false\n\t\tlet hasOptional = false\n\t\tfor (let argument of this.arguments) {\n\t\t\tif (hasOptional && !argument.optional) {\n\t\t\t\tthrow new InvalidConfigurationError(`Required argument ${argument.name} cannot follow an optional `)\n\t\t\t}\n\t\t\tif (hasVariadic) {\n\t\t\t\tthrow new InvalidConfigurationError(`A variadic argument must be the last`)\n\t\t\t}\n\t\t}\n\t\tfor (const option of this.options) {\n\t\t\tconst collision = reservedOptions.find(reserved =>\n\t\t\t\treserved.name === option.name\n\t\t\t\t|| (option.shortcut !== undefined && reserved.shortcut === option.shortcut)\n\t\t\t)\n\t\t\tif (collision !== undefined) {\n\t\t\t\tthrow new InvalidConfigurationError(`Option --${option.name} conflicts with reserved global option --${collision.name}`)\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic getDescription(): string {\n\t\treturn this.descriptionValue\n\t}\n\n\tpublic getArguments(): readonly Argument[] {\n\t\treturn [...this.arguments]\n\t}\n\n\tpublic getOptions(): readonly Option[] {\n\t\treturn [...this.options]\n\t}\n\n\t/** Appends shared options for a single parser pass after configuration validation. */\n\tpublic createParser(extraOptions: readonly Option[] = []): InputParser {\n\t\treturn new InputParser(this.arguments, mergeOptions(this.options, extraOptions))\n\t}\n\n\tpublic getUsage(args: { format?: UsageFormat; indent?: string } = {}): string {\n\t\treturn UsageFormatter.format(this.arguments, this.options, args)\n\t}\n}\n\nexport class InvalidConfigurationError extends Error {}\n"],"names":["ArgumentConfiguration","OptionMode","OptionConfiguration","InputParser","mergeOptions","UsageFormatter"],"mappings":";;;;;;;;;AAMO,MAAM,qBAAuE;AAAA,EAA7E,cAAA;AACN,kBAAA,MAAQ,oBAA2B,EAAA;AAEnC,kBAAA,MAAQ,aAAwB,EAAC;AACjC,kBAAA,MAAQ,WAAoB,EAAC;AAAA,EAAA;AAAA,EAEtB,YAAY,aAAqB;AACvC,SAAK,mBAAmB;AAAA,EACzB;AAAA,EAEO,SAAS,MAA0D;AACzE,UAAM,UAAU,EAAE,MAAM,UAAU,OAAO,UAAU,MAAA;AACnD,SAAK,UAAU,KAAK,OAAO;AAC3B,WAAO,IAAIA,SAAAA,sBAAsB,OAAO;AAAA,EACzC;AAAA,EAEO,OAAO,MAA4D;AACzE,UAAM,SAAiB,EAAE,MAAM,UAAU,OAAO,MAAMC,kBAAW,YAAY,YAAY,MAAA;AACzF,SAAK,QAAQ,KAAK,MAAM;AACxB,WAAO,IAAIC,OAAAA,oBAAoB,MAAM;AAAA,EACtC;AAAA,EAEO,SAAS,kBAAqC,IAAI;AAGxD,aAAS,YAAY,KAAK,WAAW;AAAA,IAOrC;AACA,eAAW,UAAU,KAAK,SAAS;AAClC,YAAM,YAAY,gBAAgB;AAAA,QAAK,CAAA,aACtC,SAAS,SAAS,OAAO,QACrB,OAAO,aAAa,UAAa,SAAS,aAAa,OAAO;AAAA,MAAA;AAEnE,UAAI,cAAc,QAAW;AAC5B,cAAM,IAAI,0BAA0B,YAAY,OAAO,IAAI,4CAA4C,UAAU,IAAI,EAAE;AAAA,MACxH;AAAA,IACD;AAAA,EACD;AAAA,EAEO,iBAAyB;AAC/B,WAAO,KAAK;AAAA,EACb;AAAA,EAEO,eAAoC;AAC1C,WAAO,CAAC,GAAG,KAAK,SAAS;AAAA,EAC1B;AAAA,EAEO,aAAgC;AACtC,WAAO,CAAC,GAAG,KAAK,OAAO;AAAA,EACxB;AAAA;AAAA,EAGO,aAAa,eAAkC,IAAiB;AACtE,WAAO,IAAIC,YAAAA,YAAY,KAAK,WAAWC,OAAAA,aAAa,KAAK,SAAS,YAAY,CAAC;AAAA,EAChF;AAAA,EAEO,SAAS,OAAkD,IAAY;AAC7E,WAAOC,eAAAA,eAAe,OAAO,KAAK,WAAW,KAAK,SAAS,IAAI;AAAA,EAChE;AACD;AAEO,MAAM,kCAAkC,MAAM;AAAC;;;"}
@@ -1,5 +1,5 @@
1
1
  import { ArgumentConfiguration } from "./Argument.js";
2
- import { OptionMode, OptionConfiguration } from "./Option.js";
2
+ import { OptionMode, OptionConfiguration, mergeOptions } from "./Option.js";
3
3
  import { InputParser } from "./InputParser.js";
4
4
  import { UsageFormatter } from "./UsageFormatter.js";
5
5
  var __defProp = Object.defineProperty;
@@ -24,21 +24,39 @@ class CommandConfiguration {
24
24
  this.options.push(option);
25
25
  return new OptionConfiguration(option);
26
26
  }
27
- validate() {
27
+ validate(reservedOptions = []) {
28
28
  for (let argument of this.arguments) {
29
29
  }
30
+ for (const option of this.options) {
31
+ const collision = reservedOptions.find(
32
+ (reserved) => reserved.name === option.name || option.shortcut !== void 0 && reserved.shortcut === option.shortcut
33
+ );
34
+ if (collision !== void 0) {
35
+ throw new InvalidConfigurationError(`Option --${option.name} conflicts with reserved global option --${collision.name}`);
36
+ }
37
+ }
30
38
  }
31
39
  getDescription() {
32
40
  return this.descriptionValue;
33
41
  }
34
- createParser() {
35
- return new InputParser(this.arguments, this.options);
42
+ getArguments() {
43
+ return [...this.arguments];
44
+ }
45
+ getOptions() {
46
+ return [...this.options];
47
+ }
48
+ /** Appends shared options for a single parser pass after configuration validation. */
49
+ createParser(extraOptions = []) {
50
+ return new InputParser(this.arguments, mergeOptions(this.options, extraOptions));
36
51
  }
37
52
  getUsage(args = {}) {
38
53
  return UsageFormatter.format(this.arguments, this.options, args);
39
54
  }
40
55
  }
56
+ class InvalidConfigurationError extends Error {
57
+ }
41
58
  export {
42
- CommandConfiguration
59
+ CommandConfiguration,
60
+ InvalidConfigurationError
43
61
  };
44
62
  //# sourceMappingURL=CommandConfiguration.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"CommandConfiguration.js","sources":["../../../../../packages/cli-common/src/application/CommandConfiguration.ts"],"sourcesContent":["import { Argument, ArgumentConfiguration } from './Argument'\nimport { Option, OptionConfiguration, OptionMode } from './Option'\nimport { InputParser } from './InputParser'\nimport { UsageFormat, UsageFormatter } from './UsageFormatter'\nimport { Arguments, Options } from './Input'\n\nexport class CommandConfiguration<Args extends Arguments, TOptions extends Options> {\n\tprivate descriptionValue: string = ''\n\n\tprivate arguments: Argument[] = []\n\tprivate options: Option[] = []\n\n\tpublic description(description: string) {\n\t\tthis.descriptionValue = description\n\t}\n\n\tpublic argument(name: Extract<keyof Args, string>): ArgumentConfiguration {\n\t\tconst options = { name, optional: false, variadic: false }\n\t\tthis.arguments.push(options)\n\t\treturn new ArgumentConfiguration(options)\n\t}\n\n\tpublic option(name: Extract<keyof TOptions, string>): OptionConfiguration {\n\t\tconst option: Option = { name, required: false, mode: OptionMode.VALUE_NONE, deprecated: false }\n\t\tthis.options.push(option)\n\t\treturn new OptionConfiguration(option)\n\t}\n\n\tpublic validate() {\n\t\tlet hasVariadic = false\n\t\tlet hasOptional = false\n\t\tfor (let argument of this.arguments) {\n\t\t\tif (hasOptional && !argument.optional) {\n\t\t\t\tthrow new InvalidConfigurationError(`Required argument ${argument.name} cannot follow an optional `)\n\t\t\t}\n\t\t\tif (hasVariadic) {\n\t\t\t\tthrow new InvalidConfigurationError(`A variadic argument must be the last`)\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic getDescription(): string {\n\t\treturn this.descriptionValue\n\t}\n\n\tpublic createParser(): InputParser {\n\t\treturn new InputParser(this.arguments, this.options)\n\t}\n\n\tpublic getUsage(args: { format?: UsageFormat; indent?: string } = {}): string {\n\t\treturn UsageFormatter.format(this.arguments, this.options, args)\n\t}\n}\n\nexport class InvalidConfigurationError extends Error {}\n"],"names":[],"mappings":";;;;;;;AAMO,MAAM,qBAAuE;AAAA,EAA7E,cAAA;AACN,kBAAA,MAAQ,oBAA2B,EAAA;AAEnC,kBAAA,MAAQ,aAAwB,EAAC;AACjC,kBAAA,MAAQ,WAAoB,EAAC;AAAA,EAAA;AAAA,EAEtB,YAAY,aAAqB;AACvC,SAAK,mBAAmB;AAAA,EACzB;AAAA,EAEO,SAAS,MAA0D;AACzE,UAAM,UAAU,EAAE,MAAM,UAAU,OAAO,UAAU,MAAA;AACnD,SAAK,UAAU,KAAK,OAAO;AAC3B,WAAO,IAAI,sBAAsB,OAAO;AAAA,EACzC;AAAA,EAEO,OAAO,MAA4D;AACzE,UAAM,SAAiB,EAAE,MAAM,UAAU,OAAO,MAAM,WAAW,YAAY,YAAY,MAAA;AACzF,SAAK,QAAQ,KAAK,MAAM;AACxB,WAAO,IAAI,oBAAoB,MAAM;AAAA,EACtC;AAAA,EAEO,WAAW;AAGjB,aAAS,YAAY,KAAK,WAAW;AAAA,IAOrC;AAAA,EACD;AAAA,EAEO,iBAAyB;AAC/B,WAAO,KAAK;AAAA,EACb;AAAA,EAEO,eAA4B;AAClC,WAAO,IAAI,YAAY,KAAK,WAAW,KAAK,OAAO;AAAA,EACpD;AAAA,EAEO,SAAS,OAAkD,IAAY;AAC7E,WAAO,eAAe,OAAO,KAAK,WAAW,KAAK,SAAS,IAAI;AAAA,EAChE;AACD;"}
1
+ {"version":3,"file":"CommandConfiguration.js","sources":["../../../../../packages/cli-common/src/application/CommandConfiguration.ts"],"sourcesContent":["import { Argument, ArgumentConfiguration } from './Argument.js'\nimport { mergeOptions, Option, OptionConfiguration, OptionMode } from './Option.js'\nimport { InputParser } from './InputParser.js'\nimport { UsageFormat, UsageFormatter } from './UsageFormatter.js'\nimport { Arguments, Options } from './Input.js'\n\nexport class CommandConfiguration<Args extends Arguments, TOptions extends Options> {\n\tprivate descriptionValue: string = ''\n\n\tprivate arguments: Argument[] = []\n\tprivate options: Option[] = []\n\n\tpublic description(description: string) {\n\t\tthis.descriptionValue = description\n\t}\n\n\tpublic argument(name: Extract<keyof Args, string>): ArgumentConfiguration {\n\t\tconst options = { name, optional: false, variadic: false }\n\t\tthis.arguments.push(options)\n\t\treturn new ArgumentConfiguration(options)\n\t}\n\n\tpublic option(name: Extract<keyof TOptions, string>): OptionConfiguration {\n\t\tconst option: Option = { name, required: false, mode: OptionMode.VALUE_NONE, deprecated: false }\n\t\tthis.options.push(option)\n\t\treturn new OptionConfiguration(option)\n\t}\n\n\tpublic validate(reservedOptions: readonly Option[] = []) {\n\t\tlet hasVariadic = false\n\t\tlet hasOptional = false\n\t\tfor (let argument of this.arguments) {\n\t\t\tif (hasOptional && !argument.optional) {\n\t\t\t\tthrow new InvalidConfigurationError(`Required argument ${argument.name} cannot follow an optional `)\n\t\t\t}\n\t\t\tif (hasVariadic) {\n\t\t\t\tthrow new InvalidConfigurationError(`A variadic argument must be the last`)\n\t\t\t}\n\t\t}\n\t\tfor (const option of this.options) {\n\t\t\tconst collision = reservedOptions.find(reserved =>\n\t\t\t\treserved.name === option.name\n\t\t\t\t|| (option.shortcut !== undefined && reserved.shortcut === option.shortcut)\n\t\t\t)\n\t\t\tif (collision !== undefined) {\n\t\t\t\tthrow new InvalidConfigurationError(`Option --${option.name} conflicts with reserved global option --${collision.name}`)\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic getDescription(): string {\n\t\treturn this.descriptionValue\n\t}\n\n\tpublic getArguments(): readonly Argument[] {\n\t\treturn [...this.arguments]\n\t}\n\n\tpublic getOptions(): readonly Option[] {\n\t\treturn [...this.options]\n\t}\n\n\t/** Appends shared options for a single parser pass after configuration validation. */\n\tpublic createParser(extraOptions: readonly Option[] = []): InputParser {\n\t\treturn new InputParser(this.arguments, mergeOptions(this.options, extraOptions))\n\t}\n\n\tpublic getUsage(args: { format?: UsageFormat; indent?: string } = {}): string {\n\t\treturn UsageFormatter.format(this.arguments, this.options, args)\n\t}\n}\n\nexport class InvalidConfigurationError extends Error {}\n"],"names":[],"mappings":";;;;;;;AAMO,MAAM,qBAAuE;AAAA,EAA7E,cAAA;AACN,kBAAA,MAAQ,oBAA2B,EAAA;AAEnC,kBAAA,MAAQ,aAAwB,EAAC;AACjC,kBAAA,MAAQ,WAAoB,EAAC;AAAA,EAAA;AAAA,EAEtB,YAAY,aAAqB;AACvC,SAAK,mBAAmB;AAAA,EACzB;AAAA,EAEO,SAAS,MAA0D;AACzE,UAAM,UAAU,EAAE,MAAM,UAAU,OAAO,UAAU,MAAA;AACnD,SAAK,UAAU,KAAK,OAAO;AAC3B,WAAO,IAAI,sBAAsB,OAAO;AAAA,EACzC;AAAA,EAEO,OAAO,MAA4D;AACzE,UAAM,SAAiB,EAAE,MAAM,UAAU,OAAO,MAAM,WAAW,YAAY,YAAY,MAAA;AACzF,SAAK,QAAQ,KAAK,MAAM;AACxB,WAAO,IAAI,oBAAoB,MAAM;AAAA,EACtC;AAAA,EAEO,SAAS,kBAAqC,IAAI;AAGxD,aAAS,YAAY,KAAK,WAAW;AAAA,IAOrC;AACA,eAAW,UAAU,KAAK,SAAS;AAClC,YAAM,YAAY,gBAAgB;AAAA,QAAK,CAAA,aACtC,SAAS,SAAS,OAAO,QACrB,OAAO,aAAa,UAAa,SAAS,aAAa,OAAO;AAAA,MAAA;AAEnE,UAAI,cAAc,QAAW;AAC5B,cAAM,IAAI,0BAA0B,YAAY,OAAO,IAAI,4CAA4C,UAAU,IAAI,EAAE;AAAA,MACxH;AAAA,IACD;AAAA,EACD;AAAA,EAEO,iBAAyB;AAC/B,WAAO,KAAK;AAAA,EACb;AAAA,EAEO,eAAoC;AAC1C,WAAO,CAAC,GAAG,KAAK,SAAS;AAAA,EAC1B;AAAA,EAEO,aAAgC;AACtC,WAAO,CAAC,GAAG,KAAK,OAAO;AAAA,EACxB;AAAA;AAAA,EAGO,aAAa,eAAkC,IAAiB;AACtE,WAAO,IAAI,YAAY,KAAK,WAAW,aAAa,KAAK,SAAS,YAAY,CAAC;AAAA,EAChF;AAAA,EAEO,SAAS,OAAkD,IAAY;AAC7E,WAAO,eAAe,OAAO,KAAK,WAAW,KAAK,SAAS,IAAI;AAAA,EAChE;AACD;AAEO,MAAM,kCAAkC,MAAM;AAAC;"}
@@ -1,37 +1,155 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const chalk = require("chalk");
3
+ const CliError = require("./CliError.cjs");
4
+ const CommandConfiguration = require("./CommandConfiguration.cjs");
5
+ var __defProp = Object.defineProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
+ const splitTokens = (name) => name.split(/[\s:]+/).filter((it) => it !== "");
4
9
  class CommandManager {
5
10
  constructor(commands) {
6
11
  this.commands = commands;
12
+ __publicField(this, "entries");
13
+ __publicField(this, "lookup", /* @__PURE__ */ new Map());
14
+ __publicField(this, "groups", /* @__PURE__ */ new Map());
15
+ __publicField(this, "maxTokens");
16
+ this.entries = createCatalog(commands);
17
+ for (const entry of this.entries) {
18
+ for (const name of [entry.name, ...entry.aliases]) {
19
+ const tokens = splitTokens(name);
20
+ const normalizedName = tokens.join(" ");
21
+ const existing = this.lookup.get(normalizedName);
22
+ if (existing !== void 0 && existing.create !== entry.create) {
23
+ throw new CommandConfiguration.InvalidConfigurationError(
24
+ `Command names "${existing.name}" and "${name}" both normalize to "${normalizedName}" but use different factories.`
25
+ );
26
+ }
27
+ this.lookup.set(normalizedName, entry);
28
+ for (let i = 1; i < tokens.length; i++) {
29
+ const prefix = tokens.slice(0, i).join(" ");
30
+ const group = this.groups.get(prefix) ?? [];
31
+ if (!group.includes(entry)) {
32
+ group.push(entry);
33
+ }
34
+ this.groups.set(prefix, group);
35
+ }
36
+ }
37
+ }
38
+ this.maxTokens = Math.max(1, ...[...this.lookup.keys()].map((it) => splitTokens(it).length));
39
+ this.validateNoPrefixCollision();
7
40
  }
8
- createCommand(name) {
9
- const fullName = this.findCommand(name);
10
- const factory = this.commands[fullName];
11
- const originalCommand = Object.entries(this.commands).find(([, it]) => it === factory);
12
- if (!originalCommand) {
13
- throw new Error("impl error");
41
+ getCommands() {
42
+ return [...this.entries].sort((a, b) => a.name.localeCompare(b.name));
43
+ }
44
+ /**
45
+ * Resolves the longest command name matching the leading argv tokens. `migrations diff`,
46
+ * `migrations:diff` and the `m:d` shorthand all resolve to the same command; the tokens that were
47
+ * not consumed are returned untouched as the command arguments.
48
+ */
49
+ resolve(args) {
50
+ const tokens = [];
51
+ const checkpoints = [];
52
+ for (let i = 0; i < args.length; i++) {
53
+ if (args[i].startsWith("-")) {
54
+ break;
55
+ }
56
+ const parts = splitTokens(args[i]);
57
+ if (parts.length === 0 || tokens.length + parts.length > this.maxTokens) {
58
+ break;
59
+ }
60
+ tokens.push(...parts);
61
+ checkpoints.push({ tokens: [...tokens], argvIndex: i + 1 });
62
+ }
63
+ for (let i = checkpoints.length - 1; i >= 0; i--) {
64
+ const entry = this.lookup.get(checkpoints[i].tokens.join(" "));
65
+ if (entry) {
66
+ return { type: "command", entry, args: args.slice(checkpoints[i].argvIndex) };
67
+ }
68
+ }
69
+ const group = this.groups.get(tokens.join(" "));
70
+ if (group !== void 0 && group.length > 0) {
71
+ const argvIndex = checkpoints[checkpoints.length - 1]?.argvIndex ?? 0;
72
+ return { type: "group", name: tokens.join(" "), entries: group, args: args.slice(argvIndex) };
73
+ }
74
+ for (let i = checkpoints.length - 1; i >= 0; i--) {
75
+ if (i < checkpoints.length - 1 && this.groups.has(checkpoints[i].tokens.join(" "))) {
76
+ continue;
77
+ }
78
+ const matched = this.matchShorthand(checkpoints[i].tokens);
79
+ if (matched.length === 1) {
80
+ return { type: "command", entry: matched[0], args: args.slice(checkpoints[i].argvIndex) };
81
+ }
82
+ if (matched.length > 1) {
83
+ throw new CliError.CliError(
84
+ `Command "${checkpoints[i].tokens.join(" ")}" is ambiguous. Did you mean one of these?
85
+ - ${matched.map((it) => it.name).join("\n - ")}`,
86
+ { code: "COMMAND_AMBIGUOUS", exitCode: CliError.ExitCode.InputError }
87
+ );
88
+ }
14
89
  }
15
- if (originalCommand[0] !== fullName) {
16
- console.warn(chalk.yellow(`Command name "${fullName}" is deprecated, use "${originalCommand[0]}" instead.`));
90
+ for (let i = tokens.length - 1; i >= 1; i--) {
91
+ const known = this.groups.get(tokens.slice(0, i).join(" "));
92
+ if (known !== void 0 && known.length > 0) {
93
+ throw new CliError.CliError(
94
+ `Command "${tokens.join(" ")}" not found. Available commands in "${tokens.slice(0, i).join(" ")}":
95
+ - ${known.map((it) => it.name).sort((a, b) => a.localeCompare(b)).join("\n - ")}`,
96
+ { code: "COMMAND_NOT_FOUND", exitCode: CliError.ExitCode.InputError }
97
+ );
98
+ }
17
99
  }
18
- return [fullName, factory()];
100
+ throw new CliError.CliError(`Command "${args[0] ?? ""}" not found.`, { code: "COMMAND_NOT_FOUND", exitCode: CliError.ExitCode.InputError });
19
101
  }
20
- findCommand(name) {
21
- if (this.commands[name]) {
22
- return name;
102
+ createCommand(name) {
103
+ const resolution = this.resolve([name]);
104
+ if (resolution.type === "group") {
105
+ throw new CliError.CliError(
106
+ `Command "${resolution.name}" is a command group. Available commands:
107
+ - ${resolution.entries.map((it) => it.name).join("\n - ")}`,
108
+ { code: "COMMAND_AMBIGUOUS", exitCode: CliError.ExitCode.InputError }
109
+ );
23
110
  }
24
- const pattern = new RegExp("^" + name.replace(/:/g, "\\w*\\:"));
25
- const matchedCommands = Object.keys(this.commands).filter((it) => it.match(pattern));
26
- if (matchedCommands.length === 1) {
27
- return matchedCommands[0];
111
+ return [resolution.entry.name, resolution.entry.create()];
112
+ }
113
+ /** Prefix matching of the abbreviated form: `m:d` => `migrations diff`, `w:u` => `workspace update api`. */
114
+ matchShorthand(tokens) {
115
+ if (tokens.length === 0) {
116
+ return [];
28
117
  }
29
- if (matchedCommands.length === 0) {
30
- throw `Command ${name} not found.`;
118
+ return this.entries.filter((entry) => entry.tokens.length >= tokens.length && tokens.every((token, index) => entry.tokens[index].startsWith(token)));
119
+ }
120
+ validateNoPrefixCollision() {
121
+ const names = [...this.lookup.keys()];
122
+ for (const name of names) {
123
+ for (const other of names) {
124
+ if (other !== name && other.startsWith(name + " ")) {
125
+ throw new CommandConfiguration.InvalidConfigurationError(
126
+ `Command name "${name}" is a prefix of "${other}". A command name must not be a prefix of another command name, otherwise it could never be resolved.`
127
+ );
128
+ }
129
+ }
31
130
  }
32
- throw `Command ${name} is ambiguous. Did you mean one of these?
33
- - ` + matchedCommands.join("\n - ") + "\n";
34
131
  }
35
132
  }
133
+ const createCatalog = (commands) => {
134
+ const byFactory = /* @__PURE__ */ new Map();
135
+ for (const [name, factory] of Object.entries(commands)) {
136
+ const registered = byFactory.get(factory);
137
+ if (registered) {
138
+ registered.aliases.push(name);
139
+ } else {
140
+ byFactory.set(factory, { name, aliases: [] });
141
+ }
142
+ }
143
+ return [...byFactory].map(([factory, { name, aliases }]) => {
144
+ const tokens = splitTokens(name);
145
+ return {
146
+ name: tokens.join(" "),
147
+ tokens,
148
+ group: tokens.length > 1 ? tokens[0] : void 0,
149
+ aliases,
150
+ create: factory
151
+ };
152
+ });
153
+ };
36
154
  exports.CommandManager = CommandManager;
37
155
  //# sourceMappingURL=CommandManager.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"CommandManager.cjs","sources":["../../../../../packages/cli-common/src/application/CommandManager.ts"],"sourcesContent":["import { Command } from './Command'\nimport chalk from 'chalk'\n\nexport type CommandFactoryList = { [command: string]: () => Command<any, any> }\n\nexport class CommandManager {\n\tconstructor(public readonly commands: CommandFactoryList) {}\n\n\tpublic createCommand(name: string): [string, Command<any, any>] {\n\t\tconst fullName = this.findCommand(name)\n\t\tconst factory = this.commands[fullName]\n\t\tconst originalCommand = Object.entries(this.commands).find(([, it]) => it === factory)\n\t\tif (!originalCommand) {\n\t\t\tthrow new Error('impl error')\n\t\t}\n\t\tif (originalCommand[0] !== fullName) {\n\t\t\tconsole.warn(chalk.yellow(`Command name \"${fullName}\" is deprecated, use \"${originalCommand[0]}\" instead.`))\n\t\t}\n\t\treturn [fullName, factory()]\n\t}\n\n\tprivate findCommand(name: string): string {\n\t\tif (this.commands[name]) {\n\t\t\treturn name\n\t\t}\n\t\tconst pattern = new RegExp('^' + name.replace(/:/g, '\\\\w*\\\\:'))\n\t\tconst matchedCommands = Object.keys(this.commands).filter(it => it.match(pattern))\n\t\tif (matchedCommands.length === 1) {\n\t\t\treturn matchedCommands[0]\n\t\t}\n\t\tif (matchedCommands.length === 0) {\n\t\t\tthrow `Command ${name} not found.`\n\t\t}\n\t\tthrow `Command ${name} is ambiguous. Did you mean one of these?\\n - ` + matchedCommands.join('\\n - ') + '\\n'\n\t}\n}\n"],"names":[],"mappings":";;;AAKO,MAAM,eAAe;AAAA,EAC3B,YAA4B,UAA8B;AAA9B,SAAA,WAAA;AAAA,EAA+B;AAAA,EAEpD,cAAc,MAA2C;AAC/D,UAAM,WAAW,KAAK,YAAY,IAAI;AACtC,UAAM,UAAU,KAAK,SAAS,QAAQ;AACtC,UAAM,kBAAkB,OAAO,QAAQ,KAAK,QAAQ,EAAE,KAAK,CAAC,CAAA,EAAG,EAAE,MAAM,OAAO,OAAO;AACrF,QAAI,CAAC,iBAAiB;AACrB,YAAM,IAAI,MAAM,YAAY;AAAA,IAC7B;AACA,QAAI,gBAAgB,CAAC,MAAM,UAAU;AACpC,cAAQ,KAAK,MAAM,OAAO,iBAAiB,QAAQ,yBAAyB,gBAAgB,CAAC,CAAC,YAAY,CAAC;AAAA,IAC5G;AACA,WAAO,CAAC,UAAU,SAAS;AAAA,EAC5B;AAAA,EAEQ,YAAY,MAAsB;AACzC,QAAI,KAAK,SAAS,IAAI,GAAG;AACxB,aAAO;AAAA,IACR;AACA,UAAM,UAAU,IAAI,OAAO,MAAM,KAAK,QAAQ,MAAM,SAAS,CAAC;AAC9D,UAAM,kBAAkB,OAAO,KAAK,KAAK,QAAQ,EAAE,OAAO,CAAA,OAAM,GAAG,MAAM,OAAO,CAAC;AACjF,QAAI,gBAAgB,WAAW,GAAG;AACjC,aAAO,gBAAgB,CAAC;AAAA,IACzB;AACA,QAAI,gBAAgB,WAAW,GAAG;AACjC,YAAM,WAAW,IAAI;AAAA,IACtB;AACA,UAAM,WAAW,IAAI;AAAA,OAAmD,gBAAgB,KAAK,OAAO,IAAI;AAAA,EACzG;AACD;;"}
1
+ {"version":3,"file":"CommandManager.cjs","sources":["../../../../../packages/cli-common/src/application/CommandManager.ts"],"sourcesContent":["import { Command } from './Command.js'\nimport { CliError, ExitCode } from './CliError.js'\nimport { InvalidConfigurationError } from './CommandConfiguration.js'\n\nexport type CommandFactory = () => Command<any, any>\n\n/**\n * Keys sharing the very same factory are aliases of one command — the first one registered is the\n * canonical name. Both `migrations diff` and `migrations:diff` resolve to the same command.\n */\nexport type CommandFactoryList = { [command: string]: CommandFactory }\n\nexport interface CommandCatalogEntry {\n\t/** canonical name, space separated */\n\treadonly name: string\n\treadonly tokens: readonly string[]\n\treadonly group: string | undefined\n\treadonly aliases: readonly string[]\n\treadonly create: CommandFactory\n}\n\nexport type CommandResolution =\n\t| { readonly type: 'command'; readonly entry: CommandCatalogEntry; readonly args: string[] }\n\t| { readonly type: 'group'; readonly name: string; readonly entries: readonly CommandCatalogEntry[]; readonly args: string[] }\n\nconst splitTokens = (name: string): string[] => name.split(/[\\s:]+/).filter(it => it !== '')\n\nexport class CommandManager {\n\tprivate readonly entries: CommandCatalogEntry[]\n\t/** normalized name (canonical or alias) => entry */\n\tprivate readonly lookup = new Map<string, CommandCatalogEntry>()\n\t/** every strict token prefix of a command name => commands in that group */\n\tprivate readonly groups = new Map<string, CommandCatalogEntry[]>()\n\tprivate readonly maxTokens: number\n\n\tconstructor(public readonly commands: CommandFactoryList) {\n\t\tthis.entries = createCatalog(commands)\n\t\tfor (const entry of this.entries) {\n\t\t\tfor (const name of [entry.name, ...entry.aliases]) {\n\t\t\t\tconst tokens = splitTokens(name)\n\t\t\t\tconst normalizedName = tokens.join(' ')\n\t\t\t\tconst existing = this.lookup.get(normalizedName)\n\t\t\t\tif (existing !== undefined && existing.create !== entry.create) {\n\t\t\t\t\tthrow new InvalidConfigurationError(\n\t\t\t\t\t\t`Command names \"${existing.name}\" and \"${name}\" both normalize to \"${normalizedName}\" but use different factories.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tthis.lookup.set(normalizedName, entry)\n\t\t\t\tfor (let i = 1; i < tokens.length; i++) {\n\t\t\t\t\tconst prefix = tokens.slice(0, i).join(' ')\n\t\t\t\t\tconst group = this.groups.get(prefix) ?? []\n\t\t\t\t\tif (!group.includes(entry)) {\n\t\t\t\t\t\tgroup.push(entry)\n\t\t\t\t\t}\n\t\t\t\t\tthis.groups.set(prefix, group)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tthis.maxTokens = Math.max(1, ...[...this.lookup.keys()].map(it => splitTokens(it).length))\n\t\tthis.validateNoPrefixCollision()\n\t}\n\n\tpublic getCommands(): readonly CommandCatalogEntry[] {\n\t\treturn [...this.entries].sort((a, b) => a.name.localeCompare(b.name))\n\t}\n\n\t/**\n\t * Resolves the longest command name matching the leading argv tokens. `migrations diff`,\n\t * `migrations:diff` and the `m:d` shorthand all resolve to the same command; the tokens that were\n\t * not consumed are returned untouched as the command arguments.\n\t */\n\tpublic resolve(args: string[]): CommandResolution {\n\t\tconst tokens: string[] = []\n\t\tconst checkpoints: { tokens: string[]; argvIndex: number }[] = []\n\t\tfor (let i = 0; i < args.length; i++) {\n\t\t\tif (args[i].startsWith('-')) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tconst parts = splitTokens(args[i])\n\t\t\tif (parts.length === 0 || tokens.length + parts.length > this.maxTokens) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\ttokens.push(...parts)\n\t\t\tcheckpoints.push({ tokens: [...tokens], argvIndex: i + 1 })\n\t\t}\n\n\t\tfor (let i = checkpoints.length - 1; i >= 0; i--) {\n\t\t\tconst entry = this.lookup.get(checkpoints[i].tokens.join(' '))\n\t\t\tif (entry) {\n\t\t\t\treturn { type: 'command', entry, args: args.slice(checkpoints[i].argvIndex) }\n\t\t\t}\n\t\t}\n\n\t\tconst group = this.groups.get(tokens.join(' '))\n\t\tif (group !== undefined && group.length > 0) {\n\t\t\tconst argvIndex = checkpoints[checkpoints.length - 1]?.argvIndex ?? 0\n\t\t\treturn { type: 'group', name: tokens.join(' '), entries: group, args: args.slice(argvIndex) }\n\t\t}\n\n\t\tfor (let i = checkpoints.length - 1; i >= 0; i--) {\n\t\t\t// a group named in full must be followed by a verb — never let it swallow the remaining tokens as arguments\n\t\t\tif (i < checkpoints.length - 1 && this.groups.has(checkpoints[i].tokens.join(' '))) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst matched = this.matchShorthand(checkpoints[i].tokens)\n\t\t\tif (matched.length === 1) {\n\t\t\t\treturn { type: 'command', entry: matched[0], args: args.slice(checkpoints[i].argvIndex) }\n\t\t\t}\n\t\t\tif (matched.length > 1) {\n\t\t\t\tthrow new CliError(\n\t\t\t\t\t`Command \"${checkpoints[i].tokens.join(' ')}\" is ambiguous. Did you mean one of these?\\n - ${matched.map(it => it.name).join('\\n - ')}`,\n\t\t\t\t\t{ code: 'COMMAND_AMBIGUOUS', exitCode: ExitCode.InputError },\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\t// the leading tokens name a known group, but the verb does not exist in it\n\t\tfor (let i = tokens.length - 1; i >= 1; i--) {\n\t\t\tconst known = this.groups.get(tokens.slice(0, i).join(' '))\n\t\t\tif (known !== undefined && known.length > 0) {\n\t\t\t\tthrow new CliError(\n\t\t\t\t\t`Command \"${tokens.join(' ')}\" not found. Available commands in \"${tokens.slice(0, i).join(' ')}\":\\n - ${\n\t\t\t\t\t\tknown.map(it => it.name).sort((a, b) => a.localeCompare(b)).join('\\n - ')\n\t\t\t\t\t}`,\n\t\t\t\t\t{ code: 'COMMAND_NOT_FOUND', exitCode: ExitCode.InputError },\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\tthrow new CliError(`Command \"${args[0] ?? ''}\" not found.`, { code: 'COMMAND_NOT_FOUND', exitCode: ExitCode.InputError })\n\t}\n\n\tpublic createCommand(name: string): [string, Command<any, any>] {\n\t\tconst resolution = this.resolve([name])\n\t\tif (resolution.type === 'group') {\n\t\t\tthrow new CliError(\n\t\t\t\t`Command \"${resolution.name}\" is a command group. Available commands:\\n - ${resolution.entries.map(it => it.name).join('\\n - ')}`,\n\t\t\t\t{ code: 'COMMAND_AMBIGUOUS', exitCode: ExitCode.InputError },\n\t\t\t)\n\t\t}\n\t\treturn [resolution.entry.name, resolution.entry.create()]\n\t}\n\n\t/** Prefix matching of the abbreviated form: `m:d` => `migrations diff`, `w:u` => `workspace update api`. */\n\tprivate matchShorthand(tokens: string[]): CommandCatalogEntry[] {\n\t\tif (tokens.length === 0) {\n\t\t\treturn []\n\t\t}\n\t\treturn this.entries.filter(entry => entry.tokens.length >= tokens.length && tokens.every((token, index) => entry.tokens[index].startsWith(token)))\n\t}\n\n\tprivate validateNoPrefixCollision(): void {\n\t\tconst names = [...this.lookup.keys()]\n\t\tfor (const name of names) {\n\t\t\tfor (const other of names) {\n\t\t\t\tif (other !== name && other.startsWith(name + ' ')) {\n\t\t\t\t\tthrow new InvalidConfigurationError(\n\t\t\t\t\t\t`Command name \"${name}\" is a prefix of \"${other}\". A command name must not be a prefix of another command name, otherwise it could never be resolved.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nconst createCatalog = (commands: CommandFactoryList): CommandCatalogEntry[] => {\n\tconst byFactory = new Map<CommandFactory, { name: string; aliases: string[] }>()\n\tfor (const [name, factory] of Object.entries(commands)) {\n\t\tconst registered = byFactory.get(factory)\n\t\tif (registered) {\n\t\t\tregistered.aliases.push(name)\n\t\t} else {\n\t\t\tbyFactory.set(factory, { name, aliases: [] })\n\t\t}\n\t}\n\treturn [...byFactory].map(([factory, { name, aliases }]) => {\n\t\tconst tokens = splitTokens(name)\n\t\treturn {\n\t\t\tname: tokens.join(' '),\n\t\t\ttokens,\n\t\t\tgroup: tokens.length > 1 ? tokens[0] : undefined,\n\t\t\taliases,\n\t\t\tcreate: factory,\n\t\t}\n\t})\n}\n"],"names":["InvalidConfigurationError","CliError","ExitCode"],"mappings":";;;;;;;AAyBA,MAAM,cAAc,CAAC,SAA2B,KAAK,MAAM,QAAQ,EAAE,OAAO,CAAA,OAAM,OAAO,EAAE;AAEpF,MAAM,eAAe;AAAA,EAQ3B,YAA4B,UAA8B;AAA9B,SAAA,WAAA;AAP5B,kBAAA,MAAiB,SAAA;AAEjB,kBAAA,MAAiB,8BAAa,IAAA,CAAiC;AAE/D,kBAAA,MAAiB,8BAAa,IAAA,CAAmC;AACjE,kBAAA,MAAiB,WAAA;AAGhB,SAAK,UAAU,cAAc,QAAQ;AACrC,eAAW,SAAS,KAAK,SAAS;AACjC,iBAAW,QAAQ,CAAC,MAAM,MAAM,GAAG,MAAM,OAAO,GAAG;AAClD,cAAM,SAAS,YAAY,IAAI;AAC/B,cAAM,iBAAiB,OAAO,KAAK,GAAG;AACtC,cAAM,WAAW,KAAK,OAAO,IAAI,cAAc;AAC/C,YAAI,aAAa,UAAa,SAAS,WAAW,MAAM,QAAQ;AAC/D,gBAAM,IAAIA,qBAAAA;AAAAA,YACT,kBAAkB,SAAS,IAAI,UAAU,IAAI,wBAAwB,cAAc;AAAA,UAAA;AAAA,QAErF;AACA,aAAK,OAAO,IAAI,gBAAgB,KAAK;AACrC,iBAAS,IAAI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,YAAI,UAAU,QAAQ,MAAM,WAAW,OAAO,GAAG,GAAG;AACnD,gBAAM,IAAIF,qBAAAA;AAAAA,YACT,iBAAiB,IAAI,qBAAqB,KAAK;AAAA,UAAA;AAAA,QAEjD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;AAEA,MAAM,gBAAgB,CAAC,aAAwD;AAC9E,QAAM,gCAAgB,IAAA;AACtB,aAAW,CAAC,MAAM,OAAO,KAAK,OAAO,QAAQ,QAAQ,GAAG;AACvD,UAAM,aAAa,UAAU,IAAI,OAAO;AACxC,QAAI,YAAY;AACf,iBAAW,QAAQ,KAAK,IAAI;AAAA,IAC7B,OAAO;AACN,gBAAU,IAAI,SAAS,EAAE,MAAM,SAAS,CAAA,GAAI;AAAA,IAC7C;AAAA,EACD;AACA,SAAO,CAAC,GAAG,SAAS,EAAE,IAAI,CAAC,CAAC,SAAS,EAAE,MAAM,QAAA,CAAS,MAAM;AAC3D,UAAM,SAAS,YAAY,IAAI;AAC/B,WAAO;AAAA,MACN,MAAM,OAAO,KAAK,GAAG;AAAA,MACrB;AAAA,MACA,OAAO,OAAO,SAAS,IAAI,OAAO,CAAC,IAAI;AAAA,MACvC;AAAA,MACA,QAAQ;AAAA,IAAA;AAAA,EAEV,CAAC;AACF;;"}
@@ -1,36 +1,154 @@
1
- import chalk from "chalk";
1
+ import { CliError, ExitCode } from "./CliError.js";
2
+ import { InvalidConfigurationError } from "./CommandConfiguration.js";
3
+ var __defProp = Object.defineProperty;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
+ const splitTokens = (name) => name.split(/[\s:]+/).filter((it) => it !== "");
2
7
  class CommandManager {
3
8
  constructor(commands) {
4
9
  this.commands = commands;
10
+ __publicField(this, "entries");
11
+ __publicField(this, "lookup", /* @__PURE__ */ new Map());
12
+ __publicField(this, "groups", /* @__PURE__ */ new Map());
13
+ __publicField(this, "maxTokens");
14
+ this.entries = createCatalog(commands);
15
+ for (const entry of this.entries) {
16
+ for (const name of [entry.name, ...entry.aliases]) {
17
+ const tokens = splitTokens(name);
18
+ const normalizedName = tokens.join(" ");
19
+ const existing = this.lookup.get(normalizedName);
20
+ if (existing !== void 0 && existing.create !== entry.create) {
21
+ throw new InvalidConfigurationError(
22
+ `Command names "${existing.name}" and "${name}" both normalize to "${normalizedName}" but use different factories.`
23
+ );
24
+ }
25
+ this.lookup.set(normalizedName, entry);
26
+ for (let i = 1; i < tokens.length; i++) {
27
+ const prefix = tokens.slice(0, i).join(" ");
28
+ const group = this.groups.get(prefix) ?? [];
29
+ if (!group.includes(entry)) {
30
+ group.push(entry);
31
+ }
32
+ this.groups.set(prefix, group);
33
+ }
34
+ }
35
+ }
36
+ this.maxTokens = Math.max(1, ...[...this.lookup.keys()].map((it) => splitTokens(it).length));
37
+ this.validateNoPrefixCollision();
5
38
  }
6
- createCommand(name) {
7
- const fullName = this.findCommand(name);
8
- const factory = this.commands[fullName];
9
- const originalCommand = Object.entries(this.commands).find(([, it]) => it === factory);
10
- if (!originalCommand) {
11
- throw new Error("impl error");
39
+ getCommands() {
40
+ return [...this.entries].sort((a, b) => a.name.localeCompare(b.name));
41
+ }
42
+ /**
43
+ * Resolves the longest command name matching the leading argv tokens. `migrations diff`,
44
+ * `migrations:diff` and the `m:d` shorthand all resolve to the same command; the tokens that were
45
+ * not consumed are returned untouched as the command arguments.
46
+ */
47
+ resolve(args) {
48
+ const tokens = [];
49
+ const checkpoints = [];
50
+ for (let i = 0; i < args.length; i++) {
51
+ if (args[i].startsWith("-")) {
52
+ break;
53
+ }
54
+ const parts = splitTokens(args[i]);
55
+ if (parts.length === 0 || tokens.length + parts.length > this.maxTokens) {
56
+ break;
57
+ }
58
+ tokens.push(...parts);
59
+ checkpoints.push({ tokens: [...tokens], argvIndex: i + 1 });
60
+ }
61
+ for (let i = checkpoints.length - 1; i >= 0; i--) {
62
+ const entry = this.lookup.get(checkpoints[i].tokens.join(" "));
63
+ if (entry) {
64
+ return { type: "command", entry, args: args.slice(checkpoints[i].argvIndex) };
65
+ }
66
+ }
67
+ const group = this.groups.get(tokens.join(" "));
68
+ if (group !== void 0 && group.length > 0) {
69
+ const argvIndex = checkpoints[checkpoints.length - 1]?.argvIndex ?? 0;
70
+ return { type: "group", name: tokens.join(" "), entries: group, args: args.slice(argvIndex) };
71
+ }
72
+ for (let i = checkpoints.length - 1; i >= 0; i--) {
73
+ if (i < checkpoints.length - 1 && this.groups.has(checkpoints[i].tokens.join(" "))) {
74
+ continue;
75
+ }
76
+ const matched = this.matchShorthand(checkpoints[i].tokens);
77
+ if (matched.length === 1) {
78
+ return { type: "command", entry: matched[0], args: args.slice(checkpoints[i].argvIndex) };
79
+ }
80
+ if (matched.length > 1) {
81
+ throw new CliError(
82
+ `Command "${checkpoints[i].tokens.join(" ")}" is ambiguous. Did you mean one of these?
83
+ - ${matched.map((it) => it.name).join("\n - ")}`,
84
+ { code: "COMMAND_AMBIGUOUS", exitCode: ExitCode.InputError }
85
+ );
86
+ }
12
87
  }
13
- if (originalCommand[0] !== fullName) {
14
- console.warn(chalk.yellow(`Command name "${fullName}" is deprecated, use "${originalCommand[0]}" instead.`));
88
+ for (let i = tokens.length - 1; i >= 1; i--) {
89
+ const known = this.groups.get(tokens.slice(0, i).join(" "));
90
+ if (known !== void 0 && known.length > 0) {
91
+ throw new CliError(
92
+ `Command "${tokens.join(" ")}" not found. Available commands in "${tokens.slice(0, i).join(" ")}":
93
+ - ${known.map((it) => it.name).sort((a, b) => a.localeCompare(b)).join("\n - ")}`,
94
+ { code: "COMMAND_NOT_FOUND", exitCode: ExitCode.InputError }
95
+ );
96
+ }
15
97
  }
16
- return [fullName, factory()];
98
+ throw new CliError(`Command "${args[0] ?? ""}" not found.`, { code: "COMMAND_NOT_FOUND", exitCode: ExitCode.InputError });
17
99
  }
18
- findCommand(name) {
19
- if (this.commands[name]) {
20
- return name;
100
+ createCommand(name) {
101
+ const resolution = this.resolve([name]);
102
+ if (resolution.type === "group") {
103
+ throw new CliError(
104
+ `Command "${resolution.name}" is a command group. Available commands:
105
+ - ${resolution.entries.map((it) => it.name).join("\n - ")}`,
106
+ { code: "COMMAND_AMBIGUOUS", exitCode: ExitCode.InputError }
107
+ );
21
108
  }
22
- const pattern = new RegExp("^" + name.replace(/:/g, "\\w*\\:"));
23
- const matchedCommands = Object.keys(this.commands).filter((it) => it.match(pattern));
24
- if (matchedCommands.length === 1) {
25
- return matchedCommands[0];
109
+ return [resolution.entry.name, resolution.entry.create()];
110
+ }
111
+ /** Prefix matching of the abbreviated form: `m:d` => `migrations diff`, `w:u` => `workspace update api`. */
112
+ matchShorthand(tokens) {
113
+ if (tokens.length === 0) {
114
+ return [];
26
115
  }
27
- if (matchedCommands.length === 0) {
28
- throw `Command ${name} not found.`;
116
+ return this.entries.filter((entry) => entry.tokens.length >= tokens.length && tokens.every((token, index) => entry.tokens[index].startsWith(token)));
117
+ }
118
+ validateNoPrefixCollision() {
119
+ const names = [...this.lookup.keys()];
120
+ for (const name of names) {
121
+ for (const other of names) {
122
+ if (other !== name && other.startsWith(name + " ")) {
123
+ throw new InvalidConfigurationError(
124
+ `Command name "${name}" is a prefix of "${other}". A command name must not be a prefix of another command name, otherwise it could never be resolved.`
125
+ );
126
+ }
127
+ }
29
128
  }
30
- throw `Command ${name} is ambiguous. Did you mean one of these?
31
- - ` + matchedCommands.join("\n - ") + "\n";
32
129
  }
33
130
  }
131
+ const createCatalog = (commands) => {
132
+ const byFactory = /* @__PURE__ */ new Map();
133
+ for (const [name, factory] of Object.entries(commands)) {
134
+ const registered = byFactory.get(factory);
135
+ if (registered) {
136
+ registered.aliases.push(name);
137
+ } else {
138
+ byFactory.set(factory, { name, aliases: [] });
139
+ }
140
+ }
141
+ return [...byFactory].map(([factory, { name, aliases }]) => {
142
+ const tokens = splitTokens(name);
143
+ return {
144
+ name: tokens.join(" "),
145
+ tokens,
146
+ group: tokens.length > 1 ? tokens[0] : void 0,
147
+ aliases,
148
+ create: factory
149
+ };
150
+ });
151
+ };
34
152
  export {
35
153
  CommandManager
36
154
  };
@@ -1 +1 @@
1
- {"version":3,"file":"CommandManager.js","sources":["../../../../../packages/cli-common/src/application/CommandManager.ts"],"sourcesContent":["import { Command } from './Command'\nimport chalk from 'chalk'\n\nexport type CommandFactoryList = { [command: string]: () => Command<any, any> }\n\nexport class CommandManager {\n\tconstructor(public readonly commands: CommandFactoryList) {}\n\n\tpublic createCommand(name: string): [string, Command<any, any>] {\n\t\tconst fullName = this.findCommand(name)\n\t\tconst factory = this.commands[fullName]\n\t\tconst originalCommand = Object.entries(this.commands).find(([, it]) => it === factory)\n\t\tif (!originalCommand) {\n\t\t\tthrow new Error('impl error')\n\t\t}\n\t\tif (originalCommand[0] !== fullName) {\n\t\t\tconsole.warn(chalk.yellow(`Command name \"${fullName}\" is deprecated, use \"${originalCommand[0]}\" instead.`))\n\t\t}\n\t\treturn [fullName, factory()]\n\t}\n\n\tprivate findCommand(name: string): string {\n\t\tif (this.commands[name]) {\n\t\t\treturn name\n\t\t}\n\t\tconst pattern = new RegExp('^' + name.replace(/:/g, '\\\\w*\\\\:'))\n\t\tconst matchedCommands = Object.keys(this.commands).filter(it => it.match(pattern))\n\t\tif (matchedCommands.length === 1) {\n\t\t\treturn matchedCommands[0]\n\t\t}\n\t\tif (matchedCommands.length === 0) {\n\t\t\tthrow `Command ${name} not found.`\n\t\t}\n\t\tthrow `Command ${name} is ambiguous. Did you mean one of these?\\n - ` + matchedCommands.join('\\n - ') + '\\n'\n\t}\n}\n"],"names":[],"mappings":";AAKO,MAAM,eAAe;AAAA,EAC3B,YAA4B,UAA8B;AAA9B,SAAA,WAAA;AAAA,EAA+B;AAAA,EAEpD,cAAc,MAA2C;AAC/D,UAAM,WAAW,KAAK,YAAY,IAAI;AACtC,UAAM,UAAU,KAAK,SAAS,QAAQ;AACtC,UAAM,kBAAkB,OAAO,QAAQ,KAAK,QAAQ,EAAE,KAAK,CAAC,CAAA,EAAG,EAAE,MAAM,OAAO,OAAO;AACrF,QAAI,CAAC,iBAAiB;AACrB,YAAM,IAAI,MAAM,YAAY;AAAA,IAC7B;AACA,QAAI,gBAAgB,CAAC,MAAM,UAAU;AACpC,cAAQ,KAAK,MAAM,OAAO,iBAAiB,QAAQ,yBAAyB,gBAAgB,CAAC,CAAC,YAAY,CAAC;AAAA,IAC5G;AACA,WAAO,CAAC,UAAU,SAAS;AAAA,EAC5B;AAAA,EAEQ,YAAY,MAAsB;AACzC,QAAI,KAAK,SAAS,IAAI,GAAG;AACxB,aAAO;AAAA,IACR;AACA,UAAM,UAAU,IAAI,OAAO,MAAM,KAAK,QAAQ,MAAM,SAAS,CAAC;AAC9D,UAAM,kBAAkB,OAAO,KAAK,KAAK,QAAQ,EAAE,OAAO,CAAA,OAAM,GAAG,MAAM,OAAO,CAAC;AACjF,QAAI,gBAAgB,WAAW,GAAG;AACjC,aAAO,gBAAgB,CAAC;AAAA,IACzB;AACA,QAAI,gBAAgB,WAAW,GAAG;AACjC,YAAM,WAAW,IAAI;AAAA,IACtB;AACA,UAAM,WAAW,IAAI;AAAA,OAAmD,gBAAgB,KAAK,OAAO,IAAI;AAAA,EACzG;AACD;"}
1
+ {"version":3,"file":"CommandManager.js","sources":["../../../../../packages/cli-common/src/application/CommandManager.ts"],"sourcesContent":["import { Command } from './Command.js'\nimport { CliError, ExitCode } from './CliError.js'\nimport { InvalidConfigurationError } from './CommandConfiguration.js'\n\nexport type CommandFactory = () => Command<any, any>\n\n/**\n * Keys sharing the very same factory are aliases of one command — the first one registered is the\n * canonical name. Both `migrations diff` and `migrations:diff` resolve to the same command.\n */\nexport type CommandFactoryList = { [command: string]: CommandFactory }\n\nexport interface CommandCatalogEntry {\n\t/** canonical name, space separated */\n\treadonly name: string\n\treadonly tokens: readonly string[]\n\treadonly group: string | undefined\n\treadonly aliases: readonly string[]\n\treadonly create: CommandFactory\n}\n\nexport type CommandResolution =\n\t| { readonly type: 'command'; readonly entry: CommandCatalogEntry; readonly args: string[] }\n\t| { readonly type: 'group'; readonly name: string; readonly entries: readonly CommandCatalogEntry[]; readonly args: string[] }\n\nconst splitTokens = (name: string): string[] => name.split(/[\\s:]+/).filter(it => it !== '')\n\nexport class CommandManager {\n\tprivate readonly entries: CommandCatalogEntry[]\n\t/** normalized name (canonical or alias) => entry */\n\tprivate readonly lookup = new Map<string, CommandCatalogEntry>()\n\t/** every strict token prefix of a command name => commands in that group */\n\tprivate readonly groups = new Map<string, CommandCatalogEntry[]>()\n\tprivate readonly maxTokens: number\n\n\tconstructor(public readonly commands: CommandFactoryList) {\n\t\tthis.entries = createCatalog(commands)\n\t\tfor (const entry of this.entries) {\n\t\t\tfor (const name of [entry.name, ...entry.aliases]) {\n\t\t\t\tconst tokens = splitTokens(name)\n\t\t\t\tconst normalizedName = tokens.join(' ')\n\t\t\t\tconst existing = this.lookup.get(normalizedName)\n\t\t\t\tif (existing !== undefined && existing.create !== entry.create) {\n\t\t\t\t\tthrow new InvalidConfigurationError(\n\t\t\t\t\t\t`Command names \"${existing.name}\" and \"${name}\" both normalize to \"${normalizedName}\" but use different factories.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tthis.lookup.set(normalizedName, entry)\n\t\t\t\tfor (let i = 1; i < tokens.length; i++) {\n\t\t\t\t\tconst prefix = tokens.slice(0, i).join(' ')\n\t\t\t\t\tconst group = this.groups.get(prefix) ?? []\n\t\t\t\t\tif (!group.includes(entry)) {\n\t\t\t\t\t\tgroup.push(entry)\n\t\t\t\t\t}\n\t\t\t\t\tthis.groups.set(prefix, group)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tthis.maxTokens = Math.max(1, ...[...this.lookup.keys()].map(it => splitTokens(it).length))\n\t\tthis.validateNoPrefixCollision()\n\t}\n\n\tpublic getCommands(): readonly CommandCatalogEntry[] {\n\t\treturn [...this.entries].sort((a, b) => a.name.localeCompare(b.name))\n\t}\n\n\t/**\n\t * Resolves the longest command name matching the leading argv tokens. `migrations diff`,\n\t * `migrations:diff` and the `m:d` shorthand all resolve to the same command; the tokens that were\n\t * not consumed are returned untouched as the command arguments.\n\t */\n\tpublic resolve(args: string[]): CommandResolution {\n\t\tconst tokens: string[] = []\n\t\tconst checkpoints: { tokens: string[]; argvIndex: number }[] = []\n\t\tfor (let i = 0; i < args.length; i++) {\n\t\t\tif (args[i].startsWith('-')) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tconst parts = splitTokens(args[i])\n\t\t\tif (parts.length === 0 || tokens.length + parts.length > this.maxTokens) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\ttokens.push(...parts)\n\t\t\tcheckpoints.push({ tokens: [...tokens], argvIndex: i + 1 })\n\t\t}\n\n\t\tfor (let i = checkpoints.length - 1; i >= 0; i--) {\n\t\t\tconst entry = this.lookup.get(checkpoints[i].tokens.join(' '))\n\t\t\tif (entry) {\n\t\t\t\treturn { type: 'command', entry, args: args.slice(checkpoints[i].argvIndex) }\n\t\t\t}\n\t\t}\n\n\t\tconst group = this.groups.get(tokens.join(' '))\n\t\tif (group !== undefined && group.length > 0) {\n\t\t\tconst argvIndex = checkpoints[checkpoints.length - 1]?.argvIndex ?? 0\n\t\t\treturn { type: 'group', name: tokens.join(' '), entries: group, args: args.slice(argvIndex) }\n\t\t}\n\n\t\tfor (let i = checkpoints.length - 1; i >= 0; i--) {\n\t\t\t// a group named in full must be followed by a verb — never let it swallow the remaining tokens as arguments\n\t\t\tif (i < checkpoints.length - 1 && this.groups.has(checkpoints[i].tokens.join(' '))) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst matched = this.matchShorthand(checkpoints[i].tokens)\n\t\t\tif (matched.length === 1) {\n\t\t\t\treturn { type: 'command', entry: matched[0], args: args.slice(checkpoints[i].argvIndex) }\n\t\t\t}\n\t\t\tif (matched.length > 1) {\n\t\t\t\tthrow new CliError(\n\t\t\t\t\t`Command \"${checkpoints[i].tokens.join(' ')}\" is ambiguous. Did you mean one of these?\\n - ${matched.map(it => it.name).join('\\n - ')}`,\n\t\t\t\t\t{ code: 'COMMAND_AMBIGUOUS', exitCode: ExitCode.InputError },\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\t// the leading tokens name a known group, but the verb does not exist in it\n\t\tfor (let i = tokens.length - 1; i >= 1; i--) {\n\t\t\tconst known = this.groups.get(tokens.slice(0, i).join(' '))\n\t\t\tif (known !== undefined && known.length > 0) {\n\t\t\t\tthrow new CliError(\n\t\t\t\t\t`Command \"${tokens.join(' ')}\" not found. Available commands in \"${tokens.slice(0, i).join(' ')}\":\\n - ${\n\t\t\t\t\t\tknown.map(it => it.name).sort((a, b) => a.localeCompare(b)).join('\\n - ')\n\t\t\t\t\t}`,\n\t\t\t\t\t{ code: 'COMMAND_NOT_FOUND', exitCode: ExitCode.InputError },\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\tthrow new CliError(`Command \"${args[0] ?? ''}\" not found.`, { code: 'COMMAND_NOT_FOUND', exitCode: ExitCode.InputError })\n\t}\n\n\tpublic createCommand(name: string): [string, Command<any, any>] {\n\t\tconst resolution = this.resolve([name])\n\t\tif (resolution.type === 'group') {\n\t\t\tthrow new CliError(\n\t\t\t\t`Command \"${resolution.name}\" is a command group. Available commands:\\n - ${resolution.entries.map(it => it.name).join('\\n - ')}`,\n\t\t\t\t{ code: 'COMMAND_AMBIGUOUS', exitCode: ExitCode.InputError },\n\t\t\t)\n\t\t}\n\t\treturn [resolution.entry.name, resolution.entry.create()]\n\t}\n\n\t/** Prefix matching of the abbreviated form: `m:d` => `migrations diff`, `w:u` => `workspace update api`. */\n\tprivate matchShorthand(tokens: string[]): CommandCatalogEntry[] {\n\t\tif (tokens.length === 0) {\n\t\t\treturn []\n\t\t}\n\t\treturn this.entries.filter(entry => entry.tokens.length >= tokens.length && tokens.every((token, index) => entry.tokens[index].startsWith(token)))\n\t}\n\n\tprivate validateNoPrefixCollision(): void {\n\t\tconst names = [...this.lookup.keys()]\n\t\tfor (const name of names) {\n\t\t\tfor (const other of names) {\n\t\t\t\tif (other !== name && other.startsWith(name + ' ')) {\n\t\t\t\t\tthrow new InvalidConfigurationError(\n\t\t\t\t\t\t`Command name \"${name}\" is a prefix of \"${other}\". A command name must not be a prefix of another command name, otherwise it could never be resolved.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nconst createCatalog = (commands: CommandFactoryList): CommandCatalogEntry[] => {\n\tconst byFactory = new Map<CommandFactory, { name: string; aliases: string[] }>()\n\tfor (const [name, factory] of Object.entries(commands)) {\n\t\tconst registered = byFactory.get(factory)\n\t\tif (registered) {\n\t\t\tregistered.aliases.push(name)\n\t\t} else {\n\t\t\tbyFactory.set(factory, { name, aliases: [] })\n\t\t}\n\t}\n\treturn [...byFactory].map(([factory, { name, aliases }]) => {\n\t\tconst tokens = splitTokens(name)\n\t\treturn {\n\t\t\tname: tokens.join(' '),\n\t\t\ttokens,\n\t\t\tgroup: tokens.length > 1 ? tokens[0] : undefined,\n\t\t\taliases,\n\t\t\tcreate: factory,\n\t\t}\n\t})\n}\n"],"names":[],"mappings":";;;;;AAyBA,MAAM,cAAc,CAAC,SAA2B,KAAK,MAAM,QAAQ,EAAE,OAAO,CAAA,OAAM,OAAO,EAAE;AAEpF,MAAM,eAAe;AAAA,EAQ3B,YAA4B,UAA8B;AAA9B,SAAA,WAAA;AAP5B,kBAAA,MAAiB,SAAA;AAEjB,kBAAA,MAAiB,8BAAa,IAAA,CAAiC;AAE/D,kBAAA,MAAiB,8BAAa,IAAA,CAAmC;AACjE,kBAAA,MAAiB,WAAA;AAGhB,SAAK,UAAU,cAAc,QAAQ;AACrC,eAAW,SAAS,KAAK,SAAS;AACjC,iBAAW,QAAQ,CAAC,MAAM,MAAM,GAAG,MAAM,OAAO,GAAG;AAClD,cAAM,SAAS,YAAY,IAAI;AAC/B,cAAM,iBAAiB,OAAO,KAAK,GAAG;AACtC,c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AAK,EAAE,WAAW,KAAK,CAAC,CAAC;AAAA,EAClJ;AAAA,EAEQ,4BAAkC;AACzC,UAAM,QAAQ,CAAC,GAAG,KAAK,OAAO,MAAM;AACpC,eAAW,QAAQ,OAAO;AACzB,iBAAW,SAAS,OAAO;AAC1B,YAAI,UAAU,QAAQ,MAAM,WAAW,OAAO,GAAG,GAAG;AACnD,gBAAM,IAAI;AAAA,YACT,iBAAiB,IAAI,qBAAqB,KAAK;AAAA,UAAA;AAAA,QAEjD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;AAEA,MAAM,gBAAgB,CAAC,aAAwD;AAC9E,QAAM,gCAAgB,IAAA;AACtB,aAAW,CAAC,MAAM,OAAO,KAAK,OAAO,QAAQ,QAAQ,GAAG;AACvD,UAAM,aAAa,UAAU,IAAI,OAAO;AACxC,QAAI,YAAY;AACf,iBAAW,QAAQ,KAAK,IAAI;AAAA,IAC7B,OAAO;AACN,gBAAU,IAAI,SAAS,EAAE,MAAM,SAAS,CAAA,GAAI;AAAA,IAC7C;AAAA,EACD;AACA,SAAO,CAAC,GAAG,SAAS,EAAE,IAAI,CAAC,CAAC,SAAS,EAAE,MAAM,QAAA,CAAS,MAAM;AAC3D,UAAM,SAAS,YAAY,IAAI;AAC/B,WAAO;AAAA,MACN,MAAM,OAAO,KAAK,GAAG;AAAA,MACrB;AAAA,MACA,OAAO,OAAO,SAAS,IAAI,OAAO,CAAC,IAAI;AAAA,MACvC;AAAA,MACA,QAAQ;AAAA,IAAA;AAAA,EAEV,CAAC;AACF;"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const chalk = require("chalk");
4
+ const CliError = require("./CliError.cjs");
5
+ const Output = require("./Output.cjs");
6
+ const renderCliError = (error, output) => {
7
+ const cliError = CliError.toCliError(error);
8
+ if (output.isJson) {
9
+ output.writeStderr(serializeError(cliError, CliError.isUnexpectedCliError(cliError)));
10
+ return cliError.exitCode;
11
+ }
12
+ output.writeStderr(chalk.red(`Error [${Output.escapeTerminalText(cliError.code)}]: ${Output.escapeTerminalText(cliError.message)}`));
13
+ if (cliError.exitCode === CliError.ExitCode.InternalError && typeof cliError.details === "string") {
14
+ output.writeStderr(chalk.gray(Output.escapeTerminalText(cliError.details)));
15
+ }
16
+ return cliError.exitCode;
17
+ };
18
+ const serializeError = (error, unexpected) => {
19
+ try {
20
+ return JSON.stringify(
21
+ {
22
+ ok: false,
23
+ error: {
24
+ code: error.code,
25
+ message: unexpected ? "An unexpected error occurred." : error.message,
26
+ retryable: error.retryable,
27
+ details: unexpected ? null : error.details ?? null
28
+ }
29
+ },
30
+ null,
31
+ 2
32
+ );
33
+ } catch {
34
+ return '{"ok":false,"error":{"code":"UNKNOWN","message":"Unable to serialize error","retryable":false,"details":null}}';
35
+ }
36
+ };
37
+ exports.renderCliError = renderCliError;
38
+ //# sourceMappingURL=ErrorRenderer.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ErrorRenderer.cjs","sources":["../../../../../packages/cli-common/src/application/ErrorRenderer.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { ExitCode, isUnexpectedCliError, toCliError } from './CliError.js'\nimport { escapeTerminalText, Output } from './Output.js'\n\n/**\n * The single place where an error becomes user visible. Always writes to stderr — in json mode as the\n * `{ ok: false, error }` envelope, otherwise as a colored one-liner. Returns the process exit code.\n */\nexport const renderCliError = (error: unknown, output: Output): ExitCode => {\n\tconst cliError = toCliError(error)\n\tif (output.isJson) {\n\t\toutput.writeStderr(serializeError(cliError, isUnexpectedCliError(cliError)))\n\t\treturn cliError.exitCode\n\t}\n\toutput.writeStderr(chalk.red(`Error [${escapeTerminalText(cliError.code)}]: ${escapeTerminalText(cliError.message)}`))\n\tif (cliError.exitCode === ExitCode.InternalError && typeof cliError.details === 'string') {\n\t\toutput.writeStderr(chalk.gray(escapeTerminalText(cliError.details)))\n\t}\n\treturn cliError.exitCode\n}\n\nconst serializeError = (error: ReturnType<typeof toCliError>, unexpected: boolean): string => {\n\ttry {\n\t\treturn JSON.stringify(\n\t\t\t{\n\t\t\t\tok: false,\n\t\t\t\terror: {\n\t\t\t\t\tcode: error.code,\n\t\t\t\t\tmessage: unexpected ? 'An unexpected error occurred.' : error.message,\n\t\t\t\t\tretryable: error.retryable,\n\t\t\t\t\tdetails: unexpected ? null : error.details ?? null,\n\t\t\t\t},\n\t\t\t},\n\t\t\tnull,\n\t\t\t2,\n\t\t)\n\t} catch {\n\t\treturn '{\"ok\":false,\"error\":{\"code\":\"UNKNOWN\",\"message\":\"Unable to serialize error\",\"retryable\":false,\"details\":null}}'\n\t}\n}\n"],"names":["toCliError","isUnexpectedCliError","escapeTerminalText","ExitCode"],"mappings":";;;;;AAQO,MAAM,iBAAiB,CAAC,OAAgB,WAA6B;AAC3E,QAAM,WAAWA,SAAAA,WAAW,KAAK;AACjC,MAAI,OAAO,QAAQ;AAClB,WAAO,YAAY,eAAe,UAAUC,SAAAA,qBAAqB,QAAQ,CAAC,CAAC;AAC3E,WAAO,SAAS;AAAA,EACjB;AACA,SAAO,YAAY,MAAM,IAAI,UAAUC,OAAAA,mBAAmB,SAAS,IAAI,CAAC,MAAMA,OAAAA,mBAAmB,SAAS,OAAO,CAAC,EAAE,CAAC;AACrH,MAAI,SAAS,aAAaC,SAAAA,SAAS,iBAAiB,OAAO,SAAS,YAAY,UAAU;AACzF,WAAO,YAAY,MAAM,KAAKD,OAAAA,mBAAmB,SAAS,OAAO,CAAC,CAAC;AAAA,EACpE;AACA,SAAO,SAAS;AACjB;AAEA,MAAM,iBAAiB,CAAC,OAAsC,eAAgC;AAC7F,MAAI;AACH,WAAO,KAAK;AAAA,MACX;AAAA,QACC,IAAI;AAAA,QACJ,OAAO;AAAA,UACN,MAAM,MAAM;AAAA,UACZ,SAAS,aAAa,kCAAkC,MAAM;AAAA,UAC9D,WAAW,MAAM;AAAA,UACjB,SAAS,aAAa,OAAO,MAAM,WAAW;AAAA,QAAA;AAAA,MAC/C;AAAA,MAED;AAAA,MACA;AAAA,IAAA;AAAA,EAEF,QAAQ;AACP,WAAO;AAAA,EACR;AACD;;"}
@@ -0,0 +1,38 @@
1
+ import chalk from "chalk";
2
+ import { toCliError, isUnexpectedCliError, ExitCode } from "./CliError.js";
3
+ import { escapeTerminalText } from "./Output.js";
4
+ const renderCliError = (error, output) => {
5
+ const cliError = toCliError(error);
6
+ if (output.isJson) {
7
+ output.writeStderr(serializeError(cliError, isUnexpectedCliError(cliError)));
8
+ return cliError.exitCode;
9
+ }
10
+ output.writeStderr(chalk.red(`Error [${escapeTerminalText(cliError.code)}]: ${escapeTerminalText(cliError.message)}`));
11
+ if (cliError.exitCode === ExitCode.InternalError && typeof cliError.details === "string") {
12
+ output.writeStderr(chalk.gray(escapeTerminalText(cliError.details)));
13
+ }
14
+ return cliError.exitCode;
15
+ };
16
+ const serializeError = (error, unexpected) => {
17
+ try {
18
+ return JSON.stringify(
19
+ {
20
+ ok: false,
21
+ error: {
22
+ code: error.code,
23
+ message: unexpected ? "An unexpected error occurred." : error.message,
24
+ retryable: error.retryable,
25
+ details: unexpected ? null : error.details ?? null
26
+ }
27
+ },
28
+ null,
29
+ 2
30
+ );
31
+ } catch {
32
+ return '{"ok":false,"error":{"code":"UNKNOWN","message":"Unable to serialize error","retryable":false,"details":null}}';
33
+ }
34
+ };
35
+ export {
36
+ renderCliError
37
+ };
38
+ //# sourceMappingURL=ErrorRenderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ErrorRenderer.js","sources":["../../../../../packages/cli-common/src/application/ErrorRenderer.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { ExitCode, isUnexpectedCliError, toCliError } from './CliError.js'\nimport { escapeTerminalText, Output } from './Output.js'\n\n/**\n * The single place where an error becomes user visible. Always writes to stderr — in json mode as the\n * `{ ok: false, error }` envelope, otherwise as a colored one-liner. Returns the process exit code.\n */\nexport const renderCliError = (error: unknown, output: Output): ExitCode => {\n\tconst cliError = toCliError(error)\n\tif (output.isJson) {\n\t\toutput.writeStderr(serializeError(cliError, isUnexpectedCliError(cliError)))\n\t\treturn cliError.exitCode\n\t}\n\toutput.writeStderr(chalk.red(`Error [${escapeTerminalText(cliError.code)}]: ${escapeTerminalText(cliError.message)}`))\n\tif (cliError.exitCode === ExitCode.InternalError && typeof cliError.details === 'string') {\n\t\toutput.writeStderr(chalk.gray(escapeTerminalText(cliError.details)))\n\t}\n\treturn cliError.exitCode\n}\n\nconst serializeError = (error: ReturnType<typeof toCliError>, unexpected: boolean): string => {\n\ttry {\n\t\treturn JSON.stringify(\n\t\t\t{\n\t\t\t\tok: false,\n\t\t\t\terror: {\n\t\t\t\t\tcode: error.code,\n\t\t\t\t\tmessage: unexpected ? 'An unexpected error occurred.' : error.message,\n\t\t\t\t\tretryable: error.retryable,\n\t\t\t\t\tdetails: unexpected ? null : error.details ?? null,\n\t\t\t\t},\n\t\t\t},\n\t\t\tnull,\n\t\t\t2,\n\t\t)\n\t} catch {\n\t\treturn '{\"ok\":false,\"error\":{\"code\":\"UNKNOWN\",\"message\":\"Unable to serialize error\",\"retryable\":false,\"details\":null}}'\n\t}\n}\n"],"names":[],"mappings":";;;AAQO,MAAM,iBAAiB,CAAC,OAAgB,WAA6B;AAC3E,QAAM,WAAW,WAAW,KAAK;AACjC,MAAI,OAAO,QAAQ;AAClB,WAAO,YAAY,eAAe,UAAU,qBAAqB,QAAQ,CAAC,CAAC;AAC3E,WAAO,SAAS;AAAA,EACjB;AACA,SAAO,YAAY,MAAM,IAAI,UAAU,mBAAmB,SAAS,IAAI,CAAC,MAAM,mBAAmB,SAAS,OAAO,CAAC,EAAE,CAAC;AACrH,MAAI,SAAS,aAAa,SAAS,iBAAiB,OAAO,SAAS,YAAY,UAAU;AACzF,WAAO,YAAY,MAAM,KAAK,mBAAmB,SAAS,OAAO,CAAC,CAAC;AAAA,EACpE;AACA,SAAO,SAAS;AACjB;AAEA,MAAM,iBAAiB,CAAC,OAAsC,eAAgC;AAC7F,MAAI;AACH,WAAO,KAAK;AAAA,MACX;AAAA,QACC,IAAI;AAAA,QACJ,OAAO;AAAA,UACN,MAAM,MAAM;AAAA,UACZ,SAAS,aAAa,kCAAkC,MAAM;AAAA,UAC9D,WAAW,MAAM;AAAA,UACjB,SAAS,aAAa,OAAO,MAAM,WAAW;AAAA,QAAA;AAAA,MAC/C;AAAA,MAED;AAAA,MACA;AAAA,IAAA;AAAA,EAEF,QAAQ;AACP,WAAO;AAAA,EACR;AACD;"}