/** * باندل تک‌فایل برای Docker (release/app.js) بعد از nest build. * Prisma client + engines را در release/vendor می‌گذارد (نه node_modules تا gitignore نخورد). */ const esbuild = require('esbuild'); const { existsSync, mkdirSync, rmSync, cpSync, realpathSync, readdirSync, readFileSync, writeFileSync, } = require('fs'); const path = require('path'); const { createRequire } = require('module'); // Only this engine ships to Docker; see apps/api/Dockerfile (bookworm/OpenSSL 3). const RUNTIME_ENGINE = 'libquery_engine-debian-openssl-3.0.x.so.node'; const root = path.join(__dirname, '..'); const config = require(path.join(root, 'esbuild.release.config.cjs')); const releaseDir = path.join(root, 'release'); const vendorDir = path.join(releaseDir, 'vendor'); function resolvePkg(name) { return path.dirname(require.resolve(`${name}/package.json`, { paths: [root] })); } function copyPrismaIntoRelease() { const prismaClientSrc = resolvePkg('@prisma/client'); const prismaClientDest = path.join(vendorDir, '@prisma', 'client'); mkdirSync(path.dirname(prismaClientDest), { recursive: true }); cpSync(prismaClientSrc, prismaClientDest, { recursive: true }); const candidates = [ path.join(root, 'node_modules', '.prisma', 'client'), path.join(prismaClientSrc, '..', '.prisma', 'client'), path.join(path.dirname(prismaClientSrc), '.prisma', 'client'), path.join(prismaClientSrc, '..', '..', '.prisma', 'client'), ]; const prismaEngineSrc = candidates.find((p) => existsSync(p)); if (!prismaEngineSrc) { throw new Error( 'Prisma generated client (.prisma/client) یافت نشد. ابتدا: pnpm --filter api exec prisma generate', ); } const prismaEngineDest = path.join(vendorDir, '.prisma', 'client'); mkdirSync(path.dirname(prismaEngineDest), { recursive: true }); cpSync(realpathSync(prismaEngineSrc), prismaEngineDest, { recursive: true }); // Drop engines for other platforms (local Windows dll, stale musl builds) so the // committed artifact stays small. for (const name of readdirSync(prismaEngineDest)) { const isEngine = /^(?:lib)?query_engine-/.test(name) || name.endsWith('.so.node') || name.endsWith('.dll.node'); if (isEngine && name !== RUNTIME_ENGINE) { rmSync(path.join(prismaEngineDest, name), { force: true }); } } if (!existsSync(path.join(prismaEngineDest, RUNTIME_ENGINE))) { throw new Error( `${RUNTIME_ENGINE} تولید نشد. binaryTargets در prisma/schema.prisma را بررسی کنید.`, ); } } // Copy the locked runtime closure, not Windows binaries or pnpm's symlinks. // Both native packages must remain siblings: sharp's ELF RUNPATH finds libvips // relative to @img/sharp-linux-x64. Runtime installation is deliberately offline. function copySharpIntoRelease() { const versions = {}; function copyPackage(name, from) { let source; try { // sharp 0.35 hides package.json behind exports; packaging must locate // the installed manifest without loading a native binary on Windows. const search = createRequire(path.join(from, 'package.json')).resolve.paths(name) ?? []; const manifest = search.map((dir) => path.join(dir, name, 'package.json')).find(existsSync); if (!manifest) throw new Error('Package not installed'); source = realpathSync(path.dirname(manifest)); } catch { throw new Error(`Missing image runtime package ${name}. Run pnpm install with the workspace supportedArchitectures settings before building release.`); } const pkg = JSON.parse(readFileSync(path.join(source, 'package.json'), 'utf8')); if (versions[name]) { if (versions[name] !== pkg.version) throw new Error(`Conflicting image runtime versions for ${name}`); return; } versions[name] = pkg.version; const dest = path.join(vendorDir, name); mkdirSync(path.dirname(dest), { recursive: true }); cpSync(source, dest, { recursive: true, dereference: true }); for (const dependency of Object.keys(pkg.dependencies ?? {})) copyPackage(dependency, source); if (name === 'sharp') { copyPackage('@img/sharp-linux-x64', source); copyPackage('@img/sharp-libvips-linux-x64', source); } } copyPackage('sharp', root); for (const [name, extension] of [ ['@img/sharp-linux-x64', '.node'], ['@img/sharp-libvips-linux-x64', '.so'], ]) { const lib = path.join(vendorDir, name, 'lib'); if (!existsSync(lib) || !readdirSync(lib).some((file) => file.includes(extension))) { throw new Error(`Incomplete Linux image runtime: ${name}`); } } writeFileSync(path.join(releaseDir, 'image-runtime.json'), JSON.stringify({ platform: 'linux', arch: 'x64', versions }, null, 2) + '\n'); cpSync(path.join(__dirname, 'verify-release-images.cjs'), path.join(releaseDir, 'verify-release-images.cjs')); } async function main() { const entry = config.entryPoints[0]; const fallbackEntry = path.join(root, 'dist', 'src', 'main.js'); if (!existsSync(entry)) { if (existsSync(fallbackEntry)) { config.entryPoints = [fallbackEntry]; } else { console.error('dist/main.js یافت نشد. ابتدا اجرا کنید: pnpm --filter api build'); process.exit(1); } } rmSync(releaseDir, { recursive: true, force: true }); mkdirSync(releaseDir, { recursive: true }); await esbuild.build(config); copyPrismaIntoRelease(); copySharpIntoRelease(); // SQL migrations are read at runtime by src/prisma/migrate.ts cpSync(path.join(root, 'prisma', 'migrations'), path.join(releaseDir, 'migrations'), { recursive: true, }); // Seeding runs as a separate process on a fresh database (src/prisma/bootstrap.ts) await esbuild.build({ ...config, entryPoints: [path.join(root, 'prisma', 'seed.ts')], outfile: path.join(releaseDir, 'seed.js'), }); console.log('نوشت:', config.outfile); console.log('Prisma client در release/vendor کپی شد.'); console.log('sharp و وابستگی‌های native لینوکس در release/vendor کپی شدند.'); console.log('migrations و seed.js در release کپی شدند.'); } main().catch((err) => { console.error(err); process.exit(1); });