@beezer-pet/instant-cli 0.0.0-stage → 1.0.67-beezer.3

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 (443) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/LICENSE.md +202 -0
  3. package/README.md +34 -2
  4. package/__tests__/appCommands.test.ts +228 -0
  5. package/__tests__/args.test.ts +358 -0
  6. package/__tests__/auth.test.ts +128 -0
  7. package/__tests__/authClientAddApple.test.ts +376 -0
  8. package/__tests__/authClientAddClerk.test.ts +222 -0
  9. package/__tests__/authClientAddGithub.test.ts +245 -0
  10. package/__tests__/authClientAddGoogle.test.ts +373 -0
  11. package/__tests__/authClientAddLinkedin.test.ts +262 -0
  12. package/__tests__/authClientList.test.ts +90 -0
  13. package/__tests__/authClientUpdate.test.ts +583 -0
  14. package/__tests__/authOriginManagement.test.ts +308 -0
  15. package/__tests__/backupDownload.test.ts +206 -0
  16. package/__tests__/backups.test.ts +221 -0
  17. package/__tests__/config.test.ts +57 -0
  18. package/__tests__/e2e/cli.e2e.test.ts +1267 -0
  19. package/__tests__/e2e/helpers.ts +177 -0
  20. package/__tests__/effectHelpers.ts +45 -0
  21. package/__tests__/mergeSchema.test.ts +197 -0
  22. package/__tests__/multiSelect.test.ts +236 -0
  23. package/__tests__/oauthMock.ts +76 -0
  24. package/__tests__/promptOk.test.ts +44 -0
  25. package/__tests__/redirectUriPrompt.test.ts +40 -0
  26. package/__tests__/sdkInstallSpec.test.ts +36 -0
  27. package/__tests__/select.test.ts +224 -0
  28. package/__tests__/webhooks.test.ts +728 -0
  29. package/bin/index.js +3 -0
  30. package/dist/auth/index.d.ts +4 -0
  31. package/dist/auth/index.d.ts.map +1 -0
  32. package/dist/auth/index.js +103 -0
  33. package/dist/auth/index.js.map +1 -0
  34. package/dist/commands/app/delete.d.ts +7 -0
  35. package/dist/commands/app/delete.d.ts.map +1 -0
  36. package/dist/commands/app/delete.js +57 -0
  37. package/dist/commands/app/delete.js.map +1 -0
  38. package/dist/commands/app/list.d.ts +6 -0
  39. package/dist/commands/app/list.d.ts.map +1 -0
  40. package/dist/commands/app/list.js +29 -0
  41. package/dist/commands/app/list.js.map +1 -0
  42. package/dist/commands/auth/client/add.d.ts +9 -0
  43. package/dist/commands/auth/client/add.d.ts.map +1 -0
  44. package/dist/commands/auth/client/add.js +409 -0
  45. package/dist/commands/auth/client/add.js.map +1 -0
  46. package/dist/commands/auth/client/delete.d.ts +11 -0
  47. package/dist/commands/auth/client/delete.d.ts.map +1 -0
  48. package/dist/commands/auth/client/delete.js +48 -0
  49. package/dist/commands/auth/client/delete.js.map +1 -0
  50. package/dist/commands/auth/client/list.d.ts +6 -0
  51. package/dist/commands/auth/client/list.d.ts.map +1 -0
  52. package/dist/commands/auth/client/list.js +38 -0
  53. package/dist/commands/auth/client/list.js.map +1 -0
  54. package/dist/commands/auth/client/shared.d.ts +70 -0
  55. package/dist/commands/auth/client/shared.d.ts.map +1 -0
  56. package/dist/commands/auth/client/shared.js +140 -0
  57. package/dist/commands/auth/client/shared.js.map +1 -0
  58. package/dist/commands/auth/client/update.d.ts +8 -0
  59. package/dist/commands/auth/client/update.d.ts.map +1 -0
  60. package/dist/commands/auth/client/update.js +445 -0
  61. package/dist/commands/auth/client/update.js.map +1 -0
  62. package/dist/commands/auth/email/pull.d.ts +24 -0
  63. package/dist/commands/auth/email/pull.d.ts.map +1 -0
  64. package/dist/commands/auth/email/pull.js +100 -0
  65. package/dist/commands/auth/email/pull.js.map +1 -0
  66. package/dist/commands/auth/email/push.d.ts +8 -0
  67. package/dist/commands/auth/email/push.d.ts.map +1 -0
  68. package/dist/commands/auth/email/push.js +58 -0
  69. package/dist/commands/auth/email/push.js.map +1 -0
  70. package/dist/commands/auth/email/resend.d.ts +3 -0
  71. package/dist/commands/auth/email/resend.d.ts.map +1 -0
  72. package/dist/commands/auth/email/resend.js +7 -0
  73. package/dist/commands/auth/email/resend.js.map +1 -0
  74. package/dist/commands/auth/email/reset.d.ts +5 -0
  75. package/dist/commands/auth/email/reset.d.ts.map +1 -0
  76. package/dist/commands/auth/email/reset.js +25 -0
  77. package/dist/commands/auth/email/reset.js.map +1 -0
  78. package/dist/commands/auth/email/status.d.ts +57 -0
  79. package/dist/commands/auth/email/status.d.ts.map +1 -0
  80. package/dist/commands/auth/email/status.js +96 -0
  81. package/dist/commands/auth/email/status.js.map +1 -0
  82. package/dist/commands/auth/email/verify.d.ts +5 -0
  83. package/dist/commands/auth/email/verify.d.ts.map +1 -0
  84. package/dist/commands/auth/email/verify.js +12 -0
  85. package/dist/commands/auth/email/verify.js.map +1 -0
  86. package/dist/commands/auth/origin/add.d.ts +12 -0
  87. package/dist/commands/auth/origin/add.d.ts.map +1 -0
  88. package/dist/commands/auth/origin/add.js +153 -0
  89. package/dist/commands/auth/origin/add.js.map +1 -0
  90. package/dist/commands/auth/origin/delete.d.ts +8 -0
  91. package/dist/commands/auth/origin/delete.d.ts.map +1 -0
  92. package/dist/commands/auth/origin/delete.js +38 -0
  93. package/dist/commands/auth/origin/delete.js.map +1 -0
  94. package/dist/commands/auth/origin/list.d.ts +9 -0
  95. package/dist/commands/auth/origin/list.d.ts.map +1 -0
  96. package/dist/commands/auth/origin/list.js +52 -0
  97. package/dist/commands/auth/origin/list.js.map +1 -0
  98. package/dist/commands/backup/download.d.ts +10 -0
  99. package/dist/commands/backup/download.d.ts.map +1 -0
  100. package/dist/commands/backup/download.js +174 -0
  101. package/dist/commands/backup/download.js.map +1 -0
  102. package/dist/commands/backup/list.d.ts +11 -0
  103. package/dist/commands/backup/list.d.ts.map +1 -0
  104. package/dist/commands/backup/list.js +74 -0
  105. package/dist/commands/backup/list.js.map +1 -0
  106. package/dist/commands/claim.d.ts +6 -0
  107. package/dist/commands/claim.d.ts.map +1 -0
  108. package/dist/commands/claim.js +22 -0
  109. package/dist/commands/claim.js.map +1 -0
  110. package/dist/commands/explorer.d.ts +6 -0
  111. package/dist/commands/explorer.d.ts.map +1 -0
  112. package/dist/commands/explorer.js +13 -0
  113. package/dist/commands/explorer.js.map +1 -0
  114. package/dist/commands/info.d.ts +9 -0
  115. package/dist/commands/info.d.ts.map +1 -0
  116. package/dist/commands/info.js +41 -0
  117. package/dist/commands/info.js.map +1 -0
  118. package/dist/commands/init.d.ts +5 -0
  119. package/dist/commands/init.d.ts.map +1 -0
  120. package/dist/commands/init.js +41 -0
  121. package/dist/commands/init.js.map +1 -0
  122. package/dist/commands/initWithoutFiles.d.ts +6 -0
  123. package/dist/commands/initWithoutFiles.d.ts.map +1 -0
  124. package/dist/commands/initWithoutFiles.js +64 -0
  125. package/dist/commands/initWithoutFiles.js.map +1 -0
  126. package/dist/commands/login.d.ts +9 -0
  127. package/dist/commands/login.d.ts.map +1 -0
  128. package/dist/commands/login.js +44 -0
  129. package/dist/commands/login.js.map +1 -0
  130. package/dist/commands/logout.d.ts +3 -0
  131. package/dist/commands/logout.d.ts.map +1 -0
  132. package/dist/commands/logout.js +14 -0
  133. package/dist/commands/logout.js.map +1 -0
  134. package/dist/commands/pull.d.ts +5 -0
  135. package/dist/commands/pull.d.ts.map +1 -0
  136. package/dist/commands/pull.js +15 -0
  137. package/dist/commands/pull.js.map +1 -0
  138. package/dist/commands/push.d.ts +6 -0
  139. package/dist/commands/push.d.ts.map +1 -0
  140. package/dist/commands/push.js +20 -0
  141. package/dist/commands/push.js.map +1 -0
  142. package/dist/commands/query.d.ts +7 -0
  143. package/dist/commands/query.d.ts.map +1 -0
  144. package/dist/commands/query.js +52 -0
  145. package/dist/commands/query.js.map +1 -0
  146. package/dist/commands/webhooks/add.d.ts +9 -0
  147. package/dist/commands/webhooks/add.d.ts.map +1 -0
  148. package/dist/commands/webhooks/add.js +75 -0
  149. package/dist/commands/webhooks/add.js.map +1 -0
  150. package/dist/commands/webhooks/delete.d.ts +6 -0
  151. package/dist/commands/webhooks/delete.d.ts.map +1 -0
  152. package/dist/commands/webhooks/delete.js +17 -0
  153. package/dist/commands/webhooks/delete.js.map +1 -0
  154. package/dist/commands/webhooks/disable.d.ts +7 -0
  155. package/dist/commands/webhooks/disable.d.ts.map +1 -0
  156. package/dist/commands/webhooks/disable.js +18 -0
  157. package/dist/commands/webhooks/disable.js.map +1 -0
  158. package/dist/commands/webhooks/enable.d.ts +6 -0
  159. package/dist/commands/webhooks/enable.d.ts.map +1 -0
  160. package/dist/commands/webhooks/enable.js +18 -0
  161. package/dist/commands/webhooks/enable.js.map +1 -0
  162. package/dist/commands/webhooks/events/list.d.ts +7 -0
  163. package/dist/commands/webhooks/events/list.d.ts.map +1 -0
  164. package/dist/commands/webhooks/events/list.js +31 -0
  165. package/dist/commands/webhooks/events/list.js.map +1 -0
  166. package/dist/commands/webhooks/events/payload.d.ts +8 -0
  167. package/dist/commands/webhooks/events/payload.d.ts.map +1 -0
  168. package/dist/commands/webhooks/events/payload.js +39 -0
  169. package/dist/commands/webhooks/events/payload.js.map +1 -0
  170. package/dist/commands/webhooks/events/resend.d.ts +8 -0
  171. package/dist/commands/webhooks/events/resend.d.ts.map +1 -0
  172. package/dist/commands/webhooks/events/resend.js +43 -0
  173. package/dist/commands/webhooks/events/resend.js.map +1 -0
  174. package/dist/commands/webhooks/list.d.ts +8 -0
  175. package/dist/commands/webhooks/list.d.ts.map +1 -0
  176. package/dist/commands/webhooks/list.js +29 -0
  177. package/dist/commands/webhooks/list.js.map +1 -0
  178. package/dist/commands/webhooks/shared.d.ts +40 -0
  179. package/dist/commands/webhooks/shared.d.ts.map +1 -0
  180. package/dist/commands/webhooks/shared.js +248 -0
  181. package/dist/commands/webhooks/shared.js.map +1 -0
  182. package/dist/commands/webhooks/update.d.ts +10 -0
  183. package/dist/commands/webhooks/update.d.ts.map +1 -0
  184. package/dist/commands/webhooks/update.js +189 -0
  185. package/dist/commands/webhooks/update.js.map +1 -0
  186. package/dist/context/authToken.d.ts +29 -0
  187. package/dist/context/authToken.d.ts.map +1 -0
  188. package/dist/context/authToken.js +73 -0
  189. package/dist/context/authToken.js.map +1 -0
  190. package/dist/context/currentApp.d.ts +38 -0
  191. package/dist/context/currentApp.d.ts.map +1 -0
  192. package/dist/context/currentApp.js +222 -0
  193. package/dist/context/currentApp.js.map +1 -0
  194. package/dist/context/globalOpts.d.ts +11 -0
  195. package/dist/context/globalOpts.d.ts.map +1 -0
  196. package/dist/context/globalOpts.js +13 -0
  197. package/dist/context/globalOpts.js.map +1 -0
  198. package/dist/context/platformApi.d.ts +19 -0
  199. package/dist/context/platformApi.d.ts.map +1 -0
  200. package/dist/context/platformApi.js +24 -0
  201. package/dist/context/platformApi.js.map +1 -0
  202. package/dist/context/projectInfo.d.ts +30 -0
  203. package/dist/context/projectInfo.d.ts.map +1 -0
  204. package/dist/context/projectInfo.js +153 -0
  205. package/dist/context/projectInfo.js.map +1 -0
  206. package/dist/errors.d.ts +10 -0
  207. package/dist/errors.d.ts.map +1 -0
  208. package/dist/errors.js +6 -0
  209. package/dist/errors.js.map +1 -0
  210. package/dist/index.d.ts +159 -0
  211. package/dist/index.d.ts.map +1 -0
  212. package/dist/index.js +714 -0
  213. package/dist/index.js.map +1 -0
  214. package/dist/layer.d.ts +25 -0
  215. package/dist/layer.d.ts.map +1 -0
  216. package/dist/layer.js +119 -0
  217. package/dist/layer.js.map +1 -0
  218. package/dist/lib/args.d.ts +212 -0
  219. package/dist/lib/args.d.ts.map +1 -0
  220. package/dist/lib/args.js +313 -0
  221. package/dist/lib/args.js.map +1 -0
  222. package/dist/lib/backupDownload.d.ts +21 -0
  223. package/dist/lib/backupDownload.d.ts.map +1 -0
  224. package/dist/lib/backupDownload.js +151 -0
  225. package/dist/lib/backupDownload.js.map +1 -0
  226. package/dist/lib/backups.d.ts +12 -0
  227. package/dist/lib/backups.d.ts.map +1 -0
  228. package/dist/lib/backups.js +26 -0
  229. package/dist/lib/backups.js.map +1 -0
  230. package/dist/lib/config.d.ts +6 -0
  231. package/dist/lib/config.d.ts.map +1 -0
  232. package/dist/lib/config.js +39 -0
  233. package/dist/lib/config.js.map +1 -0
  234. package/dist/lib/createApp.d.ts +12 -0
  235. package/dist/lib/createApp.d.ts.map +1 -0
  236. package/dist/lib/createApp.js +15 -0
  237. package/dist/lib/createApp.js.map +1 -0
  238. package/dist/lib/email.d.ts +47 -0
  239. package/dist/lib/email.d.ts.map +1 -0
  240. package/dist/lib/email.js +71 -0
  241. package/dist/lib/email.js.map +1 -0
  242. package/dist/lib/handleEnv.d.ts +7 -0
  243. package/dist/lib/handleEnv.d.ts.map +1 -0
  244. package/dist/lib/handleEnv.js +99 -0
  245. package/dist/lib/handleEnv.js.map +1 -0
  246. package/dist/lib/http.d.ts +30 -0
  247. package/dist/lib/http.d.ts.map +1 -0
  248. package/dist/lib/http.js +55 -0
  249. package/dist/lib/http.js.map +1 -0
  250. package/dist/lib/login.d.ts +12 -0
  251. package/dist/lib/login.d.ts.map +1 -0
  252. package/dist/lib/login.js +38 -0
  253. package/dist/lib/login.js.map +1 -0
  254. package/dist/lib/oauth.d.ts +306 -0
  255. package/dist/lib/oauth.d.ts.map +1 -0
  256. package/dist/lib/oauth.js +195 -0
  257. package/dist/lib/oauth.js.map +1 -0
  258. package/dist/lib/platformApi.d.ts +5 -0
  259. package/dist/lib/platformApi.d.ts.map +1 -0
  260. package/dist/lib/platformApi.js +11 -0
  261. package/dist/lib/platformApi.js.map +1 -0
  262. package/dist/lib/pullPerms.d.ts +7 -0
  263. package/dist/lib/pullPerms.d.ts.map +1 -0
  264. package/dist/lib/pullPerms.js +41 -0
  265. package/dist/lib/pullPerms.js.map +1 -0
  266. package/dist/lib/pullSchema.d.ts +12 -0
  267. package/dist/lib/pullSchema.d.ts.map +1 -0
  268. package/dist/lib/pullSchema.js +62 -0
  269. package/dist/lib/pullSchema.js.map +1 -0
  270. package/dist/lib/pushPerms.d.ts +13 -0
  271. package/dist/lib/pushPerms.d.ts.map +1 -0
  272. package/dist/lib/pushPerms.js +54 -0
  273. package/dist/lib/pushPerms.js.map +1 -0
  274. package/dist/lib/pushSchema.d.ts +53 -0
  275. package/dist/lib/pushSchema.d.ts.map +1 -0
  276. package/dist/lib/pushSchema.js +165 -0
  277. package/dist/lib/pushSchema.js.map +1 -0
  278. package/dist/lib/ui.d.ts +18 -0
  279. package/dist/lib/ui.d.ts.map +1 -0
  280. package/dist/lib/ui.js +32 -0
  281. package/dist/lib/ui.js.map +1 -0
  282. package/dist/lib/webhooks.d.ts +27 -0
  283. package/dist/lib/webhooks.d.ts.map +1 -0
  284. package/dist/lib/webhooks.js +95 -0
  285. package/dist/lib/webhooks.js.map +1 -0
  286. package/dist/logging.d.ts +5 -0
  287. package/dist/logging.d.ts.map +1 -0
  288. package/dist/logging.js +25 -0
  289. package/dist/logging.js.map +1 -0
  290. package/dist/old.d.ts +17 -0
  291. package/dist/old.d.ts.map +1 -0
  292. package/dist/old.js +410 -0
  293. package/dist/old.js.map +1 -0
  294. package/dist/program.d.ts +3 -0
  295. package/dist/program.d.ts.map +1 -0
  296. package/dist/program.js +3 -0
  297. package/dist/program.js.map +1 -0
  298. package/dist/renderSchemaPlan.d.ts +19 -0
  299. package/dist/renderSchemaPlan.d.ts.map +1 -0
  300. package/dist/renderSchemaPlan.js +220 -0
  301. package/dist/renderSchemaPlan.js.map +1 -0
  302. package/dist/ui/index.d.ts +292 -0
  303. package/dist/ui/index.d.ts.map +1 -0
  304. package/dist/ui/index.js +1296 -0
  305. package/dist/ui/index.js.map +1 -0
  306. package/dist/ui/lib.d.ts +124 -0
  307. package/dist/ui/lib.d.ts.map +1 -0
  308. package/dist/ui/lib.js +333 -0
  309. package/dist/ui/lib.js.map +1 -0
  310. package/dist/util/findConfigCandidates.d.ts +13 -0
  311. package/dist/util/findConfigCandidates.d.ts.map +1 -0
  312. package/dist/util/findConfigCandidates.js +211 -0
  313. package/dist/util/findConfigCandidates.js.map +1 -0
  314. package/dist/util/fs.d.ts +4 -0
  315. package/dist/util/fs.d.ts.map +1 -0
  316. package/dist/util/fs.js +22 -0
  317. package/dist/util/fs.js.map +1 -0
  318. package/dist/util/getAppName.d.ts +5 -0
  319. package/dist/util/getAppName.d.ts.map +1 -0
  320. package/dist/util/getAppName.js +14 -0
  321. package/dist/util/getAppName.js.map +1 -0
  322. package/dist/util/instantConfig.d.ts +9 -0
  323. package/dist/util/instantConfig.d.ts.map +1 -0
  324. package/dist/util/instantConfig.js +13 -0
  325. package/dist/util/instantConfig.js.map +1 -0
  326. package/dist/util/isHeadlessEnvironment.d.ts +4 -0
  327. package/dist/util/isHeadlessEnvironment.d.ts.map +1 -0
  328. package/dist/util/isHeadlessEnvironment.js +11 -0
  329. package/dist/util/isHeadlessEnvironment.js.map +1 -0
  330. package/dist/util/loadConfig.d.ts +3 -0
  331. package/dist/util/loadConfig.d.ts.map +1 -0
  332. package/dist/util/loadConfig.js +72 -0
  333. package/dist/util/loadConfig.js.map +1 -0
  334. package/dist/util/loadEnv.d.ts +2 -0
  335. package/dist/util/loadEnv.d.ts.map +1 -0
  336. package/dist/util/loadEnv.js +10 -0
  337. package/dist/util/loadEnv.js.map +1 -0
  338. package/dist/util/mergeSchema.d.ts +10 -0
  339. package/dist/util/mergeSchema.d.ts.map +1 -0
  340. package/dist/util/mergeSchema.js +338 -0
  341. package/dist/util/mergeSchema.js.map +1 -0
  342. package/dist/util/projectDir.d.ts +7 -0
  343. package/dist/util/projectDir.d.ts.map +1 -0
  344. package/dist/util/projectDir.js +43 -0
  345. package/dist/util/projectDir.js.map +1 -0
  346. package/dist/util/renamePrompt.d.ts +15 -0
  347. package/dist/util/renamePrompt.d.ts.map +1 -0
  348. package/dist/util/renamePrompt.js +57 -0
  349. package/dist/util/renamePrompt.js.map +1 -0
  350. package/dist/util/sdkInstallSpec.d.ts +13 -0
  351. package/dist/util/sdkInstallSpec.d.ts.map +1 -0
  352. package/dist/util/sdkInstallSpec.js +37 -0
  353. package/dist/util/sdkInstallSpec.js.map +1 -0
  354. package/dist/version.d.ts +3 -0
  355. package/dist/version.d.ts.map +1 -0
  356. package/dist/version.js +3 -0
  357. package/dist/version.js.map +1 -0
  358. package/package.json +91 -3
  359. package/src/auth/index.ts +125 -0
  360. package/src/commands/app/delete.ts +82 -0
  361. package/src/commands/app/list.ts +44 -0
  362. package/src/commands/auth/client/add.ts +744 -0
  363. package/src/commands/auth/client/delete.ts +60 -0
  364. package/src/commands/auth/client/list.ts +60 -0
  365. package/src/commands/auth/client/shared.ts +190 -0
  366. package/src/commands/auth/client/update.ts +830 -0
  367. package/src/commands/auth/email/pull.ts +143 -0
  368. package/src/commands/auth/email/push.ts +94 -0
  369. package/src/commands/auth/email/resend.ts +8 -0
  370. package/src/commands/auth/email/reset.ts +37 -0
  371. package/src/commands/auth/email/status.ts +154 -0
  372. package/src/commands/auth/email/verify.ts +20 -0
  373. package/src/commands/auth/origin/add.ts +237 -0
  374. package/src/commands/auth/origin/delete.ts +50 -0
  375. package/src/commands/auth/origin/list.ts +66 -0
  376. package/src/commands/backup/download.ts +231 -0
  377. package/src/commands/backup/list.ts +94 -0
  378. package/src/commands/claim.ts +31 -0
  379. package/src/commands/explorer.ts +21 -0
  380. package/src/commands/info.ts +57 -0
  381. package/src/commands/init.ts +60 -0
  382. package/src/commands/initWithoutFiles.ts +107 -0
  383. package/src/commands/login.ts +62 -0
  384. package/src/commands/logout.ts +22 -0
  385. package/src/commands/pull.ts +22 -0
  386. package/src/commands/push.ts +25 -0
  387. package/src/commands/query.ts +61 -0
  388. package/src/commands/webhooks/add.ts +111 -0
  389. package/src/commands/webhooks/delete.ts +23 -0
  390. package/src/commands/webhooks/disable.ts +24 -0
  391. package/src/commands/webhooks/enable.ts +24 -0
  392. package/src/commands/webhooks/events/list.ts +38 -0
  393. package/src/commands/webhooks/events/payload.ts +56 -0
  394. package/src/commands/webhooks/events/resend.ts +66 -0
  395. package/src/commands/webhooks/list.ts +41 -0
  396. package/src/commands/webhooks/shared.ts +339 -0
  397. package/src/commands/webhooks/update.ts +276 -0
  398. package/src/context/authToken.ts +134 -0
  399. package/src/context/currentApp.ts +301 -0
  400. package/src/context/globalOpts.ts +22 -0
  401. package/src/context/platformApi.ts +35 -0
  402. package/src/context/projectInfo.ts +219 -0
  403. package/src/errors.ts +7 -0
  404. package/src/index.ts +1212 -0
  405. package/src/layer.ts +211 -0
  406. package/src/lib/args.ts +453 -0
  407. package/src/lib/backupDownload.ts +188 -0
  408. package/src/lib/backups.ts +33 -0
  409. package/src/lib/config.ts +73 -0
  410. package/src/lib/createApp.ts +28 -0
  411. package/src/lib/email.ts +93 -0
  412. package/src/lib/handleEnv.ts +122 -0
  413. package/src/lib/http.ts +128 -0
  414. package/src/lib/login.ts +48 -0
  415. package/src/lib/oauth.ts +310 -0
  416. package/src/lib/platformApi.ts +11 -0
  417. package/src/lib/pullPerms.ts +50 -0
  418. package/src/lib/pullSchema.ts +95 -0
  419. package/src/lib/pushPerms.ts +80 -0
  420. package/src/lib/pushSchema.ts +248 -0
  421. package/src/lib/ui.ts +48 -0
  422. package/src/lib/webhooks.ts +118 -0
  423. package/src/logging.ts +41 -0
  424. package/src/old.js +487 -0
  425. package/src/program.ts +3 -0
  426. package/src/renderSchemaPlan.ts +324 -0
  427. package/src/ui/index.ts +1615 -0
  428. package/src/ui/lib.ts +424 -0
  429. package/src/util/findConfigCandidates.ts +262 -0
  430. package/src/util/fs.ts +26 -0
  431. package/src/util/getAppName.ts +14 -0
  432. package/src/util/instantConfig.ts +21 -0
  433. package/src/util/isHeadlessEnvironment.ts +15 -0
  434. package/src/util/loadConfig.ts +93 -0
  435. package/src/util/loadEnv.ts +11 -0
  436. package/src/util/mergeSchema.ts +374 -0
  437. package/src/util/projectDir.ts +54 -0
  438. package/src/util/renamePrompt.ts +79 -0
  439. package/src/util/sdkInstallSpec.ts +45 -0
  440. package/src/version.js +3 -0
  441. package/tsconfig.build.json +20 -0
  442. package/tsconfig.json +23 -0
  443. package/vitest.config.ts +22 -0
