-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmddocs.js
208 lines (187 loc) · 5.71 KB
/
mddocs.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* This file is part of the Ingram Micro Cloud Blue Connect SDK.
*
* @copyright (c) 2020. Ingram Micro. All Rights Reserved.
*/
const { promisify } = require('util');
const { spawn } = require('child_process');
const exec = promisify(require('child_process').exec);
const fs = require('fs');
const jsdoc2md = require('jsdoc-to-markdown');
const pfs = {
exists: promisify(fs.exists),
mkdir: promisify(fs.mkdir),
readdir: promisify(fs.readdir),
copyFile: promisify(fs.copyFile),
readFile: promisify(fs.readFile),
writeFile: promisify(fs.writeFile),
unlink: promisify(fs.unlink),
};
const path = require('path');
const jsDocConfig = require('./jsdoc.config');
const config = require('./mddocs.config');
const init = async () => {
if (!(await pfs.exists(config.destPath))) {
await pfs.mkdir(config.destPath);
} else {
const files = await pfs.readdir(config.destPath);
files.forEach(async (f) => {
await pfs.unlink(path.join(config.destPath, f));
});
}
};
const getCurrentBranchName = async () => {
if (process.env.TRAVIS_BRANCH) {
return process.env.TRAVIS_BRANCH;
}
const { stdout } = await exec('git rev-parse --abbrev-ref HEAD');
return stdout;
};
const getUrlPrefix = async () => {
const bs = config.urlPrefix.endsWith('/') ? '' : '/';
let currentBranch = await getCurrentBranchName();
currentBranch = currentBranch.replace(/\r?\n|\r/g, '');
return `${config.urlPrefix}${bs}${path.join(currentBranch, config.destPath)}/`;
}
const copyStatics = async () => {
const urlPrefix = await getUrlPrefix();
let idx = 0;
const staticPages = {};
if (jsDocConfig.opts && jsDocConfig.opts.readme) {
const fileName = `${('' + idx).padStart(3, '0')}_${jsDocConfig.opts.readme}`.replace(' ', '_');
const srcFile = path.join('./', jsDocConfig.opts.readme);
pfs.copyFile(
srcFile,
path.join(config.destPath, fileName),
);
staticPages[config.homeTitle] = {
name: config.homeTitle,
category: 'Home',
file: srcFile,
idx: '' + idx,
outputFileName: fileName,
url: `${urlPrefix}${fileName}`,
};
idx++;
}
if (jsDocConfig.opts && jsDocConfig.opts.tutorials) {
const files = await pfs.readdir(jsDocConfig.opts.tutorials);
for (let el of files) {
if (el.endsWith('.json')) {
const data = await pfs.readFile(path.join(jsDocConfig.opts.tutorials, el));
const tutInfo = JSON.parse(data.toString());
const mdFile = `${path.basename(el, path.extname(el))}.md`;
const fileName = `${('' + idx).padStart(3, '0')}_${mdFile}`.replace(' ', '_');
const srcFile = path.join(jsDocConfig.opts.tutorials, mdFile);
pfs.copyFile(
srcFile,
path.join(config.destPath, fileName),
);
staticPages[tutInfo.title] = {
name: tutInfo.title,
category: 'Tutorials',
file: srcFile,
idx: '' + idx,
outputFileName: fileName,
url: `${urlPrefix}${fileName}`,
};
idx++;
}
}
}
return {
idx,
staticPages,
};
};
const generateJsdocJson = () => new Promise((resolve, reject) => {
let stdout = '';
let stderr = '';
const child = spawn('jsdoc', ['-c', 'jsdoc.config.js', '-X']);
child.on('error', (err) => {
console.log(err);
reject(err);
});
child.stdout.on('data', (chunk) => {
stdout += chunk.toString();
});
child.stderr.on('data', (chunk) => {
stderr += chunk.toString();
});
child.on('close', (exCode) => {
if (exCode === 0) {
resolve({ stdout, stderr });
} else {
reject(exCode, { stdout, stderr });
}
});
});
const parse = async (idx, data) => {
const collected = {};
const results = {};
data.forEach((el) => {
if (config.objTypes.includes(el.kind)) {
const objType = el.kind.charAt(0).toUpperCase() + el.kind.slice(1);
if (!collected[el.category]) {
collected[el.category] = [];
}
collected[el.category].push({
name: `${objType} ${el.name}`,
category: el.category,
file: path.join(el.meta.path, el.meta.filename),
});
}
});
Object.keys(collected).sort().forEach((el) => {
collected[el].sort((a, b) => a.name > b.name).forEach(clz => {
if (!Object.keys(results).includes(clz.name)) {
clz.idx = '' + idx;
idx++;
results[clz.name] = clz;
}
});
});
return results;
};
const generateFileNamesAndUrls = async (data) => {
const urlPrefix = await getUrlPrefix();
Object.keys(data).forEach((k) => {
const filename = `${data[k].idx.padStart(3, '0')}_${k}.md`.replace(' ', '_');
data[k].outputFileName = filename;
data[k].url = `${urlPrefix}${filename}`;
});
return data;
};
const generateMarkdown = async (data) => {
Object.keys(data).forEach(async (k) => {
const obj = data[k];
if (obj.file.endsWith('.js')) {
const rendered = await jsdoc2md.render({ files: obj.file });
await pfs.writeFile(
path.join(config.destPath, obj.outputFileName),
rendered,
);
}
});
};
const generateReport = async (info) => {
await pfs.writeFile(
path.join(config.destPath, 'report.json'),
JSON.stringify({
generated: new Date(),
pages: Object.values(info),
}, null, 4));
}
const run = async () => {
await init();
const { idx, staticPages } = await copyStatics();
const { stdout } = await generateJsdocJson();
const data = JSON.parse(stdout);
const parsed = await parse(idx, data);
const info = await generateFileNamesAndUrls(parsed);
await generateMarkdown(info);
await generateReport({ ...staticPages, ...info });
};
run()
.then(() => console.log('Markdown documentation generated!'))
.catch((e) => console.log('Cannot generate docs: ', e));