@cedarjs/codemods 2.8.1-next.109 → 2.8.1-next.116

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.
@@ -33,11 +33,11 @@ async function getPrismaV7Context() {
33
33
  } else if (fs.existsSync(dbPathJs)) {
34
34
  dbFilePath = dbPathJs;
35
35
  }
36
- return { provider, isSqlite, isPostgres, paths, dbFilePath };
36
+ return { provider, isSqlite, isPostgres, dbFilePath };
37
37
  }
38
38
  async function prismaV7() {
39
+ const paths = getPaths();
39
40
  const context = await getPrismaV7Context();
40
- const { paths, isSqlite } = context;
41
41
  await runUpdateSchemaFile();
42
42
  await updatePrismaConfig(paths.api.prismaConfig);
43
43
  if (context.dbFilePath) {
@@ -46,14 +46,14 @@ async function prismaV7() {
46
46
  targetPaths: [context.dbFilePath],
47
47
  parser: "ts",
48
48
  options: {
49
- isSqlite,
49
+ isSqlite: context.isSqlite,
50
50
  isPostgres: context.isPostgres,
51
51
  silent: true
52
52
  }
53
53
  });
54
54
  }
55
55
  await rewriteRemainingImports();
56
- if (isSqlite || context.isPostgres) {
56
+ if (context.isSqlite || context.isPostgres) {
57
57
  await updateApiPackageJson(path.join(paths.api.base, "package.json"), {
58
58
  provider: context.provider
59
59
  });
@@ -1,5 +1,6 @@
1
1
  import path from "node:path";
2
2
  import { styleText } from "node:util";
3
+ import { getPaths } from "@cedarjs/project-config";
3
4
  import runTransform from "../../../lib/runTransform.js";
4
5
  import { getPrismaV7Context } from "./prismaV7.js";
5
6
  import rewriteRemainingImports from "./rewriteRemainingImports.js";
@@ -16,8 +17,9 @@ function step(label, output) {
16
17
  console.log(` \u2192 ${output}`);
17
18
  }
18
19
  const handler = async () => {
20
+ const paths = getPaths();
19
21
  const context = await getPrismaV7Context();
20
- const { paths, isSqlite, isPostgres, provider, dbFilePath } = context;
22
+ const { isSqlite, isPostgres, provider, dbFilePath } = context;
21
23
  console.log(styleText("bold", "\u276F Prisma v7 Migration"));
22
24
  const { results: schemaResults } = await runUpdateSchemaFile();
23
25
  if (schemaResults.length === 0) {
@@ -144,7 +146,7 @@ const handler = async () => {
144
146
  ` ${offset + 1}. Run \`yarn install\` to install new dependencies`,
145
147
  ` ${offset + 2}. Run \`yarn cedar prisma generate\` to generate the new Prisma client`,
146
148
  ` ${offset + 3}. Run \`yarn cedar prisma migrate dev\` to verify migrations work`,
147
- ` ${offset + 4}. Run \`yarn cedar lint --fix\` to fix import ordering issues etc`
149
+ ` ${offset + 4}. Run \`yarn cedar lint --fix\` to fix code formatting issues`
148
150
  );
149
151
  console.log(styleText("bold", "\n Next steps:"));
150
152
  console.log(nextSteps.join("\n"));
@@ -11,6 +11,70 @@ function transform(file, api, options = {}) {
11
11
  if (alreadyMigrated) {
12
12
  return file.source;
13
13
  }
14
+ function stealLeadingComments(fromNode, toNode) {
15
+ const fromComments = fromNode.comments;
16
+ if (!fromComments || fromComments.length === 0) {
17
+ return;
18
+ }
19
+ const leading = fromComments.filter((c) => c.leading);
20
+ if (leading.length === 0) {
21
+ return;
22
+ }
23
+ toNode.comments = [...leading, ...toNode.comments ?? []];
24
+ fromNode.comments = fromComments.filter((c) => !c.leading);
25
+ }
26
+ function insertAdapterImport(importDecl, { takeLeadingComments = false } = {}) {
27
+ const clientImport = root.find(j.ImportDeclaration, {
28
+ source: { value: NEW_CLIENT_PATH }
29
+ });
30
+ if (clientImport.length > 0) {
31
+ if (takeLeadingComments) {
32
+ stealLeadingComments(clientImport.get().node, importDecl);
33
+ }
34
+ clientImport.insertBefore(importDecl);
35
+ } else {
36
+ root.find(j.ImportDeclaration).at(0).insertBefore(importDecl);
37
+ }
38
+ }
39
+ function addAdapterToPrismaClient() {
40
+ root.find(j.NewExpression, {
41
+ callee: { type: "Identifier", name: "PrismaClient" }
42
+ }).forEach((nodePath) => {
43
+ const args = nodePath.node.arguments;
44
+ if (args.length === 0) {
45
+ nodePath.node.arguments = [
46
+ j.objectExpression([
47
+ Object.assign(
48
+ j.objectProperty(
49
+ j.identifier("adapter"),
50
+ j.identifier("adapter")
51
+ ),
52
+ { shorthand: true }
53
+ )
54
+ ])
55
+ ];
56
+ return;
57
+ }
58
+ const firstArg = args[0];
59
+ if (firstArg.type !== "ObjectExpression") {
60
+ return;
61
+ }
62
+ const hasAdapterProp = firstArg.properties.some(
63
+ (prop) => prop.type === "ObjectProperty" && prop.key.type === "Identifier" && "name" in prop.key && prop.key.name === "adapter"
64
+ );
65
+ if (!hasAdapterProp) {
66
+ firstArg.properties.push(
67
+ Object.assign(
68
+ j.objectProperty(
69
+ j.identifier("adapter"),
70
+ j.identifier("adapter")
71
+ ),
72
+ { shorthand: true }
73
+ )
74
+ );
75
+ }
76
+ });
77
+ }
14
78
  let didTransform = false;
15
79
  root.find(j.ImportDeclaration, { source: { value: OLD_PRISMA_CLIENT } }).forEach((nodePath) => {
16
80
  nodePath.node.source = j.stringLiteral(NEW_CLIENT_PATH);
@@ -31,25 +95,26 @@ function transform(file, api, options = {}) {
31
95
  source: { value: "@prisma/adapter-pg" }
32
96
  }).length > 0;
33
97
  if (!hasAdapterImport2) {
34
- const clientImport2 = root.find(j.ImportDeclaration, {
35
- source: { value: NEW_CLIENT_PATH }
36
- });
37
- const adapterImport = j.importDeclaration(
38
- [j.importSpecifier(j.identifier("PrismaPg"), j.identifier("PrismaPg"))],
39
- j.stringLiteral("@prisma/adapter-pg")
98
+ insertAdapterImport(
99
+ j.importDeclaration(
100
+ [
101
+ j.importSpecifier(
102
+ j.identifier("PrismaPg"),
103
+ j.identifier("PrismaPg")
104
+ )
105
+ ],
106
+ j.stringLiteral("@prisma/adapter-pg")
107
+ ),
108
+ { takeLeadingComments: true }
40
109
  );
41
- clientImport2.insertBefore(adapterImport);
42
110
  }
43
111
  const hasAdapter2 = root.find(j.VariableDeclarator, {
44
112
  id: { type: "Identifier", name: "adapter" }
45
113
  }).length > 0;
46
- const prismaClientNewExpr2 = root.find(j.NewExpression, {
47
- callee: { type: "Identifier", name: "PrismaClient" }
48
- });
49
- if (prismaClientNewExpr2.length > 0 && !hasAdapter2) {
50
- const prismaClientDeclaration = prismaClientNewExpr2.closest(
51
- j.VariableDeclaration
52
- );
114
+ if (!hasAdapter2) {
115
+ const prismaClientDeclaration = root.find(j.NewExpression, {
116
+ callee: { type: "Identifier", name: "PrismaClient" }
117
+ }).closest(j.VariableDeclaration);
53
118
  const adapterDecl = j.variableDeclaration("const", [
54
119
  j.variableDeclarator(
55
120
  j.identifier("adapter"),
@@ -71,43 +136,7 @@ function transform(file, api, options = {}) {
71
136
  ]);
72
137
  prismaClientDeclaration.insertBefore(adapterDecl);
73
138
  }
74
- root.find(j.NewExpression, {
75
- callee: { type: "Identifier", name: "PrismaClient" }
76
- }).forEach((nodePath) => {
77
- const args = nodePath.node.arguments;
78
- if (args.length === 0) {
79
- nodePath.node.arguments = [
80
- j.objectExpression([
81
- Object.assign(
82
- j.objectProperty(
83
- j.identifier("adapter"),
84
- j.identifier("adapter")
85
- ),
86
- { shorthand: true }
87
- )
88
- ])
89
- ];
90
- return;
91
- }
92
- const firstArg = args[0];
93
- if (firstArg.type !== "ObjectExpression") {
94
- return;
95
- }
96
- const hasAdapterProp = firstArg.properties.some(
97
- (prop) => prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.key.name === "adapter"
98
- );
99
- if (!hasAdapterProp) {
100
- firstArg.properties.push(
101
- Object.assign(
102
- j.objectProperty(
103
- j.identifier("adapter"),
104
- j.identifier("adapter")
105
- ),
106
- { shorthand: true }
107
- )
108
- );
109
- }
110
- });
139
+ addAdapterToPrismaClient();
111
140
  return root.toSource({ quote: "single" });
112
141
  }
113
142
  if (!isSqlite) {
@@ -120,29 +149,27 @@ function transform(file, api, options = {}) {
120
149
  const hasGetPathsImport = root.find(j.ImportDeclaration, {
121
150
  source: { value: "@cedarjs/project-config" }
122
151
  }).length > 0;
123
- const allImports = root.find(j.ImportDeclaration);
124
- const firstImport = allImports.at(0);
125
152
  if (!hasPathImport) {
126
- const pathImport = j.importDeclaration(
127
- [j.importDefaultSpecifier(j.identifier("path"))],
128
- j.stringLiteral("node:path")
153
+ insertAdapterImport(
154
+ j.importDeclaration(
155
+ [j.importDefaultSpecifier(j.identifier("path"))],
156
+ j.stringLiteral("node:path")
157
+ ),
158
+ { takeLeadingComments: true }
129
159
  );
130
- firstImport.insertBefore(pathImport);
131
160
  }
132
- const clientImport = root.find(j.ImportDeclaration, {
133
- source: { value: NEW_CLIENT_PATH }
134
- });
135
161
  if (!hasAdapterImport) {
136
- const adapterImport = j.importDeclaration(
137
- [
138
- j.importSpecifier(
139
- j.identifier("PrismaBetterSqlite3"),
140
- j.identifier("PrismaBetterSqlite3")
141
- )
142
- ],
143
- j.stringLiteral("@prisma/adapter-better-sqlite3")
162
+ insertAdapterImport(
163
+ j.importDeclaration(
164
+ [
165
+ j.importSpecifier(
166
+ j.identifier("PrismaBetterSqlite3"),
167
+ j.identifier("PrismaBetterSqlite3")
168
+ )
169
+ ],
170
+ j.stringLiteral("@prisma/adapter-better-sqlite3")
171
+ )
144
172
  );
145
- clientImport.insertBefore(adapterImport);
146
173
  }
147
174
  if (!hasGetPathsImport) {
148
175
  const cedarjsImports = root.find(
@@ -153,6 +180,9 @@ function transform(file, api, options = {}) {
153
180
  [j.importSpecifier(j.identifier("getPaths"), j.identifier("getPaths"))],
154
181
  j.stringLiteral("@cedarjs/project-config")
155
182
  );
183
+ const clientImport = root.find(j.ImportDeclaration, {
184
+ source: { value: NEW_CLIENT_PATH }
185
+ });
156
186
  if (cedarjsImports.length > 0) {
157
187
  cedarjsImports.at(-1).insertAfter(getPathsImport);
158
188
  } else {
@@ -210,40 +240,7 @@ const resolveSqliteUrl = (url = 'file:./db/dev.db') => {
210
240
  prismaClientDeclaration.insertBefore(adapterDecl);
211
241
  }
212
242
  }
213
- root.find(j.NewExpression, {
214
- callee: { type: "Identifier", name: "PrismaClient" }
215
- }).forEach((nodePath) => {
216
- const args = nodePath.node.arguments;
217
- if (args.length === 0) {
218
- nodePath.node.arguments = [
219
- j.objectExpression([
220
- Object.assign(
221
- j.objectProperty(
222
- j.identifier("adapter"),
223
- j.identifier("adapter")
224
- ),
225
- { shorthand: true }
226
- )
227
- ])
228
- ];
229
- return;
230
- }
231
- const firstArg = args[0];
232
- if (firstArg.type !== "ObjectExpression") {
233
- return;
234
- }
235
- const hasAdapterProp = firstArg.properties.some(
236
- (prop) => prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.key.name === "adapter"
237
- );
238
- if (!hasAdapterProp) {
239
- firstArg.properties.push(
240
- Object.assign(
241
- j.objectProperty(j.identifier("adapter"), j.identifier("adapter")),
242
- { shorthand: true }
243
- )
244
- );
245
- }
246
- });
243
+ addAdapterToPrismaClient();
247
244
  return root.toSource({ quote: "single" });
248
245
  }
249
246
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/codemods",
3
- "version": "2.8.1-next.109+be993981d",
3
+ "version": "2.8.1-next.116+784d2c048",
4
4
  "description": "Codemods to ease upgrading a CedarJS Project",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,8 +29,8 @@
29
29
  "@babel/parser": "7.29.2",
30
30
  "@babel/plugin-transform-typescript": "^7.26.8",
31
31
  "@babel/traverse": "7.29.0",
32
- "@cedarjs/cli-helpers": "2.8.1-next.109+be993981d",
33
- "@cedarjs/project-config": "2.8.1-next.109+be993981d",
32
+ "@cedarjs/cli-helpers": "2.8.1-next.116+784d2c048",
33
+ "@cedarjs/project-config": "2.8.1-next.116+784d2c048",
34
34
  "@svgr/core": "8.1.0",
35
35
  "@svgr/plugin-jsx": "8.1.0",
36
36
  "@vscode/ripgrep": "1.17.1",
@@ -48,7 +48,7 @@
48
48
  "yargs": "17.7.2"
49
49
  },
50
50
  "devDependencies": {
51
- "@cedarjs/framework-tools": "2.8.1-next.109",
51
+ "@cedarjs/framework-tools": "2.8.1-next.116",
52
52
  "@types/babel__core": "7.20.5",
53
53
  "@types/jscodeshift": "17.3.0",
54
54
  "@types/yargs": "17.0.35",
@@ -61,5 +61,5 @@
61
61
  "publishConfig": {
62
62
  "access": "public"
63
63
  },
64
- "gitHead": "be993981d7e162a83244009c3f7ab9270c6773f3"
64
+ "gitHead": "784d2c0484936b2d853e5b316d167602bd80d996"
65
65
  }