-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocumentation-builder.js
More file actions
91 lines (72 loc) · 2.96 KB
/
documentation-builder.js
File metadata and controls
91 lines (72 loc) · 2.96 KB
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
const { gerar, validar } = require('./controllers/as-lib');
const { writeFileSync } = require('fs');
const { resolve } = require('path');
const { item: [{ item: gerarReqList }, { item: validarReqList }] } = require('../doc/Dev tools.postman_collection.json');
const buildComment = (data, funcName) => `/** ${data} */
${funcName}: ${gerar[funcName]},
`;
const buildCommentValidar = (data, funcName) => `/** ${data} */
${funcName}: ${validar[funcName]},
`;
const buildFromRequest = ({ name, description = '', funcName, funcParams = [] }) => {
const params = funcParams?.map(({ key, value = 'Sem Valor' }, index, { length }) => `* @param {String} req.query.${key} - '${value}'${index < (length - 1) ? ',' : ''}`)?.join(`\n`);
const withDash = description ? ` - ${description}` : '';
return (
`${name}${withDash}
* @function ${funcName}
* ${params ? `
* @param {Object} req - object representing a request
* @param {Object} req.query - query of request
${params}` : ''}`);
}
const buildValidatorFromRequest = ({ name, description = '', funcName, funcParams = [], pathParam }) => {
const params = funcParams?.map(({ key, value = 'Sem Valor' }, index, { length }) => `* @param {String} req.query.${key} - '${value}'${index < (length - 1) ? ',' : ''}`)?.join(`\n`);
const withDash = description ? ` - ${description}` : '';
return (
`${name}${withDash}
* @function ${funcName}
* ${params ? `
* @param {Object} req - object representing a request
* @param {Object} req.query - query of request
${params}` : ''}`);
}
const generateDocsFromGerar = () => {
const finalContent = gerarReqList.map(({ name, request: { description, url: { path: [, funcName], query: funcParams } } }) => {
const current = buildFromRequest({ name, description, funcName, funcParams })
return buildComment(current, funcName);
}).join('');
const templateFile = `
module.exports = {
${finalContent}
}
`;
writeF('gerar.txt', templateFile);
};
const generateDocsFromValidar = () => {
const finalContent = validarReqList.map(({ name, request: { description, url: { path: [, funcName, pathParam], query: funcParams } } }) => {
const current = buildValidatorFromRequest({ name, description, funcName, funcParams, pathParam })
return buildCommentValidar(current, funcName);
}).join('');
const templateFile = `
module.exports = {
${finalContent}
}
`;
writeF('validar.txt', templateFile);
};
const bodyExecution = {
gerar: generateDocsFromGerar,
validar: generateDocsFromValidar
};
const writeF = (fileName, content) => {
function formatDate(date) {
return date.toISOString().replace(/\D*/g, '').substring(0, 17);
}
const fname = resolve(`./${formatDate(new Date())}-${fileName}`);
writeFileSync(fname, content, {
encoding: 'utf-8',
flag: 'w+'
});
};
// bodyExecution.validar();
(async () => console.log({ pessoa: await gerar.pessoa({ query: { txt_qtde: 1 } }) }))();