|
| 1 | +import { Template, TemplateClass } from 'e2b' |
| 2 | +import * as fs from 'fs' |
| 3 | +import Handlebars from 'handlebars' |
| 4 | +import * as path from 'path' |
| 5 | +import { TemplateJSON } from './types' |
| 6 | + |
| 7 | +// Track if helpers are registered to avoid duplicate registration |
| 8 | +let helpersRegistered = false |
| 9 | + |
| 10 | +export function registerHandlebarsHelpers() { |
| 11 | + if (helpersRegistered) return |
| 12 | + |
| 13 | + Handlebars.registerHelper('eq', function (a: any, b: any, options: any) { |
| 14 | + if (a === b) { |
| 15 | + // @ts-ignore - this context is provided by Handlebars |
| 16 | + return options.fn(this) |
| 17 | + } |
| 18 | + return '' |
| 19 | + }) |
| 20 | + |
| 21 | + Handlebars.registerHelper('escapeQuotes', function (str) { |
| 22 | + return str ? str.replace(/'/g, "\\'") : str |
| 23 | + }) |
| 24 | + |
| 25 | + Handlebars.registerHelper('escapeDoubleQuotes', function (str) { |
| 26 | + return str ? str.replace(/"/g, '\\"') : str |
| 27 | + }) |
| 28 | + |
| 29 | + helpersRegistered = true |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Transform template data for Handlebars |
| 34 | + */ |
| 35 | +export async function transformTemplateData(template: TemplateClass) { |
| 36 | + // Extract JSON structure from parsed template |
| 37 | + const jsonString = await Template.toJSON(template, false) |
| 38 | + const json = JSON.parse(jsonString) as TemplateJSON |
| 39 | + |
| 40 | + const transformedSteps: any[] = [] |
| 41 | + |
| 42 | + for (const step of json.steps) { |
| 43 | + switch (step.type) { |
| 44 | + case 'ENV': { |
| 45 | + // Keep all environment variables from one ENV instruction together |
| 46 | + const envVars: Record<string, string> = {} |
| 47 | + for (let i = 0; i < step.args.length; i += 2) { |
| 48 | + if (i + 1 < step.args.length) { |
| 49 | + envVars[step.args[i]] = step.args[i + 1] |
| 50 | + } |
| 51 | + } |
| 52 | + transformedSteps.push({ |
| 53 | + type: 'ENV', |
| 54 | + envVars, |
| 55 | + }) |
| 56 | + break |
| 57 | + } |
| 58 | + case 'COPY': { |
| 59 | + if (step.args.length >= 2) { |
| 60 | + const src = step.args[0] |
| 61 | + let dest = step.args[step.args.length - 1] |
| 62 | + if (!dest || dest === '') { |
| 63 | + dest = '.' |
| 64 | + } |
| 65 | + transformedSteps.push({ |
| 66 | + type: 'COPY', |
| 67 | + src, |
| 68 | + dest, |
| 69 | + }) |
| 70 | + } |
| 71 | + break |
| 72 | + } |
| 73 | + default: |
| 74 | + transformedSteps.push({ |
| 75 | + type: step.type, |
| 76 | + args: step.args, |
| 77 | + }) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return { |
| 82 | + ...json, |
| 83 | + steps: transformedSteps, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Convert the template to TypeScript code using Handlebars |
| 89 | + */ |
| 90 | +export async function generateTypeScriptCode( |
| 91 | + template: TemplateClass, |
| 92 | + alias: string, |
| 93 | + cpuCount?: number, |
| 94 | + memoryMB?: number |
| 95 | +): Promise<{ templateContent: string; buildContent: string }> { |
| 96 | + registerHandlebarsHelpers() |
| 97 | + const transformedData = await transformTemplateData(template) |
| 98 | + |
| 99 | + // Load and compile templates |
| 100 | + // In dist, templates are at dist/templates/, __dirname is dist/ |
| 101 | + const templatesDir = path.join(__dirname, 'templates') |
| 102 | + const templateSource = fs.readFileSync( |
| 103 | + path.join(templatesDir, 'typescript-template.hbs'), |
| 104 | + 'utf8' |
| 105 | + ) |
| 106 | + const buildSource = fs.readFileSync( |
| 107 | + path.join(templatesDir, 'typescript-build.hbs'), |
| 108 | + 'utf8' |
| 109 | + ) |
| 110 | + |
| 111 | + const templateTemplate = Handlebars.compile(templateSource) |
| 112 | + const buildTemplate = Handlebars.compile(buildSource) |
| 113 | + |
| 114 | + // Generate content |
| 115 | + const templateData = { |
| 116 | + ...transformedData, |
| 117 | + } |
| 118 | + |
| 119 | + const templateContent = templateTemplate(templateData) |
| 120 | + |
| 121 | + const buildContent = buildTemplate({ |
| 122 | + alias, |
| 123 | + cpuCount, |
| 124 | + memoryMB, |
| 125 | + }) |
| 126 | + |
| 127 | + return { |
| 128 | + templateContent: templateContent.trim(), |
| 129 | + buildContent: buildContent.trim(), |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Convert the template to Python code using Handlebars |
| 135 | + */ |
| 136 | +export async function generatePythonCode( |
| 137 | + template: TemplateClass, |
| 138 | + alias: string, |
| 139 | + cpuCount?: number, |
| 140 | + memoryMB?: number, |
| 141 | + isAsync: boolean = false |
| 142 | +): Promise<{ templateContent: string; buildContent: string }> { |
| 143 | + registerHandlebarsHelpers() |
| 144 | + const transformedData = await transformTemplateData(template) |
| 145 | + |
| 146 | + // Load and compile templates |
| 147 | + // In dist, templates are at dist/templates/, __dirname is dist/ |
| 148 | + const templatesDir = path.join(__dirname, 'templates') |
| 149 | + const templateSource = fs.readFileSync( |
| 150 | + path.join(templatesDir, 'python-template.hbs'), |
| 151 | + 'utf8' |
| 152 | + ) |
| 153 | + const buildSource = fs.readFileSync( |
| 154 | + path.join(templatesDir, `python-build-${isAsync ? 'async' : 'sync'}.hbs`), |
| 155 | + 'utf8' |
| 156 | + ) |
| 157 | + |
| 158 | + const templateTemplate = Handlebars.compile(templateSource) |
| 159 | + const buildTemplate = Handlebars.compile(buildSource) |
| 160 | + |
| 161 | + // Generate content |
| 162 | + const templateContent = templateTemplate({ |
| 163 | + ...transformedData, |
| 164 | + isAsync, |
| 165 | + }) |
| 166 | + |
| 167 | + const buildContent = buildTemplate({ |
| 168 | + alias, |
| 169 | + cpuCount, |
| 170 | + memoryMB, |
| 171 | + }) |
| 172 | + |
| 173 | + return { |
| 174 | + templateContent: templateContent.trim(), |
| 175 | + buildContent: buildContent.trim(), |
| 176 | + } |
| 177 | +} |
0 commit comments