-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_gitops.js
74 lines (59 loc) · 2.47 KB
/
change_gitops.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
const fs = require('fs').promises; // Using promises for better async handling
const yaml = require('js-yaml');
const updateOrgManifestContent = (content) => {
if (!content.spec.syncPolicy.automated) return false;
delete content.spec.syncPolicy.automated;
content.spec.syncPolicy.syncOptions = content.spec.syncPolicy.syncOptions.filter(option => option != 'Retry=true');
delete content.spec.syncPolicy.retry;
return true;
};
const updateOrgRequirementsContent = (content) => {
if (!content.spec.syncPolicy.syncOptions) return false;
delete content.spec.syncPolicy.syncOptions;
delete content.spec.syncPolicy.retry;
return true;
};
const updateIntegrationManifestContent = (content) => {
if (!content.spec.syncPolicy) return false;
delete content.spec.syncPolicy;
return true;
};
const updateFileContent = async (filePath) => {
try {
const fileContent = await fs.readFile(filePath, 'utf8');
const content = yaml.load(fileContent);
let isChangeRequired;
// Update content based on the filename
if (filePath.endsWith('org-manifest.yml')) {
isChangeRequired = updateOrgManifestContent(content);
} else if (filePath.endsWith('org-requirements.yml')) {
isChangeRequired = updateOrgRequirementsContent(content);
} else {
isChangeRequired = updateIntegrationManifestContent(content);
}
if (!isChangeRequired) return;
// Write the modified content back to the file
const newContent = yaml.dump(content);
await fs.writeFile(filePath, newContent, 'utf8');
console.log(`Updated: ${filePath}`);
} catch (err) {
console.error(`Error processing file ${filePath}: ${err}`);
}
};
const iterateFiles = async (path = process.cwd()) => {
try {
const files = await fs.readdir(path, { withFileTypes: true });
await Promise.all(files.map(async (file) => {
const fullPath = `${path}/${file.name}`;
if (file.isDirectory()) {
await iterateFiles(fullPath); // Recursively process directories
} else if (file.name.endsWith('.yml')) {
await updateFileContent(fullPath); // Process YAML files
}
}));
} catch (err) {
console.error(`Error reading directory: ${err}`);
}
};
const startPath = process.argv[2] || process.cwd();
iterateFiles(startPath).catch(err => console.error(`Failed to process files: ${err}`));