Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix: Accidentally deleted bin folder #1435

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-bulldogs-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tevm": patch
---

Fixed accidentally deleted bin folder
83 changes: 83 additions & 0 deletions tevm/bin/tevm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env node
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
import { access, mkdir, readFile, stat, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { Command } from 'commander';
import { bundler } from '@tevm/base-bundler';
import { createCache } from '@tevm/bundler-cache';
import { loadConfig } from '@tevm/config';
import { runSync } from 'effect/Effect';
import { glob } from 'glob';
// @ts-expect-error
import * as solc from 'solc';

/**
* @typedef {import('@tevm/base-bundler').FileAccessObject} FileAccessObject
*/

/** @type {FileAccessObject} */
const fao = {
existsSync: existsSync,
readFile: readFile,
readFileSync: readFileSync,
writeFileSync: writeFileSync,
statSync,
stat,
mkdirSync,
mkdir,
writeFile,
exists: async (path) => {
try {
await access(path);
return true;
} catch (e) {
return false;
}
},
};

/**
* Generate types from Solidity contracts.
*
* @param {string} cwd - The current working directory.
* @param {string[]} include - The glob pattern to include Solidity files.
*/
const generate = (cwd, include) => {
console.log('Generating types from contracts...', { dir: cwd, include });
const files = glob.sync(include, { cwd });
if (files.length === 0) {
throw new Error('No files found');
}
files.forEach(async (file) => {
const fileName = file.split('/').at(-1);
const fileDir = file.split('/').slice(0, -1).join('/');
const config = runSync(loadConfig(cwd));
const solcCache = createCache(config.cacheDir, fao, cwd);
const plugin = bundler(config, console, fao, solc, solcCache);
const tsContent = await plugin.resolveTsModule(`./${file}`, cwd, false, true);
await writeFile(path.join(fileDir, `${fileName}.ts`), tsContent.code);
});
};
Comment on lines +45 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider improvements to enhance performance, error handling, and logging.

The generate function is responsible for generating TypeScript types from Solidity contracts. While the overall logic is sound, there are a few areas that could be improved:

  1. Performance: The function uses synchronous file operations, which may block the execution until all files are processed. Consider using asynchronous file operations to improve performance and avoid blocking the execution.

  2. Error Handling: The function relies on various external dependencies and file operations. It would be beneficial to add proper error handling for potential issues with file access, configuration loading, or Solidity compilation. This would help identify and troubleshoot any problems that may arise during the generation process.

  3. Logging and Progress Tracking: The function currently logs the current working directory and include pattern at the beginning. However, it would be helpful to provide more detailed logging or progress tracking throughout the generation process. This would give better visibility into the progress and status of the type generation for each file.


// Initialize Commander
const program = new Command();

program
.name('tevm')
.description('TEVM CLI')
.version('1.0.0');

program
.command('gen')
.description('Generate types from Solidity contracts. If files in in .s.sol it will also compile bytecode')
.argument('<include>', 'Glob pattern to include Solidity files')
.option('-c, --config <path>', 'Path to the configuration file', process.cwd())
.action((include, options) => {
const cwd = options.config || process.cwd();
const includePattern = include.split(',');
generate(cwd, includePattern);
});

// Parse the arguments
program.parse(process.argv);

Loading