Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Feb 24, 2025. It is now read-only.
forked from ringcentral/slate
- Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathreplace_code_samples.js
More file actions
Latest commit
126 lines (107 loc) · 3.28 KB
/
Copy pathreplace_code_samples.js
File metadata and controls
126 lines (107 loc) · 3.28 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
constfs=require("fs");
constpath=require("path");
constYAML=require("yaml");
constmarkdownFiles=[
// "./source/includes/api-reference/_index.html.md",
"./source/includes/app-components-reference/_index.html.md",
];
constclientLibraryInfo=[
{
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",
},
];
functionupdateTemplateSectionsInFile(docsFilename){
console.log(`Reading ${docsFilename}`);
fs.readFile(docsFilename,"utf8",function(err,docsData){
if(err){
returnconsole.log(err);
}
console.log(`Read ${docsFilename}`);
letpromises=[];
for(leti=0;i<clientLibraryInfo.length;i++){
letinfo=clientLibraryInfo[i];
promises.push(
newPromise((resolve,reject)=>{
letp=path.resolve(__dirname,info.docs);
fs.readdir(path.resolve(__dirname,info.docs),function(err,files){
if(err){
returnconsole.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)returnconsole.log(err);
console.log(
"Updated Code Samples from local versions of Client Libraries."
);
});
});
functionreplaceWithSamples(path,files,lang,callback){
letpromises=[];
letsampleHolder={};
files.forEach(function(filename){
promises.push(
newPromise((resolve,reject)=>{
letp=`${path}/${filename}`;
fs.readFile(p,"utf8",function(err,data){
sampleHolder=Object.assign(sampleHolder,YAML.parse(data));
resolve();
});
})
);
});
Promise.all(promises).then((values)=>{
for(lettaginsampleHolder){
if(sampleHolder.hasOwnProperty(tag)){
for(letoperationIdinsampleHolder[tag]){
if(sampleHolder[tag].hasOwnProperty(operationId)){
replace(sampleHolder,tag,operationId,lang);
}
}
}
}
console.log("Completed replacements");
callback();
});
}
functionreplace(sampleHolder,tag,operationId,lang){
constregex=newRegExp(
`\`\`\`${lang}\n!START SAMPLE!${toCamel(operationId)}!END SAMPLE!\n`,
"g"
);
letreplacementText=`\`\`\`${lang}\n${sampleHolder[tag][operationId]}`;
docsData=docsData.replace(regex,replacementText);
}
functiontoCamel(s){
returns.replace(/([-_][a-z])/gi,($1)=>{
return$1.toUpperCase().replace("-","").replace("_","");
});
}
});
}
process.argv.slice(2).forEach(updateTemplateSectionsInFile);