@senators/bifrost-cli 0.0.0-stage → 7.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 (403) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +180 -2
  3. package/out/accountEditor-CQB91PVf.js +2 -0
  4. package/out/accountEditor-CQB91PVf.js.map +1 -0
  5. package/out/achievements-Df5qqpYQ.js +2 -0
  6. package/out/achievements-Df5qqpYQ.js.map +1 -0
  7. package/out/active-CnUKpiEo.js +2 -0
  8. package/out/active-CnUKpiEo.js.map +1 -0
  9. package/out/alliance-BsPnhi7w.js +2 -0
  10. package/out/alliance-BsPnhi7w.js.map +1 -0
  11. package/out/allianceAdmin-Bkyl6ev4.js +2 -0
  12. package/out/allianceAdmin-Bkyl6ev4.js.map +1 -0
  13. package/out/arsenal-BMCmAHzv.js +2 -0
  14. package/out/arsenal-BMCmAHzv.js.map +1 -0
  15. package/out/ban-BXNvHPMq.js +2 -0
  16. package/out/ban-BXNvHPMq.js.map +1 -0
  17. package/out/banList-3DbCToEy.js +2 -0
  18. package/out/banList-3DbCToEy.js.map +1 -0
  19. package/out/banList-dT5GF5eO.js +2 -0
  20. package/out/banList-dT5GF5eO.js.map +1 -0
  21. package/out/battleHall-C4BALP6S.js +2 -0
  22. package/out/battleHall-C4BALP6S.js.map +1 -0
  23. package/out/battleHall-P7Co1jS5.js +2 -0
  24. package/out/battleHall-P7Co1jS5.js.map +1 -0
  25. package/out/battleReport-BG0-S-bF.js +2 -0
  26. package/out/battleReport-BG0-S-bF.js.map +1 -0
  27. package/out/battleReport-C3PQTXxc.js +2 -0
  28. package/out/battleReport-C3PQTXxc.js.map +1 -0
  29. package/out/battleSimulator-BOySErLn.js +2 -0
  30. package/out/battleSimulator-BOySErLn.js.map +1 -0
  31. package/out/board-CsNgDx4j.js +2 -0
  32. package/out/board-CsNgDx4j.js.map +1 -0
  33. package/out/board-DSCl8eU8.js +2 -0
  34. package/out/board-DSCl8eU8.js.map +1 -0
  35. package/out/bonusSummary-Dmm646zj.js +2 -0
  36. package/out/bonusSummary-Dmm646zj.js.map +1 -0
  37. package/out/buddyList-B50t-9_g.js +2 -0
  38. package/out/buddyList-B50t-9_g.js.map +1 -0
  39. package/out/buildings-CXrMgFyb.js +2 -0
  40. package/out/buildings-CXrMgFyb.js.map +1 -0
  41. package/out/catalog-DmSXp_YG.js +2 -0
  42. package/out/catalog-DmSXp_YG.js.map +1 -0
  43. package/out/chat-BvtMl-I7.js +2 -0
  44. package/out/chat-BvtMl-I7.js.map +1 -0
  45. package/out/clearCache-79cjREI0.js +2 -0
  46. package/out/clearCache-79cjREI0.js.map +1 -0
  47. package/out/cli.js +3 -0
  48. package/out/cli.js.map +1 -0
  49. package/out/containers-BhguCCkz.js +2 -0
  50. package/out/containers-BhguCCkz.js.map +1 -0
  51. package/out/control-IJRy1LDx.js +2 -0
  52. package/out/control-IJRy1LDx.js.map +1 -0
  53. package/out/create-D-IygCER.js +2 -0
  54. package/out/create-D-IygCER.js.map +1 -0
  55. package/out/createMoon-BMWjdwO0.js +2 -0
  56. package/out/createMoon-BMWjdwO0.js.map +1 -0
  57. package/out/cronjob-CCJKDSuv.js +2 -0
  58. package/out/cronjob-CCJKDSuv.js.map +1 -0
  59. package/out/dailyBonus-2UwWD4D0.js +2 -0
  60. package/out/dailyBonus-2UwWD4D0.js.map +1 -0
  61. package/out/delivery-d8NKP-f1.js +2 -0
  62. package/out/delivery-d8NKP-f1.js.map +1 -0
  63. package/out/disclaimer-BmejHZqk.js +2 -0
  64. package/out/disclaimer-BmejHZqk.js.map +1 -0
  65. package/out/dump--mJa5m3e.js +2 -0
  66. package/out/dump--mJa5m3e.js.map +1 -0
  67. package/out/endpoints-CbZGTbF-.js +2 -0
  68. package/out/endpoints-CbZGTbF-.js.map +1 -0
  69. package/out/externalAuth-Dwl3VAHa.js +2 -0
  70. package/out/externalAuth-Dwl3VAHa.js.map +1 -0
  71. package/out/findDebris-DOMvMC1l.js +2 -0
  72. package/out/findDebris-DOMvMC1l.js.map +1 -0
  73. package/out/fleet-DyITa_Xu.js +2 -0
  74. package/out/fleet-DyITa_Xu.js.map +1 -0
  75. package/out/fleetAjax-BGxyUtdi.js +2 -0
  76. package/out/fleetAjax-BGxyUtdi.js.map +1 -0
  77. package/out/fleetDealer-n6LM_8IT.js +2 -0
  78. package/out/fleetDealer-n6LM_8IT.js.map +1 -0
  79. package/out/fleetMissile-CucQvNmZ.js +2 -0
  80. package/out/fleetMissile-CucQvNmZ.js.map +1 -0
  81. package/out/fleetStep1-wp-NgTsC.js +2 -0
  82. package/out/fleetStep1-wp-NgTsC.js.map +1 -0
  83. package/out/fleetStep2-C-GzHXyn.js +2 -0
  84. package/out/fleetStep2-C-GzHXyn.js.map +1 -0
  85. package/out/fleetStep3-D_fqImWm.js +2 -0
  86. package/out/fleetStep3-D_fqImWm.js.map +1 -0
  87. package/out/fleetTable-BpWLh0R1.js +2 -0
  88. package/out/fleetTable-BpWLh0R1.js.map +1 -0
  89. package/out/flyingFleet-ClIrGPJz.js +2 -0
  90. package/out/flyingFleet-ClIrGPJz.js.map +1 -0
  91. package/out/galaxy-B12wKQUU.js +2 -0
  92. package/out/galaxy-B12wKQUU.js.map +1 -0
  93. package/out/get-Cn2hFvGS.js +2 -0
  94. package/out/get-Cn2hFvGS.js.map +1 -0
  95. package/out/giveaway-CDWEArOF.js +2 -0
  96. package/out/giveaway-CDWEArOF.js.map +1 -0
  97. package/out/growth-C_-C8rLD.js +2 -0
  98. package/out/growth-C_-C8rLD.js.map +1 -0
  99. package/out/index.d.ts +29 -0
  100. package/out/index.d.ts.map +1 -0
  101. package/out/index.js +1 -0
  102. package/out/information-CqfqYCH1.js +2 -0
  103. package/out/information-CqfqYCH1.js.map +1 -0
  104. package/out/information-DK93hgyE.js +2 -0
  105. package/out/information-DK93hgyE.js.map +1 -0
  106. package/out/log-BBE1Jt9i.js +2 -0
  107. package/out/log-BBE1Jt9i.js.map +1 -0
  108. package/out/login--4vqysQx.js +2 -0
  109. package/out/login--4vqysQx.js.map +1 -0
  110. package/out/login-C_DqRApn.js +2 -0
  111. package/out/login-C_DqRApn.js.map +1 -0
  112. package/out/logout-BYnLiMja.js +2 -0
  113. package/out/logout-BYnLiMja.js.map +1 -0
  114. package/out/logout-BsCSXzv8.js +2 -0
  115. package/out/logout-BsCSXzv8.js.map +1 -0
  116. package/out/lostPassword-Pc_1CUBD.js +2 -0
  117. package/out/lostPassword-Pc_1CUBD.js.map +1 -0
  118. package/out/market-DN8cY1tr.js +2 -0
  119. package/out/market-DN8cY1tr.js.map +1 -0
  120. package/out/messageList-k_zW_D0G.js +2 -0
  121. package/out/messageList-k_zW_D0G.js.map +1 -0
  122. package/out/messages-D461yIe6.js +2 -0
  123. package/out/messages-D461yIe6.js.map +1 -0
  124. package/out/missions-5zZtlLIm.js +2 -0
  125. package/out/missions-5zZtlLIm.js.map +1 -0
  126. package/out/module-BEPeTBhK.js +2 -0
  127. package/out/module-BEPeTBhK.js.map +1 -0
  128. package/out/multiIp-CP3lub1T.js +2 -0
  129. package/out/multiIp-CP3lub1T.js.map +1 -0
  130. package/out/new-star-BaVTTcIn.js +2 -0
  131. package/out/new-star-BaVTTcIn.js.map +1 -0
  132. package/out/news-BdDDcd-M.js +2 -0
  133. package/out/news-BdDDcd-M.js.map +1 -0
  134. package/out/news-Dd1C9JDl.js +2 -0
  135. package/out/news-Dd1C9JDl.js.map +1 -0
  136. package/out/notes-DLngBta3.js +2 -0
  137. package/out/notes-DLngBta3.js.map +1 -0
  138. package/out/overview-CzcSTNla.js +2 -0
  139. package/out/overview-CzcSTNla.js.map +1 -0
  140. package/out/overview-wQWIR_CB.js +2 -0
  141. package/out/overview-wQWIR_CB.js.map +1 -0
  142. package/out/password-ZA_1lStC.js +2 -0
  143. package/out/password-ZA_1lStC.js.map +1 -0
  144. package/out/phalanx-DaMaoJCT.js +2 -0
  145. package/out/phalanx-DaMaoJCT.js.map +1 -0
  146. package/out/planet-CecZ2cAZ.js +2 -0
  147. package/out/planet-CecZ2cAZ.js.map +1 -0
  148. package/out/playerCard-BG711xQi.js +2 -0
  149. package/out/playerCard-BG711xQi.js.map +1 -0
  150. package/out/post-C_ywAss7.js +2 -0
  151. package/out/post-C_ywAss7.js.map +1 -0
  152. package/out/production-D8SZfez8.js +2 -0
  153. package/out/production-D8SZfez8.js.map +1 -0
  154. package/out/public-TVVf75qu.js +2 -0
  155. package/out/public-TVVf75qu.js.map +1 -0
  156. package/out/questions-NwzZ5_fL.js +2 -0
  157. package/out/questions-NwzZ5_fL.js.map +1 -0
  158. package/out/quickEditor-DEJQFH0f.js +2 -0
  159. package/out/quickEditor-DEJQFH0f.js.map +1 -0
  160. package/out/records-CDVglYGy.js +2 -0
  161. package/out/records-CDVglYGy.js.map +1 -0
  162. package/out/records-Djjz9kxk.js +2 -0
  163. package/out/records-Djjz9kxk.js.map +1 -0
  164. package/out/reduceFleet-y8p89weR.js +2 -0
  165. package/out/reduceFleet-y8p89weR.js.map +1 -0
  166. package/out/reduceResources-CnGJ9fuf.js +2 -0
  167. package/out/reduceResources-CnGJ9fuf.js.map +1 -0
  168. package/out/refreshStatistics-CjGWyNc_.js +2 -0
  169. package/out/refreshStatistics-CjGWyNc_.js.map +1 -0
  170. package/out/register-4x5hZcWe.js +2 -0
  171. package/out/register-4x5hZcWe.js.map +1 -0
  172. package/out/research-dTK3-tOW.js +2 -0
  173. package/out/research-dTK3-tOW.js.map +1 -0
  174. package/out/reset-CHiMI9rj.js +2 -0
  175. package/out/reset-CHiMI9rj.js.map +1 -0
  176. package/out/resources-CjnevA41.js +2 -0
  177. package/out/resources-CjnevA41.js.map +1 -0
  178. package/out/rights-WPf-j_YF.js +2 -0
  179. package/out/rights-WPf-j_YF.js.map +1 -0
  180. package/out/rules-E29jtQRj.js +2 -0
  181. package/out/rules-E29jtQRj.js.map +1 -0
  182. package/out/runner-C2bQQS2G.js +43 -0
  183. package/out/runner-C2bQQS2G.js.map +1 -0
  184. package/out/scan-RvWZeyyM.js +2 -0
  185. package/out/scan-RvWZeyyM.js.map +1 -0
  186. package/out/screens-CZ4g-KNp.js +2 -0
  187. package/out/screens-CZ4g-KNp.js.map +1 -0
  188. package/out/search-BYvqiUBF.js +2 -0
  189. package/out/search-BYvqiUBF.js.map +1 -0
  190. package/out/search-auPbK2Xc.js +2 -0
  191. package/out/search-auPbK2Xc.js.map +1 -0
  192. package/out/sendMessages-B2Uj3PW4.js +2 -0
  193. package/out/sendMessages-B2Uj3PW4.js.map +1 -0
  194. package/out/settings-DFFa6Ush.js +2 -0
  195. package/out/settings-DFFa6Ush.js.map +1 -0
  196. package/out/settings-Ddez5x9U.js +2 -0
  197. package/out/settings-Ddez5x9U.js.map +1 -0
  198. package/out/shared-DoaW4mlX.js +2 -0
  199. package/out/shared-DoaW4mlX.js.map +1 -0
  200. package/out/shipyard-DXr745hR.js +2 -0
  201. package/out/shipyard-DXr745hR.js.map +1 -0
  202. package/out/shops-CEE02c0g.js +2 -0
  203. package/out/shops-CEE02c0g.js.map +1 -0
  204. package/out/snapshot-CrJ8l50N.js +2 -0
  205. package/out/snapshot-CrJ8l50N.js.map +1 -0
  206. package/out/social-1Gdni-jZ.js +2 -0
  207. package/out/social-1Gdni-jZ.js.map +1 -0
  208. package/out/spec-CrkWB0BN.js +2 -0
  209. package/out/spec-CrkWB0BN.js.map +1 -0
  210. package/out/statistics-zg3aVAvW.js +2 -0
  211. package/out/statistics-zg3aVAvW.js.map +1 -0
  212. package/out/store-dXG7NoB4.js +2 -0
  213. package/out/store-dXG7NoB4.js.map +1 -0
  214. package/out/support-DisUQr57.js +2 -0
  215. package/out/support-DisUQr57.js.map +1 -0
  216. package/out/techtree-Ba3FEH5_.js +2 -0
  217. package/out/techtree-Ba3FEH5_.js.map +1 -0
  218. package/out/ticket-O_8jTKW8.js +2 -0
  219. package/out/ticket-O_8jTKW8.js.map +1 -0
  220. package/out/trader-zFDGaHZ9.js +2 -0
  221. package/out/trader-zFDGaHZ9.js.map +1 -0
  222. package/out/universe-DO32RnLQ.js +2 -0
  223. package/out/universe-DO32RnLQ.js.map +1 -0
  224. package/out/universeSettings-S7enRJR4.js +2 -0
  225. package/out/universeSettings-S7enRJR4.js.map +1 -0
  226. package/out/values-gwcnIHXs.js +2 -0
  227. package/out/values-gwcnIHXs.js.map +1 -0
  228. package/out/verify-sYnZ9juV.js +2 -0
  229. package/out/verify-sYnZ9juV.js.map +1 -0
  230. package/out/version-Cgs-grc5.js +2 -0
  231. package/out/version-Cgs-grc5.js.map +1 -0
  232. package/package.json +65 -4
  233. package/src/admin.ts +317 -0
  234. package/src/arguments.ts +323 -0
  235. package/src/catalog.ts +117 -0
  236. package/src/cli.ts +29 -0
  237. package/src/commands/admin/accountEditor.ts +33 -0
  238. package/src/commands/admin/active.ts +25 -0
  239. package/src/commands/admin/ban.ts +59 -0
  240. package/src/commands/admin/clearCache.ts +12 -0
  241. package/src/commands/admin/create.ts +59 -0
  242. package/src/commands/admin/cronjob.ts +94 -0
  243. package/src/commands/admin/dump.ts +25 -0
  244. package/src/commands/admin/flyingFleet.ts +25 -0
  245. package/src/commands/admin/giveaway.ts +37 -0
  246. package/src/commands/admin/information.ts +12 -0
  247. package/src/commands/admin/log.ts +37 -0
  248. package/src/commands/admin/login.ts +15 -0
  249. package/src/commands/admin/logout.ts +12 -0
  250. package/src/commands/admin/messageList.ts +19 -0
  251. package/src/commands/admin/module.ts +25 -0
  252. package/src/commands/admin/multiIp.ts +25 -0
  253. package/src/commands/admin/news.ts +40 -0
  254. package/src/commands/admin/overview.ts +12 -0
  255. package/src/commands/admin/password.ts +18 -0
  256. package/src/commands/admin/quickEditor.ts +30 -0
  257. package/src/commands/admin/refreshStatistics.ts +12 -0
  258. package/src/commands/admin/reset.ts +26 -0
  259. package/src/commands/admin/rights.ts +68 -0
  260. package/src/commands/admin/search.ts +31 -0
  261. package/src/commands/admin/sendMessages.ts +26 -0
  262. package/src/commands/admin/support.ts +48 -0
  263. package/src/commands/admin/universe.ts +49 -0
  264. package/src/commands/admin/universeSettings.ts +32 -0
  265. package/src/commands/game/achievements.ts +18 -0
  266. package/src/commands/game/alliance.ts +248 -0
  267. package/src/commands/game/allianceAdmin.ts +236 -0
  268. package/src/commands/game/arsenal.ts +12 -0
  269. package/src/commands/game/banList.ts +20 -0
  270. package/src/commands/game/battleHall.ts +21 -0
  271. package/src/commands/game/battleReport.ts +20 -0
  272. package/src/commands/game/battleSimulator.ts +41 -0
  273. package/src/commands/game/board.ts +12 -0
  274. package/src/commands/game/bonusSummary.ts +12 -0
  275. package/src/commands/game/buddyList.ts +76 -0
  276. package/src/commands/game/buildings.ts +81 -0
  277. package/src/commands/game/chat.ts +12 -0
  278. package/src/commands/game/containers.ts +33 -0
  279. package/src/commands/game/control.ts +16 -0
  280. package/src/commands/game/createMoon.ts +28 -0
  281. package/src/commands/game/dailyBonus.ts +18 -0
  282. package/src/commands/game/delivery.ts +34 -0
  283. package/src/commands/game/endpoints.ts +61 -0
  284. package/src/commands/game/findDebris.ts +16 -0
  285. package/src/commands/game/fleetAjax.ts +24 -0
  286. package/src/commands/game/fleetDealer.ts +36 -0
  287. package/src/commands/game/fleetMissile.ts +25 -0
  288. package/src/commands/game/fleetStep1.ts +74 -0
  289. package/src/commands/game/fleetStep2.ts +27 -0
  290. package/src/commands/game/fleetStep3.ts +28 -0
  291. package/src/commands/game/fleetTable.ts +96 -0
  292. package/src/commands/game/galaxy.ts +26 -0
  293. package/src/commands/game/growth.ts +313 -0
  294. package/src/commands/game/information.ts +56 -0
  295. package/src/commands/game/logout.ts +12 -0
  296. package/src/commands/game/market.ts +67 -0
  297. package/src/commands/game/messages.ts +129 -0
  298. package/src/commands/game/notes.ts +65 -0
  299. package/src/commands/game/overview.ts +49 -0
  300. package/src/commands/game/phalanx.ts +23 -0
  301. package/src/commands/game/planet.ts +78 -0
  302. package/src/commands/game/playerCard.ts +20 -0
  303. package/src/commands/game/questions.ts +23 -0
  304. package/src/commands/game/records.ts +16 -0
  305. package/src/commands/game/reduceFleet.ts +41 -0
  306. package/src/commands/game/reduceResources.ts +35 -0
  307. package/src/commands/game/research.ts +80 -0
  308. package/src/commands/game/resources.ts +49 -0
  309. package/src/commands/game/scan.ts +12 -0
  310. package/src/commands/game/search.ts +44 -0
  311. package/src/commands/game/settings.ts +121 -0
  312. package/src/commands/game/shipyard.ts +61 -0
  313. package/src/commands/game/shops.ts +89 -0
  314. package/src/commands/game/statistics.ts +22 -0
  315. package/src/commands/game/store.ts +12 -0
  316. package/src/commands/game/techtree.ts +16 -0
  317. package/src/commands/game/ticket.ts +44 -0
  318. package/src/commands/game/trader.ts +51 -0
  319. package/src/commands/game/workflows/fleet.ts +12 -0
  320. package/src/commands/game/workflows/snapshot.ts +84 -0
  321. package/src/commands/game/workflows/social.ts +37 -0
  322. package/src/commands/http/get.ts +13 -0
  323. package/src/commands/http/post.ts +44 -0
  324. package/src/commands/public/banList.ts +21 -0
  325. package/src/commands/public/battleHall.ts +20 -0
  326. package/src/commands/public/battleReport.ts +20 -0
  327. package/src/commands/public/board.ts +20 -0
  328. package/src/commands/public/disclaimer.ts +20 -0
  329. package/src/commands/public/externalAuth.ts +17 -0
  330. package/src/commands/public/index.ts +22 -0
  331. package/src/commands/public/login.ts +20 -0
  332. package/src/commands/public/lostPassword.ts +57 -0
  333. package/src/commands/public/news.ts +20 -0
  334. package/src/commands/public/register.ts +46 -0
  335. package/src/commands/public/rules.ts +20 -0
  336. package/src/commands/public/screens.ts +20 -0
  337. package/src/commands/public/verify.ts +38 -0
  338. package/src/commands/rules/new-star.ts +221 -0
  339. package/src/commands/rules/production.ts +51 -0
  340. package/src/completion/BifrostCompletion.psm1 +47 -0
  341. package/src/completion.ts +445 -0
  342. package/src/connection.ts +106 -0
  343. package/src/destination.ts +64 -0
  344. package/src/game.ts +978 -0
  345. package/src/help.ts +203 -0
  346. package/src/i18n.ts +45 -0
  347. package/src/index.ts +2 -0
  348. package/src/input/admin/accountEditor.ts +156 -0
  349. package/src/input/admin/ban.ts +23 -0
  350. package/src/input/admin/create.ts +36 -0
  351. package/src/input/admin/cronjob.ts +22 -0
  352. package/src/input/admin/fields.ts +432 -0
  353. package/src/input/admin/login.ts +9 -0
  354. package/src/input/admin/messageList.ts +21 -0
  355. package/src/input/admin/news.ts +29 -0
  356. package/src/input/admin/quickEditor.ts +115 -0
  357. package/src/input/admin/records.ts +68 -0
  358. package/src/input/admin/reset.ts +29 -0
  359. package/src/input/admin/search.ts +23 -0
  360. package/src/input/admin/sendMessages.ts +21 -0
  361. package/src/input/admin/universeSettings.ts +46 -0
  362. package/src/input/files.ts +65 -0
  363. package/src/input/game/allianceAdmin.ts +55 -0
  364. package/src/input/game/battleSimulator.ts +25 -0
  365. package/src/input/game/fleetStep1.ts +14 -0
  366. package/src/input/game/settings.ts +46 -0
  367. package/src/input/game/ticket.ts +16 -0
  368. package/src/input/parse.ts +173 -0
  369. package/src/input/paths.ts +392 -0
  370. package/src/input/protocol/context.ts +10 -0
  371. package/src/input/protocol/missions.ts +6 -0
  372. package/src/input/protocol/routes.ts +15 -0
  373. package/src/input/protocol/settings.ts +126 -0
  374. package/src/input/protocol/values.ts +13 -0
  375. package/src/input/public/externalAuth.ts +26 -0
  376. package/src/input/public/register.ts +9 -0
  377. package/src/input/rules/new-star.ts +21 -0
  378. package/src/input/rules/production.ts +14 -0
  379. package/src/input/shared.ts +51 -0
  380. package/src/input/spec.ts +155 -0
  381. package/src/input/values.ts +114 -0
  382. package/src/input/workflows/fleet.ts +22 -0
  383. package/src/input/workflows/snapshot.ts +74 -0
  384. package/src/locales/en-US/commands.ts +377 -0
  385. package/src/locales/en-US/diagnostics.ts +89 -0
  386. package/src/locales/en-US.ts +61 -0
  387. package/src/locales/zh-CN/commands.ts +307 -0
  388. package/src/locales/zh-CN/diagnostics.ts +81 -0
  389. package/src/locales/zh-CN.ts +57 -0
  390. package/src/options.ts +48 -0
  391. package/src/output.ts +201 -0
  392. package/src/pointer.ts +46 -0
  393. package/src/presentation.ts +96 -0
  394. package/src/public.ts +99 -0
  395. package/src/registry.ts +67 -0
  396. package/src/requests.ts +13 -0
  397. package/src/result.ts +27 -0
  398. package/src/rules.ts +77 -0
  399. package/src/runner.ts +214 -0
  400. package/src/selection.ts +44 -0
  401. package/src/terminal.ts +27 -0
  402. package/src/types.ts +110 -0
  403. package/src/version.ts +11 -0
