|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as http from 'http'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +import { |
| 6 | + diffFiles, |
| 7 | + renderTerminal, |
| 8 | + renderHtmlDocument, |
| 9 | + themes |
| 10 | +} from '../src'; |
| 11 | + |
| 12 | +const FIXTURES_DIR = path.join(__dirname, 'fixtures'); |
| 13 | +const OUT_DIR = path.join(__dirname, '..', 'out'); |
| 14 | + |
| 15 | +interface Fixture { |
| 16 | + name: string; |
| 17 | + oldFile: string; |
| 18 | + newFile: string; |
| 19 | + language: string; |
| 20 | +} |
| 21 | + |
| 22 | +const fixtures: Fixture[] = [ |
| 23 | + { |
| 24 | + name: 'TypeScript', |
| 25 | + oldFile: 'typescript-old.ts', |
| 26 | + newFile: 'typescript-new.ts', |
| 27 | + language: 'typescript' |
| 28 | + }, |
| 29 | + { |
| 30 | + name: 'SQL', |
| 31 | + oldFile: 'sql-old.sql', |
| 32 | + newFile: 'sql-new.sql', |
| 33 | + language: 'sql' |
| 34 | + }, |
| 35 | + { |
| 36 | + name: 'Python', |
| 37 | + oldFile: 'python-old.py', |
| 38 | + newFile: 'python-new.py', |
| 39 | + language: 'python' |
| 40 | + } |
| 41 | +]; |
| 42 | + |
| 43 | +function loadFixture(fixture: Fixture) { |
| 44 | + const oldPath = path.join(FIXTURES_DIR, fixture.oldFile); |
| 45 | + const newPath = path.join(FIXTURES_DIR, fixture.newFile); |
| 46 | + const oldContent = fs.readFileSync(oldPath, 'utf-8'); |
| 47 | + const newContent = fs.readFileSync(newPath, 'utf-8'); |
| 48 | + return { oldContent, newContent }; |
| 49 | +} |
| 50 | + |
| 51 | +function printTerminalPreview() { |
| 52 | + console.log('\n' + '='.repeat(80)); |
| 53 | + console.log(' VISUAL-DIFF TERMINAL PREVIEW'); |
| 54 | + console.log('='.repeat(80) + '\n'); |
| 55 | + |
| 56 | + const themeNames = Object.keys(themes); |
| 57 | + |
| 58 | + for (const fixture of fixtures) { |
| 59 | + const { oldContent, newContent } = loadFixture(fixture); |
| 60 | + const result = diffFiles(oldContent, newContent, fixture.oldFile, fixture.newFile); |
| 61 | + |
| 62 | + console.log('\n' + '-'.repeat(80)); |
| 63 | + console.log(` ${fixture.name} Diff`); |
| 64 | + console.log('-'.repeat(80)); |
| 65 | + |
| 66 | + for (const themeName of themeNames) { |
| 67 | + console.log(`\n>>> Theme: ${themeName}\n`); |
| 68 | + const output = renderTerminal(result, { |
| 69 | + theme: themeName, |
| 70 | + showLineNumbers: true, |
| 71 | + syntaxHighlight: true |
| 72 | + }); |
| 73 | + console.log(output); |
| 74 | + console.log(''); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +function generateHtmlPreview(): string { |
| 80 | + if (!fs.existsSync(OUT_DIR)) { |
| 81 | + fs.mkdirSync(OUT_DIR, { recursive: true }); |
| 82 | + } |
| 83 | + |
| 84 | + const htmlParts: string[] = []; |
| 85 | + |
| 86 | + htmlParts.push(`<!DOCTYPE html> |
| 87 | +<html lang="en"> |
| 88 | +<head> |
| 89 | + <meta charset="UTF-8"> |
| 90 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 91 | + <title>Visual Diff Preview</title> |
| 92 | + <style> |
| 93 | + * { box-sizing: border-box; } |
| 94 | + body { |
| 95 | + margin: 0; |
| 96 | + padding: 20px; |
| 97 | + background: #0d1117; |
| 98 | + color: #c9d1d9; |
| 99 | + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif; |
| 100 | + } |
| 101 | + .container { max-width: 1400px; margin: 0 auto; } |
| 102 | + h1 { text-align: center; margin-bottom: 40px; } |
| 103 | + h2 { margin-top: 40px; border-bottom: 1px solid #30363d; padding-bottom: 10px; } |
| 104 | + h3 { color: #8b949e; margin-top: 30px; } |
| 105 | + .theme-section { margin-bottom: 30px; } |
| 106 | + .diff-container { margin: 20px 0; } |
| 107 | + </style> |
| 108 | +</head> |
| 109 | +<body> |
| 110 | + <div class="container"> |
| 111 | + <h1>@interweb/visual-diff Preview</h1> |
| 112 | +`); |
| 113 | + |
| 114 | + const themeNames = Object.keys(themes); |
| 115 | + |
| 116 | + for (const fixture of fixtures) { |
| 117 | + const { oldContent, newContent } = loadFixture(fixture); |
| 118 | + const result = diffFiles(oldContent, newContent, fixture.oldFile, fixture.newFile); |
| 119 | + |
| 120 | + htmlParts.push(` <h2>${fixture.name} Diff</h2>\n`); |
| 121 | + |
| 122 | + for (const themeName of themeNames) { |
| 123 | + htmlParts.push(` <div class="theme-section">\n`); |
| 124 | + htmlParts.push(` <h3>Theme: ${themeName}</h3>\n`); |
| 125 | + htmlParts.push(` <div class="diff-container">\n`); |
| 126 | + |
| 127 | + const html = renderHtmlDocument(result, { |
| 128 | + theme: themeName, |
| 129 | + darkMode: true, |
| 130 | + syntaxHighlight: true |
| 131 | + }); |
| 132 | + |
| 133 | + const bodyMatch = html.match(/<body[^>]*>([\s\S]*)<\/body>/i); |
| 134 | + if (bodyMatch) { |
| 135 | + htmlParts.push(bodyMatch[1]); |
| 136 | + } |
| 137 | + |
| 138 | + htmlParts.push(` </div>\n`); |
| 139 | + htmlParts.push(` </div>\n`); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + htmlParts.push(` </div> |
| 144 | +</body> |
| 145 | +</html>`); |
| 146 | + |
| 147 | + const fullHtml = htmlParts.join(''); |
| 148 | + const outputPath = path.join(OUT_DIR, 'preview.html'); |
| 149 | + fs.writeFileSync(outputPath, fullHtml); |
| 150 | + |
| 151 | + return outputPath; |
| 152 | +} |
| 153 | + |
| 154 | +function startServer(htmlPath: string, port: number) { |
| 155 | + const html = fs.readFileSync(htmlPath, 'utf-8'); |
| 156 | + |
| 157 | + const server = http.createServer((req, res) => { |
| 158 | + res.writeHead(200, { 'Content-Type': 'text/html' }); |
| 159 | + res.end(html); |
| 160 | + }); |
| 161 | + |
| 162 | + server.listen(port, () => { |
| 163 | + console.log(`\nServer running at http://localhost:${port}`); |
| 164 | + console.log('Press Ctrl+C to stop\n'); |
| 165 | + }); |
| 166 | +} |
| 167 | + |
| 168 | +function printUsage() { |
| 169 | + console.log(` |
| 170 | +Usage: pnpm dev [options] |
| 171 | +
|
| 172 | +Options: |
| 173 | + --terminal Show terminal preview with all themes |
| 174 | + --html Generate HTML preview file |
| 175 | + --serve Start HTTP server to view HTML preview |
| 176 | + --port=PORT Port for HTTP server (default: 3456) |
| 177 | + --help Show this help message |
| 178 | +
|
| 179 | +Examples: |
| 180 | + pnpm dev --terminal # Show terminal output |
| 181 | + pnpm dev --html # Generate out/preview.html |
| 182 | + pnpm dev --html --serve # Generate and serve HTML |
| 183 | + pnpm dev # Show terminal + generate HTML |
| 184 | +`); |
| 185 | +} |
| 186 | + |
| 187 | +function main() { |
| 188 | + const args = process.argv.slice(2); |
| 189 | + |
| 190 | + if (args.includes('--help')) { |
| 191 | + printUsage(); |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + const showTerminal = args.includes('--terminal') || args.length === 0; |
| 196 | + const generateHtml = args.includes('--html') || args.length === 0; |
| 197 | + const serve = args.includes('--serve'); |
| 198 | + const portArg = args.find(a => a.startsWith('--port=')); |
| 199 | + const port = portArg ? parseInt(portArg.split('=')[1], 10) : 3456; |
| 200 | + |
| 201 | + if (showTerminal) { |
| 202 | + printTerminalPreview(); |
| 203 | + } |
| 204 | + |
| 205 | + if (generateHtml || serve) { |
| 206 | + const htmlPath = generateHtmlPreview(); |
| 207 | + console.log(`\nHTML preview generated: ${htmlPath}`); |
| 208 | + |
| 209 | + if (serve) { |
| 210 | + startServer(htmlPath, port); |
| 211 | + } else { |
| 212 | + console.log('Open this file in your browser to view the preview.\n'); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +main(); |
0 commit comments