-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpreprocesscodeblocks.js
173 lines (162 loc) · 6.31 KB
/
preprocesscodeblocks.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const { exec } = require("child_process");
const startMarker = 'docs-marker-start-';
const endMarker = 'docs-marker-end-';
function extractContentFromFile(repository, file, section) {
return new Promise((resolve, reject) => {
const result = [];
const startMarkerSec = `${startMarker}${section}`;
const endMarkerSec = `${endMarker}${section}`;
const repoFolder = `./gitrepos/${repository}/${file}`
try {
fs.readFile(repoFolder, "utf8", (error, data) => {
if (error) {
console.error(error.message);
return;
}
const lines = data.split('\n');
let foundStartMarker = false;
let lineStart = 0;
let lineEnd = 0;
let index = 0;
lines.forEach((line) => {
index++;
if (line.includes(endMarkerSec)) {
foundStartMarker = false;
lineEnd = index - 1;
}
if (foundStartMarker) {
result.push(line.replace(/`/g, '\\`'));
}
if (line.includes(startMarkerSec)) {
foundStartMarker = true;
lineStart = index + 1;
}
});
resolve([result, lineStart, lineEnd]);
});
} catch (error) {
console.error(error);
reject(error);
}
});
}
const getGitUrl = (url) => {
const rawUrl = url.substring(0, url.indexOf(`/blob/`));
return `${rawUrl}.git`;
}
const cloneRepoIfNeeded = (gitUrl, branchOrCommit) => {
return new Promise((resolve, reject) => {
const gitFolder = gitUrl.substring(gitUrl.lastIndexOf('/') + 1, gitUrl.indexOf('.git'));
const destFolder = `./gitrepos/${gitFolder}`;
let command;
if (!fs.existsSync(path.join(destFolder, '.git'))) {
command = `git clone --single-branch --branch ${branchOrCommit} ${gitUrl} ${destFolder}`;
} else {
command = `git -C ${destFolder} fetch && git -C ${destFolder} checkout ${branchOrCommit}`;
}
try {
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${command}`);
console.error(error.message);
return;
}
console.log(stdout);
console.error(stderr);
console.log(command, "successful")
resolve(true);
});
} catch (e) {
console.error(e);
reject(e);
}
});
};
const searchDirectory = async (dirPath, codeBlocks) => {
const files = fs.readdirSync(dirPath);
for (const file of files) {
const filePath = path.join(dirPath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
await searchDirectory(filePath, codeBlocks);
} else {
const extname = path.extname(filePath);
if (extname === '.md' || extname === '.mdx') {
const fileContent = fs.readFileSync(filePath, 'utf8');
const lines = fileContent.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith('```') && line.indexOf(' dynamic ') > 0) {
const lineContent = line.split(' ');
const url = lineContent[2];
const gitUrl = getGitUrl(url);
const urlInfo = extractGitHubUrlInfo(url);
const section = lineContent[3].split('=')[1];
if (urlInfo == null) {
return null
}
const { username, repository, branchOrCommit, file } = urlInfo;
await cloneRepoIfNeeded(gitUrl, branchOrCommit).then(async () => {
console.log("Repo clone/update successful")
const [extractedContent, lineStart, lineEnd] = await extractContentFromFile(repository, file, section);
console.log(`Found code block with url ${url} and section ${section} and content`, extractedContent);
codeBlocks[`${url}---${section}`] = `{\`${extractedContent.join('\n')}\`}`;
codeBlocks[`${url}---${section}-lines`] = `#L${lineStart}-L${lineEnd}`;
});
}
}
}
}
}
};
function extractGitHubUrlInfo(url) {
const regex = /^(?:https?:\/\/)?(?:www\.)?github\.com\/([^/]+)\/([^/]+)\/(?:blob|tree)\/([^/]+)\/(.+)/;
const match = url.match(regex);
if (!match) {
return null
}
const [, username, repository, branchOrCommit, file] = match;
return {
username,
repository,
branchOrCommit,
file
};
}
const codeBlocks = {};
searchDirectory('./docs', codeBlocks).then(() => {
const filePath = './codeblocks/codeblocks.json';
fs.writeFile(filePath, JSON.stringify(codeBlocks), (err) => {
if (err) {
console.error(err);
return;
}
console.log('File saved successfully.');
});
});
async function fetchAndWriteConductorClientVersions() {
await axios.get('https://orkes-api-tester.orkesconductor.com/latestJarVerson?type=orkes-java-client', {})
.then(response => {
// handle the response
console.log("VERSION", response.data);
const filePath = './codeblocks/versions.json';
const versions = {
"conductorJarVersion": response.data
}
fs.writeFile(filePath, JSON.stringify(versions), (err) => {
if (err) {
console.error(err);
return;
}
console.log('File saved successfully.');
});
})
.catch(error => {
// handle the error
console.error(error);
});
}
fetchAndWriteConductorClientVersions();