forked from moshie/teamwork-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.js
156 lines (127 loc) · 3.52 KB
/
docs.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const rimraf = require('rimraf')
const parse = require('comment-parser')
var recursive = require('recursive-readdir')
const docs = path.join(__dirname, 'docs')
function directoryExists(directory) {
return new Promise((resolve) => {
fs.stat(directory, (error, stats) => {
if (error || !stats.isDirectory()) {
return resolve(false)
}
resolve(true)
})
})
}
function removeDirectory(directory) {
return new Promise((resolve, reject) => {
rimraf(directory, {}, (error) => {
if (error) {
return reject(error)
}
resolve()
})
})
}
function createDirectory(directory) {
return new Promise((resolve, reject) => {
fs.mkdir(directory, (error) => {
if (error) {
return reject(error)
}
return resolve()
})
})
}
function resetDirectory(directory) {
return directoryExists(directory)
.then(condition => {
return condition ?
removeDirectory(directory) :
Promise.resolve()
})
.then(() => createDirectory(directory))
.then(() => createDirectory(
path.join(directory, 'categories')
))
.catch(console.error)
}
function readFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, (error, data) => {
if (error) {
reject(error)
}
resolve(data.toString())
})
})
}
function titleCase(str) {
return str
.toLowerCase()
.split(' ')
.map(word => {
return word.charAt(0).toUpperCase() + word.slice(1)
})
.join(' ')
}
function getTag(tags, name) {
const found = tags.filter(tag => {
return tag.tag == name
})
if (!found.length) {
return ''
}
var matches = found[0].source.match(/\s(.*)/)
return matches[1]
}
function markdown(file, blocks) {
var name = path.basename(file, '.js')
var content = [
`## ${titleCase(name)}`,
'',
...blocks.map(block => {
return [
`### ${block.description}`,
'',
`[**${getTag(block.tags, 'method')} ${getTag(block.tags, 'uri')}**](${getTag(block.tags, 'url')})`,
'',
'```js',
`${getTag(block.tags, 'example')}`,
'```',
''
].join('\n')
})
]
return content.join('\n')
}
function createFile(file, content) {
return new Promise((resolve, reject) => {
fs.writeFile(file, content, (error) => {
if (error) {
return reject(error)
}
resolve()
})
})
}
resetDirectory(docs)
.then(() => recursive(
path.resolve(__dirname, 'src'),
[file => path.basename(file) == 'index.js'])
)
.then(files => {
var promises = files.map(file => {
return readFile(file)
.then(data => parse(data, { dotted_names: false }))
.then(blocks => markdown(file, blocks))
.then(content => createFile(
path.join(docs, file.replace(path.resolve(__dirname, 'src'), '').replace('.js', '.md')),
content
))
})
return Promise.all(promises)
})
.catch(console.error)