@prisma/cli 3.0.0-alpha.3 → 3.0.0-alpha.4

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.
@@ -1,5 +1,3 @@
1
- import { UnsafeConfigWriteError, readLinkedProjectId, writeLinkedProjectId } from "../adapters/config.js";
2
- import { usageError } from "../shell/errors.js";
3
1
  //#region src/use-cases/create-cli-gateways.ts
4
2
  function createCliUseCaseGateways(context) {
5
3
  return {
@@ -44,17 +42,9 @@ function createCliUseCaseGateways(context) {
44
42
  },
45
43
  getDeployment: (deploymentId) => context.api.getDeployment(deploymentId)
46
44
  },
47
- projectConfigGateway: {
48
- readLinkedProjectId: () => readLinkedProjectId(context.runtime.cwd),
49
- writeLinkedProjectId: async (projectId) => {
50
- try {
51
- await writeLinkedProjectId(context.runtime.cwd, projectId);
52
- } catch (error) {
53
- if (error instanceof UnsafeConfigWriteError) throw usageError("Project link requires a writable Prisma config", error.message, "Update prisma.config.ts to use a recognizable project field, or remove it and rerun prisma-cli project link.", ["prisma-cli project link proj_123"], "project");
54
- throw error;
55
- }
56
- }
57
- },
45
+ projectStateGateway: { readRememberedProjectId: async () => {
46
+ return (await context.stateStore.readLastResolvedProject())?.id ?? null;
47
+ } },
58
48
  sessionGateway: {
59
49
  readAuthSession: async () => {
60
50
  return (await context.stateStore.read()).auth;
@@ -1,4 +1,4 @@
1
- import { CliError, authRequiredError } from "../shell/errors.js";
1
+ import { authRequiredError } from "../shell/errors.js";
2
2
  //#region src/use-cases/project.ts
3
3
  function createProjectUseCases(dependencies) {
4
4
  return {
@@ -6,44 +6,9 @@ function createProjectUseCases(dependencies) {
6
6
  const workspace = requireWorkspace(authState);
7
7
  return {
8
8
  workspace,
9
- linkedProjectId: authState.linkedProjectId,
10
9
  projects: listSortedWorkspaceProjects(dependencies.projectGateway, workspace.id).map(toProjectSummary)
11
10
  };
12
11
  },
13
- show: async (authState) => {
14
- if (!authState.linkedProjectId) return {
15
- linkedProjectId: null,
16
- workspace: null,
17
- project: null
18
- };
19
- if (!authState.authenticated || !authState.workspace) return {
20
- linkedProjectId: authState.linkedProjectId,
21
- workspace: null,
22
- project: null
23
- };
24
- const project = dependencies.projectGateway.getProjectForWorkspace(authState.workspace.id, authState.linkedProjectId);
25
- if (!project) return {
26
- linkedProjectId: authState.linkedProjectId,
27
- workspace: null,
28
- project: null
29
- };
30
- return {
31
- linkedProjectId: authState.linkedProjectId,
32
- workspace: authState.workspace,
33
- project: toProjectSummary(project)
34
- };
35
- },
36
- link: async (authState, projectId) => {
37
- const workspace = requireWorkspace(authState);
38
- const project = dependencies.projectGateway.getProjectForWorkspace(workspace.id, projectId);
39
- if (!project) throw projectNotFoundError(`The project "${projectId}" does not exist in workspace "${workspace.name}".`, "Run prisma-cli project list and choose a project id from the active workspace.");
40
- await dependencies.projectConfigGateway.writeLinkedProjectId(project.id);
41
- return {
42
- linkedProjectId: project.id,
43
- workspace,
44
- project: toProjectSummary(project)
45
- };
46
- },
47
12
  listProjectsForWorkspace: async (workspaceId) => listSortedWorkspaceProjects(dependencies.projectGateway, workspaceId).map(toProjectSummary)
48
13
  };
49
14
  }
@@ -60,16 +25,5 @@ function toProjectSummary(project) {
60
25
  name: project.name
61
26
  };
62
27
  }
63
- function projectNotFoundError(why, fix, nextSteps = ["prisma-cli project list"]) {
64
- return new CliError({
65
- code: "PROJECT_NOT_FOUND",
66
- domain: "project",
67
- summary: "Project not found",
68
- why,
69
- fix,
70
- exitCode: 1,
71
- nextSteps
72
- });
73
- }
74
28
  //#endregion
75
- export { createProjectUseCases, projectNotFoundError };
29
+ export { createProjectUseCases };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma/cli",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.0-alpha.4",
4
4
  "description": "Preview of the unified Prisma CLI.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,74 +0,0 @@
1
- import path from "node:path";
2
- import { copyFile, mkdir, mkdtemp, readFile, rm } from "node:fs/promises";
3
- import os from "node:os";
4
- import { updateConfig } from "c12/update";
5
- //#region src/adapters/config.ts
6
- const PROJECT_FIELD_PATTERN = /project\s*:\s*["']([^"']+)["']/;
7
- async function readLinkedProjectId(cwd) {
8
- const configPath = path.join(cwd, "prisma.config.ts");
9
- try {
10
- return (await readFile(configPath, "utf8")).match(PROJECT_FIELD_PATTERN)?.[1] ?? null;
11
- } catch (error) {
12
- if (error.code === "ENOENT") return null;
13
- throw error;
14
- }
15
- }
16
- var UnsafeConfigWriteError = class extends Error {
17
- constructor(message) {
18
- super(message);
19
- this.name = "UnsafeConfigWriteError";
20
- }
21
- };
22
- async function assertLinkedProjectIdWritable(cwd) {
23
- const tempDir = await mkdtemp(path.join(os.tmpdir(), "prisma-cli-config-"));
24
- const sourceConfigPath = path.join(cwd, "prisma.config.ts");
25
- const tempConfigPath = path.join(tempDir, "prisma.config.ts");
26
- try {
27
- try {
28
- await copyFile(sourceConfigPath, tempConfigPath);
29
- } catch (error) {
30
- if (error.code !== "ENOENT") throw error;
31
- }
32
- await applyLinkedProjectIdUpdate(tempDir, "proj_preflight");
33
- } catch (error) {
34
- throw toUnsafeConfigWriteError(error);
35
- } finally {
36
- await rm(tempDir, {
37
- recursive: true,
38
- force: true
39
- });
40
- }
41
- }
42
- async function writeLinkedProjectId(cwd, projectId) {
43
- try {
44
- await applyLinkedProjectIdUpdate(cwd, projectId);
45
- } catch (error) {
46
- if (error.code === "ENOENT") {
47
- await mkdir(cwd, { recursive: true });
48
- await applyLinkedProjectIdUpdate(cwd, projectId);
49
- return;
50
- }
51
- throw toUnsafeConfigWriteError(error);
52
- }
53
- }
54
- async function applyLinkedProjectIdUpdate(cwd, projectId) {
55
- await updateConfig({
56
- cwd,
57
- configFile: "prisma.config",
58
- onUpdate(config) {
59
- config.project = projectId;
60
- },
61
- onCreate() {
62
- return renderProjectConfig(projectId);
63
- }
64
- });
65
- }
66
- function renderProjectConfig(projectId) {
67
- return `export default {\n project: "${projectId}",\n};\n`;
68
- }
69
- function toUnsafeConfigWriteError(error) {
70
- if (error instanceof UnsafeConfigWriteError) return error;
71
- return new UnsafeConfigWriteError("The existing prisma.config.ts file could not be updated safely.");
72
- }
73
- //#endregion
74
- export { UnsafeConfigWriteError, assertLinkedProjectIdWritable, readLinkedProjectId, writeLinkedProjectId };