-
Notifications
You must be signed in to change notification settings - Fork 4
/
vpDocs.cjs
100 lines (82 loc) · 2.92 KB
/
vpDocs.cjs
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
const path = require('path');
const fs = require('fs-extra');
const simpleGit = require('simple-git');
const git = simpleGit();
const config = {
tempDir: path.join(process.cwd(), '.temp-repos'),
targetBasePath: path.join(process.cwd(), 'site', 'v3-docs'),
repos: [
{
url: 'https://github.com/lightning-js/blits',
branch: 'master',
sourceDir: 'docs',
targetDir: 'blits',
landing: 'getting_started/intro',
name: 'Blits',
ignoreFiles: ['README.md', '_sidebar.md', '.nojekyll', 'index.html'],
}
]
}
function prefixLinks(prefix, array) {
array.forEach((item) => {
if(item.items) {
item.items = prefixLinks(prefix, item.items);
}
if(item.link) {
item.link = prefix + item.link;
}
})
return array;
}
async function getRemoteDocs(repo) {
const clonedRepoPath = path.join(config.tempDir, path.basename(repo.url, '.git'));
const sourcePath = path.join(clonedRepoPath, repo.sourceDir);
const targetPath = path.join(config.targetBasePath, repo.targetDir);
try {
console.info('Remove existing docs');
await fs.remove(targetPath);
await fs.remove(clonedRepoPath);
console.info('Cloning repo');
await git
.clone(repo.url, clonedRepoPath)
.cwd(clonedRepoPath)
.checkout(repo.branch);
const prefix = `/${path.join('v3-docs', repo.targetDir)}`;
let sidebar = fs.readFileSync(path.join(sourcePath, 'sidebar.json'));
sidebar = prefixLinks(prefix, JSON.parse(sidebar));
console.info('Move docs to allocated folder, and remove unneeded file / directories')
await fs.move(sourcePath, targetPath);
await Promise.all(repo.ignoreFiles.map(file => fs.remove(path.join(targetPath, file))));
await fs.remove(clonedRepoPath);
console.info('Finished getting docs from : ' + repo.url);
return {
prefix,
sidebar
}
} catch (e) {
console.error('An exception occurred while getting docs from :' + repo.url);
console.error(e);
throw ('repoCloningFailed');
}
}
async function buildDocs() {
try {
console.info('Starting building the documentation');
const docs = await Promise.all(config.repos.map(repo => getRemoteDocs(repo)));
const sidebar = {};
docs.forEach((doc) => {
sidebar[`${doc.prefix}`] = doc.sidebar;
});
const p = path.join(process.cwd(), 'site', '.vitepress', 'sidebars.json');
await fs.writeFile(p, JSON.stringify(sidebar));
} catch (e) {
console.error(e);
throw('documentBuildFailed');
}
}
buildDocs().then(() => {
console.log('The documentation is ready.')
}, (err) => {
console.error(err);
console.error('Building the documentation failed.');
});