forked from firebase/firebase-admin-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-docs.js
More file actions
387 lines (364 loc) · 12.5 KB
/
generate-docs.js
File metadata and controls
387 lines (364 loc) · 12.5 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* @license
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { exec } = require('child-process-promise');
const fs = require('mz/fs');
const jsdom = require('jsdom');
const path = require('path');
const readline = require('readline');
const yargs = require('yargs');
const yaml = require('js-yaml');
const repoPath = path.resolve(`${__dirname}/..`);
// Command-line options.
const { source: sourceFile } = yargs
.option('source', {
default: `${repoPath}/src/index.d.ts`,
describe: 'Typescript source file(s)',
type: 'string'
})
.version(false)
.help().argv;
const docPath = path.resolve(`${__dirname}/html/node`);
const contentPath = path.resolve(`${__dirname}/content-sources/node`);
const tempHomePath = path.resolve(`${contentPath}/HOME_TEMP.md`);
const devsitePath = `/docs/reference/admin/node/`;
const firestoreExcludes = ['v1', 'v1beta1', 'setLogFunction'];
const firestoreHtmlPath = `${docPath}/admin.firestore.html`;
const firestoreHeader = `<section class="tsd-panel-group tsd-member-group ">
<h2>Type aliases</h2>
<div class="tsd-panel">
<p>Following types are defined in the <code>@google-cloud/firestore</code> package
and re-exported from this namespace for convenience.</p>
</div>
<ul>`;
const firestoreFooter = '\n </ul>\n</section>\n';
/**
* Strips path prefix and returns only filename.
* @param {string} path
*/
function stripPath(path) {
const parts = path.split('/');
return parts[parts.length - 1];
}
/**
* Runs Typedoc command.
*
* Additional config options come from ./typedoc.js
*/
function runTypedoc() {
const command = `${repoPath}/node_modules/.bin/typedoc ${sourceFile} \
--out ${docPath} \
--readme ${tempHomePath} \
--options ${__dirname}/typedoc.js \
--theme ${__dirname}/theme`;
console.log('Running command:\n', command);
return exec(command);
}
/**
* Moves files from subdir to root.
* @param {string} subdir Subdir to move files out of.
*/
function moveFilesToRoot(subdir) {
return exec(`mv ${docPath}/${subdir}/* ${docPath}`)
.then(() => {
exec(`rmdir ${docPath}/${subdir}`);
})
.catch(e => console.error(e));
}
/**
* Reformat links to match flat structure.
* @param {string} file File to fix links in.
*/
function fixLinks(file) {
return fs.readFile(file, 'utf8').then(data => {
const flattenedLinks = data
.replace(/\.\.\//g, '')
.replace(/(modules|interfaces|classes|enums)\//g, '');
let caseFixedLinks = flattenedLinks;
for (const lower in lowerToUpperLookup) {
const re = new RegExp(lower, 'g');
caseFixedLinks = caseFixedLinks.replace(re, lowerToUpperLookup[lower]);
}
return fs.writeFile(file, caseFixedLinks);
});
}
let tocText = '';
/**
* Generates temporary markdown file that will be sourced by Typedoc to
* create index.html.
*
* @param {string} tocRaw
* @param {string} homeRaw
*/
function generateTempHomeMdFile(tocRaw, homeRaw) {
const { toc } = yaml.safeLoad(tocRaw);
let tocPageLines = [homeRaw, '# API Reference'];
toc.forEach(group => {
tocPageLines.push(`\n## [${group.title}](${stripPath(group.path)})`);
const section = group.section || [];
section.forEach(item => {
tocPageLines.push(`- [${item.title}](${stripPath(item.path)}.html)`);
});
});
return fs.writeFile(tempHomePath, tocPageLines.join('\n'));
}
/**
* Mapping between lowercase file name and correctly cased name.
* Used to update links when filenames are capitalized.
*/
const lowerToUpperLookup = {};
/**
* Checks to see if any files listed in toc.yaml were not generated.
* If files exist, fixes filename case to match toc.yaml version.
*/
function checkForMissingFilesAndFixFilenameCase() {
// Get filenames from toc.yaml.
const filenames = tocText
.split('\n')
.filter(line => line.includes('path:'))
.map(line => line.split(devsitePath)[1]);
// Logs warning to console if a file from TOC is not found.
const fileCheckPromises = filenames.map(filename => {
// Warns if file does not exist, fixes filename case if it does.
// Preferred filename for devsite should be capitalized and taken from
// toc.yaml.
const tocFilePath = `${docPath}/${filename}.html`;
// Generated filename from Typedoc will be lowercase.
const generatedFilePath = `${docPath}/${filename.toLowerCase()}.html`;
return fs.exists(generatedFilePath).then(exists => {
if (exists) {
// Store in a lookup table for link fixing.
lowerToUpperLookup[
`${filename.toLowerCase()}.html`
] = `${filename}.html`;
return fs.rename(generatedFilePath, tocFilePath);
} else {
console.warn(
`Missing file: ${filename}.html requested ` +
`in toc.yaml but not found in ${docPath}`
);
}
});
});
return Promise.all(fileCheckPromises).then(() => filenames);
}
/**
* Gets a list of html files in generated dir and checks if any are not
* found in toc.yaml.
* Option to remove the file if not found (used for node docs).
*
* @param {Array} filenamesFromToc Filenames pulled from toc.yaml
* @param {boolean} shouldRemove Should just remove the file
*/
function checkForUnlistedFiles(filenamesFromToc, shouldRemove) {
return fs.readdir(docPath).then(files => {
const htmlFiles = files
.filter(filename => filename.slice(-4) === 'html')
.map(filename => filename.slice(0, -5));
const removePromises = [];
htmlFiles.forEach(filename => {
if (
!filenamesFromToc.includes(filename) &&
filename !== 'index' &&
filename !== 'globals'
) {
if (shouldRemove) {
console.log(
`REMOVING ${docPath}/${filename}.html - not listed in toc.yaml.`
);
removePromises.push(fs.unlink(`${docPath}/${filename}.html`));
} else {
// This is just a warning, it doesn't need to finish before
// the process continues.
console.warn(
`Unlisted file: ${filename} generated ` +
`but not listed in toc.yaml.`
);
}
}
});
if (shouldRemove) {
return Promise.all(removePromises).then(() =>
htmlFiles.filter(filename => filenamesFromToc.includes(filename))
);
} else {
return htmlFiles;
}
});
}
/**
* Writes a _toc_autogenerated.yaml as a record of all files that were
* autogenerated. Helpful to tech writers.
*
* @param {Array} htmlFiles List of html files found in generated dir.
*/
function writeGeneratedFileList(htmlFiles) {
const fileList = htmlFiles.map(filename => {
return {
title: filename,
path: `${devsitePath}${filename}`
};
});
const generatedTocYAML = yaml.safeDump({ toc: fileList });
return fs
.writeFile(`${docPath}/_toc_autogenerated.yaml`, generatedTocYAML)
.then(() => htmlFiles);
}
/**
* Fix all links in generated files to other generated files to point to top
* level of generated docs dir.
*
* @param {Array} htmlFiles List of html files found in generated dir.
*/
function fixAllLinks(htmlFiles) {
const writePromises = [];
htmlFiles.forEach(file => {
// Update links in each html file to match flattened file structure.
writePromises.push(fixLinks(`${docPath}/${file}.html`));
});
return Promise.all(writePromises);
}
/**
* Updates the auto-generated Firestore API references page, by appending
* the specified HTML content block.
*
* @param {string} contentBlock The HTML content block to be added to the Firestore docs.
*/
function updateFirestoreHtml(contentBlock) {
const dom = new jsdom.JSDOM(fs.readFileSync(firestoreHtmlPath));
const contentNode = dom.window.document.body.querySelector('.col-12');
const newSection = new jsdom.JSDOM(contentBlock);
contentNode.appendChild(newSection.window.document.body.firstChild);
fs.writeFileSync(firestoreHtmlPath, dom.window.document.documentElement.outerHTML);
}
/**
* Adds Firestore type aliases to the auto-generated API docs. These are the
* types that are imported from the @google-cloud/firestore package, and
* then re-exported from the admin.firestore namespace. Typedoc currently
* does not handle these correctly, so we need this solution instead.
*/
function addFirestoreTypeAliases() {
return new Promise((resolve, reject) => {
const fileStream = fs.createReadStream(`${repoPath}/src/index.d.ts`);
fileStream.on('error', (err) => {
reject(err);
});
const lineReader = readline.createInterface({
input: fileStream,
});
let contentBlock = firestoreHeader;
lineReader.on('line', (line) => {
line = line.trim();
if (line.startsWith('export import') && line.indexOf('_firestore.')) {
const typeName = line.split(' ')[2];
if (firestoreExcludes.indexOf(typeName) === -1) {
contentBlock += `
<li>
<a href="https://googleapis.dev/nodejs/firestore/latest/${typeName}.html">${typeName}</a>
</li>`;
}
}
});
lineReader.on('close', () => {
try {
contentBlock += firestoreFooter;
updateFirestoreHtml(contentBlock);
resolve();
} catch (err) {
reject(err);
}
});
});
}
/**
* Main document generation process.
*
* Steps for generating documentation:
* 1) Create temporary md file as source of homepage.
* 2) Run Typedoc, sourcing index.d.ts for API content and temporary md file
* for index.html content.
* 3) Write table of contents file.
* 4) Flatten file structure by moving all items up to root dir and fixing
* links as needed.
* 5) Check for mismatches between TOC list and generated file list.
*/
Promise.all([
fs.readFile(`${contentPath}/toc.yaml`, 'utf8'),
fs.readFile(`${contentPath}/HOME.md`, 'utf8')
])
// Read TOC and homepage text and assemble a homepage markdown file.
// This file will be sourced by Typedoc to generate index.html.
.then(([tocRaw, homeRaw]) => {
tocText = tocRaw;
return generateTempHomeMdFile(tocRaw, homeRaw);
})
// Run main Typedoc process (uses index.d.ts and generated temp file above).
.then(runTypedoc)
.then(output => {
// Typedoc output.
console.log(output.stdout);
// Clean up temp home markdown file. (Nothing needs to wait for this.)
fs.unlink(tempHomePath);
// Devsite doesn't like css.map files.
return fs.unlink(`${docPath}/assets/css/main.css.map`);
})
// Write out TOC file. Do this after Typedoc step to prevent Typedoc
// erroring when it finds an unexpected file in the target dir.
.then(() => fs.writeFile(`${docPath}/_toc.yaml`, tocText))
// Flatten file structure. These categories don't matter to us and it makes
// it easier to manage the docs directory.
.then(() => {
return Promise.all([
// moveFilesToRoot('classes'),
moveFilesToRoot('modules'),
moveFilesToRoot('interfaces'),
moveFilesToRoot('enums'),
]);
})
// Check for files listed in TOC that are missing and warn if so.
// Not blocking.
.then(checkForMissingFilesAndFixFilenameCase)
// Check for files that exist but aren't listed in the TOC and warn.
// (If API is node, actually remove the file.)
// Removal is blocking, warnings aren't.
.then(filenamesFromToc =>
checkForUnlistedFiles(filenamesFromToc, true)
)
// Write a _toc_autogenerated.yaml to record what files were created.
.then(htmlFiles => writeGeneratedFileList(htmlFiles))
// Correct the links in all the generated html files now that files have
// all been moved to top level.
.then(fixAllLinks)
// Add local variable include line to index.html (to access current SDK
// version number).
.then(addFirestoreTypeAliases)
.then(() => {
fs.readFile(`${docPath}/index.html`, 'utf8').then(data => {
// String to include devsite local variables.
const localVariablesIncludeString = `{% include "docs/web/_local_variables.html" %}\n`;
return fs.writeFile(
`${docPath}/index.html`,
localVariablesIncludeString + data
);
});
})
.catch(e => {
if (e.stdout) {
console.error(e.stdout);
} else {
console.error(e);
}
});