-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate.ts
52 lines (46 loc) · 1.4 KB
/
template.ts
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
import { isEmptyObj, or, isEmpty } from "../main"
import process from "process"
import { err } from "../build/const";
import generateExample from "../build/generateExample"
import path from "path"
import { trim } from "../main"
export type docs = {
use: Set<string>,
version: Set<string>,
desc?: Set<string>,
param?: Set<string>,
return?: Set<string>,
example?: Set<string>
}
function getSetValue(set: Set<string>) {
let setValue = ''
set.forEach((s) => {
setValue += `${s} \n`
})
return trim(setValue)
}
function getExample(content: string) {
return '```js\n' + content + '\n```'
}
function getUseCode(use: string) {
return '```ts\n' + use + '\n```'
}
export const generateDocs = async (doc: docs, callBack: () => string) => {
const filePath = callBack()
const splitFilePath = filePath.split(path.sep)
const file = splitFilePath[splitFilePath.length - 1]
if (or(isEmptyObj(doc), isEmpty(doc.version))) {
err(`请完善${file}文档`)
process.exit(1);
}
return `${doc.use ? getUseCode(getSetValue(doc.use)) : ''}\n
${doc.desc ? getSetValue(doc.desc) : ''}\n
**添加版本**
${getSetValue(doc.version)}\n
**参数**
${doc.param ? getSetValue(doc.param) : ''}\n
**返回**
${doc.return ? getSetValue(doc.return) : ''}\n
**例子**
${doc.example ? getExample(getSetValue(doc.example)) : await generateExample(filePath)}`
}