From dae1a17c55e2fe542da9a953225892bfed447523 Mon Sep 17 00:00:00 2001 From: cyace84 Date: Sun, 6 Aug 2023 14:56:06 +0200 Subject: [PATCH] fix array issue in docgen --- src/ast-builder/compile-ast.ts | 27 ++++++++++++++++++++++++++- src/ast-builder/getters.ts | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/ast-builder/compile-ast.ts b/src/ast-builder/compile-ast.ts index 6a378fd4..0a1707c5 100644 --- a/src/ast-builder/compile-ast.ts +++ b/src/ast-builder/compile-ast.ts @@ -2,7 +2,7 @@ import fs from "fs"; import { execSync } from "child_process"; import { Config } from "../config"; import { getAstsFromSources, getContractsList } from "./getters"; -import { basename, join, resolve } from "path"; +import { basename, extname, join, resolve } from "path"; const createDirectoryIfNotExists = (dir: string) => { if (!fs.existsSync(dir)) { @@ -51,6 +51,7 @@ export const compileAst = (config: Config) => { deleteDirectoryIfExists(ast_cache_path); compileExternalAst(config); renameAstFiles(astOutputPath); + wrapAstInArray(astOutputPath); }; /** @@ -93,3 +94,27 @@ const renameAstFiles = (dir: string) => { } }); }; + +const wrapAstInArray = (dir: string) => { + const files = fs.readdirSync(dir); + + files.forEach((file) => { + if (extname(file) === ".json") { + const filePath = join(dir, file); + const content = fs.readFileSync(filePath, "utf8"); + let jsonContent; + + try { + jsonContent = JSON.parse(content); + } catch (error) { + console.error(`Error parsing file ${file}:`, error); + return; + } + + if (!Array.isArray(jsonContent)) { + const wrappedContent = [jsonContent]; + fs.writeFileSync(filePath, JSON.stringify(wrappedContent, null, 2)); + } + } + }); +}; diff --git a/src/ast-builder/getters.ts b/src/ast-builder/getters.ts index ffdb8b64..078a3e84 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(