format-sitemap.js 909 B

123456789101112131415161718192021222324252627
  1. /* eslint-disable no-console */
  2. /* eslint-disable import/no-extraneous-dependencies */
  3. const fs = require('fs');
  4. const replace = require('replace-in-file');
  5. const dirCont = fs.readdirSync('././public');
  6. // 1. scan the public directory for any sitemap xml files (can be multiple if they were split due to size).
  7. const sitemapFiles = dirCont
  8. .filter((elm) => {
  9. return elm.match(/.*sitemap.*.xml/gi);
  10. })
  11. .map((file) => `././public/${file}`);
  12. // 2. replace any character starting from -remove-from-here until the first " character which is the end of the alternate link
  13. replace({
  14. files: sitemapFiles,
  15. // Replacement to make (string or regex)
  16. from: /-remove-from-here.*?"/g,
  17. to: '"',
  18. })
  19. .then((changedFiles) => {
  20. console.log('Modified files:', changedFiles.map((changedFile) => changedFile.file).join(', '));
  21. })
  22. .catch((error) => {
  23. console.error('Error occurred:', error);
  24. });