-
Notifications
You must be signed in to change notification settings - Fork 387
/
update-lb4-docs.js
99 lines (87 loc) · 3.49 KB
/
update-lb4-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
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: loopback.io-workflow-scripts
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
const fs = require('fs-extra');
const path = require('path');
const yaml = require('js-yaml');
const assert = require('assert');
const srcDocs = path.resolve(__dirname,'node_modules/@loopback/docs/site');
const destDocs = path.resolve(__dirname, 'pages/en/lb4');
const srcSidebars = path.resolve(srcDocs, 'sidebars');
const destSidebars= path.resolve(__dirname, '_data/sidebars');
const lb4Sidebar = yaml.load(fs.readFileSync(__dirname + '/_data/sidebars/lb4_sidebar.yml', 'utf8'));
const refs = lb4Sidebar.children.find(c => c.title === 'Reference guides');
assert(refs != null, 'Reference guides is not found in lb4_sidebar.yml');
const connectorsReference = refs.children.find(c => c.title === 'Connectors reference');
assert(connectorsReference != null, 'Connectors reference is not found in lb4_sidebar.yml');
/**
* Utility function to remove a directory.
* @param {string} dir - The path of the directory to remove.
*/
function removeDir(dir) {
try {
fs.removeSync(dir);
} catch (err) {
console.error('failed to cleanup %s due to %s',
dir, err.stack);
process.exit(1);
}
}
/**
* Utility function to copy contents of a source directory to
* a target directory. If certain files already exist, it will
* overwrite them.
* @param {string} src - The path of the source directory to copy from.
* @param {string} dest - The path of the target directory to copy to.
*/
function copyDocs(src, dest) {
try {
fs.copySync(src, dest, {overwrite: true});
} catch (err) {
console.error('failed to copy latest docs %s from %s', src, err.stack);
process.exit(1);
}
}
// Remove the original folder so we remove files deleted from @loopback/docs
removeDir(destDocs);
// copy the latest docs from @loopback/docs to pages/en/lb4 directory
copyDocs(srcDocs, destDocs);
//copy over sidebar for LoopBack 4
copyDocs(srcSidebars, destSidebars);
function copyFile(input) {
if (input && input.url) {
const lb3Path = __dirname + '/pages/en/lb3/' + input.url.replace(/\.html$/, '.md');
const lb4Path = __dirname + '/pages/en/lb4/' + input.url.replace(/\.html$/, '.md');
// Copy only if the file does not exist in the lb4 dir
if (!fs.existsSync(lb4Path)) {
// Some MD files use a different casing than HTML
// Such as SOAP-Connector.md vs. SOAP-connector.html
if (!fs.existsSync(lb3Path)) {
console.warn('File not found: %s', lb3Path);
return;
}
let fc = fs.readFileSync(lb3Path, 'utf8');
fc = fc.replace('/lb3/', '/lb4/').replace('lb3_sidebar', 'lb4_sidebar');
fs.writeFileSync(lb4Path, fc);
}
if (input.children) {
input.children.forEach(child => copyFile(child));
}
}
}
// Most of the connector doc files are in the lb3 dir, copy them for lb4
copyFile(connectorsReference);
const fileToUpdate = path.resolve(destDocs, 'Testing-the-API.md');
// bug in `jekyll-relative-links` plugin; probably safe to remove when
// https://github.com/benbalter/jekyll-relative-links/issues/5
// is resolved
if (!fs.existsSync(fileToUpdate)) return;
try {
let contents = fs.readFileSync(fileToUpdate, 'utf-8');
contents = contents.replace('include previous.md', 'include previous.html');
fs.writeFileSync(fileToUpdate, contents, 'utf-8');
} catch (err) {
console.error('failed to replace relative link %s', err.stack);
process.exit(1);
}