-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkbench.js
More file actions
executable file
Β·302 lines (261 loc) Β· 11.5 KB
/
workbench.js
File metadata and controls
executable file
Β·302 lines (261 loc) Β· 11.5 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const simpleGit = require('simple-git');
const unzipper = require('unzipper');
const os = require('os');
async function extractFERepoList(data, data_rel) {
try {
// Fetch the JSON file from the URL
const response_dev = fs.readFileSync("dev.json", "utf8");
const response_rel = fs.readFileSync("rel.json", "utf8");
const data = JSON.parse(response_dev);
const data_rel = JSON.parse(response_rel);
// Parse the JSON content
const pip_gh_pattern = /^((?:git\+)?https?:.+\.git)@[^#]+#egg=([\w-]+)$/;
// Extract modules and transform into the desired format
const repoList = data.modules.reduce((acc, module) => {
const match = module.pip.match(pip_gh_pattern);
// Check if git property exists and starts with github.com
cur_module = {};
if (match) {
// Convert github.com URL to git+https format
cur_module = {
package: match[2],
git: match[1],
};
}
const module_rel = data_rel.modules.find((m) => m.name === module.name);
if (module_rel) {
const versionMatch = module_rel.pip.match(/((?:[\d]+\.?)+)$/);
if (versionMatch) {
cur_module["version"] = versionMatch[0];
}
}
if (Object.keys(cur_module).length > 0) {
acc.push({
[module.name]: cur_module,
});
}
return acc;
}, []);
// Output the result
console.log(JSON.stringify(repoList, null, 2));
return repoList;
} catch (error) {
console.error("Error fetching or processing the data:", error);
return [];
}
}
async function extractFERepoList(data, data_rel) {
try {
// Fetch the JSON file from the URL
const response_dev = fs.readFileSync("dev.json", "utf8");
const response_rel = fs.readFileSync("rel.json", "utf8");
const data = JSON.parse(response_dev);
const data_rel = JSON.parse(response_rel);
ret = {};
// Parse the JSON content
const npm_gh_pattern = /^(@[^@]+)@((?:git\+)?https?:.+)#[^#]+$/;
// Extract modules and transform into the desired format
const repoList = data.modules.reduce((acc, module) => {
const match = module.npm.match(npm_gh_pattern);
// Check if git property exists and starts with github.com
cur_module = {};
if (match) {
// Convert github.com URL to git+https format
cur_module = {
package: match[1],
git: match[2],
};
}
const module_rel = data_rel.modules.find((m) => m.name === module.name);
if (module_rel) {
const versionMatch = module_rel.npm.match(/v?((?:[\d]+\.?)+)$/);
if (versionMatch) {
cur_module["version"] = versionMatch[0];
}
}
if (Object.keys(cur_module).length > 0) {
ret[module.name] = cur_module;
//acc.push({[module.name]:cur_module});
}
return ret;
}, []);
// Output the result
console.log(JSON.stringify({ ret }, null, 2));
return ret;
} catch (error) {
console.error("Error fetching or processing the data:", error);
return [];
}
}
// Function to handle and fix YAML block strings (e.g., | or |+)
function fixYmlBlockStrings(content) {
return content
.replace(/^\|[ \t]*$/gm, '') // Remove unnecessary | at the beginning of lines
.replace(/\|[\r\n]+/g, '\n') // Fix the newlines and keep the content formatted correctly
.replace(/\|[\+\-]*\r?\n/g, '\n'); // Handle case for |+ or |- with additional newline handling
}
// Ensure dist-dkr repo is cloned and on the correct branch
async function ensureDistDkrRepo(branch = 'develop') {
const distDkrRepoPath = path.resolve('.cache/openimis-dist_dkr');
if (!fs.existsSync(distDkrRepoPath)) {
console.log('π Cloning dist-dkr repository...');
await simpleGit().clone('https://github.com/openimis/openimis-dist_dkr.git', distDkrRepoPath);
}
const git = simpleGit(distDkrRepoPath);
const currentBranch = (await git.branch()).current;
if (currentBranch !== branch) {
console.log(`π Checking out branch '${branch}'`);
await git.checkout(branch);
}
git.pull();
return distDkrRepoPath;
}
// Copy files defined in compose.yml from the dist-dkr repo
async function copyDistDkrAssetsFromCompose(composeContent, branch = 'develop') {
// Read and parse the compose content to get the file paths
// Ensure dist-dkr repo is cloned and checked out to the right branch
const distDkrRepoPath = await ensureDistDkrRepo(branch);
const outputFiles = {};
// Ensure we are getting the paths from composeConfig
if (composeContent && composeContent.include && Array.isArray(composeContent.include)) {
for (const item of composeContent.include) {
if (item.path) {
// Resolve path within dist-dkr repo
const filePath = item.path.replace('${DB_DEFAULT:-postgresql}', 'postgresql'); // Resolve dynamic paths
const srcPath = path.join(distDkrRepoPath, filePath); // Full path inside dist-dkr repo
if (fs.existsSync(srcPath)) {
// Read the file content
let fileContent = fs.readFileSync(srcPath, 'utf8');
// Fix YAML block strings if necessary
fileContent = yaml.load(fileContent);
outputFiles[filePath] = fileContent;
console.log(`βοΈ Copied ${item.path} from dist-dkr`);
} else {
console.warn(`β οΈ File ${item.path} not found at path: ${srcPath}`);
}
}
// Handle env files and their .example equivalents
if (item.env_file && Array.isArray(item.env_file)) {
for (const envFile of item.env_file) {
// Process the .env file itself
const envSrcPath = path.join(distDkrRepoPath, envFile + '.example');
if (fs.existsSync(envSrcPath)) {
const envContent = fs.readFileSync(envSrcPath, 'utf8');
outputFiles[envFile] = envContent;
console.log(`βοΈ Copied ${envFile} from dist-dkr`);
} else {
console.warn(`β οΈ File ${envFile} not found at path: ${envSrcPath}`);
}
}
}
}
} else {
console.warn('β οΈ No include array found in compose.yml.');
}
return outputFiles;
}
const getFormattedDate = () => {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0'); // Months are 0-based, so +1
const day = String(now.getDate()).padStart(2, '0');
return `${year}${month}${day}`;
};
async function sendToExternalSolutionRepo(folderName, solutionFolder) {
const repoUrl = 'https://github.com/openimis/solutions.git';
const branchName = `solution/${folderName}/${getFormattedDate()}`;
const tempDir = path.join(__dirname, '.cache/temp_solutions_repo');
// Clean up any previous temp/unzip folder
if (!fs.existsSync(tempDir)){
await git.clone(repoUrl, tempDir);
}
const git = simpleGit();
const gitRepo = simpleGit(tempDir);
await gitRepo.checkout('develop'); // Clone the repo and checkout a new branch
const localBranches = await gitRepo.branchLocal();
if(localBranches.all.includes(branchName)){
await gitRepo.checkout(branchName);
}else{
await gitRepo.checkoutBranch(branchName, 'develop');
}
// Create or overwrite target folder in the cloned repo
const targetFolder = path.join(tempDir,folderName)
if (!fs.existsSync(targetFolder)) fs.mkdirSync(targetFolder);
fs.cpSync(solutionFolder, targetFolder, { recursive: true });
// Commit and push
await gitRepo.add('.');
await gitRepo.commit(`Add/update ${folderName} solution`);
//await gitRepo.push('origin', branchName);
console.log(`βοΈ Pushed ${folderName} to branch ${branchName}`);
}
const { createZip, processSolutions , createSolutionDirectory} = require("./solutionBuilder.js");
async function main() {
try {
const args = process.argv.slice(2);
const shouldPublish = args.includes('--publish');
const shouldDocs = args.includes('--docs');
const folderArg = args.find(arg => arg.startsWith('--folder='));
const solutionName = folderArg ? folderArg.split('=')[1] : 'default-solution';
const solutions = {
'coreMIS': './solution/solutions/coreMIS.json',
// 'SHI': './solution/solutions/HF.json',
// 'claimai': './solution/solutions/HF.json',
//'full' : './solution/solutions/full.json',
// 'SR': './solution/solutions/SR.json',
// 'IBR': './solution/solutions/IBR.json'
}
const permission = fs.readFileSync('./solution/permissions_map.json', 'utf8');
const permissionMap = JSON.parse(permission);
let outputs = {}
for (const [name, solution_path] of Object.entries(solutions)){
console.log(`generating ${name}`)
const { output, modules, assemblyBranch } = await processSolutions(
solution_path,
process.cwd(),
permissionMap,
);
outputs[name] = output;
// Get dist-dkr files from compose.yml and merge them into output
const composeFiles = await copyDistDkrAssetsFromCompose(outputs[name]['compose.yml'], assemblyBranch);
Object.assign(outputs[name], composeFiles);
const zipPath = path.join(__dirname, 'build', name +'.zip');
await createZip(outputs[name], zipPath);
baseDir = 'build/'+name
await createSolutionDirectory(baseDir, outputs[name])
// Generate aggregated Confluence markup only if --publish or --docs is set
if (shouldPublish || shouldDocs) {
console.log(`π Generating aggregated Confluence markup for ${name} using aggregator config...`);
try {
const additionalLabels = modules.join(',');
const { execSync } = require('child_process');
const publishFlag = (shouldPublish || shouldDocs) ? '--publish' : '';
execSync(`node script/generate-confluence-aggregator.js ${name} "${additionalLabels}" ${publishFlag}`, { stdio: 'inherit' });
// Copy the generated markup to build directory
const markupFile = `${name}-aggregated-markup.txt`;
if (fs.existsSync(markupFile)) {
const docsDir = path.join(__dirname, 'build', name, 'docs');
if (!fs.existsSync(docsDir)) fs.mkdirSync(docsDir, { recursive: true });
fs.copyFileSync(markupFile, path.join(docsDir, markupFile));
console.log(`βοΈ Copied ${markupFile} to build/${name}/docs/`);
}
} catch (error) {
console.error(`β Failed to generate aggregated markup for ${name}: ${error.message}`);
}
} else {
console.log('π Skipping KB page update (use --publish or --docs to enable)');
}
// Publish if requested
if (shouldPublish) {
await sendToExternalSolutionRepo(name, baseDir);
} else {
console.log('π Skipping publish step (use --publish to enable)');
}
}
} catch (error) {
console.error('Error:', error);
}
}
main();