forked from newrelic/newrelic-quickstarts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitize_dashboards.ts
63 lines (52 loc) · 1.93 KB
/
sanitize_dashboards.ts
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
import * as fs from 'fs';
import * as path from 'path';
import * as glob from 'glob';
const sanitizeRawContent = (rawConfig: string): string => {
return rawConfig
.replace(/\"accountId\"\s*:\s*\d+/g, '"accountId": 0') // Anonymize account ID
.replace(/\"accountIds\"\s*:\s\[[.\s\S]*?\]/g, '"accountIds": []')
.replace(
/\"linkedEntityGuids\"\s*:\s*\[([\t\n\r]*)?.*([\t\n\r]*)?\s*\]/g,
'"linkedEntityGuids": null'
) // Set linkedEntityGuids to null
.replace(/^.+\"permissions\".+\n?/gm, '') // Remove permissions
.replace(/[\}\]]\,(?!\s*?[\{\[\"\'\w])/g, '}'); // Remove trailing commas if any left after json blocks
};
const sanitizeDashboards = (pathsToSanitize: string[]) => {
pathsToSanitize.forEach((dashboardId) => {
const globPaths: string[] = glob.sync(
path.resolve('..', 'dashboards', dashboardId, '*.json')
);
if (globPaths.length !== 1) {
console.log(
'No dashboard or .json files exist within this directory, skipping sanitization...'
);
return;
}
const filePath: string = globPaths.pop()!;
const dashboardRawContent: string = fs.readFileSync(filePath, {encoding: 'utf8' });
const sanitizedRawContent: string = sanitizeRawContent(dashboardRawContent);
if (dashboardRawContent !== sanitizedRawContent) {
fs.writeFile(filePath, sanitizedRawContent, { encoding: 'utf8' }, function (err) {
if (err) {
console.error(`Error updating ${filePath}`);
process.exit(1);
} else {
console.log(`=> Sanitized ${filePath}`);
}
});
}
});
}
const main = () => {
const pathsToSanitize = process.argv.slice(2);
if (pathsToSanitize.length < 1)
console.log(
'You must pass a file path as an argument for the sanitize function to run.:',
"Example: 'yarn sanitize-dashboard node-js/express'"
);
sanitizeDashboards(pathsToSanitize);
}
if (require.main === module) {
main();
}