@prisma/cli 3.0.0-beta.1 → 3.0.0-beta.11

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 (66) hide show
  1. package/README.md +5 -3
  2. package/dist/adapters/git.js +8 -3
  3. package/dist/adapters/local-state.js +12 -4
  4. package/dist/adapters/mock-api.js +81 -2
  5. package/dist/adapters/token-storage.js +64 -23
  6. package/dist/cli.js +18 -3
  7. package/dist/cli2.js +9 -5
  8. package/dist/commands/app/index.js +84 -59
  9. package/dist/commands/branch/index.js +2 -27
  10. package/dist/commands/database/index.js +159 -0
  11. package/dist/commands/env.js +8 -4
  12. package/dist/controllers/app-env-api.js +54 -0
  13. package/dist/controllers/app-env-file.js +181 -0
  14. package/dist/controllers/app-env.js +286 -131
  15. package/dist/controllers/app.js +699 -244
  16. package/dist/controllers/auth.js +8 -8
  17. package/dist/controllers/branch.js +78 -48
  18. package/dist/controllers/database.js +318 -0
  19. package/dist/controllers/project.js +156 -85
  20. package/dist/lib/app/branch-database-deploy.js +325 -0
  21. package/dist/lib/app/branch-database.js +215 -0
  22. package/dist/lib/app/bun-project.js +12 -5
  23. package/dist/lib/app/compute-config.js +147 -0
  24. package/dist/lib/app/deploy-output.js +10 -1
  25. package/dist/lib/app/deploy-plan.js +58 -0
  26. package/dist/lib/app/env-config.js +1 -1
  27. package/dist/lib/app/env-file.js +82 -0
  28. package/dist/lib/app/env-vars.js +28 -2
  29. package/dist/lib/app/local-dev.js +8 -49
  30. package/dist/lib/app/preview-branch-database.js +102 -0
  31. package/dist/lib/app/preview-build-settings.js +376 -0
  32. package/dist/lib/app/preview-build.js +314 -97
  33. package/dist/lib/app/preview-provider.js +178 -65
  34. package/dist/lib/app/production-deploy-gate.js +161 -0
  35. package/dist/lib/auth/auth-ops.js +30 -21
  36. package/dist/lib/auth/guard.js +3 -2
  37. package/dist/lib/auth/login.js +116 -14
  38. package/dist/lib/database/provider.js +167 -0
  39. package/dist/lib/diagnostics.js +15 -0
  40. package/dist/lib/fs/home-path.js +24 -0
  41. package/dist/lib/git/local-branch.js +53 -0
  42. package/dist/lib/git/local-status.js +57 -0
  43. package/dist/lib/project/interactive-setup.js +1 -1
  44. package/dist/lib/project/local-pin.js +182 -33
  45. package/dist/lib/project/resolution.js +204 -50
  46. package/dist/lib/project/setup.js +66 -19
  47. package/dist/output/patterns.js +1 -1
  48. package/dist/presenters/app-env.js +149 -14
  49. package/dist/presenters/app.js +178 -23
  50. package/dist/presenters/branch.js +37 -102
  51. package/dist/presenters/database.js +274 -0
  52. package/dist/presenters/project.js +57 -39
  53. package/dist/presenters/verbose-context.js +64 -0
  54. package/dist/shell/command-meta.js +103 -13
  55. package/dist/shell/command-runner.js +57 -19
  56. package/dist/shell/diagnostics-output.js +57 -0
  57. package/dist/shell/errors.js +12 -1
  58. package/dist/shell/help.js +30 -20
  59. package/dist/shell/output.js +10 -1
  60. package/dist/shell/runtime.js +11 -7
  61. package/dist/shell/ui.js +42 -3
  62. package/dist/shell/update-check.js +247 -0
  63. package/dist/use-cases/branch.js +20 -68
  64. package/dist/use-cases/create-cli-gateways.js +2 -17
  65. package/dist/use-cases/project.js +2 -1
  66. package/package.json +26 -10