@@ -0,0 +1,1615 @@
1
+ import chalk from 'chalk';
2
+ import boxen from 'boxen';
3
+ import stringWidth from 'string-width';
4
+ import { Prompt, SelectState } from './lib.ts';
5
+ import type { AnyKey, ModifyOutputFn } from './lib.ts';
6
+
7
+ export {
8
+ clearPromptTrail,
9
+ render,
10
+ renderUnwrap,
11
+ setRawModeWindowsFriendly,
12
+ } from './lib.ts';
13
+
14
+ export namespace UI {
15
+ type Status = 'idle' | 'submitted' | 'aborted';
16
+ export const modifiers = {
17
+ piped: (modifiers: ModifyOutputFn[]): ModifyOutputFn => {
18
+ return (output: string, status?: Status) => {
19
+ return modifiers.reduce(
20
+ (acc, modifier) => modifier(acc, status),
21
+ output,
22
+ );
23
+ };
24
+ },
25
+
26
+ topPadding: (output: string) => {
27
+ return '\n' + output;
28
+ },
29
+
30
+ yPadding: (output: string) => {
31
+ return '\n' + output + '\n';
32
+ },
33
+
34
+ sidelined:
35
+ (symbol: string | null = '◆') =>
36
+ (output: string, status?: Status) => {
37
+ const result: string[] = [];
38
+ output.split('\n').forEach((line, idx) => {
39
+ if (idx === 1 && symbol) {
40
+ result.push(`${chalk.gray(symbol + ' ')}${line}`);
41
+ } else {
42
+ result.push(`${chalk.gray('│ ')}${line}`);
43
+ }
44
+ });
45
+ if (status === 'idle') {
46
+ result.push(`${chalk.gray('└ ')}`);
47
+ }
48
+ let almost = result.join('\n');
49
+ if (!almost.endsWith('\n')) {
50
+ almost += '\n';
51
+ }
52
+ return almost;
53
+ },
54
+
55
+ background: (output: string) => {
56
+ return chalk.bgBlackBright(output);
57
+ },
58
+
59
+ dimOnComplete: (output: string, status?: Status) => {
60
+ if (status === 'submitted' || status === 'aborted') {
61
+ return chalk.dim(output);
62
+ }
63
+ return output;
64
+ },
65
+ vanishOnComplete: (output: string, status?: Status) => {
66
+ if (status === 'submitted' || status === 'aborted') {
67
+ return '';
68
+ }
69
+ return output;
70
+ },
71
+ } as const;
72
+
73
+ export const ciaModifier = (symbol: string | null = '◆') =>
74
+ modifiers.piped([
75
+ UI.modifiers.topPadding,
76
+ UI.modifiers.dimOnComplete,
77
+ UI.modifiers.sidelined(symbol),
78
+ ]);
79
+
80
+ /**
81
+ * Utility that lets you use output modifiers in console.log
82
+ */
83
+ export const log = (
84
+ output: string,
85
+ modifyOutput?: ModifyOutputFn,
86
+ ...args: any[]
87
+ ) => {
88
+ const finalOutput = modifyOutput ? modifyOutput(output) : output;
89
+ if (finalOutput.endsWith('\n')) {
90
+ process.stdout.write(finalOutput, ...args);
91
+ } else {
92
+ process.stdout.write(finalOutput + '\n', ...args);
93
+ }
94
+ };
95
+
96
+ type SelectProps<T> = {
97
+ options: {
98
+ value: T;
99
+ label: string;
100
+ expandableLabel?: string | (() => string | Promise<string>);
101
+ secondary?: boolean;
102
+ }[];
103
+ promptText: string;
104
+ modifyOutput?: ModifyOutputFn;
105
+ defaultValue?: T;
106
+ };
107
+ export class Select<T> extends Prompt<T> {
108
+ config(status: 'idle' | 'submitted' | 'aborted'): string {
109
+ return status;
110
+ }
111
+
112
+ private readonly data: SelectState<T>;
113
+ private readonly options: SelectProps<T>['options'];
114
+ private readonly params: SelectProps<T>;
115
+ private expandedIdx: number | null = null;
116
+ private stickyExpanded = false;
117
+ private readonly expansionCache: Map<number, string> = new Map();
118
+ private readonly expansionLoading: Set<number> = new Set();
119
+
120
+ constructor(params: SelectProps<T>) {
121
+ super(params.modifyOutput);
122
+ this.on('attach', (terminal) => terminal.toggleCursor('hide'));
123
+ this.on('input', (input, key) => {
124
+ if (input === 'j' || (key?.ctrl && key.name === 'n')) {
125
+ this.data.selectedIdx =
126
+ (this.data.selectedIdx + 1) % this.options.length;
127
+ this.applyNavigation();
128
+ } else if (input === 'k' || (key?.ctrl && key.name === 'p')) {
129
+ this.data.selectedIdx =
130
+ (this.data.selectedIdx - 1 + this.options.length) %
131
+ this.options.length;
132
+ this.applyNavigation();
133
+ }
134
+ this.requestLayout();
135
+ });
136
+ this.on('detach', (terminal) => {
137
+ terminal.toggleCursor('show');
138
+ });
139
+ this.options = params.options;
140
+ this.params = params;
141
+ this.data = new SelectState<T>(
142
+ params.options.map((option) => option.value),
143
+ );
144
+
145
+ // Set initial selected index based on defaultValue if provided
146
+ if (params.defaultValue !== undefined) {
147
+ const defaultIndex = params.options.findIndex(
148
+ (option) => option.value === params.defaultValue,
149
+ );
150
+ if (defaultIndex !== -1) {
151
+ this.data.selectedIdx = defaultIndex;
152
+ }
153
+ }
154
+
155
+ this.data.bind(this as any);
156
+
157
+ this.on('input', (_input, key) => {
158
+ if (key?.name === 'tab') {
159
+ const hasExpandable = this.options.some((o) => o.expandableLabel);
160
+ if (!hasExpandable) return;
161
+ this.stickyExpanded = !this.stickyExpanded;
162
+ if (this.stickyExpanded) {
163
+ this.expandFocused();
164
+ } else {
165
+ this.expandedIdx = null;
166
+ }
167
+ this.requestLayout();
168
+ return;
169
+ }
170
+ if (key?.name === 'up' || key?.name === 'down') {
171
+ this.applyNavigation();
172
+ this.requestLayout();
173
+ }
174
+ });
175
+ }
176
+
177
+ private applyNavigation() {
178
+ if (this.stickyExpanded) {
179
+ this.expandFocused();
180
+ } else {
181
+ this.expandedIdx = null;
182
+ }
183
+ }
184
+
185
+ private expandFocused() {
186
+ const focused = this.data.selectedIdx;
187
+ const focusedOption = this.options[focused];
188
+ const exp = focusedOption?.expandableLabel;
189
+ if (!exp) {
190
+ this.expandedIdx = null;
191
+ return;
192
+ }
193
+ this.expandedIdx = focused;
194
+ if (
195
+ typeof exp === 'function' &&
196
+ !this.expansionCache.has(focused) &&
197
+ !this.expansionLoading.has(focused)
198
+ ) {
199
+ this.expansionLoading.add(focused);
200
+ Promise.resolve()
201
+ .then(() => (exp as () => string | Promise<string>)())
202
+ .then(
203
+ (content) => {
204
+ this.expansionLoading.delete(focused);
205
+ this.expansionCache.set(focused, content);
206
+ if (this.expandedIdx === focused) this.requestLayout();
207
+ },
208
+ (err) => {
209
+ this.expansionLoading.delete(focused);
210
+ this.expansionCache.set(
211
+ focused,
212
+ chalk.red(
213
+ ` Error loading expansion: ${err?.message ?? err}`,
214
+ ),
215
+ );
216
+ if (this.expandedIdx === focused) this.requestLayout();
217
+ },
218
+ );
219
+ }
220
+ }
221
+
222
+ private getExpansionContent(
223
+ option: SelectProps<T>['options'][number],
224
+ idx: number,
225
+ ): string | null {
226
+ const exp = option.expandableLabel;
227
+ if (!exp) return null;
228
+ if (typeof exp === 'string') return exp;
229
+ if (this.expansionLoading.has(idx)) return chalk.dim(' Loading…');
230
+ const cached = this.expansionCache.get(idx);
231
+ return cached ?? chalk.dim(' Loading…');
232
+ }
233
+
234
+ result(): T {
235
+ return this.data.items[this.data.selectedIdx]!;
236
+ }
237
+
238
+ render(status: 'idle' | 'submitted' | 'aborted'): string {
239
+ if (status === 'submitted') {
240
+ return `${this.params.promptText}
241
+ ${chalk.hex('#EA570B').bold('●')} ${this.params.options[this.data.selectedIdx]?.label}`;
242
+ }
243
+
244
+ const renderRow = (
245
+ option: SelectProps<T>['options'][number],
246
+ originalIdx: number,
247
+ ) => {
248
+ const isSelected = originalIdx === this.data.selectedIdx;
249
+ const cursor = isSelected ? chalk.hex('#EA570B').bold('●') : '○';
250
+ const label = isSelected
251
+ ? chalk.bold(option.label)
252
+ : chalk.dim(option.label);
253
+ const expandedContent =
254
+ isSelected && this.expandedIdx === originalIdx
255
+ ? this.getExpansionContent(option, originalIdx)
256
+ : null;
257
+ const expanded = expandedContent !== null ? '\n' + expandedContent : '';
258
+ return `${cursor} ${label}${expanded}`;
259
+ };
260
+
261
+ const rowLineCount = (originalIdx: number) => {
262
+ const opt = this.options[originalIdx]!;
263
+ const labelLines = opt.label.split('\n').length;
264
+ const isFocused = originalIdx === this.data.selectedIdx;
265
+ const expContent =
266
+ isFocused && this.expandedIdx === originalIdx
267
+ ? this.getExpansionContent(opt, originalIdx)
268
+ : null;
269
+ const expLines =
270
+ expContent !== null ? expContent.split('\n').length : 0;
271
+ return labelLines + expLines;
272
+ };
273
+
274
+ const mainEntries: {
275
+ opt: SelectProps<T>['options'][number];
276
+ originalIdx: number;
277
+ }[] = [];
278
+ const secondaryEntries: typeof mainEntries = [];
279
+ this.options.forEach((o, i) => {
280
+ if (o.secondary) secondaryEntries.push({ opt: o, originalIdx: i });
281
+ else mainEntries.push({ opt: o, originalIdx: i });
282
+ });
283
+
284
+ const totalRows = process.stdout.rows ?? 24;
285
+ const secondaryHeight =
286
+ secondaryEntries.length === 0
287
+ ? 0
288
+ : secondaryEntries.reduce(
289
+ (acc, { originalIdx }) => acc + rowLineCount(originalIdx),
290
+ 0,
291
+ ) + 1; // +1 for divider
292
+ const hasExpandable = this.options.some((o) => o.expandableLabel);
293
+ // chrome = prompt(1) + secondaries(+divider) + hint + safety(2)
294
+ const chrome = 1 + secondaryHeight + (hasExpandable ? 1 : 0) + 2;
295
+ const mainBudget = Math.max(3, totalRows - chrome);
296
+
297
+ const focusedMainPos = mainEntries.findIndex(
298
+ (e) => e.originalIdx === this.data.selectedIdx,
299
+ );
300
+ let visiblePositions: number[] = [];
301
+
302
+ if (focusedMainPos >= 0) {
303
+ let used = rowLineCount(mainEntries[focusedMainPos]!.originalIdx);
304
+ visiblePositions = [focusedMainPos];
305
+ let lo = focusedMainPos - 1;
306
+ let hi = focusedMainPos + 1;
307
+ while (lo >= 0 || hi < mainEntries.length) {
308
+ let progressed = false;
309
+ if (lo >= 0) {
310
+ const h = rowLineCount(mainEntries[lo]!.originalIdx);
311
+ if (used + h <= mainBudget) {
312
+ visiblePositions.unshift(lo);
313
+ used += h;
314
+ lo--;
315
+ progressed = true;
316
+ } else {
317
+ lo = -1;
318
+ }
319
+ }
320
+ if (hi < mainEntries.length) {
321
+ const h = rowLineCount(mainEntries[hi]!.originalIdx);
322
+ if (used + h <= mainBudget) {
323
+ visiblePositions.push(hi);
324
+ used += h;
325
+ hi++;
326
+ progressed = true;
327
+ } else {
328
+ hi = mainEntries.length;
329
+ }
330
+ }
331
+ if (!progressed) break;
332
+ }
333
+ } else {
334
+ // Cursor is on a secondary — show as many mains as fit, top-down.
335
+ let used = 0;
336
+ for (let i = 0; i < mainEntries.length; i++) {
337
+ const h = rowLineCount(mainEntries[i]!.originalIdx);
338
+ if (used + h > mainBudget) break;
339
+ visiblePositions.push(i);
340
+ used += h;
341
+ }
342
+ }
343
+
344
+ const firstVisible = visiblePositions[0] ?? 0;
345
+ const lastVisible =
346
+ visiblePositions[visiblePositions.length - 1] ?? mainEntries.length - 1;
347
+ const aboveCount = firstVisible;
348
+ const belowCount = Math.max(0, mainEntries.length - 1 - lastVisible);
349
+
350
+ const mainLines: string[] = [];
351
+ if (aboveCount > 0) mainLines.push(chalk.dim(` ↑ ${aboveCount} more`));
352
+ for (const pos of visiblePositions) {
353
+ mainLines.push(
354
+ renderRow(mainEntries[pos]!.opt, mainEntries[pos]!.originalIdx),
355
+ );
356
+ }
357
+ if (belowCount > 0) mainLines.push(chalk.dim(` ↓ ${belowCount} more`));
358
+ const mainBlock = mainLines.join('\n');
359
+
360
+ const secondaryBlock =
361
+ secondaryEntries.length === 0
362
+ ? ''
363
+ : chalk.gray(
364
+ '\n───────────────── Additional Options ─────────────────\n',
365
+ ) +
366
+ secondaryEntries
367
+ .map((e) => renderRow(e.opt, e.originalIdx))
368
+ .join('\n');
369
+
370
+ const expandHint = hasExpandable
371
+ ? '\n' +
372
+ chalk.dim(
373
+ this.stickyExpanded ? ' (tab to collapse)' : ' (tab to expand)',
374
+ )
375
+ : '';
376
+
377
+ return `${this.params.promptText}
378
+ ${mainBlock}${secondaryBlock}${expandHint}`;
379
+ }
380
+ }
381
+
382
+ export type MultiSelectOption<T> = {
383
+ value: T;
384
+ label: string;
385
+ };
386
+
387
+ type MultiSelectProps<T> = {
388
+ options: MultiSelectOption<T>[];
389
+ promptText: string;
390
+ initialSelected?: T[];
391
+ filter?: (filter: string, option: MultiSelectOption<T>) => boolean;
392
+ minSelected?: number;
393
+ pageSize?: number;
394
+ modifyOutput?: ModifyOutputFn;
395
+ };
396
+
397
+ const DEFAULT_MULTI_SELECT_PAGE_SIZE = 10;
398
+
399
+ const defaultMultiSelectFilter = <T>(
400
+ filter: string,
401
+ option: MultiSelectOption<T>,
402
+ ) => option.label.toLowerCase().includes(filter.toLowerCase());
403
+
404
+ export class MultiSelect<T> extends Prompt<T[]> {
405
+ private filterText = '';
406
+ private cursorIdx = 0;
407
+ private windowStart = 0;
408
+ private selected: Set<number>;
409
+ private errorText: string | undefined;
410
+ private readonly props: MultiSelectProps<T>;
411
+ private readonly pageSize: number;
412
+
413
+ constructor(props: MultiSelectProps<T>) {
414
+ super(props.modifyOutput);
415
+ this.props = props;
416
+ this.pageSize = props.pageSize ?? DEFAULT_MULTI_SELECT_PAGE_SIZE;
417
+ this.selected = new Set(
418
+ (props.initialSelected ?? [])
419
+ .map((v) => props.options.findIndex((o) => o.value === v))
420
+ .filter((i) => i >= 0),
421
+ );
422
+ this.on('attach', (terminal) => {
423
+ terminal.setAllowInteraction(false);
424
+ terminal.toggleCursor('hide');
425
+ });
426
+ this.on('detach', (terminal) => terminal.toggleCursor('show'));
427
+ this.on('input', (input, key) => this.handleKey(input, key));
428
+ }
429
+
430
+ private visibleIndices(): number[] {
431
+ const fn = this.props.filter ?? defaultMultiSelectFilter;
432
+ if (!this.filterText) return this.props.options.map((_, i) => i);
433
+ return this.props.options
434
+ .map((opt, i) => ({ opt, i }))
435
+ .filter(({ opt }) => fn(this.filterText, opt))
436
+ .map(({ i }) => i);
437
+ }
438
+
439
+ private clampCursor(visible: number[]) {
440
+ if (visible.length === 0) {
441
+ this.cursorIdx = 0;
442
+ } else if (this.cursorIdx >= visible.length) {
443
+ this.cursorIdx = visible.length - 1;
444
+ } else if (this.cursorIdx < 0) {
445
+ this.cursorIdx = visible.length - 1;
446
+ }
447
+ }
448
+
449
+ private adjustWindow(visible: number[]) {
450
+ if (visible.length <= this.pageSize) {
451
+ this.windowStart = 0;
452
+ return;
453
+ }
454
+ if (this.cursorIdx < this.windowStart) {
455
+ this.windowStart = this.cursorIdx;
456
+ } else if (this.cursorIdx >= this.windowStart + this.pageSize) {
457
+ this.windowStart = this.cursorIdx - this.pageSize + 1;
458
+ }
459
+ const maxStart = Math.max(0, visible.length - this.pageSize);
460
+ if (this.windowStart > maxStart) this.windowStart = maxStart;
461
+ if (this.windowStart < 0) this.windowStart = 0;
462
+ }
463
+
464
+ private handleKey(input: string | undefined, key: AnyKey) {
465
+ if (key.name === 'escape') {
466
+ return this.terminal?.resolve({ data: undefined, status: 'aborted' });
467
+ }
468
+ if (key.name === 'return') {
469
+ const min = this.props.minSelected ?? 0;
470
+ if (this.selected.size < min) {
471
+ this.errorText =
472
+ min === 1
473
+ ? 'Select at least one option.'
474
+ : `Select at least ${min} options.`;
475
+ this.requestLayout();
476
+ return;
477
+ }
478
+ return this.terminal?.resolve({
479
+ data: this.result(),
480
+ status: 'submitted',
481
+ });
482
+ }
483
+
484
+ const visible = this.visibleIndices();
485
+
486
+ if (key.name === 'up' || (key.ctrl && key.name === 'p')) {
487
+ this.cursorIdx -= 1;
488
+ this.clampCursor(visible);
489
+ this.adjustWindow(visible);
490
+ this.requestLayout();
491
+ return;
492
+ }
493
+ if (key.name === 'down' || (key.ctrl && key.name === 'n')) {
494
+ this.cursorIdx += 1;
495
+ if (visible.length > 0) this.cursorIdx %= visible.length;
496
+ this.adjustWindow(visible);
497
+ this.requestLayout();
498
+ return;
499
+ }
500
+ if (key.name === 'pageup' || (key.ctrl && key.name === 'u')) {
501
+ this.cursorIdx -= this.pageSize;
502
+ if (this.cursorIdx < 0) this.cursorIdx = 0;
503
+ this.adjustWindow(visible);
504
+ this.requestLayout();
505
+ return;
506
+ }
507
+ if (key.name === 'pagedown' || (key.ctrl && key.name === 'd')) {
508
+ this.cursorIdx += this.pageSize;
509
+ if (this.cursorIdx >= visible.length) {
510
+ this.cursorIdx = Math.max(0, visible.length - 1);
511
+ }
512
+ this.adjustWindow(visible);
513
+ this.requestLayout();
514
+ return;
515
+ }
516
+ if (key.name === 'space') {
517
+ if (visible.length === 0) return;
518
+ const optIdx = visible[this.cursorIdx]!;
519
+ if (this.selected.has(optIdx)) {
520
+ this.selected.delete(optIdx);
521
+ } else {
522
+ this.selected.add(optIdx);
523
+ }
524
+ this.errorText = undefined;
525
+ this.requestLayout();
526
+ return;
527
+ }
528
+ if (key.name === 'backspace') {
529
+ if (this.filterText.length > 0) {
530
+ this.filterText = this.filterText.slice(0, -1);
531
+ this.cursorIdx = 0;
532
+ this.windowStart = 0;
533
+ this.requestLayout();
534
+ }
535
+ return;
536
+ }
537
+ if (key.name?.length === 1 && !key.ctrl && !key.meta && input) {
538
+ this.filterText += input;
539
+ this.cursorIdx = 0;
540
+ this.windowStart = 0;
541
+ this.requestLayout();
542
+ }
543
+ }
544
+
545
+ result(): T[] {
546
+ return [...this.selected]
547
+ .sort((a, b) => a - b)
548
+ .map((i) => this.props.options[i]!.value);
549
+ }
550
+
551
+ private renderOption(optIdx: number, isCursor: boolean) {
552
+ const option = this.props.options[optIdx]!;
553
+ const isSelected = this.selected.has(optIdx);
554
+ const checkbox = isSelected ? chalk.green('◉') : '○';
555
+ const cursorMark = isCursor ? chalk.hex('#EA570B').bold('❯') : ' ';
556
+ const label = isCursor ? chalk.bold(option.label) : option.label;
557
+ return `${cursorMark} ${checkbox} ${label}`;
558
+ }
559
+
560
+ render(status: 'idle' | 'submitted' | 'aborted'): string {
561
+ if (status === 'submitted') {
562
+ const labels = [...this.selected]
563
+ .sort((a, b) => a - b)
564
+ .map((i) => this.props.options[i]!.label);
565
+ return `${this.props.promptText}
566
+ ${chalk.hex('#EA570B').bold('●')} ${labels.length === 0 ? chalk.dim('(none)') : labels.join(', ')}`;
567
+ }
568
+
569
+ const visible = this.visibleIndices();
570
+ this.adjustWindow(visible);
571
+ const filterLine = `${chalk.dim('filter:')} ${this.filterText}${chalk.inverse(' ')}`;
572
+ const errorLine = this.errorText ? ` ${chalk.red(this.errorText)}` : '';
573
+
574
+ let optionsBlock: string;
575
+ if (visible.length === 0) {
576
+ optionsBlock = chalk.dim(' (no matches)');
577
+ } else {
578
+ const windowEnd = Math.min(
579
+ this.windowStart + this.pageSize,
580
+ visible.length,
581
+ );
582
+ const aboveCount = this.windowStart;
583
+ const belowCount = visible.length - windowEnd;
584
+ const lines: string[] = [];
585
+ lines.push(
586
+ aboveCount > 0
587
+ ? chalk.dim(` ↑ ${aboveCount} more`)
588
+ : chalk.dim(' '),
589
+ );
590
+ for (let i = this.windowStart; i < windowEnd; i++) {
591
+ lines.push(this.renderOption(visible[i]!, i === this.cursorIdx));
592
+ }
593
+ lines.push(
594
+ belowCount > 0
595
+ ? chalk.dim(` ↓ ${belowCount} more`)
596
+ : chalk.dim(' '),
597
+ );
598
+ optionsBlock = lines.join('\n');
599
+ }
600
+
601
+ const hint = chalk.dim(
602
+ ` ↑↓ or ctrl-n/p navigate · pgup/pgdn or ctrl-u/d page · space toggle · enter submit · esc cancel`,
603
+ );
604
+ const counts = chalk.dim(
605
+ ` ${visible.length} of ${this.props.options.length} shown · ${this.selected.size} selected`,
606
+ );
607
+
608
+ return `${this.props.promptText}${errorLine}
609
+ ${filterLine}
610
+ ${optionsBlock}
611
+ ${counts}
612
+ ${hint}`;
613
+ }
614
+ }
615
+
616
+ /**
617
+ * @deprecated use the modifyOutput prop instead
618
+ * left as an example of how to do wrapper prompts
619
+ */
620
+ export class Sidelined<T> extends Prompt<T> {
621
+ override result(): T {
622
+ return this.inner.result();
623
+ }
624
+ override render(status: 'idle' | 'submitted' | 'aborted'): string {
625
+ return (
626
+ this.inner
627
+ .render(status)
628
+ .split('\n')
629
+ .map((line) => `${chalk.gray(' │ ')}${line}`)
630
+ .join('\n') + '\n'
631
+ );
632
+ }
633
+ config(status: 'idle' | 'submitted' | 'aborted'): string {
634
+ return status;
635
+ }
636
+
637
+ private readonly inner: Prompt<T>;
638
+
639
+ constructor(inner: Prompt<T>) {
640
+ super();
641
+ this.inner = inner;
642
+ this.on('input', (input, arg2) => {
643
+ this.inner.input(input, arg2);
644
+ this.requestLayout();
645
+ });
646
+ this.on('detach', (terminal) => {
647
+ this.inner.detach(terminal);
648
+ });
649
+ this.on('attach', (terminal) => {
650
+ this.inner.attach(terminal);
651
+ });
652
+ }
653
+ }
654
+
655
+ export type TextInputProps = {
656
+ placeholder?: string;
657
+ prompt: string;
658
+ modifyOutput?: ModifyOutputFn;
659
+ defaultValue?: string;
660
+ validate?: (value: string) => string | undefined;
661
+ headless?: boolean;
662
+ sensitive?: boolean;
663
+ };
664
+
665
+ export class TextInput extends Prompt<string> {
666
+ override result(): string {
667
+ return this.value;
668
+ }
669
+ override render(status: 'idle' | 'submitted' | 'aborted'): string {
670
+ const value = this.props.sensitive
671
+ ? '*'.repeat(this.value.length)
672
+ : this.value;
673
+ if (status === 'submitted') {
674
+ return `${this.props.prompt}
675
+ ${value}`;
676
+ }
677
+ if (status === 'aborted') {
678
+ return `${this.props.prompt} ${value} (CANCELLED)\n`;
679
+ }
680
+ let inputDisplay = '';
681
+ if (value === '') {
682
+ inputDisplay = this.props.placeholder
683
+ ? `${chalk.inverse(this.props.placeholder.substring(0, 1))}${chalk.dim(this.props.placeholder.substring(1))}`
684
+ : chalk.inverse(' ');
685
+ } else {
686
+ inputDisplay = `${value}${chalk.inverse(' ')}`;
687
+ }
688
+ const errorText = this.errorText
689
+ ? ` ${chalk.red(this.errorText)}`
690
+ : '';
691
+ return `${this.props.prompt}${errorText}
692
+ ${inputDisplay}`;
693
+ }
694
+
695
+ public value: string;
696
+ private errorText: string | undefined;
697
+ private readonly props: TextInputProps;
698
+
699
+ constructor(props: TextInputProps) {
700
+ super(props.modifyOutput);
701
+ this.on('attach', (terminal) => {
702
+ terminal.setAllowInteraction(false); // needed for validation
703
+ terminal.toggleCursor('hide');
704
+ });
705
+ this.on('detach', (terminal) => {
706
+ terminal.toggleCursor('show');
707
+ });
708
+ this.on('input', (input, keyInfo) => {
709
+ if (keyInfo.name === 'escape') {
710
+ if (!this.props.headless) {
711
+ return this?.terminal?.resolve({
712
+ data: undefined,
713
+ status: 'aborted',
714
+ });
715
+ }
716
+ }
717
+ if (keyInfo.name === 'tab') {
718
+ if (
719
+ this.value === '' &&
720
+ this.props.defaultValue &&
721
+ this.props.placeholder === this.props.defaultValue
722
+ ) {
723
+ this.value = this.props.defaultValue;
724
+ this.requestLayout();
725
+ }
726
+ return;
727
+ }
728
+ if (keyInfo.name === 'return') {
729
+ if (this.value === '' && this.props.defaultValue) {
730
+ this.value = this.props.defaultValue;
731
+ return this.terminal?.resolve({
732
+ data: this.props.defaultValue,
733
+ status: 'submitted',
734
+ });
735
+ }
736
+ // Do the validation
737
+ if (this.props.validate) {
738
+ const validationResult = this.props.validate(this.value);
739
+ if (validationResult) {
740
+ this.errorText = validationResult;
741
+ this.requestLayout();
742
+ return;
743
+ } else {
744
+ return this.terminal?.resolve({
745
+ data: this.value,
746
+ status: 'submitted',
747
+ });
748
+ }
749
+ } else {
750
+ return this.terminal?.resolve({
751
+ data: this.value,
752
+ status: 'submitted',
753
+ });
754
+ }
755
+ }
756
+ if (keyInfo.name === 'backspace') {
757
+ this.value = this.value.slice(0, -1);
758
+ this.errorText = undefined;
759
+ } else if (keyInfo.name?.length === 1) {
760
+ this.value += input;
761
+ this.errorText = undefined;
762
+ } else if (keyInfo.name === 'space') {
763
+ this.value += ' ';
764
+ this.errorText = undefined;
765
+ } else if (input !== undefined) {
766
+ this.value += input;
767
+ this.errorText = undefined;
768
+ }
769
+ this.requestLayout();
770
+ });
771
+ this.value = '';
772
+ this.errorText = '';
773
+ this.props = props;
774
+ }
775
+
776
+ setValue(value: string) {
777
+ this.value = value;
778
+ this.requestLayout();
779
+ }
780
+
781
+ setPrompt(prompt: string) {
782
+ this.props.prompt = prompt;
783
+ this.requestLayout();
784
+ }
785
+ }
786
+
787
+ type SpinnerProps<T> = {
788
+ modifyOutput?: ModifyOutputFn;
789
+ promise: Promise<T>;
790
+ workingText?: string;
791
+ doneText?: string;
792
+ errorText?: string | ((e: unknown) => string);
793
+ disappearWhenDone?: boolean;
794
+ };
795
+
796
+ export class Spinner<T> extends Prompt<T> {
797
+ private props: SpinnerProps<T>;
798
+ private promiseResult: T | null = null;
799
+ private promiseError: Error | null = null;
800
+ private spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
801
+ private frameIndex = 0;
802
+ private intervalId: NodeJS.Timeout | null = null;
803
+ private messages: string[] = [];
804
+
805
+ result(): T {
806
+ if (this.promiseError) {
807
+ throw this.promiseError;
808
+ }
809
+ return this.promiseResult!;
810
+ }
811
+
812
+ addMessage(message: string): void {
813
+ this.messages.push(message);
814
+ this.requestLayout();
815
+ }
816
+
817
+ updateText(text: string): void {
818
+ this.props.workingText = text;
819
+ this.requestLayout();
820
+ }
821
+
822
+ render(status: 'idle' | 'submitted' | 'aborted'): string {
823
+ const workingText = this.props.workingText || 'Loading...';
824
+ const doneText = this.props.doneText || 'Done';
825
+ const errorText = this.props.errorText || 'Error';
826
+
827
+ if (status === 'aborted') {
828
+ return `${chalk.yellow('⚠')} Aborted\n`;
829
+ }
830
+
831
+ if (this.promiseError) {
832
+ const finalError =
833
+ errorText instanceof Function
834
+ ? errorText(this.promiseError)
835
+ : errorText;
836
+ return `${chalk.red('✗')} ${finalError}\n`;
837
+ }
838
+
839
+ if (status === 'submitted') {
840
+ if (this.props.disappearWhenDone) {
841
+ return '';
842
+ }
843
+ return `${chalk.green('✓')} ${doneText}\n`;
844
+ }
845
+
846
+ const frame = this.spinnerFrames[this.frameIndex];
847
+ let messages = this.messages.join('\n');
848
+ if (this.messages.length > 0) {
849
+ messages += '\n';
850
+ }
851
+ return `${messages}${chalk.hex('#EA570B')(frame)} ${workingText}\n${messages}`;
852
+ }
853
+
854
+ constructor(props: SpinnerProps<T>) {
855
+ super(props.modifyOutput);
856
+ this.props = props;
857
+
858
+ this.on('attach', (terminal) => {
859
+ terminal.setAllowInteraction(false);
860
+ terminal.toggleCursor('hide');
861
+ this.intervalId = setInterval(() => {
862
+ this.frameIndex = (this.frameIndex + 1) % this.spinnerFrames.length;
863
+ this.requestLayout();
864
+ }, 80);
865
+
866
+ this.props.promise
867
+ .then((result) => {
868
+ this.promiseResult = result;
869
+ if (this.intervalId) clearInterval(this.intervalId);
870
+ return terminal.resolve({
871
+ data: result as any,
872
+ status: 'submitted',
873
+ });
874
+ })
875
+ .catch((error) => {
876
+ this.promiseError = error;
877
+ if (this.intervalId) clearInterval(this.intervalId);
878
+ return terminal.resolve({
879
+ data: error as any,
880
+ status: 'submitted',
881
+ });
882
+ });
883
+ });
884
+
885
+ this.on('detach', (terminal) => {
886
+ terminal.toggleCursor('show');
887
+ if (this.intervalId) {
888
+ clearInterval(this.intervalId);
889
+ this.intervalId = null;
890
+ }
891
+ });
892
+ }
893
+ }
894
+
895
+ export type ConfirmationProps = {
896
+ promptText: string;
897
+ defaultValue?: boolean;
898
+ modifyOutput?: ModifyOutputFn;
899
+ yesText?: string;
900
+ inline?: boolean;
901
+ noText?: string;
902
+ };
903
+
904
+ export class Confirmation extends Prompt<boolean> {
905
+ override result(): boolean {
906
+ return this.value ?? this.props.defaultValue ?? false;
907
+ }
908
+
909
+ override render(status: 'idle' | 'submitted' | 'aborted'): string {
910
+ const renderLabel = (
911
+ text: string,
912
+ active: boolean,
913
+ width: number = 8,
914
+ ): string => {
915
+ return boxen(text, {
916
+ backgroundColor: active
917
+ ? '#EA570B'
918
+ : status === 'idle'
919
+ ? 'blackBright'
920
+ : undefined,
921
+ borderStyle: 'none',
922
+ align: 'center',
923
+ width,
924
+ });
925
+ };
926
+
927
+ const yesStyle = renderLabel(
928
+ this.props.yesText ?? 'Yes',
929
+ this.value === true,
930
+ 9,
931
+ );
932
+ const noStyle = renderLabel(
933
+ this.props.noText ?? 'No',
934
+ this.value === false,
935
+ 8,
936
+ );
937
+
938
+ const display = `${this.props.promptText}${this.props.inline ? ' ' : '\n'}${yesStyle} ${noStyle}`;
939
+ if (status === 'submitted') {
940
+ return chalk.dim(display);
941
+ }
942
+ return display;
943
+ }
944
+
945
+ private value: boolean | null = null;
946
+ private readonly props: ConfirmationProps;
947
+
948
+ constructor(props: ConfirmationProps) {
949
+ super(props.modifyOutput);
950
+ this.props = props;
951
+ this.on('attach', (terminal) => {
952
+ terminal.toggleCursor('hide');
953
+ });
954
+ this.on('detach', (terminal) => {
955
+ terminal.toggleCursor('show');
956
+ });
957
+ this.value = props.defaultValue || false;
958
+ this.on('input', (input, key) => {
959
+ if (['right', 'left', 'tab', 'j', 'k', 'h', 'l'].includes(key.name!)) {
960
+ this.value = !this.value;
961
+ }
962
+ if (input) {
963
+ if (input.toLowerCase() === 'y') {
964
+ return this.terminal!.resolve({ data: true, status: 'submitted' });
965
+ } else if (input.toLowerCase() === 'n') {
966
+ return this.terminal!.resolve({ data: false, status: 'submitted' });
967
+ }
968
+ }
969
+ this.requestLayout();
970
+ });
971
+ }
972
+ }
973
+
974
+ class FocusHandle {
975
+ private parent: FocusHandle | null = null;
976
+ private _focus: Focus;
977
+ key: string;
978
+
979
+ private onFocusCallback: (() => void) | null = null;
980
+ private onBlurCallback: (() => void) | null = null;
981
+ private onKeyCallbacks: ((
982
+ str: string | undefined,
983
+ key: AnyKey,
984
+ propagate: () => void,
985
+ ) => any)[] = [];
986
+
987
+ constructor(focus: Focus, key: string, parent: FocusHandle | null) {
988
+ this._focus = focus;
989
+ this.key = key;
990
+ this.parent = parent;
991
+ }
992
+
993
+ child(childKey: string): FocusHandle {
994
+ const newHandle = new FocusHandle(this._focus, childKey, this);
995
+ this._focus.register(newHandle);
996
+ return newHandle;
997
+ }
998
+
999
+ setFocus(key: string) {
1000
+ this._focus?.setFocus(key);
1001
+ }
1002
+
1003
+ onKey(
1004
+ callback: (
1005
+ str: string | undefined,
1006
+ key: AnyKey,
1007
+ propagate: () => void,
1008
+ ) => any,
1009
+ ): FocusHandle {
1010
+ this.onKeyCallbacks.push(callback);
1011
+ return this;
1012
+ }
1013
+
1014
+ onFocus(callback: () => void): FocusHandle {
1015
+ this.onFocusCallback = callback;
1016
+ return this;
1017
+ }
1018
+
1019
+ onBlur(callback: () => void): FocusHandle {
1020
+ this.onBlurCallback = callback;
1021
+ return this;
1022
+ }
1023
+
1024
+ getFocused(): string {
1025
+ return this._focus.getFocused();
1026
+ }
1027
+
1028
+ isFocused(): boolean {
1029
+ return this._focus.isFocused(this.key);
1030
+ }
1031
+
1032
+ _notifyKey(str: string | undefined, key: AnyKey) {
1033
+ this.onKeyCallbacks.forEach((callback) => {
1034
+ callback(str, key, () => {
1035
+ this.parent?._notifyKey(str, key);
1036
+ });
1037
+ });
1038
+ }
1039
+
1040
+ _notifyFocusState(key: string) {
1041
+ if (key === this.key) {
1042
+ this.onFocusCallback?.();
1043
+ } else {
1044
+ this.onBlurCallback?.();
1045
+ }
1046
+ }
1047
+ }
1048
+
1049
+ export class Focus {
1050
+ private selected: string;
1051
+ private prompt: Prompt<any>;
1052
+ private handles: Record<string, FocusHandle>;
1053
+
1054
+ constructor(prompt: Prompt<any>) {
1055
+ this.prompt = prompt;
1056
+ this.prompt.on('input', (arg1, arg2) => {
1057
+ this.handles[this.selected]?._notifyKey?.(arg1, arg2);
1058
+ this.prompt.requestLayout();
1059
+ });
1060
+ this.handles = {};
1061
+ this.selected = 'root';
1062
+ }
1063
+
1064
+ register(handle: FocusHandle) {
1065
+ this.handles[handle.key] = handle;
1066
+ }
1067
+
1068
+ root(): FocusHandle {
1069
+ const rootHandle = new FocusHandle(this, 'root', null);
1070
+ this.register(rootHandle);
1071
+ this.setFocus('root');
1072
+ return rootHandle;
1073
+ }
1074
+
1075
+ getFocused(): string {
1076
+ return this.selected;
1077
+ }
1078
+
1079
+ setFocus(key: string) {
1080
+ this.selected = key;
1081
+
1082
+ this.handles[key]?._notifyFocusState?.(key);
1083
+
1084
+ this.prompt.requestLayout();
1085
+ }
1086
+
1087
+ isFocused(key: string): boolean {
1088
+ return this.selected === key;
1089
+ }
1090
+ }
1091
+
1092
+ type App = {
1093
+ admin_token: string;
1094
+ magic_code_email_template: null;
1095
+ id: string;
1096
+ title: string;
1097
+ created_at: string;
1098
+ };
1099
+ type Org = {
1100
+ id: string;
1101
+ title: string;
1102
+ role: string;
1103
+ };
1104
+
1105
+ type MenuItem = {
1106
+ label: string;
1107
+ onSelect: () => void;
1108
+ value?: string;
1109
+ };
1110
+
1111
+ type MenuProps = {
1112
+ focus: FocusHandle;
1113
+ items: MenuItem[];
1114
+ width?: number;
1115
+ maxHeight?: number;
1116
+ emptyState?: string;
1117
+ showIdxWhileBlurred?: boolean;
1118
+ resetIdxOnFocus?: boolean;
1119
+ onHoverChange?: (value: string | undefined) => void;
1120
+ enableSearch?: boolean;
1121
+ };
1122
+
1123
+ export class Menu {
1124
+ items: MenuItem[] = [];
1125
+ selectedIdx: number = 0;
1126
+ focus: FocusHandle;
1127
+ width = 40;
1128
+ maxHeight = 10;
1129
+ scrollOffset = 0;
1130
+ emptyState?: string;
1131
+ showIdxWhileBlurred: boolean;
1132
+ onHoverChange?: (value: string | undefined) => void;
1133
+ enableSearch: boolean;
1134
+ searchMode: boolean = false;
1135
+ searchQuery: string = '';
1136
+ allItems: { label: string; onSelect: () => void; value?: string }[] = [];
1137
+
1138
+ constructor(props: MenuProps) {
1139
+ this.width = props.width ?? 40;
1140
+ this.maxHeight = props.maxHeight ?? 10;
1141
+ this.focus = props.focus;
1142
+ this.emptyState = props.emptyState;
1143
+ this.showIdxWhileBlurred = props.showIdxWhileBlurred ?? false;
1144
+ this.onHoverChange = props.onHoverChange;
1145
+ this.enableSearch = props.enableSearch ?? false;
1146
+
1147
+ if (props.resetIdxOnFocus) {
1148
+ this.focus.onFocus(() => {
1149
+ this.setSelectedItem(0);
1150
+ });
1151
+ }
1152
+
1153
+ this.focus.onKey((key, keyInfo, propagate) => {
1154
+ if (this.enableSearch && key === '/' && !this.searchMode) {
1155
+ this.searchMode = true;
1156
+ this.searchQuery = '';
1157
+ return;
1158
+ }
1159
+
1160
+ if (this.searchMode) {
1161
+ if (keyInfo.name === 'escape') {
1162
+ this.searchMode = false;
1163
+ this.searchQuery = '';
1164
+ this.items = this.allItems;
1165
+ this.setSelectedItem(0);
1166
+ } else if (keyInfo.name === 'backspace') {
1167
+ this.searchQuery = this.searchQuery.slice(0, -1);
1168
+ this.filterItems();
1169
+ } else if (keyInfo.name === 'up' || keyInfo.name === 'down') {
1170
+ // Allow arrow keys to navigate in search mode
1171
+ } else if (keyInfo.name === 'return') {
1172
+ this.searchMode = false;
1173
+ const item = this.items[this.selectedIdx];
1174
+ if (item) {
1175
+ item.onSelect();
1176
+ } else {
1177
+ // bail out of search if you hit enter on nothing
1178
+ this.items = this.allItems;
1179
+ this.searchQuery = '';
1180
+ this.setSelectedItem(0);
1181
+ this.searchMode = false;
1182
+ }
1183
+ } else if (key && key.length === 1) {
1184
+ this.searchQuery += key;
1185
+ this.filterItems();
1186
+ }
1187
+ if (keyInfo.name === 'up' || keyInfo.name === 'down') {
1188
+ // Continue to navigation logic below
1189
+ } else {
1190
+ return;
1191
+ }
1192
+ }
1193
+
1194
+ if (key === 'j' || keyInfo.name == 'down') {
1195
+ const newIndex = Math.min(
1196
+ this.selectedIdx + 1,
1197
+ this.items.length - 1,
1198
+ );
1199
+ this.setSelectedItem(newIndex);
1200
+ } else if (key === 'k' || keyInfo.name == 'up') {
1201
+ const newIndex = Math.max(this.selectedIdx - 1, 0);
1202
+ this.setSelectedItem(newIndex);
1203
+ } else if (keyInfo.name === 'return' || keyInfo.name === 'right') {
1204
+ this.items[this.selectedIdx]?.onSelect();
1205
+ } else {
1206
+ propagate();
1207
+ }
1208
+ });
1209
+
1210
+ this.allItems = props.items;
1211
+ this.items = props.items;
1212
+ this.setSelectedItem(0);
1213
+ }
1214
+
1215
+ public isSearching(): boolean {
1216
+ return this.searchMode;
1217
+ }
1218
+
1219
+ private filterItems() {
1220
+ if (this.searchQuery === '') {
1221
+ this.items = this.allItems;
1222
+ } else {
1223
+ this.items = this.allItems.filter((item) =>
1224
+ item.label.toLowerCase().includes(this.searchQuery.toLowerCase()),
1225
+ );
1226
+ }
1227
+ this.setSelectedItem(0);
1228
+ }
1229
+
1230
+ private adjustScroll() {
1231
+ if (this.selectedIdx < this.scrollOffset) {
1232
+ this.scrollOffset = this.selectedIdx;
1233
+ } else if (this.selectedIdx >= this.scrollOffset + this.maxHeight) {
1234
+ this.scrollOffset = this.selectedIdx - this.maxHeight + 1;
1235
+ }
1236
+ this.scrollOffset = Math.max(
1237
+ 0,
1238
+ Math.min(this.scrollOffset, this.items.length - this.maxHeight),
1239
+ );
1240
+ }
1241
+
1242
+ addItem(item: { label: string; onSelect: () => void; value?: string }) {
1243
+ this.items.push(item);
1244
+ }
1245
+
1246
+ setSelectedItem(index: number) {
1247
+ const maxIndex = Math.max(0, this.items.length - 1);
1248
+ this.selectedIdx = Math.max(0, Math.min(index, maxIndex));
1249
+ this.adjustScroll();
1250
+ if (this.onHoverChange && this.items[this.selectedIdx]) {
1251
+ this.onHoverChange(this.items[this.selectedIdx].value);
1252
+ }
1253
+ }
1254
+
1255
+ setItemList(items: { label: string; onSelect: () => void }[]) {
1256
+ this.allItems = items;
1257
+ this.items = items;
1258
+ this.setSelectedItem(this.selectedIdx);
1259
+ }
1260
+
1261
+ render(): string {
1262
+ let output = '';
1263
+
1264
+ if (this.searchMode) {
1265
+ const searchLine = (
1266
+ ' Search: ' +
1267
+ this.searchQuery +
1268
+ chalk.inverse(' ')
1269
+ ).padEnd(this.width);
1270
+ output += chalk.hex('#EA570B')(searchLine) + '\n';
1271
+ }
1272
+
1273
+ if (this.items.length === 0) {
1274
+ return output + (this.emptyState ?? chalk.dim('No items'));
1275
+ }
1276
+
1277
+ const hasItemsAbove = this.scrollOffset > 0;
1278
+ const hasItemsBelow =
1279
+ this.scrollOffset + this.maxHeight < this.items.length;
1280
+
1281
+ const visibleItems = this.items.slice(
1282
+ this.scrollOffset,
1283
+ this.scrollOffset + this.maxHeight,
1284
+ );
1285
+ visibleItems.forEach((item, index) => {
1286
+ const actualIndex = this.scrollOffset + index;
1287
+ const isSelected =
1288
+ this.selectedIdx === actualIndex && this.focus.isFocused();
1289
+ const isSelectedButBlurred =
1290
+ this.selectedIdx === actualIndex && !this.focus.isFocused();
1291
+
1292
+ const labelWithSpace = ' ' + item.label;
1293
+ const labelWidth = stringWidth(labelWithSpace);
1294
+ const paddingNeeded = Math.max(0, this.width - 1 - labelWidth);
1295
+ let line = labelWithSpace + ' '.repeat(paddingNeeded) + ' ';
1296
+
1297
+ if (index === 0 && hasItemsAbove) {
1298
+ line = labelWithSpace + ' '.repeat(paddingNeeded) + chalk.dim('▲');
1299
+ } else if (index === visibleItems.length - 1 && hasItemsBelow) {
1300
+ line = labelWithSpace + ' '.repeat(paddingNeeded) + chalk.dim('▼');
1301
+ }
1302
+
1303
+ if (isSelected) {
1304
+ output += chalk.bold.hex('#EA570B').inverse(line) + '\n';
1305
+ } else if (isSelectedButBlurred && this.showIdxWhileBlurred) {
1306
+ output += chalk.bgBlackBright(line) + '\n';
1307
+ } else {
1308
+ output += line + '\n';
1309
+ }
1310
+ });
1311
+ return output;
1312
+ }
1313
+ }
1314
+
1315
+ export interface AppSelectorApi {
1316
+ getDash: () => { apps: App[]; orgs: Org[] };
1317
+ createEphemeralApp: (title: string) => Promise<{
1318
+ appId: string;
1319
+ adminToken: string;
1320
+ }>;
1321
+ getAppsForOrg: (orgId: string) => Promise<{
1322
+ apps: any[];
1323
+ }>;
1324
+ createApp: (
1325
+ title: string,
1326
+ orgId?: string,
1327
+ ) => Promise<{
1328
+ appId: string;
1329
+ adminToken: string;
1330
+ }>;
1331
+ }
1332
+
1333
+ type AppSelectorProps = {
1334
+ allowEphemeral: boolean;
1335
+ allowCreate: boolean;
1336
+ modifyOutput?: (output: string) => string;
1337
+ api: AppSelectorApi;
1338
+ startingMenuIndex?: number;
1339
+ defaultAppName?: string;
1340
+ };
1341
+
1342
+ export class AppSelector extends Prompt<{
1343
+ appId: string;
1344
+ adminToken: string;
1345
+ approach: 'ephemeral' | 'import' | 'create';
1346
+ }> {
1347
+ props: AppSelectorProps;
1348
+ api: AppSelectorApi;
1349
+ dashResponse: { apps: App[]; orgs: Org[] };
1350
+ creatingEphemeral = false;
1351
+ selectedAppName = '';
1352
+ selectedOrg: Org | null = null;
1353
+ appNameInput: TextInput;
1354
+ focus: FocusHandle;
1355
+ appList: Menu;
1356
+ orgList: Menu;
1357
+
1358
+ HEIGHT = 10;
1359
+ RIGHT_WIDTH = 70;
1360
+
1361
+ result(): {
1362
+ appId: string;
1363
+ adminToken: string;
1364
+ approach: 'ephemeral' | 'import' | 'create';
1365
+ } {
1366
+ throw new Error('Method not implemented.');
1367
+ }
1368
+
1369
+ rightView(): string {
1370
+ let inner = '';
1371
+
1372
+ // Use hoveredLeftMenuItem to determine what to show
1373
+ if (this.focus.getFocused() === 'appList') {
1374
+ inner = this.appList.render();
1375
+ }
1376
+
1377
+ if (this.focus.getFocused() === 'pickOrg') {
1378
+ inner = this.orgList.render();
1379
+ }
1380
+
1381
+ if (this.focus.getFocused() === 'newApp') {
1382
+ return boxen(this.appNameInput.render('idle'), {
1383
+ height: this.HEIGHT,
1384
+ width: this.RIGHT_WIDTH,
1385
+ borderStyle: 'none',
1386
+ padding: {
1387
+ left: 2,
1388
+ top: 1,
1389
+ },
1390
+ textAlignment: 'left',
1391
+ });
1392
+ }
1393
+
1394
+ return boxen(inner, {
1395
+ height: this.HEIGHT,
1396
+ width: this.RIGHT_WIDTH,
1397
+ borderStyle: 'none',
1398
+ });
1399
+ }
1400
+
1401
+ render(status: 'idle' | 'submitted' | 'aborted'): string {
1402
+ if (status === 'submitted') {
1403
+ return boxen(' Selected App: ' + this.selectedAppName, {
1404
+ width: 50,
1405
+ dimBorder: true,
1406
+ textAlignment: 'center',
1407
+ });
1408
+ }
1409
+
1410
+ const rightSide = this.rightView();
1411
+
1412
+ const curFocus = this.focus.getFocused();
1413
+ let left =
1414
+ curFocus === 'appList'
1415
+ ? 'Select an app '
1416
+ : curFocus === 'pickOrg'
1417
+ ? 'Select an org '
1418
+ : curFocus === 'newApp'
1419
+ ? 'Create app '
1420
+ : '';
1421
+
1422
+ if (this.selectedOrg?.title) {
1423
+ left += `(${this.selectedOrg.title}) `;
1424
+ }
1425
+
1426
+ const keybindings: string[] = [];
1427
+
1428
+ if (curFocus === 'newApp') {
1429
+ keybindings.push('<enter>: create app');
1430
+ keybindings.push('<esc>: back');
1431
+ if (this.props.allowEphemeral) {
1432
+ keybindings.push('<tab>: toggle temporary app');
1433
+ }
1434
+ }
1435
+
1436
+ if (curFocus === 'appList') {
1437
+ keybindings.push('<tab>: change org');
1438
+ keybindings.push('<enter>: select app');
1439
+ if (this.appList.isSearching()) {
1440
+ keybindings.push('<esc>: cancel search');
1441
+ } else {
1442
+ keybindings.push('/: search');
1443
+ }
1444
+ }
1445
+
1446
+ if (curFocus === 'pickOrg') {
1447
+ keybindings.push('<tab>/<esc>: back');
1448
+ keybindings.push('<enter>: select org');
1449
+ if (this.orgList.isSearching()) {
1450
+ keybindings.push('<esc>: cancel search');
1451
+ } else {
1452
+ keybindings.push('/: search');
1453
+ }
1454
+ }
1455
+
1456
+ return (
1457
+ boxen(rightSide, {
1458
+ title: left,
1459
+ dimBorder: true,
1460
+ }) +
1461
+ '\n' +
1462
+ chalk.dim(' ' + keybindings.join(' '))
1463
+ );
1464
+ }
1465
+
1466
+ createAppList = (apps: App[]): MenuItem[] => {
1467
+ const items: MenuItem[] = [];
1468
+
1469
+ if (this.props.allowCreate) {
1470
+ items.push({
1471
+ label: chalk.italic('+ New App'),
1472
+ onSelect: () => {
1473
+ this.focus.setFocus('newApp');
1474
+ },
1475
+ });
1476
+ }
1477
+
1478
+ apps
1479
+ .sort(
1480
+ (a, b) =>
1481
+ new Date(b.created_at).getTime() - new Date(a.created_at).getTime(),
1482
+ )
1483
+ .forEach((app) => {
1484
+ items.push({
1485
+ label: app.title,
1486
+ onSelect: () => {
1487
+ this.selectedAppName = app.title;
1488
+ this.terminal?.resolve({
1489
+ status: 'submitted',
1490
+ data: {
1491
+ appId: app.id,
1492
+ approach: 'import',
1493
+ adminToken: app.admin_token,
1494
+ },
1495
+ });
1496
+ },
1497
+ });
1498
+ });
1499
+
1500
+ return items;
1501
+ };
1502
+
1503
+ constructor(props: AppSelectorProps) {
1504
+ super(props.modifyOutput);
1505
+ this.props = props;
1506
+ this.api = props.api;
1507
+ this.dashResponse = this.api.getDash();
1508
+ this.focus = new Focus(this).root();
1509
+
1510
+ this.appList = new Menu({
1511
+ enableSearch: true,
1512
+ width: this.RIGHT_WIDTH,
1513
+ focus: this.focus.child('appList').onKey((_key, keyInfo) => {
1514
+ if (keyInfo.name === 'tab') {
1515
+ this.focus.setFocus('pickOrg');
1516
+ }
1517
+ }),
1518
+ items: this.createAppList(this.dashResponse.apps),
1519
+ emptyState: ' No Apps ',
1520
+ });
1521
+
1522
+ this.orgList = new Menu({
1523
+ enableSearch: true,
1524
+ width: this.RIGHT_WIDTH,
1525
+ focus: this.focus.child('pickOrg').onKey((_, keyInfo) => {
1526
+ if (keyInfo.name === 'escape' || keyInfo.name === 'tab') {
1527
+ this.focus.setFocus('appList');
1528
+ }
1529
+ }),
1530
+ items: this.dashResponse.orgs.map((org) => ({
1531
+ label: org.title,
1532
+ onSelect: () => {
1533
+ this.selectedOrg = org;
1534
+ this.api.getAppsForOrg(org.id).then((apps) => {
1535
+ this.appList.setItemList(this.createAppList(apps.apps));
1536
+ this.focus.setFocus('appList');
1537
+ this.requestLayout();
1538
+ });
1539
+ },
1540
+ })),
1541
+ });
1542
+
1543
+ this.orgList.addItem({
1544
+ label: 'Personal',
1545
+ onSelect: () => {
1546
+ this.selectedOrg = null;
1547
+ this.appList.setItemList(this.createAppList(this.dashResponse.apps));
1548
+ this.focus.setFocus('appList');
1549
+ this.requestLayout();
1550
+ },
1551
+ });
1552
+
1553
+ this.focus.setFocus('appList');
1554
+
1555
+ const defaultAppName = props.defaultAppName || 'My Awesome App';
1556
+ this.appNameInput = new TextInput({
1557
+ prompt: 'Enter New App Name',
1558
+ placeholder: defaultAppName,
1559
+ defaultValue: defaultAppName,
1560
+ headless: true,
1561
+ });
1562
+
1563
+ this.focus.child('newApp').onKey((key, keyInfo) => {
1564
+ if (keyInfo.name === 'escape' || keyInfo.name == 'left') {
1565
+ this.focus.setFocus('appList');
1566
+ }
1567
+ if (keyInfo.name === 'return') {
1568
+ const name = this.appNameInput.value || defaultAppName;
1569
+ if (this.creatingEphemeral) {
1570
+ this.props.api.createEphemeralApp(name).then((pair) => {
1571
+ this.selectedAppName = name;
1572
+ this.terminal?.resolve({
1573
+ status: 'submitted',
1574
+ data: {
1575
+ ...pair,
1576
+ approach: 'ephemeral',
1577
+ },
1578
+ });
1579
+ });
1580
+ } else {
1581
+ this.props.api
1582
+ .createApp(name, this.selectedOrg?.id)
1583
+ .then((pair) => {
1584
+ this.selectedAppName = name;
1585
+ this.terminal?.resolve({
1586
+ status: 'submitted',
1587
+ data: {
1588
+ ...pair,
1589
+ approach: 'create',
1590
+ },
1591
+ });
1592
+ });
1593
+ }
1594
+ } else if (keyInfo.name === 'tab' && this.props.allowEphemeral) {
1595
+ this.creatingEphemeral = !this.creatingEphemeral;
1596
+ this.appNameInput.setPrompt(
1597
+ this.creatingEphemeral
1598
+ ? `Enter New ${chalk.bold('Temporary')} App Name`
1599
+ : 'Enter New App Name',
1600
+ );
1601
+ } else {
1602
+ this.appNameInput.input(key, keyInfo);
1603
+ }
1604
+ });
1605
+
1606
+ this.on('attach', (terminal) => {
1607
+ this.terminal?.setAllowInteraction(false);
1608
+ terminal.toggleCursor('hide');
1609
+ });
1610
+ this.on('detach', (terminal) => {
1611
+ terminal.toggleCursor('show');
1612
+ });
1613
+ }
1614
+ }
1615
+ }