Skip to content

Commit

Permalink
fix array issue in docgen
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyace84 committed Aug 6, 2023
1 parent 87a0d29 commit dae1a17
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/ast-builder/compile-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -51,6 +51,7 @@ export const compileAst = (config: Config) => {
deleteDirectoryIfExists(ast_cache_path);
compileExternalAst(config);
renameAstFiles(astOutputPath);
wrapAstInArray(astOutputPath);
};

/**
Expand Down Expand Up @@ -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));
}
}
});
};
2 changes: 1 addition & 1 deletion src/ast-builder/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit dae1a17

Please sign in to comment.