diff --git a/src/ast-builder/builder.ts b/src/ast-builder/builder.ts index 009008d5..f4d4f599 100644 --- a/src/ast-builder/builder.ts +++ b/src/ast-builder/builder.ts @@ -1,17 +1,17 @@ -import fs from 'fs'; -import { SolcInput, SolcOutput } from 'solidity-ast/solc'; -import { Sources } from './types'; -import { getContractName, getAstsFromSources } from './getters'; +import fs from "fs"; +import { SolcInput, SolcOutput } from "solidity-ast/solc"; +import { Sources } from "./types"; +import { getContractName, getAstsFromSources } from "./getters"; import { replaceSrc, sortASTByDependency, updateDependices, -} from './ast-updater'; -import { Config } from '../config'; -import { Build } from '../site'; -import path from 'path'; +} from "./ast-updater"; +import { Config } from "../config"; +import { Build } from "../site"; +import path from "path"; -import { compileAst } from './compile-ast'; +import { compileAst } from "./compile-ast"; const createRawOutput = (sources: Sources) => { const output: SolcOutput = { sources: {} }; @@ -28,7 +28,7 @@ const createInput = (solcOutput: SolcOutput) => { const sources = solcOutput.sources; const SolcInput: SolcInput = { sources: {} }; for (const key of Object.keys(sources)) { - const fileContent = fs.readFileSync(key, 'utf8').toString(); + const fileContent = fs.readFileSync(key, "utf8").toString(); SolcInput.sources[key] = { content: fileContent }; } return SolcInput; @@ -37,7 +37,7 @@ const createInput = (solcOutput: SolcOutput) => { export function isMainContract(absolutePath: string, astPath: string) { // Extract the contract name from absolute path if (!fileExists(absolutePath) || !fileExists(astPath)) { - throw new Error('File does not exist'); + throw new Error("File does not exist"); } const contractName = getContractName(absolutePath); @@ -52,7 +52,7 @@ function fileExists(filePath: string) { try { return fs.statSync(filePath).isFile(); } catch (err: any) { - if (err.code === 'ENOENT') { + if (err.code === "ENOENT") { return false; } else { throw err; @@ -62,19 +62,19 @@ function fileExists(filePath: string) { export const makeBuild = async ( config: Config, - contractList: string[] = [], + contractList: string[] = [] ) => { compileAst(config); const { sources: astSources, fullSources } = getAstsFromSources( config.astOutputDir!, - config.root!, + config.root! )!; const solcOutput = createRawOutput(astSources); - const sourcesList = Object.values(fullSources).map(source => source.asts); + const sourcesList = Object.values(fullSources).map((source) => source.asts); const sortedSources = sortASTByDependency(sourcesList, solcOutput); updateDependices(fullSources, sortedSources, config); - const ph = path.join(config.root!, 'build/astBuild.json'); + const ph = path.join(config.root!, "build/astBuild.json"); fs.writeFileSync(ph, JSON.stringify(sortedSources, null, 2)); solcOutput.sources = sortedSources; const solcInput = createInput(solcOutput); diff --git a/src/ast-builder/compile-ast.ts b/src/ast-builder/compile-ast.ts index ae53f788..6a378fd4 100644 --- a/src/ast-builder/compile-ast.ts +++ b/src/ast-builder/compile-ast.ts @@ -2,39 +2,55 @@ import fs from "fs"; import { execSync } from "child_process"; import { Config } from "../config"; import { getAstsFromSources, getContractsList } from "./getters"; -import path from "path"; +import { basename, join, resolve } from "path"; + +const createDirectoryIfNotExists = (dir: string) => { + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir); + } +}; + +const moveFiles = (sourceDir: string, destinationDir: string) => { + let files = fs.readdirSync(sourceDir); + files.forEach((file) => { + let oldPath = join(sourceDir, file); + let newPath = join(destinationDir, file); + fs.renameSync(oldPath, newPath); + }); +}; + +const deleteDirectoryIfExists = (dir: string) => { + if (fs.existsSync(dir)) { + fs.rmSync(dir, { recursive: true }); + } +}; /** * It takes the config object, gets a list of contracts, and then compiles the AST for each contract * @param {Config} config - The configuration object that we created earlier. */ -export const compileAst = async (config: Config) => { +export const compileAst = (config: Config) => { const contracts = getContractsList(config.sourcesDir!, config.exclude!); - let astOutputPath = path.resolve(config.root!, config.astOutputDir!); - if (fs.existsSync(astOutputPath)) { - fs.rmSync(astOutputPath, { recursive: true, force: true }); - } + let astOutputPath = resolve(config.root!, config.astOutputDir!); + deleteDirectoryIfExists(astOutputPath); let ast_cache_path = `ast-cache`; - let ast_path = `$PWD/ast`; + let ast_path = resolve(process.cwd(), "ast"); - if (!fs.existsSync(ast_cache_path)) { - execSync(`mkdir ${ast_cache_path}`); - } - - if (!fs.existsSync(ast_path)) { - execSync(`mkdir ${ast_path}`); - } + createDirectoryIfNotExists(ast_cache_path); + createDirectoryIfNotExists(ast_path); contracts.forEach((contract) => { execSync( - `${config.compilerPath} --ast-compact-json $PWD/${config.sourcesDir}/${contract} --output-dir=$PWD/${ast_cache_path}` + `${config.compilerPath} --ast-compact-json ${config.sourcesDir}/${contract} --output-dir=${ast_cache_path}` ); - execSync(`mv $PWD/${ast_cache_path}/* ${astOutputPath}`); + moveFiles(ast_cache_path, astOutputPath); }); - execSync(`rm -rf $PWD/${ast_cache_path}`); + + deleteDirectoryIfExists(ast_cache_path); compileExternalAst(config); + renameAstFiles(astOutputPath); }; /** @@ -59,3 +75,21 @@ export const compileExternalAst = async (config: Config) => { } }); }; + +const renameAstFiles = (dir: string) => { + const files = fs.readdirSync(dir); + + files.forEach((file) => { + if (file.endsWith(".sol_json.ast")) { + const oldPath = join(dir, file); + const newFileName = basename(file, ".sol_json.ast") + ".ast.json"; + const newPath = join(dir, newFileName); + fs.renameSync(oldPath, newPath); + } else if (file.endsWith(".tsol_json.ast")) { + const oldPath = join(dir, file); + const newFileName = basename(file, ".tsol_json.ast") + ".ast.json"; + const newPath = join(dir, newFileName); + fs.renameSync(oldPath, newPath); + } + }); +}; diff --git a/src/ast-builder/getters.ts b/src/ast-builder/getters.ts index 078a3e84..ffdb8b64 100644 --- a/src/ast-builder/getters.ts +++ b/src/ast-builder/getters.ts @@ -121,7 +121,7 @@ export const getAstsFromSources = (astDir: string, root: string) => { const sources: Sources = {}; const fullSources: FullSources = {}; astSources.forEach(astSourceFullPath => { - const astContent: SourceUnit[] = require(astSourceFullPath) as SourceUnit[]; + const astContent: SourceUnit[] = [require(astSourceFullPath) as SourceUnit]; const withNormalPathAsts = renameAbsolutePaths(root, astContent); const astContractName = getContractName(astSourceFullPath)!; const mainAst = getMainAst(