convex 1.48.0 → 1.49.0-alpha.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 (248) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/browser.bundle.js +1 -1
  3. package/dist/browser.bundle.js.map +1 -1
  4. package/dist/cjs/bundler/context.js +7 -5
  5. package/dist/cjs/bundler/context.js.map +2 -2
  6. package/dist/cjs/bundler/fs.js +47 -6
  7. package/dist/cjs/bundler/fs.js.map +2 -2
  8. package/dist/cjs/cli/account.js +237 -0
  9. package/dist/cjs/cli/account.js.map +7 -0
  10. package/dist/cjs/cli/configure.js +15 -2
  11. package/dist/cjs/cli/configure.js.map +2 -2
  12. package/dist/cjs/cli/lib/accountManagement.js +196 -0
  13. package/dist/cjs/cli/lib/accountManagement.js.map +7 -0
  14. package/dist/cjs/cli/lib/accounts.js +334 -0
  15. package/dist/cjs/cli/lib/accounts.js.map +7 -0
  16. package/dist/cjs/cli/lib/deployKeyWarning.js +43 -0
  17. package/dist/cjs/cli/lib/deployKeyWarning.js.map +7 -0
  18. package/dist/cjs/cli/lib/deployment.js +2 -0
  19. package/dist/cjs/cli/lib/deployment.js.map +2 -2
  20. package/dist/cjs/cli/lib/deploymentSelection.js +27 -18
  21. package/dist/cjs/cli/lib/deploymentSelection.js.map +2 -2
  22. package/dist/cjs/cli/lib/envvars.js +32 -0
  23. package/dist/cjs/cli/lib/envvars.js.map +2 -2
  24. package/dist/cjs/cli/lib/fsUtils.js +1 -1
  25. package/dist/cjs/cli/lib/fsUtils.js.map +2 -2
  26. package/dist/cjs/cli/lib/generateDocs.js +1 -1
  27. package/dist/cjs/cli/lib/generateDocs.js.map +1 -1
  28. package/dist/cjs/cli/lib/login.js +42 -4
  29. package/dist/cjs/cli/lib/login.js.map +3 -3
  30. package/dist/cjs/cli/lib/mcp/requestContext.js +5 -1
  31. package/dist/cjs/cli/lib/mcp/requestContext.js.map +2 -2
  32. package/dist/cjs/cli/lib/plugins/claudeCode.js +188 -0
  33. package/dist/cjs/cli/lib/plugins/claudeCode.js.map +7 -0
  34. package/dist/cjs/cli/lib/plugins/codex.js +167 -0
  35. package/dist/cjs/cli/lib/plugins/codex.js.map +7 -0
  36. package/dist/cjs/cli/lib/plugins/harness.js +50 -0
  37. package/dist/cjs/cli/lib/plugins/harness.js.map +7 -0
  38. package/dist/cjs/cli/lib/plugins/marketplaces.js +76 -0
  39. package/dist/cjs/cli/lib/plugins/marketplaces.js.map +7 -0
  40. package/dist/cjs/cli/lib/plugins/skills.js +98 -0
  41. package/dist/cjs/cli/lib/plugins/skills.js.map +7 -0
  42. package/dist/cjs/cli/lib/plugins/utils.js +125 -0
  43. package/dist/cjs/cli/lib/plugins/utils.js.map +7 -0
  44. package/dist/cjs/cli/lib/utils/utils.js +18 -9
  45. package/dist/cjs/cli/lib/utils/utils.js.map +2 -2
  46. package/dist/cjs/cli/login.js +213 -114
  47. package/dist/cjs/cli/login.js.map +3 -3
  48. package/dist/cjs/cli/logout.js +113 -12
  49. package/dist/cjs/cli/logout.js.map +3 -3
  50. package/dist/cjs/cli/mcp.js +43 -10
  51. package/dist/cjs/cli/mcp.js.map +3 -3
  52. package/dist/cjs/cli/plugins.js +114 -0
  53. package/dist/cjs/cli/plugins.js.map +7 -0
  54. package/dist/cjs/cli/program.js +3 -1
  55. package/dist/cjs/cli/program.js.map +2 -2
  56. package/dist/cjs/index.js +1 -1
  57. package/dist/cjs/index.js.map +1 -1
  58. package/dist/cjs/server/impl/actions_impl.js.map +2 -2
  59. package/dist/cjs-types/bundler/context.d.ts +5 -0
  60. package/dist/cjs-types/bundler/context.d.ts.map +1 -1
  61. package/dist/cjs-types/bundler/fs.d.ts +7 -2
  62. package/dist/cjs-types/bundler/fs.d.ts.map +1 -1
  63. package/dist/cjs-types/cli/account.d.ts +6 -0
  64. package/dist/cjs-types/cli/account.d.ts.map +1 -0
  65. package/dist/cjs-types/cli/configure.d.ts.map +1 -1
  66. package/dist/cjs-types/cli/lib/accountManagement.d.ts +10 -0
  67. package/dist/cjs-types/cli/lib/accountManagement.d.ts.map +1 -0
  68. package/dist/cjs-types/cli/lib/accounts.d.ts +59 -0
  69. package/dist/cjs-types/cli/lib/accounts.d.ts.map +1 -0
  70. package/dist/cjs-types/cli/lib/deployKeyWarning.d.ts +3 -0
  71. package/dist/cjs-types/cli/lib/deployKeyWarning.d.ts.map +1 -0
  72. package/dist/cjs-types/cli/lib/deployment.d.ts +2 -0
  73. package/dist/cjs-types/cli/lib/deployment.d.ts.map +1 -1
  74. package/dist/cjs-types/cli/lib/deploymentSelection.d.ts +2 -1
  75. package/dist/cjs-types/cli/lib/deploymentSelection.d.ts.map +1 -1
  76. package/dist/cjs-types/cli/lib/envvars.d.ts +6 -0
  77. package/dist/cjs-types/cli/lib/envvars.d.ts.map +1 -1
  78. package/dist/cjs-types/cli/lib/fsUtils.d.ts +1 -0
  79. package/dist/cjs-types/cli/lib/fsUtils.d.ts.map +1 -1
  80. package/dist/cjs-types/cli/lib/login.d.ts +3 -1
  81. package/dist/cjs-types/cli/lib/login.d.ts.map +1 -1
  82. package/dist/cjs-types/cli/lib/mcp/requestContext.d.ts.map +1 -1
  83. package/dist/cjs-types/cli/lib/mcp/tools/insights.d.ts +2 -2
  84. package/dist/cjs-types/cli/lib/plugins/claudeCode.d.ts +25 -0
  85. package/dist/cjs-types/cli/lib/plugins/claudeCode.d.ts.map +1 -0
  86. package/dist/cjs-types/cli/lib/plugins/codex.d.ts +25 -0
  87. package/dist/cjs-types/cli/lib/plugins/codex.d.ts.map +1 -0
  88. package/dist/cjs-types/cli/lib/plugins/harness.d.ts +33 -0
  89. package/dist/cjs-types/cli/lib/plugins/harness.d.ts.map +1 -0
  90. package/dist/cjs-types/cli/lib/plugins/marketplaces.d.ts +40 -0
  91. package/dist/cjs-types/cli/lib/plugins/marketplaces.d.ts.map +1 -0
  92. package/dist/cjs-types/cli/lib/plugins/skills.d.ts +18 -0
  93. package/dist/cjs-types/cli/lib/plugins/skills.d.ts.map +1 -0
  94. package/dist/cjs-types/cli/lib/plugins/utils.d.ts +21 -0
  95. package/dist/cjs-types/cli/lib/plugins/utils.d.ts.map +1 -0
  96. package/dist/cjs-types/cli/lib/utils/utils.d.ts +5 -0
  97. package/dist/cjs-types/cli/lib/utils/utils.d.ts.map +1 -1
  98. package/dist/cjs-types/cli/login.d.ts +21 -0
  99. package/dist/cjs-types/cli/login.d.ts.map +1 -1
  100. package/dist/cjs-types/cli/logout.d.ts +8 -1
  101. package/dist/cjs-types/cli/logout.d.ts.map +1 -1
  102. package/dist/cjs-types/cli/mcp.d.ts.map +1 -1
  103. package/dist/cjs-types/cli/plugins.d.ts +3 -0
  104. package/dist/cjs-types/cli/plugins.d.ts.map +1 -0
  105. package/dist/cjs-types/cli/program.d.ts.map +1 -1
  106. package/dist/cjs-types/index.d.ts +1 -1
  107. package/dist/cjs-types/index.d.ts.map +1 -1
  108. package/dist/cjs-types/server/impl/actions_impl.d.ts +3 -2
  109. package/dist/cjs-types/server/impl/actions_impl.d.ts.map +1 -1
  110. package/dist/cli.bundle.cjs +10059 -7094
  111. package/dist/cli.bundle.cjs.map +4 -4
  112. package/dist/esm/bundler/context.js +7 -5
  113. package/dist/esm/bundler/context.js.map +2 -2
  114. package/dist/esm/bundler/fs.js +47 -6
  115. package/dist/esm/bundler/fs.js.map +2 -2
  116. package/dist/esm/cli/account.js +221 -0
  117. package/dist/esm/cli/account.js.map +7 -0
  118. package/dist/esm/cli/configure.js +15 -2
  119. package/dist/esm/cli/configure.js.map +2 -2
  120. package/dist/esm/cli/lib/accountManagement.js +179 -0
  121. package/dist/esm/cli/lib/accountManagement.js.map +7 -0
  122. package/dist/esm/cli/lib/accounts.js +289 -0
  123. package/dist/esm/cli/lib/accounts.js.map +7 -0
  124. package/dist/esm/cli/lib/deployKeyWarning.js +27 -0
  125. package/dist/esm/cli/lib/deployKeyWarning.js.map +7 -0
  126. package/dist/esm/cli/lib/deployment.js +2 -2
  127. package/dist/esm/cli/lib/deployment.js.map +2 -2
  128. package/dist/esm/cli/lib/deploymentSelection.js +35 -20
  129. package/dist/esm/cli/lib/deploymentSelection.js.map +2 -2
  130. package/dist/esm/cli/lib/envvars.js +35 -1
  131. package/dist/esm/cli/lib/envvars.js.map +2 -2
  132. package/dist/esm/cli/lib/fsUtils.js +1 -1
  133. package/dist/esm/cli/lib/fsUtils.js.map +2 -2
  134. package/dist/esm/cli/lib/generateDocs.js +1 -1
  135. package/dist/esm/cli/lib/generateDocs.js.map +1 -1
  136. package/dist/esm/cli/lib/login.js +52 -4
  137. package/dist/esm/cli/lib/login.js.map +3 -3
  138. package/dist/esm/cli/lib/mcp/requestContext.js +5 -1
  139. package/dist/esm/cli/lib/mcp/requestContext.js.map +2 -2
  140. package/dist/esm/cli/lib/plugins/claudeCode.js +160 -0
  141. package/dist/esm/cli/lib/plugins/claudeCode.js.map +7 -0
  142. package/dist/esm/cli/lib/plugins/codex.js +140 -0
  143. package/dist/esm/cli/lib/plugins/codex.js.map +7 -0
  144. package/dist/esm/cli/lib/plugins/harness.js +17 -0
  145. package/dist/esm/cli/lib/plugins/harness.js.map +7 -0
  146. package/dist/esm/cli/lib/plugins/marketplaces.js +53 -0
  147. package/dist/esm/cli/lib/plugins/marketplaces.js.map +7 -0
  148. package/dist/esm/cli/lib/plugins/skills.js +65 -0
  149. package/dist/esm/cli/lib/plugins/skills.js.map +7 -0
  150. package/dist/esm/cli/lib/plugins/utils.js +86 -0
  151. package/dist/esm/cli/lib/plugins/utils.js.map +7 -0
  152. package/dist/esm/cli/lib/utils/utils.js +16 -9
  153. package/dist/esm/cli/lib/utils/utils.js.map +2 -2
  154. package/dist/esm/cli/login.js +225 -112
  155. package/dist/esm/cli/login.js.map +3 -3
  156. package/dist/esm/cli/logout.js +110 -13
  157. package/dist/esm/cli/logout.js.map +2 -2
  158. package/dist/esm/cli/mcp.js +34 -10
  159. package/dist/esm/cli/mcp.js.map +2 -2
  160. package/dist/esm/cli/plugins.js +99 -0
  161. package/dist/esm/cli/plugins.js.map +7 -0
  162. package/dist/esm/cli/program.js +3 -1
  163. package/dist/esm/cli/program.js.map +2 -2
  164. package/dist/esm/index.js +1 -1
  165. package/dist/esm/index.js.map +1 -1
  166. package/dist/esm/server/impl/actions_impl.js.map +2 -2
  167. package/dist/esm-types/bundler/context.d.ts +5 -0
  168. package/dist/esm-types/bundler/context.d.ts.map +1 -1
  169. package/dist/esm-types/bundler/fs.d.ts +7 -2
  170. package/dist/esm-types/bundler/fs.d.ts.map +1 -1
  171. package/dist/esm-types/cli/account.d.ts +6 -0
  172. package/dist/esm-types/cli/account.d.ts.map +1 -0
  173. package/dist/esm-types/cli/configure.d.ts.map +1 -1
  174. package/dist/esm-types/cli/lib/accountManagement.d.ts +10 -0
  175. package/dist/esm-types/cli/lib/accountManagement.d.ts.map +1 -0
  176. package/dist/esm-types/cli/lib/accounts.d.ts +59 -0
  177. package/dist/esm-types/cli/lib/accounts.d.ts.map +1 -0
  178. package/dist/esm-types/cli/lib/deployKeyWarning.d.ts +3 -0
  179. package/dist/esm-types/cli/lib/deployKeyWarning.d.ts.map +1 -0
  180. package/dist/esm-types/cli/lib/deployment.d.ts +2 -0
  181. package/dist/esm-types/cli/lib/deployment.d.ts.map +1 -1
  182. package/dist/esm-types/cli/lib/deploymentSelection.d.ts +2 -1
  183. package/dist/esm-types/cli/lib/deploymentSelection.d.ts.map +1 -1
  184. package/dist/esm-types/cli/lib/envvars.d.ts +6 -0
  185. package/dist/esm-types/cli/lib/envvars.d.ts.map +1 -1
  186. package/dist/esm-types/cli/lib/fsUtils.d.ts +1 -0
  187. package/dist/esm-types/cli/lib/fsUtils.d.ts.map +1 -1
  188. package/dist/esm-types/cli/lib/login.d.ts +3 -1
  189. package/dist/esm-types/cli/lib/login.d.ts.map +1 -1
  190. package/dist/esm-types/cli/lib/mcp/requestContext.d.ts.map +1 -1
  191. package/dist/esm-types/cli/lib/mcp/tools/insights.d.ts +2 -2
  192. package/dist/esm-types/cli/lib/plugins/claudeCode.d.ts +25 -0
  193. package/dist/esm-types/cli/lib/plugins/claudeCode.d.ts.map +1 -0
  194. package/dist/esm-types/cli/lib/plugins/codex.d.ts +25 -0
  195. package/dist/esm-types/cli/lib/plugins/codex.d.ts.map +1 -0
  196. package/dist/esm-types/cli/lib/plugins/harness.d.ts +33 -0
  197. package/dist/esm-types/cli/lib/plugins/harness.d.ts.map +1 -0
  198. package/dist/esm-types/cli/lib/plugins/marketplaces.d.ts +40 -0
  199. package/dist/esm-types/cli/lib/plugins/marketplaces.d.ts.map +1 -0
  200. package/dist/esm-types/cli/lib/plugins/skills.d.ts +18 -0
  201. package/dist/esm-types/cli/lib/plugins/skills.d.ts.map +1 -0
  202. package/dist/esm-types/cli/lib/plugins/utils.d.ts +21 -0
  203. package/dist/esm-types/cli/lib/plugins/utils.d.ts.map +1 -0
  204. package/dist/esm-types/cli/lib/utils/utils.d.ts +5 -0
  205. package/dist/esm-types/cli/lib/utils/utils.d.ts.map +1 -1
  206. package/dist/esm-types/cli/login.d.ts +21 -0
  207. package/dist/esm-types/cli/login.d.ts.map +1 -1
  208. package/dist/esm-types/cli/logout.d.ts +8 -1
  209. package/dist/esm-types/cli/logout.d.ts.map +1 -1
  210. package/dist/esm-types/cli/mcp.d.ts.map +1 -1
  211. package/dist/esm-types/cli/plugins.d.ts +3 -0
  212. package/dist/esm-types/cli/plugins.d.ts.map +1 -0
  213. package/dist/esm-types/cli/program.d.ts.map +1 -1
  214. package/dist/esm-types/index.d.ts +1 -1
  215. package/dist/esm-types/index.d.ts.map +1 -1
  216. package/dist/esm-types/server/impl/actions_impl.d.ts +3 -2
  217. package/dist/esm-types/server/impl/actions_impl.d.ts.map +1 -1
  218. package/dist/react.bundle.js +1 -1
  219. package/dist/react.bundle.js.map +1 -1
  220. package/package.json +2 -2
  221. package/src/bundler/context.ts +9 -5
  222. package/src/bundler/fs.ts +54 -5
  223. package/src/cli/account.ts +320 -0
  224. package/src/cli/configure.ts +18 -2
  225. package/src/cli/lib/accountManagement.ts +262 -0
  226. package/src/cli/lib/accounts.ts +400 -0
  227. package/src/cli/lib/deployKeyWarning.ts +43 -0
  228. package/src/cli/lib/deployment.ts +2 -2
  229. package/src/cli/lib/deploymentSelection.ts +37 -18
  230. package/src/cli/lib/envvars.ts +43 -1
  231. package/src/cli/lib/fsUtils.ts +5 -2
  232. package/src/cli/lib/generateDocs.ts +1 -1
  233. package/src/cli/lib/login.ts +64 -3
  234. package/src/cli/lib/mcp/requestContext.ts +7 -1
  235. package/src/cli/lib/plugins/claudeCode.ts +201 -0
  236. package/src/cli/lib/plugins/codex.ts +177 -0
  237. package/src/cli/lib/plugins/harness.ts +49 -0
  238. package/src/cli/lib/plugins/marketplaces.ts +94 -0
  239. package/src/cli/lib/plugins/skills.ts +101 -0
  240. package/src/cli/lib/plugins/utils.ts +121 -0
  241. package/src/cli/lib/utils/utils.ts +19 -15
  242. package/src/cli/login.ts +293 -168
  243. package/src/cli/logout.ts +142 -17
  244. package/src/cli/mcp.ts +48 -11
  245. package/src/cli/plugins.ts +154 -0
  246. package/src/cli/program.ts +4 -0
  247. package/src/index.ts +1 -1
  248. package/src/server/impl/actions_impl.ts +3 -2
