This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
/
build_docs.js
128 lines (111 loc) · 3.9 KB
/
build_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
/**
* @fileoverview Generates the Puppeteer docs at /web/tools/puppeteer/
*
* @author Eric Bidelman <e.bidelman@google.com>
*/
'use strict';
const gulp = require('gulp');
const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const jsYaml = require('js-yaml');
const {promisify} = require('util');
const semver = require('semver');
const checkoutRepoAtTag = require('../reference-docs/get-source-code');
const wfHelper = require('../wfHelper');
const readFile = promisify(fs.readFile);
const ensureDir = promisify(fs.ensureDir);
const writeFile = promisify(fs.writeFile);
const ORG = 'GoogleChrome';
const USER = 'puppeteer';
const GITHUB_PROJECT_URL = `https://github.com/${ORG}/${USER}`;
const GIT_URL = `${GITHUB_PROJECT_URL}.git`;
const NUM_RELEASES_TO_INCLUDE = 3;
const DOC_FILES = [
'README.md',
'docs/troubleshooting.md',
'examples/README.md',
];
const PPTR_FOLDER_PATH = path.join(
__dirname, '..', '..', 'src', 'content', 'en', 'tools', 'puppeteer'
);
/**
* Writes a file to disk.
* @param {string} outputFile Path to output file
* @param {string} content File content to write.
*/
async function saveFile(outputFile, content) {
const filePath = path.join(PPTR_FOLDER_PATH, '_src', outputFile);
await ensureDir(path.dirname(filePath));
await writeFile(filePath, content);
}
/**
* Updates _toc.yaml file with latest releases and saves file to disk.
* @param {string} tocYaml Path to toc yaml file.
* @param {!Array<string>} tags
*/
async function generateTOC(tocYaml, tags) {
const items = tags.map((tag, i) => {
const obj = {
title: tag,
section: [{
title: 'Docs',
path: `${GITHUB_PROJECT_URL}/blob/${tag}/docs/api.md`,
status: 'external',
}, {
title: 'Release Notes',
path: `${GITHUB_PROJECT_URL}/releases/tag/${tag}`,
status: 'external',
}],
};
// Latest release.
if (i === 0) {
obj.status = 'new';
}
return obj;
});
items.push({
title: 'Old releases',
path: `${GITHUB_PROJECT_URL}/releases`,
status: 'external',
});
const filePath = path.join(PPTR_FOLDER_PATH, '_src', 'api_section.yaml');
await writeFile(filePath, jsYaml.safeDump({toc: items}));
}
/**
* Extracts a devsite START/END region from file.
* @param {string} content
* @param {string} region Name of region to pull out.
* @return {string|null}
*/
function extractRegion(content, region) {
const re = new RegExp(
`<!-- \\[START ${region}\\] -->([\\s\\S]*)<!-- \\[END ${region}\\] -->`);
const m = content.match(re);
return m && m[0] || null;
}
gulp.task('puppeteer:build', async () => {
const result = await wfHelper.promisedExec(
`git ls-remote --tags ${GITHUB_PROJECT_URL}`, '.');
const refs = result.split('\n');
const tags = refs.map((ref) => ref.split('refs/tags/')[1])
.sort(semver.rcompare).slice(0, NUM_RELEASES_TO_INCLUDE);
const latestTag = tags[0];
const tmpDir = path.join(os.tmpdir(), Date.now().toString(), latestTag);
await checkoutRepoAtTag(GIT_URL, latestTag, tmpDir);
// Extract sections from README and create separate .md files for them.
const readme = DOC_FILES.shift();
let content = await readFile(path.join(tmpDir, readme), {encoding: 'utf-8'});
await saveFile('_index/badges.md', extractRegion(content, 'badges'));
await saveFile('_index/usecases.md', extractRegion(content, 'usecases'));
await saveFile('_index/getstarted.md', extractRegion(content, 'getstarted'));
await saveFile('_index/debugging.md', extractRegion(content, 'debugging'));
await saveFile(
'_index/runtimesettings.md', extractRegion(content, 'runtimesettings'));
await saveFile('_index/faq.md', extractRegion(content, 'faq'));
for (const iFile of DOC_FILES) {
let content = await readFile(path.join(tmpDir, iFile), {encoding: 'utf-8'});
await saveFile(iFile, content);
}
await generateTOC(path.join(PPTR_FOLDER_PATH, '_toc.yaml'), tags);
});