import fs, { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs' import { access, mkdir, readFile, stat, writeFile } from 'node:fs/promises' import path from 'node:path' import type { FileAccessObject } from '@tevm/base-bundler' import { loadConfig } from '@tevm/config' import { Logger } from 'effect' import { provide, runSync } from 'effect/Effect' import { glob } from 'glob' import { Box, Newline, Text } from 'ink' import Spinner from 'ink-spinner' import { argument, option } from 'pastel' import { useEffect, useState } from 'react' import zod from 'zod' import RawOutput from '../components/RawOutput.js' import { formatJsonFailure, formatJsonSuccess } from '../utils/output.js' // Define FileAccessObject for use with the Tevm bundler const fao: FileAccessObject = { existsSync, readFile, readFileSync, writeFileSync, statSync, stat, mkdirSync, mkdir, writeFile, exists: async (path: string) => { try { await access(path) return true } catch (_e) { return false } }, } // Add command description for help output export const description = 'Generate strongly typed TypeScript from Solidity\nExample: tevm generate contract Counter --dir .' export const args = zod.tuple([ zod.enum(['contract', 'test', 'script', 'all']).describe( argument({ name: 'type', description: 'Type of file to generate (contract: generates TS types from .sol files)', }), ), zod .string() .optional() .describe( argument({ name: 'name', description: 'Name of the file or glob pattern', }), ), ]) export const options = zod.object({ force: zod .boolean() .default(false) .describe( option({ description: 'Overwrite existing files', alias: 'f', }), ), dir: zod .string() .default(process.cwd()) .describe( option({ description: 'Working directory (defaults to current directory)', alias: 'd', }), ), include: zod .string() .default('src/**/*.sol') .describe( option({ description: 'Glob pattern(s) for Solidity files, comma-separated', alias: 'i', }), ), output: zod .string() .optional() .describe( option({ description: 'Custom output directory for generated files', alias: 'o', }), ), verbose: zod .boolean() .default(false) .describe( option({ description: 'Show verbose output', alias: 'v', }), ), }) type Props = { args: zod.infer options: zod.infer } export default function Generate({ args, options }: Props) { const [type, name] = args const [isGenerating, setIsGenerating] = useState(true) const [currentTask, setCurrentTask] = useState('') const [result, setResult] = useState<{ success: boolean files: string[] errors: string[] }>({ success: false, files: [], errors: [] }) useEffect(() => { const generateTypes = async () => { setIsGenerating(true) try { // Handle different generation types if (type === 'contract') { await generateContractTypes() } else if (type === 'test') { setResult({ success: false, files: [], errors: ['Test generation is not implemented yet'], }) } else if (type === 'script') { setResult({ success: false, files: [], errors: ['Script generation is not implemented yet'], }) } else if (type === 'all') { await generateContractTypes() // Add other type generations as they are implemented } } catch (error) { setResult({ success: false, files: [], errors: [error instanceof Error ? error.message : String(error)], }) } finally { setIsGenerating(false) setCurrentTask('') } } const generateContractTypes = async () => { const cwd = path.resolve(options.dir) const [{ bundler }, { createCache }, { default: solc }] = await Promise.all([ import('@tevm/base-bundler'), import('@tevm/bundler-cache'), import('solc'), ]) const includePatterns = options.include.split(',') const errors: string[] = [] const generatedFiles: string[] = [] if (options.verbose) { console.log('Generating types from contracts...', { dir: cwd, include: includePatterns, name }) } try { setCurrentTask('Searching for Solidity files...') let files = glob.sync(includePatterns, { cwd }) // Filter files by name if provided if (name) { const namePattern = name.includes('*') ? name : `*${name}*` const nameRegex = new RegExp(namePattern.replace(/\*/g, '.*')) files = files.filter((file) => { const fileName = path.basename(file) return nameRegex.test(fileName) }) } if (files.length === 0) { throw new Error(`No files found matching the provided patterns${name ? ` and name "${name}"` : ''}`) } setCurrentTask(`Found ${files.length} files. Starting type generation...`) // Process each file using the Tevm bundler for (let i = 0; i < files.length; i++) { const file = files[i]! // Non-null assertion as we're iterating within bounds setCurrentTask(`Processing file ${i + 1}/${files.length}: ${file}`) try { const fileName = path.basename(file) const fileDir = path.dirname(file) const sourcePath = path.resolve(cwd, file) const outputPath = options.output ? path.resolve(cwd, options.output, `${fileName}.ts`) : path.resolve(cwd, fileDir, `${fileName}.ts`) if (!options.force && fs.existsSync(outputPath)) { errors.push(`File already exists: ${outputPath}. Use --force to overwrite.`) continue } // Actual implementation using Tevm bundler try { setCurrentTask(`Loading config for ${fileName}...`) const configEffect = loadConfig(cwd) as unknown as import('effect').Effect.Effect< Parameters[0] & { cacheDir: string }, unknown, never > const config = process.env['TEVM_JSON'] === 'true' ? runSync(provide(configEffect, Logger.replace(Logger.defaultLogger, Logger.none))) : runSync(configEffect) setCurrentTask(`Creating cache for ${fileName}...`) const solcCache = createCache(config.cacheDir, fao, cwd) setCurrentTask(`Bundling ${fileName}...`) const logger = process.env['TEVM_JSON'] === 'true' ? { debug: () => undefined, error: () => undefined, info: () => undefined, log: () => undefined, warn: () => undefined, } : console const plugin = bundler(config, logger as Console, fao, solc, solcCache, 'tevm/contract') const tsContent = await plugin.resolveTsModule(sourcePath, cwd, false, true) // Ensure directory exists const dir = path.dirname(outputPath) if (!fs.existsSync(dir)) { mkdirSync(dir, { recursive: true }) } // Write the generated TypeScript file setCurrentTask(`Writing types to ${outputPath}...`) await writeFile(outputPath, tsContent.code) generatedFiles.push(outputPath) if (options.verbose) { console.log(`Generated: ${outputPath}`) } } catch (bundlerError) { const errorMessage = bundlerError instanceof Error ? bundlerError.message : String(bundlerError) errors.push(`Bundler error processing ${file}: ${errorMessage}`) // Log the full error in verbose mode for debugging if (options.verbose) { console.error(`Detailed error for ${file}:`, bundlerError) } } } catch (err) { errors.push(`Error processing ${file}: ${err instanceof Error ? err.message : String(err)}`) } } setResult({ success: errors.length === 0, files: generatedFiles, errors, }) } catch (err) { setResult({ success: false, files: [], errors: [err instanceof Error ? err.message : String(err)], }) } } generateTypes() }, []) if (isGenerating) { return ( {' '} Generating {type} {name ? `"${name}"` : 'files'} {options.force ? ' (force)' : ''} {currentTask && ( {currentTask} )} ) } if (process.env['TEVM_JSON'] === 'true') { return result.success ? ( ) : ( ) } return ( {result.success ? ( <> Successfully generated {result.files.length} files: {result.files.map((file, index) => ( - {file} ))} ) : ( <> Generation failed with errors: {result.errors.map((error, index) => ( - {error} ))} )} {result.files.length > 0 && result.errors.length > 0 && ( <> Completed with warnings: {result.errors.map((error, index) => ( - {error} ))} )} ) }