|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Script to compare directly-generated JS/DTS files with TS-compiled outputs. |
| 4 | + * |
| 5 | + * This creates two directories with the different codegen approaches and |
| 6 | + * automatically opens the diffs in VS Code. |
| 7 | + */ |
| 8 | + |
| 9 | +import { apiCodegen } from "../dist/esm/cli/codegen_templates/api.js"; |
| 10 | +import { serverCodegen } from "../dist/esm/cli/codegen_templates/server.js"; |
| 11 | +import { noSchemaDataModelDTS } from "../dist/esm/cli/codegen_templates/dataModel.js"; |
| 12 | +import * as fs from "fs"; |
| 13 | +import * as path from "path"; |
| 14 | +import * as os from "os"; |
| 15 | +import { execSync } from "child_process"; |
| 16 | +import prettier from "prettier"; |
| 17 | + |
| 18 | +const modulePaths = ["foo.ts", "bar/baz.ts"]; |
| 19 | + |
| 20 | +console.log("Generating codegen outputs for comparison...\n"); |
| 21 | + |
| 22 | +// Generate all versions of our files |
| 23 | +const apiJsDts = apiCodegen(modulePaths, { generateJavaScriptApi: true }); |
| 24 | +const apiJs = apiJsDts.JS; |
| 25 | +const apiDts = apiJsDts.DTS; |
| 26 | +const apiTsResult = apiCodegen(modulePaths, { generateJavaScriptApi: false }); |
| 27 | +const apiTs = apiTsResult.TS; |
| 28 | + |
| 29 | +const serverJsDts = serverCodegen({ generateJavaScriptApi: true }); |
| 30 | +const serverJs = serverJsDts.JS; |
| 31 | +const serverDts = serverJsDts.DTS; |
| 32 | +const serverTsResult = serverCodegen({ generateJavaScriptApi: false }); |
| 33 | +const serverTs = serverTsResult.TS; |
| 34 | + |
| 35 | +const dataModelDts = noSchemaDataModelDTS(); |
| 36 | + |
| 37 | +const tmpDir = fs.mkdtempSync( |
| 38 | + path.join(os.tmpdir(), "convex-codegen-compare-"), |
| 39 | +); |
| 40 | + |
| 41 | +try { |
| 42 | + // Create directory structure |
| 43 | + const directDir = path.join(tmpDir, "direct"); |
| 44 | + const compiledDir = path.join(tmpDir, "compiled"); |
| 45 | + const tsSourceDir = path.join(tmpDir, "ts_source"); |
| 46 | + |
| 47 | + fs.mkdirSync(directDir, { recursive: true }); |
| 48 | + fs.mkdirSync(compiledDir, { recursive: true }); |
| 49 | + fs.mkdirSync(tsSourceDir); |
| 50 | + |
| 51 | + // Write directly-generated JS/DTS files (formatted with prettier) |
| 52 | + const formattedApiJs = await prettier.format(apiJs, { |
| 53 | + parser: "typescript", |
| 54 | + pluginSearchDirs: false, |
| 55 | + }); |
| 56 | + const formattedApiDts = await prettier.format(apiDts, { |
| 57 | + parser: "typescript", |
| 58 | + pluginSearchDirs: false, |
| 59 | + }); |
| 60 | + const formattedServerJs = await prettier.format(serverJs, { |
| 61 | + parser: "typescript", |
| 62 | + pluginSearchDirs: false, |
| 63 | + }); |
| 64 | + const formattedServerDts = await prettier.format(serverDts, { |
| 65 | + parser: "typescript", |
| 66 | + pluginSearchDirs: false, |
| 67 | + }); |
| 68 | + const formattedDataModelDts = await prettier.format(dataModelDts, { |
| 69 | + parser: "typescript", |
| 70 | + pluginSearchDirs: false, |
| 71 | + }); |
| 72 | + |
| 73 | + fs.writeFileSync(path.join(directDir, "api.js"), formattedApiJs); |
| 74 | + fs.writeFileSync(path.join(directDir, "api.d.ts"), formattedApiDts); |
| 75 | + fs.writeFileSync(path.join(directDir, "server.js"), formattedServerJs); |
| 76 | + fs.writeFileSync(path.join(directDir, "server.d.ts"), formattedServerDts); |
| 77 | + fs.writeFileSync( |
| 78 | + path.join(directDir, "dataModel.d.ts"), |
| 79 | + formattedDataModelDts, |
| 80 | + ); |
| 81 | + |
| 82 | + // Format TS files with prettier BEFORE compilation (matching runtime behavior) |
| 83 | + const formattedApiTs = await prettier.format(apiTs, { |
| 84 | + parser: "typescript", |
| 85 | + pluginSearchDirs: false, |
| 86 | + }); |
| 87 | + const formattedServerTs = await prettier.format(serverTs, { |
| 88 | + parser: "typescript", |
| 89 | + pluginSearchDirs: false, |
| 90 | + }); |
| 91 | + const formattedDataModelTs = await prettier.format(dataModelDts, { |
| 92 | + parser: "typescript", |
| 93 | + pluginSearchDirs: false, |
| 94 | + }); |
| 95 | + |
| 96 | + // Write the formatted TS files we'll compile |
| 97 | + fs.writeFileSync(path.join(tsSourceDir, "api.ts"), formattedApiTs); |
| 98 | + fs.writeFileSync(path.join(tsSourceDir, "server.ts"), formattedServerTs); |
| 99 | + fs.writeFileSync( |
| 100 | + path.join(tsSourceDir, "dataModel.ts"), |
| 101 | + formattedDataModelTs, |
| 102 | + ); |
| 103 | + |
| 104 | + // Create tsconfig.json for compilation |
| 105 | + const tsconfig = { |
| 106 | + compilerOptions: { |
| 107 | + target: "ES2020", |
| 108 | + module: "ES2020", |
| 109 | + declaration: true, |
| 110 | + emitDeclarationOnly: false, |
| 111 | + skipLibCheck: true, |
| 112 | + esModuleInterop: true, |
| 113 | + moduleResolution: "bundler", |
| 114 | + outDir: compiledDir, |
| 115 | + rootDir: tsSourceDir, |
| 116 | + paths: { |
| 117 | + "convex/server": [ |
| 118 | + path.join( |
| 119 | + tmpDir, |
| 120 | + "node_modules/convex/dist/esm-types/server/index.d.ts", |
| 121 | + ), |
| 122 | + ], |
| 123 | + "convex/values": [ |
| 124 | + path.join( |
| 125 | + tmpDir, |
| 126 | + "node_modules/convex/dist/esm-types/values/index.d.ts", |
| 127 | + ), |
| 128 | + ], |
| 129 | + }, |
| 130 | + }, |
| 131 | + include: [path.join(tsSourceDir, "*.ts")], |
| 132 | + }; |
| 133 | + fs.writeFileSync( |
| 134 | + path.join(tmpDir, "tsconfig.json"), |
| 135 | + JSON.stringify(tsconfig, null, 2), |
| 136 | + ); |
| 137 | + |
| 138 | + // Set up the convex package |
| 139 | + const convexPackageDir = path.join(tmpDir, "node_modules", "convex"); |
| 140 | + const convexDistDir = path.join(convexPackageDir, "dist"); |
| 141 | + fs.mkdirSync(convexDistDir, { recursive: true }); |
| 142 | + |
| 143 | + const projectRoot = process.cwd(); |
| 144 | + const distDir = path.join(projectRoot, "dist"); |
| 145 | + |
| 146 | + // Copy package.json |
| 147 | + fs.copyFileSync( |
| 148 | + path.join(projectRoot, "package.json"), |
| 149 | + path.join(convexPackageDir, "package.json"), |
| 150 | + ); |
| 151 | + |
| 152 | + // Symlink dist directories |
| 153 | + fs.symlinkSync(path.join(distDir, "esm"), path.join(convexDistDir, "esm")); |
| 154 | + fs.symlinkSync( |
| 155 | + path.join(distDir, "esm-types"), |
| 156 | + path.join(convexDistDir, "esm-types"), |
| 157 | + ); |
| 158 | + |
| 159 | + // Create stub user modules that are imported by api.ts |
| 160 | + for (const modulePath of modulePaths) { |
| 161 | + const fullPath = path.join(tmpDir, modulePath); |
| 162 | + const dir = path.dirname(fullPath); |
| 163 | + fs.mkdirSync(dir, { recursive: true }); |
| 164 | + fs.writeFileSync(fullPath.replace(/\.ts$/, ".js"), "export const foo = 1;"); |
| 165 | + fs.writeFileSync( |
| 166 | + fullPath.replace(/\.ts$/, ".d.ts"), |
| 167 | + "export declare const foo: number;", |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + // Compile the TS files |
| 172 | + const tscPath = path.join(projectRoot, "node_modules", ".bin", "tsc"); |
| 173 | + try { |
| 174 | + execSync(`${tscPath} --project ${path.join(tmpDir, "tsconfig.json")}`, { |
| 175 | + cwd: tmpDir, |
| 176 | + stdio: "pipe", |
| 177 | + }); |
| 178 | + } catch (error) { |
| 179 | + console.error("TypeScript compilation failed:"); |
| 180 | + console.error(error.stdout?.toString()); |
| 181 | + console.error(error.stderr?.toString()); |
| 182 | + process.exit(1); |
| 183 | + } |
| 184 | + |
| 185 | + // Format the compiled outputs with prettier |
| 186 | + for (const file of [ |
| 187 | + "api.js", |
| 188 | + "api.d.ts", |
| 189 | + "server.js", |
| 190 | + "server.d.ts", |
| 191 | + "dataModel.d.ts", |
| 192 | + ]) { |
| 193 | + const filePath = path.join(compiledDir, file); |
| 194 | + const content = fs.readFileSync(filePath, "utf-8"); |
| 195 | + const formatted = await prettier.format(content, { |
| 196 | + parser: "typescript", |
| 197 | + pluginSearchDirs: false, |
| 198 | + }); |
| 199 | + fs.writeFileSync(filePath, formatted); |
| 200 | + } |
| 201 | + |
| 202 | + console.log("✓ Generated codegen outputs\n"); |
| 203 | + console.log("Directories created:"); |
| 204 | + console.log(` Direct (JS/DTS): ${directDir}`); |
| 205 | + console.log(` Compiled (TS): ${compiledDir}`); |
| 206 | + console.log(` TS Source: ${tsSourceDir}`); |
| 207 | + console.log(); |
| 208 | + console.log("To compare with diff:"); |
| 209 | + console.log(` diff -r ${directDir} ${compiledDir}`); |
| 210 | + console.log(); |
| 211 | + console.log("To open diffs in VS Code:"); |
| 212 | + const filesToCompare = [ |
| 213 | + "api.d.ts", |
| 214 | + "api.js", |
| 215 | + "server.d.ts", |
| 216 | + "server.js", |
| 217 | + "dataModel.d.ts", |
| 218 | + ]; |
| 219 | + for (const file of filesToCompare) { |
| 220 | + console.log(` code --diff ${directDir}/${file} ${compiledDir}/${file}`); |
| 221 | + } |
| 222 | + console.log(); |
| 223 | +} catch (error) { |
| 224 | + console.error("Error:", error); |
| 225 | + // Clean up on error |
| 226 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 227 | + process.exit(1); |
| 228 | +} |
0 commit comments