Skip to content

Commit

Permalink
feat: add output and template dir config
Browse files Browse the repository at this point in the history
  • Loading branch information
haixiangyan committed May 1, 2021
1 parent 60f1986 commit 3fd763c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
31 changes: 22 additions & 9 deletions lib/AutoDocAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ import {ejsTemplateDir, outputHtmlDir} from './constants'

class AutoDocAgent extends Agent {
docMetaCollection = []
outputDir = outputHtmlDir
templateDir = ejsTemplateDir

constructor(app, options) {
super(app, options);

if (options) {
this.outputDir = options.outputDir || outputHtmlDir
this.templateDir = options.templateDir || ejsTemplateDir
}
}

getDocMeta(method, url) {
Expand All @@ -20,20 +27,26 @@ class AutoDocAgent extends Agent {
}

// Render html file using ejs template engine
render(outputDir = outputHtmlDir, ejsOptions = {}) {
render(options, ejsOptions = {}) {
const { outputFilename } = options

// No output filename
if (!outputFilename) throw new Error('Please give an output html filename')

// ejs data
const data = {
docMetaCollection: this.docMetaCollection
}

ejs.renderFile(path.join(ejsTemplateDir, 'index.ejs'), data, ejsOptions, (err, html) => {
if (err) {
throw err
}
// Render html with ejs template engine
ejs.renderFile(path.join(this.templateDir, 'index.ejs'), data, ejsOptions, (err, html) => {
if (err) throw err

// Create dir
if (!fs.existsSync(this.outputDir)) fs.mkdirSync(this.outputDir);

if (!fs.existsSync(outputDir)){
fs.mkdirSync(outputDir);
}
fs.writeFileSync(path.join(outputDir, 'index.html'), html)
// Output html file
fs.writeFileSync(path.join(this.outputDir, outputFilename), html)
})
}
}
Expand Down
57 changes: 57 additions & 0 deletions lib/autodoc/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<h1>Doc</h1>


<div>
<h2>Get all users</h2>
<p>Send a get request to get all users from the server</p>

<!-- Request -->
<h3>Request</h3>
<p>
<span>get</span>
<span>/?a=1&amp;b=2</span>
</p>
<p>Params: </p>
<pre>{
&#34;a&#34;: &#34;1&#34;,
&#34;b&#34;: &#34;2&#34;
}</pre>
<p>Body: </p>
<pre></pre>

<!-- Response -->
<h3>Response</h3>
<p>Body: </p>
<pre>{
&#34;msg&#34;: &#34;get success&#34;,
&#34;code&#34;: 0
}</pre>
</div>

<div>
<h2>Post a user</h2>
<p>Create a user and add it to the database</p>

<!-- Request -->
<h3>Request</h3>
<p>
<span>post</span>
<span>/?a=1&amp;b=2</span>
</p>
<p>Params: </p>
<pre>{
&#34;a&#34;: &#34;1&#34;,
&#34;b&#34;: &#34;2&#34;
}</pre>
<p>Body: </p>
<pre>[object Object]</pre>

<!-- Response -->
<h3>Response</h3>
<p>Body: </p>
<pre>{
&#34;msg&#34;: &#34;post success&#34;,
&#34;code&#34;: 0
}</pre>
</div>

2 changes: 1 addition & 1 deletion test/AutoDocAgent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ describe('AutoDocAgent', function () {
})
});

afterAll(() => agent.render())
afterAll(() => agent.render({ outputFilename: 'test.html' }))
});

0 comments on commit 3fd763c

Please sign in to comment.