@@ -0,0 +1,181 @@
1
+ import { CliError } from "../shell/errors.js";
2
+ import { formatScopeLabel } from "../lib/app/env-config.js";
3
+ import { apiCallError, findVariableByNaturalKey, toMetadata } from "./app-env-api.js";
4
+ //#region src/controllers/app-env-file.ts
5
+ async function runEnvAddFile(context, client, projectId, resolved, filePath, assignments, verboseContext) {
6
+ const existing = await findVariablesByNaturalKey(client, projectId, assignments.map((assignment) => assignment.key), resolved, context.runtime.signal);
7
+ const existingKeys = assignments.map((assignment) => assignment.key).filter((key) => existing.has(key));
8
+ if (existingKeys.length > 0) throw new CliError({
9
+ code: "ENV_VARIABLE_ALREADY_EXISTS",
10
+ domain: "app",
11
+ summary: `${existingKeys.length} environment variable(s) already exist in ${formatScopeLabel(resolved.scope)}`,
12
+ why: `Existing keys: ${formatKeyList(existingKeys)}.`,
13
+ fix: "Split the input file by key state: update existing keys and add new keys separately.",
14
+ exitCode: 1,
15
+ nextSteps: splitFileNextSteps(filePath, resolved.scope, {
16
+ existingKeys,
17
+ first: "update-existing"
18
+ }),
19
+ meta: { keys: existingKeys }
20
+ });
21
+ const warnings = await missingPreviewDefaultWarnings(client, projectId, resolved.scope, assignments.map((assignment) => assignment.key), context.runtime.signal);
22
+ const variables = [];
23
+ for (const assignment of assignments) try {
24
+ const { data, error, response } = await client.POST("/v1/environment-variables", {
25
+ body: {
26
+ projectId,
27
+ class: resolved.apiTarget.class,
28
+ ...resolved.apiTarget.branchId !== null ? { branchId: resolved.apiTarget.branchId } : {},
29
+ key: assignment.key,
30
+ value: assignment.value
31
+ },
32
+ signal: context.runtime.signal
33
+ });
34
+ if (error || !data) throw apiCallError(`Failed to add ${assignment.key}`, response, error);
35
+ variables.push(toMetadata(data.data, resolved.descriptor));
36
+ } catch (error) {
37
+ throw envFileApplyFailedError("add", filePath, resolved.scope, assignment.key, variables, error);
38
+ }
39
+ return {
40
+ command: "project.env.add",
41
+ result: {
42
+ projectId,
43
+ verboseContext,
44
+ scope: resolved.descriptor,
45
+ variables,
46
+ file: {
47
+ path: filePath,
48
+ count: variables.length
49
+ }
50
+ },
51
+ warnings,
52
+ nextSteps: []
53
+ };
54
+ }
55
+ async function runEnvUpdateFile(context, client, projectId, resolved, filePath, assignments, verboseContext) {
56
+ const existing = await findVariablesByNaturalKey(client, projectId, assignments.map((assignment) => assignment.key), resolved, context.runtime.signal);
57
+ const missingKeys = assignments.map((assignment) => assignment.key).filter((key) => !existing.has(key));
58
+ if (missingKeys.length > 0) throw new CliError({
59
+ code: "ENV_VARIABLE_NOT_FOUND",
60
+ domain: "app",
61
+ summary: `${missingKeys.length} environment variable(s) not found in ${formatScopeLabel(resolved.scope)}`,
62
+ why: `Missing keys: ${formatKeyList(missingKeys)}.`,
63
+ fix: "Split the input file by key state: add missing keys and update existing keys separately.",
64
+ exitCode: 1,
65
+ nextSteps: splitFileNextSteps(filePath, resolved.scope, {
66
+ missingKeys,
67
+ first: "add-missing"
68
+ }),
69
+ meta: { keys: missingKeys }
70
+ });
71
+ const variables = [];
72
+ for (const assignment of assignments) {
73
+ const existingVariable = existing.get(assignment.key);
74
+ if (!existingVariable) continue;
75
+ try {
76
+ const { data, error, response } = await client.PATCH("/v1/environment-variables/{envVarId}", {
77
+ params: { path: { envVarId: existingVariable.id } },
78
+ body: { value: assignment.value },
79
+ signal: context.runtime.signal
80
+ });
81
+ if (error || !data) throw apiCallError(`Failed to update value for ${assignment.key}`, response, error);
82
+ variables.push(toMetadata(data.data, resolved.descriptor));
83
+ } catch (error) {
84
+ throw envFileApplyFailedError("update", filePath, resolved.scope, assignment.key, variables, error);
85
+ }
86
+ }
87
+ return {
88
+ command: "project.env.update",
89
+ result: {
90
+ projectId,
91
+ verboseContext,
92
+ scope: resolved.descriptor,
93
+ variables,
94
+ file: {
95
+ path: filePath,
96
+ count: variables.length
97
+ }
98
+ },
99
+ warnings: [],
100
+ nextSteps: []
101
+ };
102
+ }
103
+ async function findVariablesByNaturalKey(client, projectId, keys, resolved, signal) {
104
+ const found = /* @__PURE__ */ new Map();
105
+ for (const key of keys) {
106
+ const row = await findVariableByNaturalKey(client, projectId, key, resolved, signal);
107
+ if (row) found.set(key, row);
108
+ }
109
+ return found;
110
+ }
111
+ async function missingPreviewDefaultWarnings(client, projectId, scope, keys, signal) {
112
+ if (scope.kind !== "branch") return [];
113
+ const previewScope = {
114
+ scope: {
115
+ kind: "role",
116
+ role: "preview"
117
+ },
118
+ descriptor: {
119
+ kind: "role",
120
+ role: "preview"
121
+ },
122
+ apiTarget: {
123
+ class: "preview",
124
+ branchId: null
125
+ }
126
+ };
127
+ const missing = [];
128
+ for (const key of keys) if (!await findVariableByNaturalKey(client, projectId, key, previewScope, signal)) missing.push(key);
129
+ if (missing.length === 0) return [];
130
+ if (missing.length === 1) return [`Variable "${missing[0]}" does not exist in preview. It will only exist on ${formatScopeLabel(scope)}.`];
131
+ return [`Variables ${formatKeyList(missing)} do not exist in preview. They will only exist on ${formatScopeLabel(scope)}.`];
132
+ }
133
+ function envFileApplyFailedError(command, filePath, scope, failedKey, writtenVariables, error) {
134
+ const writtenKeys = writtenVariables.map((variable) => variable.key);
135
+ const cause = error instanceof CliError ? error.summary : error instanceof Error ? error.message : "Unknown error.";
136
+ return new CliError({
137
+ code: "ENV_FILE_APPLY_FAILED",
138
+ domain: "app",
139
+ summary: `Failed to ${command} "${failedKey}" from "${filePath}"`,
140
+ why: writtenKeys.length === 0 ? `No variables were written before ${failedKey} failed. Cause: ${cause}` : `Written keys before failure: ${formatKeyList(writtenKeys)}. Cause: ${cause}`,
141
+ fix: "Inspect the target scope, then retry the remaining keys once the API issue is resolved.",
142
+ exitCode: 1,
143
+ nextSteps: [`prisma-cli project env list ${formatScopeFlag(scope)}`, retryStepForApplyFailure(command, filePath, scope, writtenKeys)],
144
+ meta: {
145
+ file: filePath,
146
+ failedKey,
147
+ writtenKeys
148
+ }
149
+ });
150
+ }
151
+ function retryStepForApplyFailure(command, filePath, scope, writtenKeys) {
152
+ if (command === "update") return `prisma-cli project env update --file ${filePath} ${formatScopeFlag(scope)}`;
153
+ if (writtenKeys.length === 0) return `prisma-cli project env add --file ${filePath} ${formatScopeFlag(scope)}`;
154
+ return `prisma-cli project env add --file <remaining.env> ${formatScopeFlag(scope)}`;
155
+ }
156
+ function splitFileNextSteps(filePath, scope, options) {
157
+ const scopeFlag = formatScopeFlag(scope);
158
+ const existingFile = `${filePath}.existing`;
159
+ const newFile = `${filePath}.new`;
160
+ if (options.first === "update-existing") return [
161
+ `# existing keys: ${formatKeyList(options.existingKeys)}`,
162
+ `prisma-cli project env update --file ${existingFile} ${scopeFlag}`,
163
+ "# new keys only",
164
+ `prisma-cli project env add --file ${newFile} ${scopeFlag}`
165
+ ];
166
+ return [
167
+ `# missing keys: ${formatKeyList(options.missingKeys)}`,
168
+ `prisma-cli project env add --file ${newFile} ${scopeFlag}`,
169
+ "# existing keys only",
170
+ `prisma-cli project env update --file ${existingFile} ${scopeFlag}`
171
+ ];
172
+ }
173
+ function formatKeyList(keys) {
174
+ return keys.map((key) => `"${key}"`).join(", ");
175
+ }
176
+ function formatScopeFlag(scope) {
177
+ if (scope.kind === "role") return `--role ${scope.role}`;
178
+ return `--branch ${scope.branchName}`;
179
+ }
180
+ //#endregion
181
+ export { runEnvAddFile, runEnvUpdateFile };