-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
128 lines (100 loc) · 3.83 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const saveFile = require('fs').writeFileSync;
const fs = require('fs')
exports.createDocumentation = function(endpoints, config) {
// console.log(endpoints[0]['name'])
let data = "";
endpoints.map(endpoint => {
const endp = (JSON.parse(endpoint))
endp.map(item => {
let vars = item.fields.reduce((acc, field) =>
acc += `<tr>
<td style="text-align: left">${field.name}</td>
<td style="text-align: left">${field.type}</td>
<td style="text-align: left">${field.required}</td>
</tr>`,
"")
data = data + `
<section>
<h2 id="header-2">${item.name}</h2>
<blockquote>
<p>${item.method} <b>${item.path}</b></p>
<p>${item.description}</p>
</blockquote>
<table>
<thead>
<tr>
<th style="text-align: left">field</th>
<th style="text-align: left">type</th>
<th style="text-align: left">required</th>
</tr>
</thead>
<tbody>
${vars}
</tbody>
</table>
</section>`
});
});
const writeText = `<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${config.info.sitename} - API Docs</title>
<link rel="stylesheet"
href="https://pages-themes.github.io/minimal/assets/css/style.css?v=814b8723af0aa0ada9b5784da6b73d862bb74150">
</head>
<body>
<div class="wrapper">
<header>
<h1>${config.info.sitename}</h1>
<img src="${config.info.logourl}" alt="Logo" width="100%" />
<p>${config.info.sitedescription}</p>
</header>
${data}
<footer>
<p>Generated using <a href="https://www.npmjs.com/package/serverless-functions-api-docs">serverless-functions-api-docs</a></p>
<p><small>Created by <a href="https://github.com/raulcr98">raulcr98</a>. Theme by <a href="https://github.com/orderedlist">orderedlist</a></small></p>
</footer>
</div>
<script src="https://pages-themes.github.io/minimal/minimal/assets/js/scale.fix.js"></script>
</body>
</html>`;
fs.writeFileSync(config.outputfile, writeText, function (err) {
if (err) throw err;
console.log('File is created successfully.');
});
// console.log(writeText)
}
exports.initFiles = function() {
try {
fs.readFileSync('../../doc_config.js')
} catch (error) {
fs.writeFileSync('../../doc_config.js', `
const netlifydoc = require('serverless-functions-api-docs/index');
const config = {
basedir: "functions",
outputfile: "index.html",
info: {
sitename: "SITE NAME",
logourl: "SITELOGO URL",
sitedescription: "SITE DESCRIPTION"
}
}
netlifydoc.createDoc(config);
`, function (err) {
if (err) throw err;
console.log('Configuration file is created successfully.');
});
}
}
exports.updatePackage = function() {
const pkgJsonPath = require.main.paths[0].split('node_modules')[0] + 'package.json';
const json = require(pkgJsonPath);
if (!json.hasOwnProperty('scripts')) {
json.scripts = {};
}
json.scripts['createdoc'] = 'node doc_config.js';
saveFile(pkgJsonPath, JSON.stringify(json, null, 2));
}