|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +/** |
| 7 | + * This script copies registry template files into the Next.js build |
| 8 | + * so they're available as static assets in serverless environments |
| 9 | + */ |
| 10 | + |
| 11 | +async function bundleRegistryTemplates() { |
| 12 | + console.log('📦 Bundling registry templates...'); |
| 13 | + |
| 14 | + try { |
| 15 | + // Source: registry package templates |
| 16 | + const registryPath = path.resolve(__dirname, '../../../packages/registry/templates'); |
| 17 | + |
| 18 | + // Destination: Next.js public directory (served as static assets) |
| 19 | + const publicRegistryPath = path.resolve(__dirname, '../public/registry-templates'); |
| 20 | + |
| 21 | + // Clean and create destination directory |
| 22 | + if (fs.existsSync(publicRegistryPath)) { |
| 23 | + fs.rmSync(publicRegistryPath, { recursive: true }); |
| 24 | + } |
| 25 | + fs.mkdirSync(publicRegistryPath, { recursive: true }); |
| 26 | + |
| 27 | + // Copy all template files |
| 28 | + copyDirectory(registryPath, publicRegistryPath); |
| 29 | + |
| 30 | + console.log('✅ Registry templates bundled successfully!'); |
| 31 | + console.log(`📁 Templates available at: ${publicRegistryPath}`); |
| 32 | + |
| 33 | + // Also create a manifest of all available templates |
| 34 | + const manifest = createTemplateManifest(publicRegistryPath); |
| 35 | + fs.writeFileSync( |
| 36 | + path.join(publicRegistryPath, 'manifest.json'), |
| 37 | + JSON.stringify(manifest, null, 2) |
| 38 | + ); |
| 39 | + |
| 40 | + console.log(`📋 Template manifest created with ${manifest.templates.length} templates`); |
| 41 | + |
| 42 | + } catch (error) { |
| 43 | + console.error('❌ Failed to bundle registry templates:', error); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function copyDirectory(src, dest) { |
| 49 | + if (!fs.existsSync(src)) { |
| 50 | + throw new Error(`Source directory does not exist: ${src}`); |
| 51 | + } |
| 52 | + |
| 53 | + const entries = fs.readdirSync(src, { withFileTypes: true }); |
| 54 | + |
| 55 | + for (const entry of entries) { |
| 56 | + const srcPath = path.join(src, entry.name); |
| 57 | + const destPath = path.join(dest, entry.name); |
| 58 | + |
| 59 | + if (entry.isDirectory()) { |
| 60 | + fs.mkdirSync(destPath, { recursive: true }); |
| 61 | + copyDirectory(srcPath, destPath); |
| 62 | + } else { |
| 63 | + fs.copyFileSync(srcPath, destPath); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function createTemplateManifest(templatesPath) { |
| 69 | + const templates = []; |
| 70 | + |
| 71 | + function scanDirectory(dir, relativePath = '') { |
| 72 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 73 | + |
| 74 | + for (const entry of entries) { |
| 75 | + const fullPath = path.join(dir, entry.name); |
| 76 | + const relPath = path.join(relativePath, entry.name); |
| 77 | + |
| 78 | + if (entry.isDirectory()) { |
| 79 | + scanDirectory(fullPath, relPath); |
| 80 | + } else if (entry.name === '_meta.ts') { |
| 81 | + // Found a template directory |
| 82 | + const templatePath = relativePath || '.'; |
| 83 | + templates.push({ |
| 84 | + name: templatePath.replace(/\\/g, '/'), // normalize path separators |
| 85 | + path: templatePath, |
| 86 | + metaFile: relPath.replace(/\\/g, '/'), |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + scanDirectory(templatesPath); |
| 93 | + |
| 94 | + return { |
| 95 | + generatedAt: new Date().toISOString(), |
| 96 | + templates, |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +bundleRegistryTemplates(); |
0 commit comments