@@ -0,0 +1,445 @@
1
+ import { fileURLToPath } from 'node:url'
2
+
3
+ import { flagValue } from './arguments.ts'
4
+ import type { CommandEntry } from './catalog.ts'
5
+ import { LOCALE_MODES, messages } from './i18n.ts'
6
+ import {
7
+ assignOptions,
8
+ assignPath,
9
+ inputChoices,
10
+ inputOptions,
11
+ member,
12
+ PATH_SLOTS,
13
+ resolvePath,
14
+ } from './input/paths.ts'
15
+ import type { InputOption } from './input/paths.ts'
16
+ import type { InputNode } from './input/spec.ts'
17
+ import { isObject } from './input/values.ts'
18
+ import {
19
+ BOOLEAN_FLAGS,
20
+ COMPLETION_FLAGS,
21
+ CONNECTION_FLAGS,
22
+ GLOBAL_FLAGS,
23
+ isReservedFlag,
24
+ } from './options.ts'
25
+ import { PRESENTATION_MODES } from './presentation.ts'
26
+ import { commands, findCommand } from './registry.ts'
27
+ import { UsageError } from './result.ts'
28
+ import type { Fields } from './types.ts'
29
+
30
+ function wordsBeforeCursor(words: readonly string[]): {
31
+ positionals: string[]
32
+ options: [string, string][]
33
+ flags: string[]
34
+ pending?: string | undefined
35
+ ended: boolean
36
+ } {
37
+ const positionals: string[] = []
38
+ const options: [string, string][] = []
39
+ const flags: string[] = []
40
+ let pending: string | undefined
41
+ let ended = false
42
+ for (const word of words) {
43
+ if (ended) positionals.push(word)
44
+ else if (pending !== undefined) {
45
+ options.push([pending, word])
46
+ pending = undefined
47
+ } else if (word === '--') ended = true
48
+ else if (word.startsWith('--')) {
49
+ const separator = word.indexOf('=')
50
+ if (separator >= 0) options.push([word.slice(2, separator), word.slice(separator + 1)])
51
+ else if (![...BOOLEAN_FLAGS, ...COMPLETION_FLAGS].includes(word.slice(2)))
52
+ pending = word.slice(2)
53
+ else flags.push(word.slice(2))
54
+ } else positionals.push(word)
55
+ }
56
+ return { positionals, options, flags, pending, ended }
57
+ }
58
+
59
+ function inlineValue(text: string): unknown {
60
+ try {
61
+ return JSON.parse(text)
62
+ } catch {
63
+ return text
64
+ }
65
+ }
66
+
67
+ function namedValue(schema: InputNode, text: string): unknown {
68
+ const value = text.startsWith('@@') ? text.slice(1) : text
69
+ try {
70
+ return flagValue(schema, value)
71
+ } catch {
72
+ return value
73
+ }
74
+ }
75
+
76
+ async function lookup(command: string): Promise<InputNode | undefined> {
77
+ const selected = await findCommand(command)?.load()
78
+ return selected?.extension ? selected.input : undefined
79
+ }
80
+
81
+ async function suppliedInput(
82
+ schema: InputNode,
83
+ options: readonly [string, string][],
84
+ ): Promise<Fields> {
85
+ let input: Fields = {}
86
+ for (const [name, text] of options) {
87
+ if (name !== 'input') continue
88
+ const value = inlineValue(text)
89
+ if (isObject(value)) input = value
90
+ }
91
+ const named = options
92
+ .filter(
93
+ ([name, text]) =>
94
+ !GLOBAL_FLAGS.includes(name) && (!text.startsWith('@') || text.startsWith('@@')),
95
+ )
96
+ .map(([name, text]) => ({ name, text }))
97
+ return assignOptions(named, input, {
98
+ async resolve(name, current) {
99
+ try {
100
+ return await resolvePath(schema, name, current, lookup)
101
+ } catch {
102
+ return undefined
103
+ }
104
+ },
105
+ unresolved() {},
106
+ assign(current, entry, path) {
107
+ try {
108
+ return assignPath(current, path, namedValue(path.schema, entry.text))
109
+ } catch {
110
+ /**
111
+ * Incomplete values do not hide completions for other fields.
112
+ *
113
+ * 尚未输入完整的值不影响其他字段的补全。
114
+ */
115
+ return current
116
+ }
117
+ },
118
+ })
119
+ }
120
+
121
+ function concreteOption(template: string, prefix: string): string {
122
+ const supplied = prefix.split('.')
123
+ const parts: string[] = []
124
+ for (const [index, part] of template.split('.').entries()) {
125
+ if (PATH_SLOTS.includes(part)) {
126
+ const value = supplied[index]
127
+ if (value === undefined || value === '') break
128
+ parts.push(value)
129
+ } else parts.push(part)
130
+ }
131
+ return parts.join('.')
132
+ }
133
+
134
+ /**
135
+ * Include input fields of each selected snapshot extension in the available options.
136
+ *
137
+ * 将已选快照扩展的输入字段纳入可用选项。
138
+ */
139
+ async function declaredOptions(
140
+ schema: InputNode,
141
+ prefix: string,
142
+ input: Fields,
143
+ ): Promise<string[]> {
144
+ const candidates: string[] = []
145
+ const options: InputOption[] = inputOptions(schema)
146
+ for (const option of options) {
147
+ const name = concreteOption(option.name, prefix)
148
+ const path = await resolvePath(schema, name, input, lookup)
149
+ if (path === undefined) continue
150
+ if (option.schema.reference === 'command') {
151
+ const parent = path.steps
152
+ .slice(0, -1)
153
+ .reduce<unknown>((value, step) => member(value, step.key), input)
154
+ const target =
155
+ isObject(parent) && typeof parent.command === 'string'
156
+ ? await lookup(parent.command)
157
+ : undefined
158
+ if (target !== undefined) options.push(...inputOptions(target, name))
159
+ }
160
+ if (name.startsWith(prefix)) candidates.push('--' + name)
161
+ }
162
+ return candidates
163
+ }
164
+
165
+ async function choices(
166
+ name: string,
167
+ entry: CommandEntry | undefined,
168
+ options: readonly [string, string][],
169
+ ): Promise<string[]> {
170
+ if (name === 'format') return ['json', 'text']
171
+ if (name === 'color' || name === 'links') return [...PRESENTATION_MODES]
172
+ if (name === 'locale') return [...LOCALE_MODES]
173
+ if (entry === undefined || isReservedFlag(name)) return []
174
+ const command = await entry.load()
175
+ const input = await suppliedInput(command.input, options)
176
+ const path = await resolvePath(command.input, name, input, lookup)
177
+ return path === undefined ? [] : inputChoices(path.schema)
178
+ }
179
+
180
+ /**
181
+ * Completion reads only command metadata and supplied words; it never executes a business command.
182
+ *
183
+ * 补全仅读取命令描述及已输入参数,不执行任何业务命令。
184
+ */
185
+ export async function completionCandidates(words: readonly string[]): Promise<string[]> {
186
+ try {
187
+ const prefix = words.at(-1) ?? ''
188
+ const context = wordsBeforeCursor(words.slice(0, -1))
189
+ const commandName = context.positionals.join(' ')
190
+ const mode = context.flags.includes('help')
191
+ ? 'help'
192
+ : context.flags.includes('version')
193
+ ? 'version'
194
+ : context.positionals[0] === 'completion'
195
+ ? 'script'
196
+ : 'business'
197
+ const entry = mode === 'business' ? findCommand(commandName) : undefined
198
+ if (context.pending !== undefined)
199
+ return (await choices(context.pending, entry, context.options)).filter((value) =>
200
+ value.startsWith(prefix),
201
+ )
202
+ if (!context.ended && prefix.startsWith('--') && prefix.includes('=')) {
203
+ const separator = prefix.indexOf('=')
204
+ const name = prefix.slice(2, separator)
205
+ return (await choices(name, entry, context.options))
206
+ .filter((value) => value.startsWith(prefix.slice(separator + 1)))
207
+ .map((value) => prefix.slice(0, separator + 1) + value)
208
+ }
209
+ const paths = [
210
+ ...commands.map(({ name }) => name),
211
+ ...COMPLETION_SHELLS.map((shell) => 'completion ' + shell),
212
+ ]
213
+ const candidates: string[] = []
214
+ if (!prefix.startsWith('-'))
215
+ for (const path of paths) {
216
+ const parts = path.split(' ')
217
+ if (context.positionals.every((word, index) => parts[index] === word)) {
218
+ const next = parts[context.positionals.length]
219
+ if (next !== undefined && next.startsWith(prefix)) candidates.push(next)
220
+ }
221
+ }
222
+ if (!context.ended && (prefix === '' || prefix.startsWith('--'))) {
223
+ const matching = commands.filter(
224
+ (candidate) =>
225
+ candidate.name === commandName || candidate.name.startsWith(commandName + ' '),
226
+ )
227
+ const offline =
228
+ mode !== 'business' ||
229
+ (matching.length > 0 && matching.every((candidate) => candidate.domain === 'rules'))
230
+ const textOutput = mode === 'help' || mode === 'script'
231
+ const flags = [...BOOLEAN_FLAGS, ...GLOBAL_FLAGS].filter(
232
+ (flag) =>
233
+ (mode === 'business' || flag !== 'input') &&
234
+ (!textOutput || flag !== 'select') &&
235
+ (!offline ||
236
+ (!Object.hasOwn(CONNECTION_FLAGS, flag) &&
237
+ flag !== 'session' &&
238
+ flag !== 'auto-login' &&
239
+ flag !== 'no-auto-login')),
240
+ )
241
+ candidates.push(...flags.map((flag) => '--' + flag))
242
+ if (mode === 'script') candidates.push(...COMPLETION_FLAGS.map((name) => '--' + name))
243
+ if (entry !== undefined) {
244
+ const command = await entry.load()
245
+ const input = await suppliedInput(command.input, context.options)
246
+ candidates.push(...(await declaredOptions(command.input, prefix.slice(2), input)))
247
+ }
248
+ }
249
+ const used = new Set([...context.flags, ...context.options.map(([name]) => name)])
250
+ if (used.has('auto-login') || used.has('no-auto-login')) {
251
+ used.add('auto-login')
252
+ used.add('no-auto-login')
253
+ }
254
+ return [
255
+ ...new Set(
256
+ candidates.filter(
257
+ (value) =>
258
+ value.startsWith(prefix) && (!value.startsWith('--') || !used.has(value.slice(2))),
259
+ ),
260
+ ),
261
+ ].toSorted()
262
+ } catch {
263
+ return []
264
+ }
265
+ }
266
+
267
+ type Shell = 'bash' | 'zsh' | 'fish' | 'powershell'
268
+
269
+ const POWERSHELL_MODULE = fileURLToPath(
270
+ new URL('../src/completion/BifrostCompletion.psm1', import.meta.url),
271
+ )
272
+
273
+ function shellLiteral(value: string): string {
274
+ return "'" + value.replaceAll("'", "'\\''") + "'"
275
+ }
276
+
277
+ function powershellLiteral(value: string): string {
278
+ return "'" + value.replaceAll("'", "''") + "'"
279
+ }
280
+
281
+ const SCRIPTS: Readonly<Record<Shell, string>> = {
282
+ /**
283
+ * Bash splits punctuation into words; source gaps distinguish fragments from separate values.
284
+ *
285
+ * Bash 将标点拆成词;原始行中的间隔用于区分相邻片段与独立参数值。
286
+ */
287
+ bash: [
288
+ '_bifrost_complete() {',
289
+ ' local candidate word gap retained head quote index last decoded offset character next',
290
+ ' local line="${COMP_LINE:0:COMP_POINT}"',
291
+ ' local -a words=()',
292
+ ' for ((index=0; index<=COMP_CWORD; index++)); do',
293
+ ' word="${COMP_WORDS[index]}"',
294
+ ' if ((index == COMP_CWORD)); then word="$2"; fi',
295
+ ' if [[ -z "$word" ]]; then gap="$line"; line=;',
296
+ ' else gap="${line%%"$word"*}"; line="${line#*"$word"}"; fi',
297
+ ' last=$((${#words[@]} - 1))',
298
+ ' if ((last >= 0)) && [[ -z "$gap" || "$gap" == \\\' || "$gap" == \\" ]]; then words[last]+="$gap$word";',
299
+ ' else words+=("$word"); fi',
300
+ ' done',
301
+ ' last=$((${#words[@]} - 1))',
302
+ ' retained="${words[last]%"$2"}"',
303
+ ' if [[ "$retained" == *\\\' || "$retained" == *\\" ]]; then retained="${retained:0:${#retained}-1}"; fi',
304
+ ' for index in "${!words[@]}"; do',
305
+ ' word="${words[index]}"',
306
+ ' head=',
307
+ ' if [[ "$word" == --*=* ]]; then head="${word%%=*}="; word="${word#*=}"; fi',
308
+ ' if [[ "$word" == \\\'* || "$word" == \\"* ]]; then',
309
+ ' quote="${word:0:1}"; word="${word:1}"',
310
+ ' if [[ "$word" == *"$quote" ]]; then word="${word:0:${#word}-1}"; fi',
311
+ ' if [[ "$quote" == \\" ]]; then',
312
+ ' decoded=',
313
+ ' for ((offset=0; offset<${#word}; offset++)); do',
314
+ ' character="${word:offset:1}"',
315
+ ' if [[ "$character" == \\\\ ]]; then',
316
+ ' next="${word:offset+1:1}"',
317
+ " case \"$next\" in '\\'|'\"'|'$'|'`') character=\"$next\"; ((offset++));;",
318
+ " $'\\n') ((offset++)); continue;;",
319
+ ' esac',
320
+ ' fi',
321
+ ' decoded+="$character"',
322
+ ' done',
323
+ ' word="$decoded"',
324
+ ' fi',
325
+ ' else',
326
+ ' word="${word//\\\\ / }"',
327
+ ' fi',
328
+ ' words[index]="$head$word"',
329
+ ' done',
330
+ ' COMPREPLY=()',
331
+ ' while IFS= read -r candidate; do',
332
+ ' COMPREPLY+=("${candidate#"$retained"}")',
333
+ ' done < <(command bifrost __complete -- "${words[@]:1}" 2>/dev/null)',
334
+ '}',
335
+ 'complete -F _bifrost_complete bifrost',
336
+ '',
337
+ ].join('\n'),
338
+ zsh: [
339
+ '#compdef bifrost',
340
+ '_bifrost_complete() {',
341
+ ' local -a candidates',
342
+ ' candidates=("${(@f)$(bifrost __complete -- "${(@Q)words[@]:1:$((CURRENT - 2))}" "${(Q)PREFIX}" 2>/dev/null)}")',
343
+ ' compadd -- "${candidates[@]}"',
344
+ '}',
345
+ 'compdef _bifrost_complete bifrost',
346
+ '',
347
+ ].join('\n'),
348
+ fish: [
349
+ 'function __bifrost_complete',
350
+ /**
351
+ * Tokenize without expansion so completion never evaluates variables or command substitutions.
352
+ *
353
+ * 仅分词、不展开,使补全不会求值变量或命令替换。
354
+ */
355
+ ' set -l words (commandline --tokenize --cut-at-cursor --current-process)',
356
+ ' set -l current (commandline -ct)',
357
+ ' set current (commandline --tokenize --input "$current")',
358
+ /**
359
+ * Tokenizing an empty token yields no elements; restore it as one empty argument because the
360
+ * runner reads the last argument as the word being completed.
361
+ *
362
+ * 对空词分词不会产生元素;执行器把最后一个实参当作正在补全的词,因此须补回一个空实参。
363
+ */
364
+ " test (count $current) -gt 0; or set current ''",
365
+ ' command bifrost __complete -- $words[2..-1] "$current" 2>/dev/null',
366
+ 'end',
367
+ "complete -c bifrost -f -a '(__bifrost_complete)'",
368
+ '',
369
+ ].join('\n'),
370
+ powershell: `Import-Module -Name ${powershellLiteral(POWERSHELL_MODULE)} -ErrorAction Stop\n`,
371
+ }
372
+
373
+ export const COMPLETION_SHELLS: readonly string[] = Object.keys(SCRIPTS)
374
+
375
+ /**
376
+ * Shell diagnostics use the generating invocation's locale and remain quoted script literals.
377
+ *
378
+ * Shell 诊断使用生成脚本时的调用语言,并按脚本文本规则转义。
379
+ */
380
+ function enhancements(): Readonly<Record<Shell, string>> {
381
+ const diagnostic = messages().diagnostics
382
+ return {
383
+ /**
384
+ * ble.sh uses its syntax source for programmable-completion predictions.
385
+ *
386
+ * ble.sh 通过 syntax 来源生成基于命令补全的预测。
387
+ */
388
+ bash: [
389
+ 'if [[ $- == *i* && -t 1 ]]; then',
390
+ ' if (( BASH_VERSINFO[0] < 4 )) || [[ ! ${BLE_VERSION-} ]] || ! declare -F bleopt >/dev/null ||',
391
+ ' ! bleopt complete_auto_complete complete_auto_complete_opts >/dev/null ||',
392
+ ' ! bleopt complete_auto_complete=1 complete_auto_complete_opts-=syntax-disabled; then',
393
+ ` printf '%s\\n' ${shellLiteral(diagnostic.bleRequired)} >&2`,
394
+ ' fi',
395
+ 'fi',
396
+ '',
397
+ ].join('\n'),
398
+ zsh: [
399
+ 'if [[ -o interactive && -t 1 ]]; then',
400
+ ' if (( ! $+functions[_zsh_autosuggest_start] )); then',
401
+ ` print -ru2 -- ${shellLiteral(diagnostic.zshAutosuggest)}`,
402
+ ' else',
403
+ ' (( ${ZSH_AUTOSUGGEST_STRATEGY[(Ie)completion]} )) || ZSH_AUTOSUGGEST_STRATEGY+=(completion)',
404
+ ' fi',
405
+ 'fi',
406
+ '',
407
+ ].join('\n'),
408
+ fish: [
409
+ 'if status is-interactive; and isatty stdout',
410
+ ' set -g fish_autosuggestion_enabled 1',
411
+ 'end',
412
+ '',
413
+ ].join('\n'),
414
+ powershell: [
415
+ "if ($Host.Name -eq 'ConsoleHost' -and -not [Console]::IsOutputRedirected) {",
416
+ " if (-not (Get-Module -Name PSReadLine) -or (Get-Module -Name PSReadLine).Version -lt [version]'2.2.2') {",
417
+ ` [Console]::Error.WriteLine(${powershellLiteral(diagnostic.psReadLine)})`,
418
+ ' } elseif (-not (Get-Module -ListAvailable -Name CompletionPredictor)) {',
419
+ ` [Console]::Error.WriteLine(${powershellLiteral(diagnostic.psPredictor)})`,
420
+ ' } else {',
421
+ ' try {',
422
+ ' Import-Module -Name CompletionPredictor -ErrorAction Stop',
423
+ ' switch ((Get-PSReadLineOption).PredictionSource) {',
424
+ " 'None' { Set-PSReadLineOption -PredictionSource Plugin }",
425
+ " 'History' { Set-PSReadLineOption -PredictionSource HistoryAndPlugin }",
426
+ ' }',
427
+ ' } catch {',
428
+ ` [Console]::Error.WriteLine((${powershellLiteral(diagnostic.psEnableFailed('{0}'))} -f $_.Exception.Message))`,
429
+ ' }',
430
+ ' }',
431
+ '}',
432
+ '',
433
+ ].join('\n'),
434
+ }
435
+ }
436
+
437
+ export function isCompletionShell(shell: string): shell is Shell {
438
+ return Object.hasOwn(SCRIPTS, shell)
439
+ }
440
+
441
+ export function completionScript(shell: string, enhance = false): string {
442
+ if (!isCompletionShell(shell))
443
+ throw new UsageError(messages().diagnostics.shellChoice(COMPLETION_SHELLS))
444
+ return SCRIPTS[shell] + (enhance ? enhancements()[shell] : '')
445
+ }
@@ -0,0 +1,106 @@
1
+ import type { Fetch, Session } from '@senators/bifrost'
2
+
3
+ import { stringOption } from './arguments.ts'
4
+ import type { Invocation } from './arguments.ts'
5
+ import { messages } from './i18n.ts'
6
+ import type { InputSource } from './input/files.ts'
7
+ import { readArgument } from './input/files.ts'
8
+ import { checkInput } from './input/parse.ts'
9
+ import { enumeration, numberInput, optional, required, stringInput } from './input/spec.ts'
10
+ import type { InputSpec } from './input/spec.ts'
11
+ import { checkNames, isObject } from './input/values.ts'
12
+ import { CONNECTION_FLAGS } from './options.ts'
13
+ import { UsageError } from './result.ts'
14
+ import type { Fields, SessionInfo, SessionInput } from './types.ts'
15
+
16
+ const manualSessionInput = {
17
+ kind: 'object',
18
+ fields: {
19
+ baseUrl: required(stringInput),
20
+ universe: required(numberInput),
21
+ username: optional(stringInput),
22
+ password: optional(stringInput),
23
+ sessionToken: optional(stringInput),
24
+ language: optional(stringInput),
25
+ forwardedIp: optional(stringInput),
26
+ autoLogin: optional(enumeration([false])),
27
+ },
28
+ } satisfies InputSpec<SessionInput>
29
+
30
+ const automaticSessionInput = {
31
+ kind: 'object',
32
+ fields: {
33
+ ...manualSessionInput.fields,
34
+ username: required(stringInput),
35
+ password: required(stringInput),
36
+ autoLogin: required(enumeration([true])),
37
+ },
38
+ } satisfies InputSpec<SessionInput>
39
+
40
+ function missingConnection(names: readonly string[]): UsageError {
41
+ const flags = Object.entries(CONNECTION_FLAGS)
42
+ .filter(([, name]) => names.includes(name))
43
+ .map(([flag]) => `--${flag}`)
44
+ return new UsageError(messages().diagnostics.missingConnection(names, flags))
45
+ }
46
+
47
+ /**
48
+ * Create one in-memory SDK session from explicit connection input.
49
+ *
50
+ * 根据显式连接输入创建本次调用的内存 SDK 会话。
51
+ */
52
+ export async function connect(
53
+ invocation: Invocation,
54
+ read: InputSource,
55
+ fetch?: Fetch,
56
+ ): Promise<{ session: Session; input: SessionInput }> {
57
+ const document = stringOption(invocation.values, 'session')
58
+ let fields: Fields = {}
59
+ if (document !== undefined) {
60
+ const value = await readArgument(document, 'session', read)
61
+ if (!isObject(value)) throw new UsageError(messages().diagnostics.sessionObject)
62
+ fields = { ...value }
63
+ }
64
+ for (const [flag, name] of Object.entries(CONNECTION_FLAGS)) {
65
+ const supplied = stringOption(invocation.values, flag)
66
+ if (supplied === undefined) continue
67
+ if (supplied.startsWith('@') && !supplied.startsWith('@@')) fields[name] = await read(supplied)
68
+ else {
69
+ const value = supplied.startsWith('@@') ? supplied.slice(1) : supplied
70
+ if (name === 'universe' && value.trim() === '')
71
+ throw new UsageError(messages().diagnostics.invalidParameter('session.universe'))
72
+ fields[name] = name === 'universe' ? Number(value) : value
73
+ }
74
+ }
75
+ if (typeof invocation.values['auto-login'] === 'boolean')
76
+ fields.autoLogin = invocation.values['auto-login']
77
+ const sessionInput = fields.autoLogin === true ? automaticSessionInput : manualSessionInput
78
+ const missing = Object.entries(sessionInput.fields)
79
+ .filter(([name, field]) => !field.optional && fields[name] === undefined)
80
+ .map(([name]) => name)
81
+ if (missing.length > 0) {
82
+ checkNames(fields, Object.keys(sessionInput.fields), 'session')
83
+ throw missingConnection(missing)
84
+ }
85
+ checkInput<SessionInput>(sessionInput, fields, 'session', 'session')
86
+ const { Session } = await import('@senators/bifrost')
87
+ return {
88
+ session: new Session({ ...fields, ...(fetch === undefined ? {} : { fetch }) }),
89
+ input: fields,
90
+ }
91
+ }
92
+
93
+ /**
94
+ * Echo only SDK-held connection state; credentials are not inferred from a cookie.
95
+ *
96
+ * 仅返回 SDK 持有的连接状态,不从 Cookie 推断账号凭据。
97
+ */
98
+ export function sessionInfo(session: Session | undefined): SessionInfo | null {
99
+ return session === undefined
100
+ ? null
101
+ : {
102
+ baseUrl: session.baseUrl.href,
103
+ universe: session.universe,
104
+ sessionToken: session.sessionToken,
105
+ }
106
+ }
@@ -0,0 +1,64 @@
1
+ import { randomUUID } from 'node:crypto'
2
+ import { lstat, open, rename, rm } from 'node:fs/promises'
3
+ import { dirname, join, resolve } from 'node:path'
4
+
5
+ import { messages } from './i18n.ts'
6
+ import { UsageError } from './result.ts'
7
+
8
+ export type OutputDestination = {
9
+ write: (text: string) => Promise<void>
10
+ close: () => Promise<void>
11
+ }
12
+
13
+ /**
14
+ * Prepare the destination before execution; publish a complete result by replacing its file.
15
+ *
16
+ * 执行前准备输出位置;文件输出在内容完整写入后替换目标。
17
+ */
18
+ export async function prepareOutput(
19
+ path: string | undefined,
20
+ stdout: (text: string) => void | Promise<void>,
21
+ ): Promise<OutputDestination> {
22
+ if (path === undefined || path === '-')
23
+ return {
24
+ write: async (text) => {
25
+ await stdout(text)
26
+ },
27
+ close: async () => {},
28
+ }
29
+ if (path === '') throw new UsageError(messages().diagnostics.outputFilename)
30
+ const filename = resolve(path)
31
+ const target = await lstat(filename).catch((error: unknown) => {
32
+ if (!(error instanceof Error && 'code' in error && error.code === 'ENOENT')) throw error
33
+ return undefined
34
+ })
35
+ if (target !== undefined && !target.isFile())
36
+ throw new UsageError(messages().diagnostics.outputRegularFile(path))
37
+ const temporary = join(dirname(filename), `.bifrost-output-${randomUUID()}.tmp`)
38
+ const file = await open(temporary, 'wx', 0o600)
39
+ let closed = false
40
+ return {
41
+ write: async (text) => {
42
+ await file.writeFile(text, 'utf8')
43
+ await file.close()
44
+ closed = true
45
+ await rename(temporary, filename)
46
+ },
47
+ close: async () => {
48
+ const errors: unknown[] = []
49
+ if (!closed)
50
+ try {
51
+ await file.close()
52
+ closed = true
53
+ } catch (error) {
54
+ errors.push(error)
55
+ }
56
+ try {
57
+ await rm(temporary, { force: true })
58
+ } catch (error) {
59
+ errors.push(error)
60
+ }
61
+ if (errors.length > 0) throw new AggregateError(errors, messages().diagnostics.outputCleanup)
62
+ },
63
+ }
64
+ }