UNPKG

codalware-auth

Version:

Complete authentication system with enterprise security, attack protection, team workspaces, waitlist, billing, UI components, 2FA, and account recovery - production-ready in 5 minutes. Enhanced CLI with verification, rollback, and App Router scaffolding.

158 lines (132 loc) 5.49 kB
const fs = require('fs'); const path = require('path'); const filePath = path.join(__dirname, 'copy-full-template.js'); const lines = fs.readFileSync(filePath, 'utf8').split('\n'); // Find the copyMainTemplate function let startLine = -1; for (let i = 0; i < lines.length; i++) { if (lines[i].match(/^async function copyMainTemplate\(options = \{\}\)/)) { startLine = i; break; } } if (startLine === -1) { console.error('Could not find copyMainTemplate function'); process.exit(1); } // Find the end of the function let endLine = startLine; let braceCount = 0; for (let i = startLine; i < lines.length; i++) { const line = lines[i]; braceCount += (line.match(/{/g) || []).length; braceCount -= (line.match(/}/g) || []).length; if (braceCount === 0 && i > startLine) { endLine = i; break; } } console.log(`Found function from line ${startLine + 1} to ${endLine + 1}`); // New function implementation const newFunction = `async function copyMainTemplate(options = {}) { const packageRoot = path.join(__dirname, '..'); const projectRoot = process.cwd(); const srcBase = path.join(packageRoot, 'src'); console.log('🏠 Copying AuthCore as main source code...\\n'); console.log('📍 From:', packageRoot); console.log('📍 To:', projectRoot); console.log(''); // Detect project structure (src or root) const useSrcDirectory = fs.existsSync(path.join(projectRoot, 'src')); const baseTarget = useSrcDirectory ? path.join(projectRoot, 'src') : projectRoot; if (useSrcDirectory) { console.log('📂 Detected "src" directory structure.'); } else { console.log('📂 Detected root directory structure.'); } console.log(''); const allCopied = []; try { // Copy entire src/ tree console.log('📦 Copying entire AuthCore source tree...'); const srcEntries = fs.readdirSync(srcBase, { withFileTypes: true }); for (const entry of srcEntries) { const sourcePath = path.join(srcBase, entry.name); const targetPath = path.join(baseTarget, entry.name); if (entry.isDirectory()) { const copied = copyDirectoryRecursive(sourcePath, targetPath, { rewriteImports: true, excludePatterns: ['.eslintrc.json'] }); allCopied.push(...copied); console.log(\` ✓ Copied \${entry.name}/ (\${copied.length} files)\`); } else if (entry.isFile()) { const content = fs.readFileSync(sourcePath, 'utf8'); const rewriteImports = ['.ts', '.tsx', '.js', '.jsx'].includes(path.extname(entry.name)); let finalContent = content; if (rewriteImports) { // Rewrite relative imports from src/* to @/* finalContent = content.replace( /from\\s+['"](\\.\\.\\/)+(components|hooks|auth|validation|utils|i18n|types|config|lib|adapters|email|package|pages|app)['"]/g, "from '@/$2'" ); } fs.writeFileSync(targetPath, finalContent); allCopied.push(targetPath); console.log(\` ✓ Copied \${entry.name}\`); } } console.log(\`✅ Copied \${allCopied.length} files from src/ tree\\n\`); // Copy supporting directories const rootDirectories = ['config', 'locales', 'styles', 'prisma']; for (const dir of rootDirectories) { const dirSource = path.join(packageRoot, dir); if (fs.existsSync(dirSource)) { const dirTarget = path.join(projectRoot, dir); const copied = copyDirectoryRecursive(dirSource, dirTarget); allCopied.push(...copied); console.log(\`✅ Copied \${dir}/ (\${copied.length} files)\`); } } // Copy root config.ts if exists const rootConfigPath = path.join(packageRoot, 'config.ts'); if (fs.existsSync(rootConfigPath)) { fs.copyFileSync(rootConfigPath, path.join(projectRoot, 'config.ts')); allCopied.push(path.join(projectRoot, 'config.ts')); console.log('✅ Copied config.ts\\n'); } // Create README createMainTemplateReadme(projectRoot); // Create .env.example createEnvExample(projectRoot); console.log('✨ Main template copy complete!\\n'); console.log(\`📦 Copied \${allCopied.length} source files\`); console.log(''); console.log('📖 Read AUTHCORE_TEMPLATE_README.md for usage instructions'); console.log(''); console.log('💡 Quick Start:'); console.log(" import { LoginForm } from '@/components';"); console.log(" import { useAuth } from '@/hooks';"); console.log(''); console.log(\`🏠 Your \${useSrcDirectory ? 'src/' : ''} directory is now AuthCore - customize everything!\`); console.log(''); return allCopied; } catch (error) { console.error('❌ Failed to copy main template:', error.message); if (process.env.DEBUG) { console.error(error); } process.exit(1); } }`; // Replace the old function lines with new function lines const newFunctionLines = newFunction.split('\n'); const newLines = [ ...lines.slice(0, startLine), ...newFunctionLines, ...lines.slice(endLine + 1) ]; // Write back fs.writeFileSync(filePath, newLines.join('\n'), 'utf8'); console.log('✅ Successfully updated copyMainTemplate function'); console.log(`Replaced ${endLine - startLine + 1} lines with ${newFunctionLines.length} lines`);