@cedarjs/codemods 2.7.0-rc.107 → 2.7.0-rc.118

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 CHANGED
@@ -235,8 +235,8 @@ yarn build
235
235
  The CLI is meant to be run on a Cedar project (i.e. it expects you to be cd'd into a Cedar project), but you can provide it as an environment variable too!
236
236
 
237
237
  ```shell
238
- CEDAR_CWD=/path/to/rw-project node "./packages/codemods/dist/codemods.js" {your-codemod-name}
239
- # ☝️ this is the path to your rw project (not the framework!)
238
+ CEDAR_CWD=/path/to/cedar-project node ./packages/codemods/dist/codemods.js {your-codemod-name}
239
+ # ☝️ this is the path to your Cedar project (not the framework!)
240
240
  ```
241
241
 
242
242
  > **💡 Tip**
@@ -36,69 +36,48 @@ __export(prismaV7Prep_exports, {
36
36
  module.exports = __toCommonJS(prismaV7Prep_exports);
37
37
  var import_node_fs = __toESM(require("node:fs"));
38
38
  var import_node_path = __toESM(require("node:path"));
39
- var import_jscodeshift = __toESM(require("jscodeshift"));
40
39
  var import_project_config = require("@cedarjs/project-config");
41
- var import_prettify = __toESM(require("../../../lib/prettify"));
42
- function getParserForFile(filePath) {
43
- if (filePath.endsWith(".tsx") || filePath.endsWith(".jsx")) {
44
- return import_jscodeshift.default.withParser("tsx");
40
+ const CODE_FILE_GLOB = "**/*.{ts,tsx,cts,mts,js,jsx,cjs,mjs}";
41
+ const PRISMA_CLIENT_REEXPORT = "export * from '@prisma/client'";
42
+ function insertDbReexport(source) {
43
+ if (source.includes(PRISMA_CLIENT_REEXPORT)) {
44
+ return source;
45
45
  }
46
- return import_jscodeshift.default.withParser("ts");
47
- }
48
- function getPrettierParserForFile(filePath) {
49
- if (filePath.endsWith(".ts") || filePath.endsWith(".tsx") || filePath.endsWith(".cts") || filePath.endsWith(".mts")) {
50
- return "typescript";
46
+ const lines = source.split("\n");
47
+ const prismaImportIndex = lines.findIndex(
48
+ (line) => /from\s+['"]@prisma\/client['"]/.test(line)
49
+ );
50
+ if (prismaImportIndex < 0) {
51
+ throw new Error("Unexpected src/lib/db content");
51
52
  }
52
- return "babel";
53
+ lines.splice(prismaImportIndex + 1, 0, "", PRISMA_CLIENT_REEXPORT);
54
+ return lines.join("\n");
53
55
  }
54
- function transformDbFile(file) {
55
- const parser = getParserForFile(file.path);
56
- const root = parser(file.source);
57
- const existingExport = root.find(parser.ExportAllDeclaration, {
58
- source: { value: "@prisma/client" }
59
- });
60
- if (existingExport.length > 0) {
61
- return file.source;
62
- }
63
- const prismaClientImport = root.find(parser.ImportDeclaration, {
64
- source: { value: "@prisma/client" }
65
- });
66
- if (prismaClientImport.length === 0) {
67
- return file.source;
56
+ async function collectCodeFiles(dir) {
57
+ try {
58
+ return await Array.fromAsync(import_node_fs.default.promises.glob(CODE_FILE_GLOB, { cwd: dir }));
59
+ } catch (error) {
60
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT") {
61
+ return [];
62
+ }
63
+ throw error;
68
64
  }
69
- const importNode = prismaClientImport.get();
70
- const exportStatement = parser.exportAllDeclaration(
71
- parser.literal("@prisma/client"),
72
- null
73
- );
74
- importNode.insertAfter(exportStatement);
75
- return root.toSource();
76
- }
77
- function transformOtherFile(file) {
78
- const parser = getParserForFile(file.path);
79
- const root = parser(file.source);
80
- const isInRootScripts = file.path.startsWith((0, import_project_config.getPaths)().scripts + import_node_path.default.sep);
81
- const importPath = isInRootScripts ? "api/src/lib/db" : "src/lib/db";
82
- root.find(parser.ImportDeclaration, {
83
- source: { value: "@prisma/client" }
84
- }).forEach((importDecl) => {
85
- importDecl.get("source").replace(parser.literal(importPath));
86
- });
87
- return root.toSource();
88
65
  }
89
66
  async function getPrismaV7PrepContext() {
90
67
  const paths = (0, import_project_config.getPaths)();
91
68
  const prismaConfigPath = paths.api.prismaConfig;
92
69
  const dataMigrationsPath = await (0, import_project_config.getDataMigrationsPath)(prismaConfigPath);
93
- const dbPath = import_node_path.default.join(paths.api.lib, "db.ts");
70
+ const dbPathTs = import_node_path.default.join(paths.api.lib, "db.ts");
94
71
  const dbPathJs = import_node_path.default.join(paths.api.lib, "db.js");
95
- let dbFilePath = dbPath;
96
- if (!import_node_fs.default.existsSync(dbPath)) {
72
+ let dbFilePath = null;
73
+ if (import_node_fs.default.existsSync(dbPathTs)) {
74
+ dbFilePath = dbPathTs;
75
+ } else if (import_node_fs.default.existsSync(dbPathJs)) {
97
76
  dbFilePath = dbPathJs;
98
77
  }
99
78
  return {
100
79
  dataMigrationsPath,
101
- dbFilePath: import_node_fs.default.existsSync(dbFilePath) ? dbFilePath : null,
80
+ dbFilePath,
102
81
  paths
103
82
  };
104
83
  }
@@ -106,42 +85,33 @@ async function updateDbFile(dbFilePath) {
106
85
  if (!dbFilePath) {
107
86
  return "skipped";
108
87
  }
109
- const source = import_node_fs.default.readFileSync(dbFilePath, "utf-8");
110
- const transformed = transformDbFile({ source, path: dbFilePath });
111
- import_node_fs.default.writeFileSync(
112
- dbFilePath,
113
- await (0, import_prettify.default)(transformed, {
114
- parser: getPrettierParserForFile(dbFilePath)
115
- })
116
- );
88
+ const source = await import_node_fs.default.promises.readFile(dbFilePath, "utf-8");
89
+ const transformed = insertDbReexport(source);
90
+ if (transformed === source) {
91
+ return "unmodified";
92
+ }
93
+ await import_node_fs.default.promises.writeFile(dbFilePath, transformed);
117
94
  return "updated";
118
95
  }
119
96
  async function rewritePrismaImportsInDirectory(dir, dbFilePath) {
120
- if (!import_node_fs.default.existsSync(dir)) {
121
- return {
122
- filesSeen: 0,
123
- filesUpdated: 0
124
- };
97
+ const scriptsDir = (0, import_project_config.ensurePosixPath)((0, import_project_config.getPaths)().scripts);
98
+ const normalizedDbFilePath = dbFilePath ? (0, import_project_config.ensurePosixPath)(dbFilePath) : null;
99
+ const fileMatches = await collectCodeFiles(dir);
100
+ const files = fileMatches.map((relativePath) => import_node_path.default.join(dir, relativePath)).filter((filePath) => (0, import_project_config.ensurePosixPath)(filePath) !== normalizedDbFilePath);
101
+ if (files.length === 0) {
102
+ return "skipped";
125
103
  }
126
- const files = import_node_fs.default.readdirSync(dir, { recursive: true, encoding: "utf8" }).filter(
127
- (file) => file.endsWith(".ts") || file.endsWith(".cts") || file.endsWith(".mts") || file.endsWith(".tsx") || file.endsWith(".js") || file.endsWith(".cjs") || file.endsWith(".mjs") || file.endsWith(".jsx")
128
- ).map((file) => import_node_path.default.join(dir, file)).filter((file) => import_node_fs.default.statSync(file).isFile()).filter((file) => file !== dbFilePath);
129
- let filesUpdated = 0;
130
- for (const file of files) {
131
- const source = import_node_fs.default.readFileSync(file, "utf-8");
132
- const transformed = transformOtherFile({ source, path: file });
133
- const prettified = await (0, import_prettify.default)(transformed, {
134
- parser: getPrettierParserForFile(file)
135
- });
136
- if (prettified !== source) {
137
- filesUpdated += 1;
138
- import_node_fs.default.writeFileSync(file, prettified);
104
+ for (const filePath of files) {
105
+ const source = await import_node_fs.default.promises.readFile(filePath, "utf-8");
106
+ const isScriptFile = (0, import_project_config.ensurePosixPath)(filePath).startsWith(scriptsDir);
107
+ const importPath = isScriptFile ? "api/src/lib/db" : "src/lib/db";
108
+ const importPattern = /(['"])@prisma\/client\1/g;
109
+ const transformed = source.replace(importPattern, `$1${importPath}$1`);
110
+ if (transformed !== source) {
111
+ await import_node_fs.default.promises.writeFile(filePath, transformed);
139
112
  }
140
113
  }
141
- return {
142
- filesSeen: files.length,
143
- filesUpdated
144
- };
114
+ return "updated";
145
115
  }
146
116
  async function prismaV7Prep() {
147
117
  const context = await getPrismaV7PrepContext();
@@ -37,62 +37,49 @@ var import_tasuku = __toESM(require("tasuku"));
37
37
  var import_prismaV7Prep = require("./prismaV7Prep");
38
38
  const command = "prisma-v7-prep";
39
39
  const description = "(v2.7.x) Prepares for Prisma v7 by funneling imports through src/lib/db";
40
- const handler = () => {
41
- (0, import_tasuku.default)("Prisma v7 Prep", async ({ setError }) => {
42
- try {
43
- const context = await (0, import_prismaV7Prep.getPrismaV7PrepContext)();
44
- await (0, import_tasuku.default)(
45
- "Resolve project paths",
46
- async ({ setOutput }) => {
47
- setOutput(
48
- `api/src: ${context.paths.api.src} | dataMigrations: ${context.dataMigrationsPath} | scripts: ` + context.paths.scripts
49
- );
50
- }
40
+ const handler = async () => {
41
+ const context = await (0, import_prismaV7Prep.getPrismaV7PrepContext)();
42
+ await import_tasuku.default.group((task2) => [
43
+ task2("Add api/src/lib/db re-export", async ({ setOutput }) => {
44
+ const result = await (0, import_prismaV7Prep.updateDbFile)(context.dbFilePath);
45
+ if (result === "skipped") {
46
+ setOutput("Skipped (no api/src/lib/db.ts or api/src/lib/db.js found)");
47
+ return;
48
+ } else if (result === "unmodified") {
49
+ setOutput("Skipped (no changes needed)");
50
+ return;
51
+ }
52
+ setOutput(`Updated ${context.dbFilePath}`);
53
+ }),
54
+ task2(
55
+ "Rewrite imports in api/src",
56
+ () => (0, import_prismaV7Prep.rewritePrismaImportsInDirectory)(
57
+ context.paths.api.src,
58
+ context.dbFilePath
59
+ )
60
+ ),
61
+ task2("Rewrite imports in api/db/dataMigrations", async ({ setOutput }) => {
62
+ const result = await (0, import_prismaV7Prep.rewritePrismaImportsInDirectory)(
63
+ context.dataMigrationsPath,
64
+ context.dbFilePath
51
65
  );
52
- await (0, import_tasuku.default)("Update api/src/lib/db re-export", async ({ setOutput }) => {
53
- const result = await (0, import_prismaV7Prep.updateDbFile)(context.dbFilePath);
54
- if (result === "skipped") {
55
- setOutput("Skipped (no api/src/lib/db.ts or api/src/lib/db.js found)");
56
- return;
57
- }
58
- setOutput(`Updated ${context.dbFilePath}`);
59
- });
60
- await (0, import_tasuku.default)("Rewrite imports in api/src", async ({ setOutput }) => {
61
- const result = await (0, import_prismaV7Prep.rewritePrismaImportsInDirectory)(
62
- context.paths.api.src,
63
- context.dbFilePath
64
- );
65
- setOutput(`Updated ${result.filesUpdated}/${result.filesSeen} files`);
66
- });
67
- await (0, import_tasuku.default)(
68
- "Rewrite imports in api/db/dataMigrations",
69
- async ({ setOutput }) => {
70
- const result = await (0, import_prismaV7Prep.rewritePrismaImportsInDirectory)(
71
- context.dataMigrationsPath,
72
- context.dbFilePath
73
- );
74
- if (result.filesSeen === 0) {
75
- setOutput("Skipped (directory missing or empty)");
76
- return;
77
- }
78
- setOutput(`Updated ${result.filesUpdated}/${result.filesSeen} files`);
79
- }
66
+ if (result === "skipped") {
67
+ setOutput("Skipped (directory missing or empty)");
68
+ }
69
+ }),
70
+ task2("Rewrite imports in scripts", async ({ setOutput }) => {
71
+ const result = await (0, import_prismaV7Prep.rewritePrismaImportsInDirectory)(
72
+ context.paths.scripts,
73
+ context.dbFilePath
80
74
  );
81
- await (0, import_tasuku.default)("Rewrite imports in scripts", async ({ setOutput }) => {
82
- const result = await (0, import_prismaV7Prep.rewritePrismaImportsInDirectory)(
83
- context.paths.scripts,
84
- context.dbFilePath
85
- );
86
- if (result.filesSeen === 0) {
87
- setOutput("Skipped (directory missing or empty)");
88
- return;
89
- }
90
- setOutput(`Updated ${result.filesUpdated}/${result.filesSeen} files`);
91
- });
92
- } catch (e) {
93
- setError("Failed to codemod your project \n" + e?.message);
94
- }
95
- });
75
+ if (result === "skipped") {
76
+ setOutput("Skipped (directory missing or empty)");
77
+ }
78
+ })
79
+ ]);
80
+ console.log(
81
+ "Some imports might be in the wrong order. If that's the case, you can run `yarn cedar lint --fix` to reorder them."
82
+ );
96
83
  };
97
84
  // Annotate the CommonJS export names for ESM import in node:
98
85
  0 && (module.exports = {
@@ -31,12 +31,17 @@ __export(prettify_exports, {
31
31
  default: () => prettify_default
32
32
  });
33
33
  module.exports = __toCommonJS(prettify_exports);
34
- var import_path = __toESM(require("path"));
34
+ var import_node_fs = __toESM(require("node:fs"));
35
+ var import_node_path = __toESM(require("node:path"));
35
36
  var import_prettier = require("prettier");
36
37
  var import_project_config = require("@cedarjs/project-config");
37
38
  const getPrettierConfig = async () => {
39
+ const basePath = (0, import_project_config.getPaths)().base;
40
+ const prettierConfigCjsPath = import_node_path.default.join(basePath, "prettier.config.cjs");
41
+ const prettierConfigMjsPath = import_node_path.default.join(basePath, "prettier.config.mjs");
42
+ const prettierConfigPath = import_node_fs.default.existsSync(prettierConfigCjsPath) ? prettierConfigCjsPath : prettierConfigMjsPath;
38
43
  try {
39
- const { default: prettierConfig } = await import(`file://${import_path.default.join((0, import_project_config.getPaths)().base, "prettier.config.cjs")}`);
44
+ const { default: prettierConfig } = await import(`file://${prettierConfigPath}`);
40
45
  return prettierConfig;
41
46
  } catch {
42
47
  return void 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/codemods",
3
- "version": "2.7.0-rc.107",
3
+ "version": "2.7.0-rc.118",
4
4
  "description": "Codemods to ease upgrading a CedarJS Project",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,7 +30,7 @@
30
30
  "@babel/plugin-transform-typescript": "^7.26.8",
31
31
  "@babel/runtime-corejs3": "7.29.0",
32
32
  "@babel/traverse": "7.29.0",
33
- "@cedarjs/project-config": "2.7.0-rc.107",
33
+ "@cedarjs/project-config": "2.7.0-rc.118",
34
34
  "@svgr/core": "8.1.0",
35
35
  "@svgr/plugin-jsx": "8.1.0",
36
36
  "@vscode/ripgrep": "1.17.0",
@@ -49,16 +49,18 @@
49
49
  "yargs": "17.7.2"
50
50
  },
51
51
  "devDependencies": {
52
- "@cedarjs/framework-tools": "2.7.0-rc.107",
52
+ "@cedarjs/framework-tools": "2.7.0-rc.118",
53
53
  "@types/babel__core": "7.20.5",
54
54
  "@types/jscodeshift": "0.12.0",
55
55
  "@types/yargs": "17.0.35",
56
+ "memfs": "4.56.10",
56
57
  "publint": "0.3.17",
58
+ "ts-dedent": "2.2.0",
57
59
  "tsx": "4.21.0",
58
60
  "vitest": "3.2.4"
59
61
  },
60
62
  "publishConfig": {
61
63
  "access": "public"
62
64
  },
63
- "gitHead": "e4c28ed97a1f4859162b32aaede569ab4412e06b"
65
+ "gitHead": "79d6f4fc16419ba0d8001b257f54813189c3b0c3"
64
66
  }