This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
forked from ringcentral/slate
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathreplace_code_samples.js
126 lines (107 loc) · 3.28 KB
/
replace_code_samples.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
const fs = require("fs");
const path = require("path");
const YAML = require("yaml");
const markdownFiles = [
// "./source/includes/api-reference/_index.html.md",
"./source/includes/app-components-reference/_index.html.md",
];
const clientLibraryInfo = [
{
lang: "ruby",
docs: "build-client_libs_with_samples/ruby/samples",
},
{
lang: "javascript--nodejs",
docs: "build-client_libs_with_samples/node/samples",
},
{
lang: "javascript",
docs: "build-client_libs_with_samples/node/samples",
},
{
lang: "php",
docs: "build-client_libs_with_samples/php/samples",
},
{
lang: "java",
docs: "build-client_libs_with_samples/java/samples",
},
{
lang: "python",
docs: "build-client_libs_with_samples/python/samples",
},
];
function updateTemplateSectionsInFile(docsFilename) {
console.log(`Reading ${docsFilename}`);
fs.readFile(docsFilename, "utf8", function (err, docsData) {
if (err) {
return console.log(err);
}
console.log(`Read ${docsFilename}`);
let promises = [];
for (let i = 0; i < clientLibraryInfo.length; i++) {
let info = clientLibraryInfo[i];
promises.push(
new Promise((resolve, reject) => {
let p = path.resolve(__dirname, info.docs);
fs.readdir(path.resolve(__dirname, info.docs), function (err, files) {
if (err) {
return console.log("Unable to scan directory: " + err);
}
replaceWithSamples(p, files, info.lang, resolve);
});
})
);
}
Promise.all(promises).then((values) => {
fs.writeFile(docsFilename, docsData, function (err) {
if (err) return console.log(err);
console.log(
"Updated Code Samples from local versions of Client Libraries."
);
});
});
function replaceWithSamples(path, files, lang, callback) {
let promises = [];
let sampleHolder = {};
files.forEach(function (filename) {
promises.push(
new Promise((resolve, reject) => {
let p = `${path}/${filename}`;
fs.readFile(p, "utf8", function (err, data) {
sampleHolder = Object.assign(sampleHolder, YAML.parse(data));
resolve();
});
})
);
});
Promise.all(promises).then((values) => {
for (let tag in sampleHolder) {
if (sampleHolder.hasOwnProperty(tag)) {
for (let operationId in sampleHolder[tag]) {
if (sampleHolder[tag].hasOwnProperty(operationId)) {
replace(sampleHolder, tag, operationId, lang);
}
}
}
}
console.log("Completed replacements");
callback();
});
}
function replace(sampleHolder, tag, operationId, lang) {
const regex = new RegExp(
`\`\`\`${lang}\n!START SAMPLE!${toCamel(operationId)}!END SAMPLE!\n`,
"g"
);
let replacementText = `\`\`\`${lang}\n${sampleHolder[tag][operationId]}`;
docsData = docsData.replace(regex, replacementText);
}
function toCamel(s) {
return s.replace(/([-_][a-z])/gi, ($1) => {
return $1.toUpperCase().replace("-", "").replace("_", "");
});
}
});
}
process.argv.slice(2).forEach(updateTemplateSectionsInFile);