ripe-solstice-30115
04/14/2025, 8:50 PMconst fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const lambdaDir = path.join(__dirname, "../bin/lambdas/api");
const tempDir = path.join(__dirname, "../temp_lambda_build");
// Get all `.js` files inside `bin/lambdas/api/`
const functionFiles = fs.readdirSync(lambdaDir).filter((file) => file.endsWith(".js"));
function copyDir(src, dest) {
console.log(`Copying directory ${src} to ${dest}`);
fs.mkdirSync(dest, { recursive: true });
const entries = fs.readdirSync(src, { withFileTypes: true });
for (let entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
// Functions that need special dependencies
const specialFunctions = {
'google-signin': ['jsonwebtoken'],
"create-appointment": ['stripe', '@prisma/client']
};
// Create temp directory if it doesn't exist
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
}
functionFiles.forEach((file) => {
const functionName = file.replace(".js", "");
const filePath = path.join(lambdaDir, file);
const zipPath = path.join(lambdaDir, `${functionName}.zip`);
console.log(`Processing ${functionName}...`);
// Check if this function needs special dependencies
if (specialFunctions[functionName]) {
const functionTempDir = path.join(tempDir, functionName);
try {
// Create temp directory for this function
if (fs.existsSync(functionTempDir)) {
execSync(`rm -rf ${functionTempDir}`);
}
fs.mkdirSync(functionTempDir, { recursive: true });
// Copy the function file
fs.copyFileSync(filePath, path.join(functionTempDir, file));
// Create a package.json with only required dependencies
const packageJson = {
dependencies: {}
};
// Get main package.json to extract version numbers
const mainPackageJson = JSON.parse(fs.readFileSync(path.join(__dirname, "../package.json"), "utf8"));
// Add only the dependencies this function needs
specialFunctions[functionName].forEach(dep => {
if (mainPackageJson.dependencies[dep]) {
packageJson.dependencies[dep] = mainPackageJson.dependencies[dep];
}
});
if (functionName === 'create-appointment') {
// Special handling for Prisma
packageJson.dependencies['@prisma/client'] = mainPackageJson.dependencies['@prisma/client'];
packageJson.dependencies['prisma'] = mainPackageJson.dependencies['prisma'] || mainPackageJson.devDependencies['prisma'];
// Write the package.json first
fs.writeFileSync(
path.join(functionTempDir, "package.json"),
JSON.stringify(packageJson, null, 2)
);
// 1. Install only Prisma first
console.log(`Installing Prisma for ${functionName}...`);
execSync(`cd ${functionTempDir} && pnpm add prisma @prisma/client`, { stdio: 'inherit' });
// 2. Copy Prisma schema and set up config
console.log(`Copying Prisma schema for ${functionName}...`);
copyDir(path.join(__dirname, "../prisma"), path.join(functionTempDir, 'prisma'));
// 3. Generate Prisma client explicitly
console.log(`Generating Prisma client for ${functionName}...`);
execSync(`cd ${functionTempDir} && pnpx prisma generate --schema=./prisma/schema`, { stdio: 'inherit' });
// 4. Verify .prisma client was generated
const prismaClientPath = path.join(functionTempDir, 'node_modules/.prisma');
if (!fs.existsSync(prismaClientPath)) {
throw new Error('Prisma client generation failed - .prisma directory not found');
}
console.log('✔ Prisma client generated successfully');
// 5. Now install remaining dependencies
console.log(`Installing remaining dependencies for ${functionName}...`);
const remainingDeps = specialFunctions[functionName].filter(dep => dep !== '@prisma/client');
if (remainingDeps.length > 0) {
execSync(`cd ${functionTempDir} && pnpm add ${remainingDeps.join(' ')}`, { stdio: 'inherit' });
}
} else {
// For other special functions
fs.writeFileSync(
path.join(functionTempDir, "package.json"),
JSON.stringify(packageJson, null, 2)
);
execSync(`cd ${functionTempDir} && pnpm install`, { stdio: 'inherit' });
}
// Create zip file
console.log(`Zipping ${functionName}...`);
execSync(`cd ${functionTempDir} && zip -r ${zipPath} .`);
console.log(`✔ Created ${zipPath} with dependencies`);
} catch (error) {
console.error(`❌ Failed to process ${file}:`, error);
}
} else {
// For functions without special dependencies, just zip the JS file
try {
console.log(`Zipping ${functionName} (no dependencies)...`);
execSync(`zip -j ${zipPath} ${filePath}`);
console.log(`✔ Created ${zipPath}`);
} catch (error) {
console.error(`❌ Failed to zip ${file}:`, error);
}
}
});
// // Clean up temp directory
// if (fs.existsSync(tempDir)) {
// execSync(`rm -rf ${tempDir}`);
// }
console.log("All functions processed successfully!");
quick-house-41860
04/15/2025, 7:54 AMaws.lambda.Function
at that source code. For uploading you can use aws.s3.BucketObjectv2
(see)ripe-solstice-30115
04/15/2025, 9:47 AMcreate-appointment
) — it significantly increases the size of the zip file.quick-house-41860
04/15/2025, 7:11 PM