package/src/cli/login.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Command, Option } from "@commander-js/extra-typings";
2
- import * as dotenv from "dotenv";
3
2
  import { BigBrainAuth, Context, oneoffContext } from "../bundler/context.js";
4
3
  import { logFinishedStep, logMessage, logWarning } from "../bundler/log.js";
5
4
  import {
@@ -21,12 +20,7 @@ import {
21
20
  teamDashboardUrl,
22
21
  } from "./lib/dashboard.js";
23
22
  import { promptSearch, promptYesNo } from "./lib/utils/prompts.js";
24
- import {
25
- CONVEX_DEPLOY_KEY_ENV_VAR_NAME,
26
- CONVEX_DEPLOYMENT_TOKEN_ENV_VAR_NAME,
27
- ENV_VAR_FILE_PATH,
28
- validateOrSelectTeam,
29
- } from "./lib/utils/utils.js";
23
+ import { validateOrSelectTeam } from "./lib/utils/utils.js";
30
24
  import {
31
25
  selectProject,
32
26
  updateEnvAndConfigForDeploymentSelection,
@@ -40,31 +34,24 @@ import {
40
34
  removeAnonymousPrefix,
41
35
  } from "./lib/deployment.js";
42
36
  import {
43
- readGlobalConfig,
44
37
  globalConfigPath,
38
+ readGlobalConfig,
45
39
  } from "./lib/utils/globalConfig.js";
46
40
  import { getTeamsForUser } from "./lib/api.js";
47
-
48
- /**
49
- * The env file a deploy key came from, or null if it came from the shell.
50
- * `dotenv` doesn't overwrite variables that are already set, so a value from
51
- * the shell outranks these files -- match on the value to tell them apart.
52
- */
53
- function deployKeyEnvFile(
54
- ctx: Context,
55
- envVarName: string,
56
- value: string,
57
- ): string | null {
58
- for (const file of [ENV_VAR_FILE_PATH, ".env"]) {
59
- if (!ctx.fs.exists(file)) {
60
- continue;
61
- }
62
- if (dotenv.parse(ctx.fs.readUtf8File(file))[envVarName] === value) {
63
- return file;
64
- }
65
- }
66
- return null;
67
- }
41
+ import {
42
+ getAccountSelection,
43
+ initializeDefaultAccount,
44
+ legacyAuthForFirstAccount,
45
+ accountConfigPath,
46
+ readAccountAuth,
47
+ writeAccountCredentials,
48
+ } from "./lib/accounts.js";
49
+ import {
50
+ getDeployKeySource,
51
+ loadProjectEnvironmentVariables,
52
+ } from "./lib/envvars.js";
53
+ import { bindAccountToDirectory } from "./lib/accountManagement.js";
54
+ import { warnIfDeployKeyOverridesLogin } from "./lib/deployKeyWarning.js";
68
55
 
69
56
  /**
70
57
  * A project or deployment key in the environment outranks the account token for
@@ -84,11 +71,7 @@ async function reportDeployKeyInWorkingDirectory(
84
71
  return;
85
72
  }
86
73
  const key = auth.kind === "projectKey" ? auth.projectKey : auth.deploymentKey;
87
- const envVarName = process.env[CONVEX_DEPLOY_KEY_ENV_VAR_NAME]
88
- ? CONVEX_DEPLOY_KEY_ENV_VAR_NAME
89
- : CONVEX_DEPLOYMENT_TOKEN_ENV_VAR_NAME;
90
- const envFile = deployKeyEnvFile(ctx, envVarName, key);
91
- const source = envFile ?? "shell environment";
74
+ const { envVarName, source } = getDeployKeySource(key);
92
75
  // Big Brain only strips a `project:`/`team:` prefix itself, so a deployment
93
76
  // key authorizes only without its `dev:`/`prod:` prefix. Split on the first
94
77
  // `|` like the server does, so a secret containing one survives.
@@ -100,149 +83,291 @@ async function reportDeployKeyInWorkingDirectory(
100
83
  logWarning(`Working Directory: Invalid ${envVarName} in ${source}`);
101
84
  }
102
85
 
103
- const loginStatus = new Command("status")
104
- .description("Check login status and list accessible teams")
105
- .allowExcessArguments(false)
106
- .action(async () => {
107
- const ctx = await oneoffContext({
108
- url: undefined,
109
- adminKey: undefined,
110
- envFile: undefined,
111
- });
112
-
113
- // `_updateBigBrainAuth` below replaces any deploy key in `ctx` with the
114
- // account token, so read the deploy key first.
115
- const auth = ctx.bigBrainAuth();
116
-
117
- const globalConfig = readGlobalConfig(ctx);
118
- if (globalConfig === null) {
119
- logMessage(`No Convex account token found in: ${globalConfigPath()}`);
120
- logMessage("Status: Not logged in");
121
- await reportDeployKeyInWorkingDirectory(ctx, auth);
122
- return;
123
- }
124
- logMessage(`Convex account token found in: ${globalConfigPath()}`);
125
-
126
- const accessToken = globalConfig.accessToken;
127
- if (!(await isAuthorizedHeader(ctx, `Bearer ${accessToken}`))) {
128
- logMessage("Status: Not logged in");
129
- await reportDeployKeyInWorkingDirectory(ctx, auth);
130
- return;
131
- }
132
-
133
- logMessage("Status: Logged in");
134
- // Describe the account, even if a deploy key outranks it for other commands.
135
- ctx._updateBigBrainAuth({
136
- kind: "accessToken",
137
- header: `Bearer ${accessToken}`,
138
- accessToken,
139
- });
140
- const teams = await getTeamsForUser(ctx);
141
- logMessage(
142
- `Teams: ${teams.length} team${teams.length === 1 ? "" : "s"} accessible`,
143
- );
144
- for (const team of teams) {
145
- logMessage(` - ${team.name} (${team.slug})`);
146
- }
147
- await reportDeployKeyInWorkingDirectory(ctx, auth);
148
- });
149
-
150
- export const login = new Command("login")
151
- .description("Login to Convex")
152
- .allowExcessArguments(false)
153
- .option(
154
- "--device-name <name>",
155
- "Provide a name for the device being authorized",
156
- )
157
- .option(
158
- "-f, --force",
159
- "Proceed with login even if a valid access token already exists for this device",
160
- )
161
- .option(
162
- "--no-open",
163
- "Don't automatically open the login link in the default browser",
164
- )
165
- .addOption(
166
- new Option(
167
- "--login-flow <mode>",
168
- `How to log in; defaults to guessing based on the environment.`,
86
+ function createLoginStatusCommand() {
87
+ return new Command("status")
88
+ .description(
89
+ "Check login status and list accessible teams. Exits with code 1 when not logged in.",
169
90
  )
170
- .choices(["paste", "auto", "poll"] as const)
171
- .default("auto" as const),
172
- )
173
- .addOption(new Option("--link-deployments").hideHelp())
174
- // These options are hidden from the help/usage message, but allow overriding settings for testing.
175
- // Change the auth credentials with the auth provider
176
- .addOption(new Option("--override-auth-url <url>").hideHelp())
177
- .addOption(new Option("--override-auth-client <id>").hideHelp())
178
- .addOption(new Option("--override-auth-username <username>").hideHelp())
179
- .addOption(new Option("--override-auth-password <password>").hideHelp())
180
- // Skip the auth provider login and directly use this access token
181
- .addOption(new Option("--override-access-token <token>").hideHelp())
182
- // Automatically accept opt ins without prompting
183
- .addOption(new Option("--accept-opt-ins").hideHelp())
184
- // Dump the access token from the auth provider and skip authorization with Convex
185
- .addOption(new Option("--dump-access-token").hideHelp())
186
- // Hidden option for tests to check if the user is logged in.
187
- .addOption(new Option("--check-login").hideHelp())
188
- // Redirect to Vercel SSO integration URL
189
- .addOption(
190
- new Option(
191
- "--vercel",
192
- "Redirect to Vercel SSO integration for login",
193
- ).hideHelp(),
194
- )
195
- // Override the Vercel URL slug (defaults to 'convex')
196
- .addOption(new Option("--vercel-override <slug>").hideHelp())
197
- .addCommand(loginStatus)
198
- .addHelpCommand(false)
199
- .action(async (options, cmd: Command) => {
200
- const ctx = await oneoffContext({
201
- url: undefined,
202
- adminKey: undefined,
203
- envFile: undefined,
204
- });
205
- if (
206
- !options.force &&
207
- (await checkAuthorization(ctx, !!options.acceptOptIns))
208
- ) {
209
- logFinishedStep(
210
- "This device has previously been authorized and is ready for use with Convex.",
211
- );
212
- await handleLinkingDeployments(ctx, {
213
- interactive: !!options.linkDeployments,
91
+ .allowExcessArguments(false)
92
+ .action(async () => {
93
+ const ctx = await oneoffContext({
94
+ url: undefined,
95
+ adminKey: undefined,
96
+ envFile: undefined,
214
97
  });
215
- return;
216
- }
217
- if (!options.force && options.checkLogin) {
218
- const isLoggedIn = await checkAuthorization(ctx, !!options.acceptOptIns);
219
- if (!isLoggedIn) {
220
- return ctx.crash({
98
+
99
+ // Keep the directory's deploy key for separate reporting after selecting
100
+ // the account whose teams will be listed.
101
+ const auth = ctx.bigBrainAuth();
102
+ let accountAuth = auth?.kind === "accessToken" ? auth : null;
103
+ if (accountAuth === null) {
104
+ const account = await getAccountSelection(ctx);
105
+ if (account !== null) {
106
+ accountAuth = await readAccountAuth(ctx, account);
107
+ } else {
108
+ const globalConfig = readGlobalConfig(ctx);
109
+ if (globalConfig !== null) {
110
+ accountAuth = {
111
+ kind: "accessToken",
112
+ accessToken: globalConfig.accessToken,
113
+ header: `Bearer ${globalConfig.accessToken}`,
114
+ account: null,
115
+ };
116
+ }
117
+ }
118
+ }
119
+ if (accountAuth === null) {
120
+ logMessage(`No Convex account token found in: ${globalConfigPath()}`);
121
+ logMessage("Status: Not logged in");
122
+ await reportDeployKeyInWorkingDirectory(ctx, auth);
123
+ return await ctx.crash({
221
124
  exitCode: 1,
222
125
  errorType: "fatal",
223
- errForSentry: "You are not logged in.",
224
- printedMessage: "You are not logged in.",
126
+ printedMessage: null,
225
127
  });
226
128
  }
227
- }
228
- if (!!options.overrideAuthUsername !== !!options.overrideAuthPassword) {
229
- cmd.error(
230
- "If overriding credentials, both username and password must be provided",
231
- );
232
- }
129
+ warnIfDeployKeyOverridesLogin(ctx, true);
130
+ if (accountAuth.account) {
131
+ logMessage(`Account: ${accountAuth.account.name}`);
132
+ logMessage(`Selected by: ${accountAuth.account.source}`);
133
+ logMessage(
134
+ `Convex account token found in: ${accountConfigPath(accountAuth.account.name)}`,
135
+ );
136
+ } else if (process.env.CONVEX_OVERRIDE_ACCESS_TOKEN) {
137
+ logMessage("Using CONVEX_OVERRIDE_ACCESS_TOKEN from the environment");
138
+ } else {
139
+ logMessage(`Convex account token found in: ${globalConfigPath()}`);
140
+ }
233
141
 
234
- const uuid = loadUuidForAnonymousUser(ctx);
235
- await performLogin(ctx, {
236
- ...options,
237
- anonymousId: uuid,
238
- vercel: options.vercel,
239
- vercelOverride: options.vercelOverride,
240
- });
142
+ if (!(await isAuthorizedHeader(ctx, accountAuth.header))) {
143
+ logMessage("Status: Not logged in");
144
+ await reportDeployKeyInWorkingDirectory(ctx, auth);
145
+ return await ctx.crash({
146
+ exitCode: 1,
147
+ errorType: "fatal",
148
+ printedMessage: null,
149
+ });
150
+ }
241
151
 
242
- await handleLinkingDeployments(ctx, {
243
- interactive: !!options.linkDeployments,
152
+ logMessage("Status: Logged in");
153
+ ctx._updateBigBrainAuth(accountAuth);
154
+ const teams = await getTeamsForUser(ctx);
155
+ logMessage(
156
+ `Teams: ${teams.length} team${teams.length === 1 ? "" : "s"} accessible`,
157
+ );
158
+ for (const team of teams) {
159
+ logMessage(` - ${team.name} (${team.slug})`);
160
+ }
161
+ await reportDeployKeyInWorkingDirectory(ctx, auth);
244
162
  });
245
- });
163
+ }
164
+
165
+ export function createLoginCommand() {
166
+ return (
167
+ new Command("login")
168
+ .description("Login to Convex")
169
+ .allowExcessArguments(false)
170
+ .option("--account <name>", "Log in to a named account")
171
+ .option(
172
+ "--select",
173
+ "Select this account for this directory and its subdirectories",
174
+ )
175
+ .option(
176
+ "--device-name <name>",
177
+ "Provide a name for the device being authorized",
178
+ )
179
+ .option(
180
+ "-f, --force",
181
+ "Proceed with login even if a valid access token already exists for this device",
182
+ )
183
+ .option(
184
+ "--no-open",
185
+ "Don't automatically open the login link in the default browser",
186
+ )
187
+ .addOption(
188
+ new Option(
189
+ "--login-flow <mode>",
190
+ `How to log in; defaults to guessing based on the environment.`,
191
+ )
192
+ .choices(["paste", "auto", "poll"] as const)
193
+ .default("auto" as const),
194
+ )
195
+ .addOption(new Option("--link-deployments").hideHelp())
196
+ // These options are hidden from the help/usage message, but allow overriding settings for testing.
197
+ // Change the auth credentials with the auth provider
198
+ .addOption(new Option("--override-auth-url <url>").hideHelp())
199
+ .addOption(new Option("--override-auth-client <id>").hideHelp())
200
+ .addOption(new Option("--override-auth-username <username>").hideHelp())
201
+ .addOption(new Option("--override-auth-password <password>").hideHelp())
202
+ // Skip the auth provider login and directly use this access token
203
+ .addOption(new Option("--override-access-token <token>").hideHelp())
204
+ // Automatically accept opt ins without prompting
205
+ .addOption(new Option("--accept-opt-ins").hideHelp())
206
+ // Dump the access token from the auth provider and skip authorization with Convex
207
+ .addOption(new Option("--dump-access-token").hideHelp())
208
+ // Hidden option for tests to check if the user is logged in.
209
+ .addOption(new Option("--check-login").hideHelp())
210
+ // Redirect to Vercel SSO integration URL
211
+ .addOption(
212
+ new Option(
213
+ "--vercel",
214
+ "Redirect to Vercel SSO integration for login",
215
+ ).hideHelp(),
216
+ )
217
+ // Override the Vercel URL slug (defaults to 'convex')
218
+ .addOption(new Option("--vercel-override <slug>").hideHelp())
219
+ .addCommand(createLoginStatusCommand())
220
+ .addHelpCommand(false)
221
+ .action(async (options, cmd: Command) => {
222
+ const ctx = await oneoffContext({
223
+ url: undefined,
224
+ adminKey: undefined,
225
+ envFile: undefined,
226
+ skipAuth: true,
227
+ });
228
+ const account = await getAccountSelection(ctx, {
229
+ name: options.account,
230
+ });
231
+ if (options.select && account === null) {
232
+ cmd.error("Pass --account <name> with --select to name this login.");
233
+ }
234
+ loadProjectEnvironmentVariables();
235
+ let namingLegacyAccount = false;
236
+ if (account !== null) {
237
+ let auth = options.force
238
+ ? null
239
+ : await readAccountAuth(ctx, account, true);
240
+ if (
241
+ auth === null &&
242
+ !options.force &&
243
+ !options.overrideAccessToken &&
244
+ !options.overrideAuthUsername &&
245
+ !process.env.CONVEX_OVERRIDE_ACCESS_TOKEN
246
+ ) {
247
+ auth = await legacyAuthForFirstAccount(ctx, account);
248
+ namingLegacyAccount = auth !== null;
249
+ }
250
+ ctx._updateBigBrainAuth(auth);
251
+ } else {
252
+ const accessToken =
253
+ process.env.CONVEX_OVERRIDE_ACCESS_TOKEN ||
254
+ readGlobalConfig(ctx)?.accessToken;
255
+ ctx._updateBigBrainAuth(
256
+ accessToken
257
+ ? {
258
+ kind: "accessToken",
259
+ accessToken,
260
+ header: `Bearer ${accessToken}`,
261
+ account: null,
262
+ }
263
+ : null,
264
+ );
265
+ }
266
+ warnIfDeployKeyOverridesLogin(ctx, ctx.bigBrainAuth() !== null);
267
+ if (
268
+ !options.force &&
269
+ (await checkAuthorization(ctx, !!options.acceptOptIns))
270
+ ) {
271
+ const auth = ctx.bigBrainAuth();
272
+ if (
273
+ namingLegacyAccount &&
274
+ account !== null &&
275
+ auth?.kind === "accessToken"
276
+ ) {
277
+ await writeAccountCredentials(
278
+ ctx,
279
+ account.name,
280
+ auth.accessToken,
281
+ {},
282
+ null,
283
+ );
284
+ }
285
+ logFinishedStep(
286
+ account !== null
287
+ ? namingLegacyAccount
288
+ ? `Saved account "${account.name}" using your existing login.`
289
+ : `Account "${account.name}" is already logged in.`
290
+ : "This device has previously been authorized and is ready for use with Convex.",
291
+ );
292
+ if (account !== null && auth?.kind === "accessToken") {
293
+ await initializeDefaultAccount(
294
+ ctx,
295
+ account,
296
+ namingLegacyAccount,
297
+ !!options.select,
298
+ );
299
+ }
300
+ if (options.select && account !== null) {
301
+ if (!(await bindAccountToDirectory(ctx, account.name))) {
302
+ return await ctx.crash({
303
+ exitCode: 1,
304
+ errorType: "fatal",
305
+ printedMessage: `Logged in as "${account.name}", but the directory selection could not be changed.`,
306
+ });
307
+ }
308
+ }
309
+ await handleLinkingDeployments(ctx, {
310
+ interactive: !!options.linkDeployments,
311
+ });
312
+ return;
313
+ }
314
+ if (!options.force && options.checkLogin) {
315
+ const isLoggedIn = await checkAuthorization(
316
+ ctx,
317
+ !!options.acceptOptIns,
318
+ );
319
+ if (!isLoggedIn) {
320
+ return ctx.crash({
321
+ exitCode: 1,
322
+ errorType: "fatal",
323
+ errForSentry: "You are not logged in.",
324
+ printedMessage: "You are not logged in.",
325
+ });
326
+ }
327
+ }
328
+ if (!!options.overrideAuthUsername !== !!options.overrideAuthPassword) {
329
+ cmd.error(
330
+ "If overriding credentials, both username and password must be provided",
331
+ );
332
+ }
333
+
334
+ const uuid = loadUuidForAnonymousUser(ctx);
335
+ await performLogin(ctx, {
336
+ ...options,
337
+ account,
338
+ anonymousId: uuid,
339
+ vercel: options.vercel,
340
+ vercelOverride: options.vercelOverride,
341
+ });
342
+
343
+ const auth = ctx.bigBrainAuth();
344
+ if (account !== null && auth?.kind === "accessToken") {
345
+ await initializeDefaultAccount(
346
+ ctx,
347
+ account,
348
+ namingLegacyAccount,
349
+ !!options.select,
350
+ );
351
+ }
352
+ if (options.select && account !== null) {
353
+ if (!(await bindAccountToDirectory(ctx, account.name))) {
354
+ return await ctx.crash({
355
+ exitCode: 1,
356
+ errorType: "fatal",
357
+ printedMessage: `Logged in as "${account.name}", but the directory selection could not be changed.`,
358
+ });
359
+ }
360
+ }
361
+ warnIfDeployKeyOverridesLogin(ctx, true);
362
+
363
+ await handleLinkingDeployments(ctx, {
364
+ interactive: !!options.linkDeployments,
365
+ });
366
+ })
367
+ );
368
+ }
369
+
370
+ export const login = createLoginCommand();
246
371
 
247
372
  async function handleLinkingDeployments(
248
373
  ctx: Context,
package/src/cli/logout.ts CHANGED
@@ -1,24 +1,149 @@
1
1
  import { Command } from "@commander-js/extra-typings";
2
2
  import { oneoffContext } from "../bundler/context.js";
3
- import { logFinishedStep } from "../bundler/log.js";
3
+ import { logFinishedStep, logMessage, logWarning } from "../bundler/log.js";
4
4
  import { recursivelyDelete } from "./lib/fsUtils.js";
5
5
  import { globalConfigPath } from "./lib/utils/globalConfig.js";
6
+ import {
7
+ getAccountSelection,
8
+ accountConfigPath,
9
+ readAccountToken,
10
+ readAccountSettings,
11
+ updateAccountSettings,
12
+ savedAccountNames,
13
+ } from "./lib/accounts.js";
14
+ import path from "path";
15
+ import { rootDirectory } from "./lib/utils/utils.js";
16
+ import { promptOptions } from "./lib/utils/prompts.js";
6
17
 
7
- export const logout = new Command("logout")
8
- .description("Log out of Convex on this machine")
9
- .allowExcessArguments(false)
10
- .action(async () => {
11
- const ctx = await oneoffContext({
12
- url: undefined,
13
- adminKey: undefined,
14
- envFile: undefined,
15
- });
18
+ export function createLogoutCommand() {
19
+ return new Command("logout")
20
+ .description(
21
+ "Log out of the selected account. Directory selections are preserved.",
22
+ )
23
+ .option("--account <name>", "Remove only this account's saved credentials")
24
+ .option(
25
+ "--all",
26
+ "Remove all saved account credentials, preserving account selections",
27
+ )
28
+ .allowExcessArguments(false)
29
+ .action(async (options, cmd) => {
30
+ if (options.all && options.account !== undefined)
31
+ cmd.error("Pass either --account or --all, not both.");
32
+ const ctx = await oneoffContext({
33
+ url: undefined,
34
+ adminKey: undefined,
35
+ envFile: undefined,
36
+ skipAuth: true,
37
+ });
38
+
39
+ let all = !!options.all;
40
+ let account = all
41
+ ? null
42
+ : await getAccountSelection(ctx, { name: options.account });
43
+ if (
44
+ !all &&
45
+ options.account === undefined &&
46
+ process.stdin.isTTY &&
47
+ account !== null
48
+ ) {
49
+ const token = readAccountToken(ctx, account.name);
50
+ const settings = await readAccountSettings(ctx);
51
+ const selectedName = account.name;
52
+ // A matching compatibility token is another copy of this account's login.
53
+ const hasOtherLogins =
54
+ savedAccountNames(ctx).some((name) => name !== selectedName) ||
55
+ (typeof settings.accessToken === "string" &&
56
+ settings.accessToken.length > 0 &&
57
+ settings.accessToken !== token);
58
+ if (hasOtherLogins) {
59
+ const scope = await promptOptions(ctx, {
60
+ message: "Which saved logins would you like to log out of?",
61
+ choices: [
62
+ { name: `Only account "${account.name}"`, value: "account" },
63
+ { name: "All saved logins on this device", value: "all" },
64
+ ],
65
+ default: "account",
66
+ });
67
+ if (scope === "all") {
68
+ account = null;
69
+ all = true;
70
+ }
71
+ }
72
+ }
16
73
 
17
- if (ctx.fs.exists(globalConfigPath())) {
18
- recursivelyDelete(ctx, globalConfigPath());
19
- }
74
+ let warnAboutLegacyCredentials = false;
75
+ if (account !== null) {
76
+ const selectedName = account.name;
77
+ await updateAccountSettings(
78
+ ctx,
79
+ (settings) => {
80
+ const token = readAccountToken(ctx, selectedName);
81
+ // A migrated account can share its token with old CLI versions.
82
+ if (token !== null && settings.accessToken === token) {
83
+ delete settings.accessToken;
84
+ } else if (
85
+ settings.defaultAccount === selectedName &&
86
+ typeof settings.accessToken === "string" &&
87
+ settings.accessToken.length > 0
88
+ ) {
89
+ // Editing the default or logging in with an older CLI can leave this
90
+ // token belonging to a different account.
91
+ warnAboutLegacyCredentials = true;
92
+ }
93
+ },
94
+ () => {
95
+ const configPath = accountConfigPath(selectedName);
96
+ recursivelyDelete(ctx, configPath, {
97
+ force: true,
98
+ followSymlinks: false,
99
+ });
100
+ },
101
+ );
102
+ } else {
103
+ await updateAccountSettings(
104
+ ctx,
105
+ async (settings) => {
106
+ if (
107
+ !all &&
108
+ savedAccountNames(ctx).length > 0 &&
109
+ settings.accessToken === undefined
110
+ ) {
111
+ return await ctx.crash({
112
+ exitCode: 1,
113
+ errorType: "fatal",
114
+ printedMessage:
115
+ "No account is selected. Use `npx convex logout --account <name>` or `npx convex logout --all`.",
116
+ });
117
+ }
118
+ delete settings.accessToken;
119
+ },
120
+ () => {
121
+ const accountsDir = path.join(rootDirectory(), "accounts");
122
+ if (all)
123
+ recursivelyDelete(ctx, accountsDir, {
124
+ force: true,
125
+ followSymlinks: false,
126
+ });
127
+ },
128
+ );
129
+ }
130
+
131
+ logFinishedStep(
132
+ account !== null
133
+ ? `Removed credentials for account "${account.name}".\n Run \`npx convex login --account ${account.name}\` to log in.`
134
+ : `${all ? "Removed all saved Convex account credentials." : "Removed your saved Convex login credentials."}\n Run \`npx convex login\` to log in.`,
135
+ );
136
+ if (warnAboutLegacyCredentials) {
137
+ logWarning(
138
+ `Saved credentials remain in ${globalConfigPath()} for older CLI versions. To remove all saved account credentials, run \`npx convex logout --all\`.`,
139
+ );
140
+ }
141
+ if (process.env.CONVEX_OVERRIDE_ACCESS_TOKEN) {
142
+ logMessage(
143
+ "CONVEX_OVERRIDE_ACCESS_TOKEN still supplies credentials. Unset it to stop using that token.",
144
+ );
145
+ }
146
+ });
147
+ }
20
148
 
21
- logFinishedStep(
22
- "You have been logged out of Convex.\n Run `npx convex dev` to log in.",
23
- );
24
- });
149
+ export const logout = createLogoutCommand();