|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Bundle T-Ruby source files into a TypeScript module |
| 5 | + * |
| 6 | + * This script reads all Ruby files from vendor/t-ruby and generates |
| 7 | + * a TypeScript file that embeds them for use in the WASM environment. |
| 8 | + */ |
| 9 | + |
| 10 | +import { readFileSync, writeFileSync, existsSync, readdirSync, statSync } from "node:fs"; |
| 11 | +import { dirname, join, relative } from "node:path"; |
| 12 | +import { fileURLToPath } from "node:url"; |
| 13 | + |
| 14 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 15 | +const rootDir = join(__dirname, ".."); |
| 16 | +const vendorDir = join(rootDir, "vendor", "t-ruby"); |
| 17 | +const outputFile = join(rootDir, "src", "vm", "TRubyBundle.ts"); |
| 18 | + |
| 19 | +/** |
| 20 | + * Recursively collect all .rb files from a directory |
| 21 | + */ |
| 22 | +function collectRubyFiles(dir, basePath = "") { |
| 23 | + const files = []; |
| 24 | + |
| 25 | + if (!existsSync(dir)) { |
| 26 | + return files; |
| 27 | + } |
| 28 | + |
| 29 | + for (const entry of readdirSync(dir)) { |
| 30 | + const fullPath = join(dir, entry); |
| 31 | + const relativePath = basePath ? `${basePath}/${entry}` : entry; |
| 32 | + |
| 33 | + if (statSync(fullPath).isDirectory()) { |
| 34 | + files.push(...collectRubyFiles(fullPath, relativePath)); |
| 35 | + } else if (entry.endsWith(".rb")) { |
| 36 | + files.push({ |
| 37 | + path: relativePath, |
| 38 | + content: readFileSync(fullPath, "utf-8"), |
| 39 | + }); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return files; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Escape string for use in TypeScript template literal |
| 48 | + */ |
| 49 | +function escapeForTemplate(str) { |
| 50 | + return str |
| 51 | + .replace(/\\/g, "\\\\") |
| 52 | + .replace(/`/g, "\\`") |
| 53 | + .replace(/\$\{/g, "\\${"); |
| 54 | +} |
| 55 | + |
| 56 | +console.log("Bundling T-Ruby source files..."); |
| 57 | + |
| 58 | +const files = collectRubyFiles(vendorDir); |
| 59 | + |
| 60 | +if (files.length === 0) { |
| 61 | + console.log("No Ruby files found in vendor/t-ruby. Using fallback bundle."); |
| 62 | + |
| 63 | + // Generate fallback bundle (minimal implementation) |
| 64 | + const fallbackContent = `/** |
| 65 | + * T-Ruby Bundle - Auto-generated |
| 66 | + * |
| 67 | + * This file contains bundled T-Ruby source files for WASM environment. |
| 68 | + * Generated at build time from vendor/t-ruby directory. |
| 69 | + * |
| 70 | + * @internal |
| 71 | + */ |
| 72 | +
|
| 73 | +/** Bundled T-Ruby source files */ |
| 74 | +export const T_RUBY_BUNDLE: Record<string, string> = {}; |
| 75 | +
|
| 76 | +/** Flag indicating if real T-Ruby is bundled */ |
| 77 | +export const HAS_T_RUBY_BUNDLE = false; |
| 78 | +`; |
| 79 | + |
| 80 | + writeFileSync(outputFile, fallbackContent); |
| 81 | + console.log("Generated fallback bundle."); |
| 82 | +} else { |
| 83 | + console.log(`Found ${files.length} Ruby files to bundle.`); |
| 84 | + |
| 85 | + // Generate TypeScript bundle |
| 86 | + let content = `/** |
| 87 | + * T-Ruby Bundle - Auto-generated |
| 88 | + * |
| 89 | + * This file contains bundled T-Ruby source files for WASM environment. |
| 90 | + * Generated at build time from vendor/t-ruby directory. |
| 91 | + * |
| 92 | + * DO NOT EDIT MANUALLY - This file is auto-generated by scripts/bundle-t-ruby.mjs |
| 93 | + * |
| 94 | + * @internal |
| 95 | + */ |
| 96 | +
|
| 97 | +/** Bundled T-Ruby source files */ |
| 98 | +export const T_RUBY_BUNDLE: Record<string, string> = { |
| 99 | +`; |
| 100 | + |
| 101 | + for (const file of files) { |
| 102 | + content += ` "${file.path}": \`${escapeForTemplate(file.content)}\`,\n`; |
| 103 | + } |
| 104 | + |
| 105 | + content += `}; |
| 106 | +
|
| 107 | +/** Flag indicating if real T-Ruby is bundled */ |
| 108 | +export const HAS_T_RUBY_BUNDLE = true; |
| 109 | +`; |
| 110 | + |
| 111 | + writeFileSync(outputFile, content); |
| 112 | + console.log(`Generated bundle with ${files.length} files: ${outputFile}`); |
| 113 | +} |
0 commit comments