Skip to content

Commit

Permalink
feat: complete sanitize-dashboard script
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmcadoo committed Oct 27, 2021
1 parent 5607281 commit 53abc94
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 46 additions & 0 deletions utils/sanitize_dashboards.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
});
}
}
});

0 comments on commit 53abc94

Please sign in to comment.