Skip to content

Commit

Permalink
feat: allow users to keep contracts filestructure for documentation f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
julien-devatom committed Jan 9, 2022
1 parent 690343f commit 8ce38ae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/dodocTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export interface Error {
}

export interface Doc {
path?: string;
name?: string;
title?: string;
notice?: string;
Expand Down
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extendConfig((config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) =>
testMode: userConfig.dodoc?.testMode || false,
outputDir: userConfig.dodoc?.outputDir || './docs',
templatePath: userConfig.dodoc?.templatePath || path.join(__dirname, './template.sqrl'),
keepFileStructure: userConfig.dodoc?.keepFileStructure || true,
};
});

Expand All @@ -49,6 +50,7 @@ task(TASK_COMPILE, async (args, hre, runSuper) => {
const qualifiedNames = await hre.artifacts.getAllFullyQualifiedNames();

// Loops through all the qualified names to get all the compiled contracts
const sourcesPath = hre.config.paths.sources.substr(process.cwd().length + 1); // trick to get relative path to files, and trim the first /
for (const qualifiedName of qualifiedNames) {
const [source, name] = qualifiedName.split(':');

Expand All @@ -68,7 +70,7 @@ task(TASK_COMPILE, async (args, hre, runSuper) => {
console.log(JSON.stringify(info.devdoc, null, 4));
}

const doc = decodeAbi(info.abi);
const doc = { ...decodeAbi(info.abi), path: source.substr(sourcesPath.length).split('/').slice(0, -1).join('/') }; // remove filename

// Fetches info from userdoc
for (const errorSig in info.userdoc?.errors) {
Expand Down Expand Up @@ -169,17 +171,23 @@ task(TASK_COMPILE, async (args, hre, runSuper) => {

for (let i = 0; i < docs.length; i += 1) {
const result = Sqrl.render(template, docs[i]);

let docfileName = `${docs[i].name}.md`;
let testFileName = `${docs[i].name}.json`;
if (config.keepFileStructure && docs[i].path !== undefined && !fs.existsSync(path.join(config.outputDir, <string>docs[i].path))) {
await fs.promises.mkdir(path.join(config.outputDir, <string>docs[i].path), { recursive: true });
docfileName = path.join(<string>docs[i].path, docfileName);
testFileName = path.join(<string>docs[i].path, testFileName);
}
await fs.promises.writeFile(
path.join(config.outputDir, `${docs[i].name}.md`),
path.join(config.outputDir, docfileName),
result, {
encoding: 'utf-8',
},
);

if (config.testMode) {
await fs.promises.writeFile(
path.join(config.outputDir, `${docs[i].name}.json`),
path.join(config.outputDir, testFileName),
JSON.stringify(docs[i], null, 4), {
encoding: 'utf-8',
},
Expand Down
4 changes: 3 additions & 1 deletion src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare module 'hardhat/types/config' {
testMode?: boolean;
templatePath?: string;
outputDir?: string;
keepFileStructure?: boolean;
}
}

Expand All @@ -19,7 +20,8 @@ declare module 'hardhat/types/config' {
runOnCompile: boolean;
testMode: boolean;
templatePath: string;
outputDir: string
outputDir: string;
keepFileStructure: boolean;
}
}
}

0 comments on commit 8ce38ae

Please sign in to comment.