@prisma/cli 3.0.0-beta.0 → 3.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -3
- package/dist/adapters/git.js +8 -3
- package/dist/adapters/local-state.js +12 -4
- package/dist/adapters/mock-api.js +81 -2
- package/dist/adapters/token-storage.js +63 -22
- package/dist/cli.js +17 -2
- package/dist/cli2.js +7 -3
- package/dist/commands/app/index.js +26 -13
- package/dist/commands/branch/index.js +2 -27
- package/dist/commands/database/index.js +159 -0
- package/dist/commands/env.js +8 -4
- package/dist/commands/project/index.js +28 -2
- package/dist/controllers/app-env-api.js +54 -0
- package/dist/controllers/app-env-file.js +181 -0
- package/dist/controllers/app-env.js +284 -132
- package/dist/controllers/app.js +493 -344
- package/dist/controllers/auth.js +8 -8
- package/dist/controllers/branch.js +78 -48
- package/dist/controllers/database.js +318 -0
- package/dist/controllers/project.js +302 -75
- package/dist/lib/app/branch-database-deploy.js +373 -0
- package/dist/lib/app/branch-database.js +316 -0
- package/dist/lib/app/bun-project.js +12 -5
- package/dist/lib/app/deploy-output.js +10 -1
- package/dist/lib/app/env-config.js +1 -1
- package/dist/lib/app/env-file.js +82 -0
- package/dist/lib/app/env-vars.js +28 -2
- package/dist/lib/app/local-dev.js +34 -18
- package/dist/lib/app/preview-branch-database.js +102 -0
- package/dist/lib/app/preview-build-settings.js +385 -0
- package/dist/lib/app/preview-build.js +272 -81
- package/dist/lib/app/preview-provider.js +163 -54
- package/dist/lib/app/production-deploy-gate.js +161 -0
- package/dist/lib/auth/auth-ops.js +69 -19
- package/dist/lib/auth/guard.js +3 -2
- package/dist/lib/auth/login.js +109 -14
- package/dist/lib/database/provider.js +167 -0
- package/dist/lib/diagnostics.js +15 -0
- package/dist/lib/git/local-branch.js +41 -0
- package/dist/lib/git/local-status.js +57 -0
- package/dist/lib/project/interactive-setup.js +56 -0
- package/dist/lib/project/local-pin.js +182 -33
- package/dist/lib/project/resolution.js +287 -105
- package/dist/lib/project/setup.js +132 -0
- package/dist/presenters/app-env.js +149 -14
- package/dist/presenters/app.js +170 -20
- package/dist/presenters/auth.js +19 -6
- package/dist/presenters/branch.js +37 -102
- package/dist/presenters/database.js +274 -0
- package/dist/presenters/project.js +100 -47
- package/dist/presenters/verbose-context.js +64 -0
- package/dist/shell/command-arguments.js +6 -0
- package/dist/shell/command-meta.js +139 -16
- package/dist/shell/command-runner.js +38 -8
- package/dist/shell/diagnostics-output.js +63 -0
- package/dist/shell/errors.js +14 -1
- package/dist/shell/output.js +13 -2
- package/dist/shell/runtime.js +3 -3
- package/dist/shell/ui.js +23 -1
- package/dist/shell/update-check.js +247 -0
- package/dist/use-cases/auth.js +15 -4
- package/dist/use-cases/branch.js +20 -68
- package/dist/use-cases/create-cli-gateways.js +2 -17
- package/dist/use-cases/project.js +2 -1
- package/package.json +12 -10
|
@@ -24,66 +24,85 @@ function workspaceIdFromClaims(claims) {
|
|
|
24
24
|
const id = sub.slice(10).trim();
|
|
25
25
|
return id.length > 0 ? id : null;
|
|
26
26
|
}
|
|
27
|
-
async function performLogin(env) {
|
|
27
|
+
async function performLogin(env, signal) {
|
|
28
28
|
await login({
|
|
29
|
-
tokenStorage: new FileTokenStorage(env),
|
|
30
|
-
env
|
|
29
|
+
tokenStorage: new FileTokenStorage(env, signal),
|
|
30
|
+
env,
|
|
31
|
+
signal
|
|
31
32
|
});
|
|
32
33
|
}
|
|
33
|
-
async function readAuthState(env) {
|
|
34
|
+
async function readAuthState(env, signal) {
|
|
34
35
|
const rawServiceToken = env[SERVICE_TOKEN_ENV_VAR];
|
|
35
36
|
if (rawServiceToken !== void 0) {
|
|
36
37
|
const serviceToken = rawServiceToken.trim();
|
|
37
38
|
if (serviceToken.length === 0) throw new Error(`${SERVICE_TOKEN_ENV_VAR} is set but empty. Provide a valid token or unset the variable.`);
|
|
38
|
-
return readServiceTokenAuthState(serviceToken, env);
|
|
39
|
+
return readServiceTokenAuthState(serviceToken, env, signal);
|
|
39
40
|
}
|
|
40
|
-
const tokens = await new FileTokenStorage(env).getTokens();
|
|
41
|
+
const tokens = await new FileTokenStorage(env, signal).getTokens();
|
|
41
42
|
if (!tokens) return {
|
|
42
43
|
authenticated: false,
|
|
43
44
|
provider: null,
|
|
44
45
|
user: null,
|
|
45
|
-
workspace: null
|
|
46
|
+
workspace: null,
|
|
47
|
+
credential: null
|
|
46
48
|
};
|
|
49
|
+
const client = await requireComputeAuth(env, signal);
|
|
50
|
+
const currentPrincipal = await readCurrentPrincipalAuthState(client, signal);
|
|
51
|
+
if (currentPrincipal) return currentPrincipal;
|
|
47
52
|
const claims = decodeJwtPayload(tokens.accessToken);
|
|
48
53
|
return buildAuthState({
|
|
49
54
|
workspaceIdFromCredential: tokens.workspaceId,
|
|
50
55
|
claims,
|
|
51
|
-
env
|
|
56
|
+
env,
|
|
57
|
+
client,
|
|
58
|
+
signal
|
|
52
59
|
});
|
|
53
60
|
}
|
|
54
|
-
async function readServiceTokenAuthState(token, env) {
|
|
61
|
+
async function readServiceTokenAuthState(token, env, signal) {
|
|
62
|
+
const client = await requireComputeAuth(env, signal);
|
|
63
|
+
const currentPrincipal = await readCurrentPrincipalAuthState(client, signal);
|
|
64
|
+
if (currentPrincipal) return currentPrincipal;
|
|
55
65
|
const claims = decodeJwtPayload(token);
|
|
56
66
|
const workspaceId = workspaceIdFromClaims(claims);
|
|
57
67
|
if (!workspaceId) return {
|
|
58
68
|
authenticated: false,
|
|
59
69
|
provider: null,
|
|
60
70
|
user: null,
|
|
61
|
-
workspace: null
|
|
71
|
+
workspace: null,
|
|
72
|
+
credential: null
|
|
62
73
|
};
|
|
63
74
|
return buildAuthState({
|
|
64
75
|
workspaceIdFromCredential: workspaceId,
|
|
65
76
|
claims,
|
|
66
|
-
env
|
|
77
|
+
env,
|
|
78
|
+
client,
|
|
79
|
+
signal
|
|
67
80
|
});
|
|
68
81
|
}
|
|
69
|
-
async function buildAuthState({ workspaceIdFromCredential, claims, env }) {
|
|
82
|
+
async function buildAuthState({ workspaceIdFromCredential, claims, env, client, signal }) {
|
|
70
83
|
let workspaceId = workspaceIdFromCredential;
|
|
71
84
|
let workspaceName = workspaceIdFromCredential;
|
|
72
|
-
|
|
85
|
+
client ??= await requireComputeAuth(env, signal);
|
|
73
86
|
if (client) try {
|
|
74
|
-
const { data, response } = await client.GET("/v1/workspaces/{id}", {
|
|
87
|
+
const { data, response } = await client.GET("/v1/workspaces/{id}", {
|
|
88
|
+
params: { path: { id: workspaceIdFromCredential } },
|
|
89
|
+
signal
|
|
90
|
+
});
|
|
75
91
|
if (response?.status === 401) return {
|
|
76
92
|
authenticated: false,
|
|
77
93
|
provider: null,
|
|
78
94
|
user: null,
|
|
79
|
-
workspace: null
|
|
95
|
+
workspace: null,
|
|
96
|
+
credential: null
|
|
80
97
|
};
|
|
81
98
|
if (data?.data?.id) {
|
|
82
99
|
workspaceId = data.data.id;
|
|
83
100
|
workspaceName = data.data.id;
|
|
84
101
|
}
|
|
85
102
|
if (data?.data?.name) workspaceName = data.data.name;
|
|
86
|
-
} catch {
|
|
103
|
+
} catch {
|
|
104
|
+
signal?.throwIfAborted();
|
|
105
|
+
}
|
|
87
106
|
const email = emailFromClaims(claims);
|
|
88
107
|
return {
|
|
89
108
|
authenticated: true,
|
|
@@ -92,11 +111,42 @@ async function buildAuthState({ workspaceIdFromCredential, claims, env }) {
|
|
|
92
111
|
workspace: {
|
|
93
112
|
id: workspaceId,
|
|
94
113
|
name: workspaceName
|
|
95
|
-
}
|
|
114
|
+
},
|
|
115
|
+
credential: null
|
|
96
116
|
};
|
|
97
117
|
}
|
|
98
|
-
async function
|
|
99
|
-
|
|
118
|
+
async function readCurrentPrincipalAuthState(client, signal) {
|
|
119
|
+
if (!client) return null;
|
|
120
|
+
try {
|
|
121
|
+
const { data, response } = await client.GET("/v1/me", { signal });
|
|
122
|
+
if (response?.status === 401) return {
|
|
123
|
+
authenticated: false,
|
|
124
|
+
provider: null,
|
|
125
|
+
user: null,
|
|
126
|
+
workspace: null,
|
|
127
|
+
credential: null
|
|
128
|
+
};
|
|
129
|
+
const principal = data?.data;
|
|
130
|
+
if (!principal) return null;
|
|
131
|
+
if (!principal.credential) return null;
|
|
132
|
+
return {
|
|
133
|
+
authenticated: true,
|
|
134
|
+
provider: null,
|
|
135
|
+
user: principal.user ? {
|
|
136
|
+
id: principal.user.id,
|
|
137
|
+
email: principal.user.email,
|
|
138
|
+
name: principal.user.name
|
|
139
|
+
} : null,
|
|
140
|
+
workspace: principal.workspace,
|
|
141
|
+
credential: principal.credential
|
|
142
|
+
};
|
|
143
|
+
} catch {
|
|
144
|
+
signal?.throwIfAborted();
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async function performLogout(env, signal) {
|
|
149
|
+
await new FileTokenStorage(env, signal).clearTokens();
|
|
100
150
|
}
|
|
101
151
|
//#endregion
|
|
102
152
|
export { performLogin, performLogout, readAuthState };
|
package/dist/lib/auth/guard.js
CHANGED
|
@@ -11,7 +11,8 @@ import { createManagementApiClient, createManagementApiSdk } from "@prisma/manag
|
|
|
11
11
|
*
|
|
12
12
|
* Returns null if not authenticated.
|
|
13
13
|
*/
|
|
14
|
-
async function requireComputeAuth(env = process.env) {
|
|
14
|
+
async function requireComputeAuth(env = process.env, signal) {
|
|
15
|
+
signal?.throwIfAborted();
|
|
15
16
|
const rawToken = env[SERVICE_TOKEN_ENV_VAR];
|
|
16
17
|
if (rawToken !== void 0) {
|
|
17
18
|
const token = rawToken.trim();
|
|
@@ -21,7 +22,7 @@ async function requireComputeAuth(env = process.env) {
|
|
|
21
22
|
token
|
|
22
23
|
});
|
|
23
24
|
}
|
|
24
|
-
const tokenStorage = new FileTokenStorage(env);
|
|
25
|
+
const tokenStorage = new FileTokenStorage(env, signal);
|
|
25
26
|
if (!await tokenStorage.getTokens()) return null;
|
|
26
27
|
return createManagementApiSdk({
|
|
27
28
|
clientId: CLIENT_ID,
|
package/dist/lib/auth/login.js
CHANGED
|
@@ -4,6 +4,7 @@ import open from "open";
|
|
|
4
4
|
import { AuthError, createManagementApiSdk } from "@prisma/management-api-sdk";
|
|
5
5
|
import events from "node:events";
|
|
6
6
|
import http from "node:http";
|
|
7
|
+
import readline from "node:readline/promises";
|
|
7
8
|
//#region src/lib/auth/login.ts
|
|
8
9
|
var AuthError$1 = class extends Error {
|
|
9
10
|
constructor(message) {
|
|
@@ -14,11 +15,15 @@ var AuthError$1 = class extends Error {
|
|
|
14
15
|
async function login(options = {}) {
|
|
15
16
|
const hostname = options.hostname ?? "localhost";
|
|
16
17
|
const port = options.port ?? 0;
|
|
18
|
+
const input = options.input ?? process.stdin;
|
|
19
|
+
const output = options.output ?? process.stderr;
|
|
20
|
+
const interactive = input.isTTY === true;
|
|
17
21
|
const server = http.createServer();
|
|
18
22
|
server.listen({
|
|
19
23
|
host: hostname,
|
|
20
24
|
port
|
|
21
25
|
});
|
|
26
|
+
const pasteAbort = new AbortController();
|
|
22
27
|
try {
|
|
23
28
|
const state = new LoginState({
|
|
24
29
|
hostname,
|
|
@@ -28,9 +33,30 @@ async function login(options = {}) {
|
|
|
28
33
|
apiBaseUrl: options.apiBaseUrl,
|
|
29
34
|
authBaseUrl: options.authBaseUrl,
|
|
30
35
|
openUrl: options.openUrl,
|
|
31
|
-
env: options.env
|
|
36
|
+
env: options.env,
|
|
37
|
+
signal: options.signal,
|
|
38
|
+
output
|
|
32
39
|
});
|
|
33
|
-
|
|
40
|
+
let completed = false;
|
|
41
|
+
let completion;
|
|
42
|
+
const completeOnce = (url) => {
|
|
43
|
+
if (!completion) completion = state.handleCallback(url).then(() => {
|
|
44
|
+
completed = true;
|
|
45
|
+
}, (error) => {
|
|
46
|
+
completion = void 0;
|
|
47
|
+
throw error;
|
|
48
|
+
});
|
|
49
|
+
return completion;
|
|
50
|
+
};
|
|
51
|
+
const httpResult = new Promise((resolve, reject) => {
|
|
52
|
+
const onAbort = () => {
|
|
53
|
+
reject(options.signal?.reason);
|
|
54
|
+
};
|
|
55
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
56
|
+
const settle = (callback) => {
|
|
57
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
58
|
+
callback();
|
|
59
|
+
};
|
|
34
60
|
server.on("request", async (req, res) => {
|
|
35
61
|
const url = new URL(`http://${state.host}${req.url}`);
|
|
36
62
|
if (url.pathname !== "/auth/callback") {
|
|
@@ -38,36 +64,87 @@ async function login(options = {}) {
|
|
|
38
64
|
res.end("Not found");
|
|
39
65
|
return;
|
|
40
66
|
}
|
|
67
|
+
if (completed) {
|
|
68
|
+
const workspaceName = await state.resolveWorkspaceName();
|
|
69
|
+
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
70
|
+
res.end(renderSuccessPage(workspaceName));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
41
73
|
try {
|
|
42
|
-
await
|
|
74
|
+
await completeOnce(url);
|
|
75
|
+
const workspaceName = await state.resolveWorkspaceName();
|
|
76
|
+
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
77
|
+
res.end(renderSuccessPage(workspaceName));
|
|
78
|
+
settle(resolve);
|
|
43
79
|
} catch (error) {
|
|
44
80
|
res.statusCode = 400;
|
|
45
81
|
const message = error instanceof Error ? error.message : String(error);
|
|
46
82
|
res.end(message);
|
|
47
|
-
reject(error);
|
|
83
|
+
settle(() => reject(error));
|
|
48
84
|
return;
|
|
49
85
|
}
|
|
50
|
-
const workspaceName = await state.resolveWorkspaceName();
|
|
51
|
-
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
52
|
-
res.end(renderSuccessPage(workspaceName));
|
|
53
|
-
resolve();
|
|
54
86
|
});
|
|
55
87
|
});
|
|
56
|
-
|
|
57
|
-
|
|
88
|
+
options.signal?.throwIfAborted();
|
|
89
|
+
const callbackResult = interactive ? Promise.race([httpResult, consumePastedCallback({
|
|
90
|
+
input,
|
|
91
|
+
output,
|
|
92
|
+
signal: pasteAbort.signal,
|
|
93
|
+
complete: completeOnce
|
|
94
|
+
})]) : httpResult;
|
|
95
|
+
await Promise.all([state.openLoginPage(interactive), callbackResult]);
|
|
58
96
|
} finally {
|
|
97
|
+
pasteAbort.abort();
|
|
59
98
|
if (server.listening) await new Promise((resolve) => server.close(() => resolve()));
|
|
60
99
|
}
|
|
61
100
|
}
|
|
101
|
+
async function consumePastedCallback(options) {
|
|
102
|
+
if (!options.input.isTTY) return;
|
|
103
|
+
const rl = readline.createInterface({
|
|
104
|
+
input: options.input,
|
|
105
|
+
output: options.output
|
|
106
|
+
});
|
|
107
|
+
try {
|
|
108
|
+
for (;;) {
|
|
109
|
+
let answer;
|
|
110
|
+
try {
|
|
111
|
+
answer = await rl.question("Paste the callback URL here: ", { signal: options.signal });
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (error?.name === "AbortError") return;
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
const trimmed = answer.trim().replace(/^["']|["']$/g, "");
|
|
117
|
+
let url;
|
|
118
|
+
try {
|
|
119
|
+
if (!trimmed) throw new Error("empty input");
|
|
120
|
+
url = new URL(trimmed);
|
|
121
|
+
} catch {
|
|
122
|
+
options.output.write("That didn't look like a URL. Paste the full localhost callback URL and try again.\n");
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
await options.complete(url);
|
|
127
|
+
return;
|
|
128
|
+
} catch (error) {
|
|
129
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
130
|
+
options.output.write(`Sign-in didn't complete (${message}). Paste the callback URL to try again.\n`);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
} finally {
|
|
135
|
+
rl.close();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
62
138
|
var LoginState = class {
|
|
63
139
|
latestVerifier;
|
|
64
140
|
latestState;
|
|
65
141
|
sdk;
|
|
66
142
|
openUrl;
|
|
67
143
|
tokenStorage;
|
|
144
|
+
output;
|
|
68
145
|
constructor(options) {
|
|
69
146
|
this.options = options;
|
|
70
|
-
this.tokenStorage = options.tokenStorage ?? new FileTokenStorage(options.env);
|
|
147
|
+
this.tokenStorage = options.tokenStorage ?? new FileTokenStorage(options.env, options.signal);
|
|
71
148
|
this.sdk = createManagementApiSdk({
|
|
72
149
|
clientId: options.clientId ?? "cmm3lndn701oo0uefvxzo0ivw",
|
|
73
150
|
redirectUri: `http://${options.hostname}:${options.port}/auth/callback`,
|
|
@@ -76,8 +153,10 @@ var LoginState = class {
|
|
|
76
153
|
authBaseUrl: options.authBaseUrl
|
|
77
154
|
});
|
|
78
155
|
this.openUrl = options.openUrl ?? open;
|
|
156
|
+
this.output = options.output;
|
|
79
157
|
}
|
|
80
|
-
async openLoginPage() {
|
|
158
|
+
async openLoginPage(interactive) {
|
|
159
|
+
this.options.signal?.throwIfAborted();
|
|
81
160
|
const { url, state, verifier } = await this.sdk.getLoginUrl({
|
|
82
161
|
scope: "workspace:admin offline_access",
|
|
83
162
|
additionalParams: {
|
|
@@ -88,7 +167,19 @@ var LoginState = class {
|
|
|
88
167
|
});
|
|
89
168
|
this.latestState = state;
|
|
90
169
|
this.latestVerifier = verifier;
|
|
91
|
-
|
|
170
|
+
this.options.signal?.throwIfAborted();
|
|
171
|
+
if (interactive) this.printLoginInstructions(url);
|
|
172
|
+
try {
|
|
173
|
+
await this.openUrl(url);
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (!interactive) throw error;
|
|
176
|
+
}
|
|
177
|
+
this.options.signal?.throwIfAborted();
|
|
178
|
+
}
|
|
179
|
+
printLoginInstructions(url) {
|
|
180
|
+
const output = this.output;
|
|
181
|
+
if (!output) return;
|
|
182
|
+
output.write(`\nOpen this URL to sign in: ${url}\n\nIf the browser opens on another machine, finish sign-in there. When it\nredirects to localhost, copy the full localhost URL from the address bar\nand paste it here.\n\n`);
|
|
92
183
|
}
|
|
93
184
|
async handleCallback(url) {
|
|
94
185
|
if (url.pathname !== "/auth/callback") throw new AuthError$1("Not a callback URL");
|
|
@@ -115,10 +206,14 @@ var LoginState = class {
|
|
|
115
206
|
try {
|
|
116
207
|
const tokens = await this.tokenStorage.getTokens();
|
|
117
208
|
if (!tokens?.workspaceId) return null;
|
|
118
|
-
const { data } = await this.sdk.client.GET("/v1/workspaces/{id}", {
|
|
209
|
+
const { data } = await this.sdk.client.GET("/v1/workspaces/{id}", {
|
|
210
|
+
params: { path: { id: tokens.workspaceId } },
|
|
211
|
+
signal: this.options.signal
|
|
212
|
+
});
|
|
119
213
|
const name = data?.data?.name;
|
|
120
214
|
return typeof name === "string" && name.trim().length > 0 ? name.trim() : null;
|
|
121
215
|
} catch {
|
|
216
|
+
this.options.signal?.throwIfAborted();
|
|
122
217
|
return null;
|
|
123
218
|
}
|
|
124
219
|
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { CliError } from "../../shell/errors.js";
|
|
2
|
+
//#region src/lib/database/provider.ts
|
|
3
|
+
function createManagementDatabaseProvider(client) {
|
|
4
|
+
return {
|
|
5
|
+
async listDatabases(options) {
|
|
6
|
+
const databases = [];
|
|
7
|
+
let cursor;
|
|
8
|
+
while (true) {
|
|
9
|
+
const result = await client.GET("/v1/databases", {
|
|
10
|
+
params: { query: {
|
|
11
|
+
projectId: options.projectId,
|
|
12
|
+
branchGitName: options.branchName,
|
|
13
|
+
cursor
|
|
14
|
+
} },
|
|
15
|
+
signal: options.signal
|
|
16
|
+
});
|
|
17
|
+
if (result.error || !result.data) throw databaseApiError("Failed to list databases", result.response, result.error);
|
|
18
|
+
databases.push(...result.data.data);
|
|
19
|
+
if (!result.data.pagination.hasMore || !result.data.pagination.nextCursor) break;
|
|
20
|
+
cursor = result.data.pagination.nextCursor;
|
|
21
|
+
}
|
|
22
|
+
return databases.map((database) => normalizeDatabase(database, options.projectId));
|
|
23
|
+
},
|
|
24
|
+
async showDatabase(databaseId, options) {
|
|
25
|
+
const result = await client.GET("/v1/databases/{databaseId}", {
|
|
26
|
+
params: { path: { databaseId } },
|
|
27
|
+
signal: options?.signal
|
|
28
|
+
});
|
|
29
|
+
if (result.response?.status === 404) return null;
|
|
30
|
+
if (result.error || !result.data) throw databaseApiError("Failed to show database", result.response, result.error);
|
|
31
|
+
const database = result.data.data;
|
|
32
|
+
return normalizeDatabase(database, requireDatabaseProjectId(database, options?.projectId));
|
|
33
|
+
},
|
|
34
|
+
async createDatabase(options) {
|
|
35
|
+
const result = await client.POST("/v1/databases", {
|
|
36
|
+
body: {
|
|
37
|
+
projectId: options.projectId,
|
|
38
|
+
name: options.name,
|
|
39
|
+
source: { type: "empty" },
|
|
40
|
+
...options.branchName ? { branchGitName: options.branchName } : {},
|
|
41
|
+
...options.region ? { region: options.region } : {}
|
|
42
|
+
},
|
|
43
|
+
signal: options.signal
|
|
44
|
+
});
|
|
45
|
+
if (result.error || !result.data) throw databaseApiError("Failed to create database", result.response, result.error);
|
|
46
|
+
return normalizeCreatedDatabase(result.data.data, options.projectId);
|
|
47
|
+
},
|
|
48
|
+
async removeDatabase(databaseId, options) {
|
|
49
|
+
const result = await client.DELETE("/v1/databases/{databaseId}", {
|
|
50
|
+
params: { path: { databaseId } },
|
|
51
|
+
signal: options?.signal
|
|
52
|
+
});
|
|
53
|
+
if (result.error) throw databaseApiError("Failed to remove database", result.response, result.error);
|
|
54
|
+
},
|
|
55
|
+
async listConnections(databaseId, options) {
|
|
56
|
+
const result = await client.GET("/v1/databases/{databaseId}/connections", {
|
|
57
|
+
params: { path: { databaseId } },
|
|
58
|
+
signal: options?.signal
|
|
59
|
+
});
|
|
60
|
+
if (result.error || !result.data) throw databaseApiError("Failed to list database connections", result.response, result.error);
|
|
61
|
+
return result.data.data.map((connection) => normalizeConnection(connection, databaseId));
|
|
62
|
+
},
|
|
63
|
+
async createConnection(options) {
|
|
64
|
+
const result = await client.POST("/v1/databases/{databaseId}/connections", {
|
|
65
|
+
params: { path: { databaseId: options.databaseId } },
|
|
66
|
+
body: { name: options.name },
|
|
67
|
+
signal: options.signal
|
|
68
|
+
});
|
|
69
|
+
if (result.error || !result.data) throw databaseApiError("Failed to create database connection", result.response, result.error);
|
|
70
|
+
return normalizeCreatedConnection(result.data.data, options.databaseId);
|
|
71
|
+
},
|
|
72
|
+
async removeConnection(connectionId, options) {
|
|
73
|
+
const result = await client.DELETE("/v1/connections/{id}", {
|
|
74
|
+
params: { path: { id: connectionId } },
|
|
75
|
+
signal: options?.signal
|
|
76
|
+
});
|
|
77
|
+
if (result.error) throw databaseApiError("Failed to remove database connection", result.response, result.error);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function normalizeDatabase(database, fallbackProjectId) {
|
|
82
|
+
return {
|
|
83
|
+
id: database.id,
|
|
84
|
+
name: database.name,
|
|
85
|
+
projectId: database.projectId ?? fallbackProjectId,
|
|
86
|
+
branchId: database.branchId ?? database.branch?.id ?? null,
|
|
87
|
+
branchName: database.branchGitName ?? database.branchName ?? database.branch?.gitName ?? database.branch?.name ?? null,
|
|
88
|
+
region: normalizeRegion(database),
|
|
89
|
+
status: database.status ?? null,
|
|
90
|
+
isDefault: database.isDefault ?? null,
|
|
91
|
+
createdAt: database.createdAt ?? null
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function normalizeConnection(connection, fallbackDatabaseId) {
|
|
95
|
+
return {
|
|
96
|
+
id: connection.id,
|
|
97
|
+
name: connection.name ?? connection.id,
|
|
98
|
+
databaseId: connection.databaseId ?? fallbackDatabaseId,
|
|
99
|
+
createdAt: connection.createdAt ?? null
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function normalizeCreatedDatabase(database, fallbackProjectId) {
|
|
103
|
+
const rawConnection = database.connections?.[0];
|
|
104
|
+
if (!rawConnection) throw new CliError({
|
|
105
|
+
code: "DATABASE_CONNECTION_MISSING",
|
|
106
|
+
domain: "database",
|
|
107
|
+
summary: "Created database did not return a connection string",
|
|
108
|
+
why: "The Management API created the database but did not include the one-time connection payload.",
|
|
109
|
+
fix: "Create a connection explicitly with prisma-cli database connection create <database>.",
|
|
110
|
+
exitCode: 1,
|
|
111
|
+
nextSteps: [`prisma-cli database connection create ${database.id}`]
|
|
112
|
+
});
|
|
113
|
+
return {
|
|
114
|
+
database: normalizeDatabase(database, fallbackProjectId),
|
|
115
|
+
...normalizeCreatedConnection(rawConnection, database.id)
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function normalizeCreatedConnection(connection, fallbackDatabaseId) {
|
|
119
|
+
const connectionString = extractConnectionString(connection);
|
|
120
|
+
if (!connectionString) throw new CliError({
|
|
121
|
+
code: "DATABASE_CONNECTION_STRING_MISSING",
|
|
122
|
+
domain: "database",
|
|
123
|
+
summary: "Created connection did not return a connection string",
|
|
124
|
+
why: "Database connection strings are one-time-view secrets, but the Management API did not include one in this create response.",
|
|
125
|
+
fix: "Create another database connection and store the returned URL immediately.",
|
|
126
|
+
exitCode: 1,
|
|
127
|
+
nextSteps: [`prisma-cli database connection create ${fallbackDatabaseId}`]
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
connection: normalizeConnection(connection, fallbackDatabaseId),
|
|
131
|
+
connectionString
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function normalizeRegion(database) {
|
|
135
|
+
if (typeof database.region === "string") return database.region;
|
|
136
|
+
return database.region?.id ?? database.regionId ?? null;
|
|
137
|
+
}
|
|
138
|
+
function requireDatabaseProjectId(database, fallbackProjectId) {
|
|
139
|
+
const projectId = database.projectId ?? fallbackProjectId;
|
|
140
|
+
if (projectId) return projectId;
|
|
141
|
+
throw new CliError({
|
|
142
|
+
code: "DATABASE_API_ERROR",
|
|
143
|
+
domain: "database",
|
|
144
|
+
summary: "Database response did not include a project id",
|
|
145
|
+
why: "The Management API returned database metadata without project context.",
|
|
146
|
+
fix: "Re-run with --trace for the underlying API response details.",
|
|
147
|
+
exitCode: 1,
|
|
148
|
+
nextSteps: []
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
function extractConnectionString(connection) {
|
|
152
|
+
return connection.endpoints?.pooled?.connectionString ?? connection.connectionString ?? connection.endpoints?.direct?.connectionString ?? connection.endpoints?.accelerate?.connectionString ?? null;
|
|
153
|
+
}
|
|
154
|
+
function databaseApiError(summary, response, error) {
|
|
155
|
+
const status = response?.status ?? 0;
|
|
156
|
+
return new CliError({
|
|
157
|
+
code: error?.error?.code ?? "DATABASE_API_ERROR",
|
|
158
|
+
domain: "database",
|
|
159
|
+
summary,
|
|
160
|
+
why: error?.error?.message ?? `The Management API returned status ${status || "unknown"}.`,
|
|
161
|
+
fix: error?.error?.hint ?? "Re-run with --trace for the underlying API response details.",
|
|
162
|
+
exitCode: 1,
|
|
163
|
+
nextSteps: []
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
//#endregion
|
|
167
|
+
export { createManagementDatabaseProvider, normalizeConnection, normalizeDatabase };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { resolveLocalStateFilePath } from "../adapters/local-state.js";
|
|
2
|
+
import { resolveStateDir } from "../shell/runtime.js";
|
|
3
|
+
import { readLocalGitState } from "./git/local-status.js";
|
|
4
|
+
//#region src/lib/diagnostics.ts
|
|
5
|
+
async function collectCommandDiagnostics(context, options = {}) {
|
|
6
|
+
const stateDir = resolveStateDir(context.runtime);
|
|
7
|
+
return {
|
|
8
|
+
cwd: context.runtime.cwd,
|
|
9
|
+
stateFilePath: resolveLocalStateFilePath(stateDir),
|
|
10
|
+
git: await readLocalGitState(context.runtime.cwd, context.runtime.signal),
|
|
11
|
+
durationMs: options.durationMs
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { collectCommandDiagnostics };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { access, readFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
//#region src/lib/git/local-branch.ts
|
|
4
|
+
async function readLocalGitBranch(cwd, signal) {
|
|
5
|
+
const headPath = await resolveGitHeadPath(path.join(cwd, ".git"), signal);
|
|
6
|
+
if (!headPath) return null;
|
|
7
|
+
try {
|
|
8
|
+
const head = (await readFile(headPath, {
|
|
9
|
+
encoding: "utf8",
|
|
10
|
+
signal
|
|
11
|
+
})).trim();
|
|
12
|
+
if (head.startsWith("ref: refs/heads/")) return head.slice(16);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
if (signal.aborted) throw error;
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
async function resolveGitHeadPath(gitPath, signal) {
|
|
20
|
+
signal.throwIfAborted();
|
|
21
|
+
try {
|
|
22
|
+
const raw = await readFile(gitPath, {
|
|
23
|
+
encoding: "utf8",
|
|
24
|
+
signal
|
|
25
|
+
});
|
|
26
|
+
if (raw.startsWith("gitdir:")) return path.join(path.resolve(path.dirname(gitPath), raw.slice(7).trim()), "HEAD");
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (signal.aborted) throw error;
|
|
29
|
+
}
|
|
30
|
+
signal.throwIfAborted();
|
|
31
|
+
try {
|
|
32
|
+
await access(path.join(gitPath, "HEAD"));
|
|
33
|
+
signal.throwIfAborted();
|
|
34
|
+
return path.join(gitPath, "HEAD");
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (signal.aborted) throw error;
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
export { readLocalGitBranch };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
//#region src/lib/git/local-status.ts
|
|
3
|
+
async function readLocalGitState(cwd, signal) {
|
|
4
|
+
signal.throwIfAborted();
|
|
5
|
+
if ((await runGit(cwd, ["rev-parse", "--is-inside-work-tree"], signal))?.trim() !== "true") return null;
|
|
6
|
+
const [ref, sha, status] = await Promise.all([
|
|
7
|
+
runGit(cwd, [
|
|
8
|
+
"symbolic-ref",
|
|
9
|
+
"--quiet",
|
|
10
|
+
"--short",
|
|
11
|
+
"HEAD"
|
|
12
|
+
], signal),
|
|
13
|
+
runGit(cwd, [
|
|
14
|
+
"rev-parse",
|
|
15
|
+
"--short",
|
|
16
|
+
"HEAD"
|
|
17
|
+
], signal),
|
|
18
|
+
runGit(cwd, ["status", "--porcelain"], signal)
|
|
19
|
+
]);
|
|
20
|
+
return {
|
|
21
|
+
ref: cleanGitValue(ref),
|
|
22
|
+
sha: cleanGitValue(sha),
|
|
23
|
+
dirty: status === null ? null : status.trim().length > 0
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function runGit(cwd, args, signal) {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
signal.throwIfAborted();
|
|
29
|
+
execFile("git", args, {
|
|
30
|
+
cwd,
|
|
31
|
+
signal,
|
|
32
|
+
timeout: 2e3
|
|
33
|
+
}, (error, stdout) => {
|
|
34
|
+
if (signal.aborted) {
|
|
35
|
+
reject(error);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (error) {
|
|
39
|
+
resolve(null);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
resolve(stdout);
|
|
43
|
+
}).on("error", (error) => {
|
|
44
|
+
if (signal.aborted) {
|
|
45
|
+
reject(error);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
resolve(null);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function cleanGitValue(value) {
|
|
53
|
+
const cleaned = value?.trim();
|
|
54
|
+
return cleaned ? cleaned : null;
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
export { readLocalGitState };
|