completionId: cgen-8fa6bb3905804fa3bce31636cb009737

cgen-8fa6bb3905804fa3bce31636cb009737
This commit is contained in:
Builder.io 2025-11-16 01:49:06 +00:00
parent 65c110e03b
commit 98478f3129

View file

@ -2,11 +2,10 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { execSync } from "child_process";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
function copyDir(src, dest) {
function processDir(src, dest, processFile) {
if (!fs.existsSync(src)) {
console.warn(`Source directory not found: ${src}`);
return;
@ -25,13 +24,49 @@ function copyDir(src, dest) {
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDir(srcPath, destPath);
processDir(srcPath, destPath, processFile);
} else {
fs.copyFileSync(srcPath, destPath);
processFile(srcPath, destPath);
}
}
}
console.log(`✓ Copied ${src} to ${dest}`);
function copyFile(src, dest) {
fs.copyFileSync(src, dest);
}
function processAndCopyFile(src, dest) {
let content = fs.readFileSync(src, "utf-8");
// Fix relative imports to include .ts extension for Vercel
// This helps Node.js module resolution work correctly at runtime
if (src.endsWith(".ts")) {
// Add .ts extension to relative imports if not already present
content = content.replace(
/from\s+['"](\.[^'"]*?)(?<!\.ts)(['"])/g,
'from "$1.ts"$2'
);
// Also handle import statements
content = content.replace(
/import\s+([^'"]*?)\s+from\s+['"](\.[^'"]*?)(?<!\.ts)(['"])/g,
'import $1 from "$2.ts"$3'
);
}
// Copy the original TypeScript file
fs.copyFileSync(src, dest);
// Also create a .js version by replacing imports
if (src.endsWith(".ts") && !src.endsWith(".d.ts")) {
const jsPath = dest.replace(/\.ts$/, ".js");
// For .js files, remove .ts extensions from imports
const jsContent = content.replace(
/from\s+['"](\.[^'"]*?)\.ts(['"])/g,
'from "$1.js"$2'
);
fs.writeFileSync(jsPath, jsContent);
}
}
// When this file is at code/copy-api.js:
@ -41,47 +76,6 @@ function copyDir(src, dest) {
const srcApi = path.resolve(__dirname, "api");
const destApi = path.resolve(__dirname, "..", "api");
copyDir(srcApi, destApi);
// Compile TypeScript files to JavaScript for Vercel
try {
console.log("Transpiling TypeScript API files...");
const tscPath = path.resolve(__dirname, "node_modules/.bin/tsc");
// Use tsx to transpile the API directory
execSync(`npx tsx --eval "
const ts = require('typescript');
const path = require('path');
const fs = require('fs');
function transpileDir(dir) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
transpileDir(fullPath);
} else if (entry.name.endsWith('.ts') && !entry.name.endsWith('.d.ts')) {
const source = fs.readFileSync(fullPath, 'utf-8');
const result = ts.transpileModule(source, {
compilerOptions: {
module: ts.ModuleKind.ES2020,
target: ts.ScriptTarget.ES2020
}
});
const jsPath = fullPath.replace(/\.ts$/, '.js');
fs.writeFileSync(jsPath, result.outputText);
console.log('✓ Transpiled', fullPath, 'to', jsPath);
}
}
}
transpileDir('${destApi}');
"`, { stdio: 'inherit' });
console.log("✓ TypeScript transpilation complete");
} catch (error) {
console.warn("Note: TypeScript transpilation may not be necessary if using proper runtime");
// Don't fail the build if transpilation fails
}
console.log(`Processing API files from ${srcApi} to ${destApi}...`);
processDir(srcApi, destApi, processAndCopyFile);
console.log(`✓ API files processed and copied to ${destApi}`);