From 53abc94b3b49e227399dca76fa196575743ca722 Mon Sep 17 00:00:00 2001 From: Clark McAdoo Date: Wed, 27 Oct 2021 17:47:30 -0500 Subject: [PATCH] feat: complete sanitize-dashboard script --- utils/package.json | 3 ++- utils/sanitize_dashboards.js | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 utils/sanitize_dashboards.js diff --git a/utils/package.json b/utils/package.json index aeb694f42a..71fd64bed8 100644 --- a/utils/package.json +++ b/utils/package.json @@ -11,7 +11,8 @@ "validate-images": "node validate_images.js", "format": "prettier --write \"./**/*.js\"", "validate-icons-and-logos": "node validate_icons_and_logos.js", - "generate-uuids": "node generate-uuids.js" + "generate-uuids": "node generate-uuids.js", + "sanitize-dashboard": "node sanitize_dashboards.js" }, "devDependencies": { "@actions/core": "^1.4.0", diff --git a/utils/sanitize_dashboards.js b/utils/sanitize_dashboards.js new file mode 100644 index 0000000000..2c5dc5674d --- /dev/null +++ b/utils/sanitize_dashboards.js @@ -0,0 +1,46 @@ +const fs = require('fs'); +const { readdir } = require('fs').promises; +// Removes the first two arguments passed by `node`, these aren't needed for this function to run - CM +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 \nExample: 'yarn sanitize-dashboard node-js/express'" + ); + +const sanitizeDashboard = (fileContent) => { + return fileContent + .replace(/\"accountId\"\s*:\s*\d+\s*/g, '"accountId": 0') // Anonymize account ID + .replace(/^.+\"linkedEntityGuids\".+\n?/gm, '') // Remove linkedEntityGuids + .replace(/^.+\"permissions\".+\n?/gm, '') // Remove permissions + .replace(/[\}\)\]]\,(?!\s*?[\{\[\"\'\w])/g, '}'); // Remove trailing commas if any left after json blocks +}; + +pathsToSanitize.forEach(async (i) => { + const directory = `../quickstarts/${i}/dashboards/`; + const files = await readdir(directory, { withFileTypes: true }); + const jsonFiles = files.filter((file) => file.name.includes('.json')); + + if (jsonFiles.length < 1) { + console.log( + 'No .json files exist within this directory, skipping sanitization...' + ); + } + + for (let file of jsonFiles) { + const filePath = directory + file.name; + const quickStart = fs.readFileSync(filePath, { encoding: 'utf8' }); + const cleanFile = sanitizeDashboard(quickStart); + + if (cleanFile !== file) { + fs.writeFile(filePath, cleanFile, { encoding: 'utf8' }, function (err) { + if (err) { + console.error(`Error updating ${filePath}`); + process.exit(1); + } else { + console.log(`=> Sanitized ${filePath}`); + } + }); + } + } +});