import-categories.mjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import glob from 'glob'
  2. import fs from 'fs'
  3. import { resolve, join, basename } from 'path'
  4. import { ICONS_SRC_DIR } from './helpers.mjs'
  5. glob.sync(join(ICONS_SRC_DIR, '*-filled.svg')).forEach(function(file, i) {
  6. const fileOriginal = file.replace(/\-filled.svg$/, '.svg')
  7. if (fs.existsSync(fileOriginal)) {
  8. const dataOriginal = fs.readFileSync(fileOriginal).toString()
  9. const categoryOriginal = dataOriginal.match(/category: ([a-zA-Z-]+)/),
  10. tagsOriginal = dataOriginal.match(/tags: (\[.*?\])/)
  11. if (categoryOriginal || tagsOriginal) {
  12. let data = fs.readFileSync(file).toString()
  13. data = data.replace(/(---[\s\S]+?---)/, function(m, headerContent) {
  14. console.log('categoryOriginal', fileOriginal, categoryOriginal && categoryOriginal[1], tagsOriginal && tagsOriginal[1])
  15. if (categoryOriginal) {
  16. headerContent = headerContent.replace(/category: .*\n/, '')
  17. headerContent = headerContent.replace(/---/, `---\ncategory: ${categoryOriginal[1]}`)
  18. }
  19. if (tagsOriginal) {
  20. headerContent = headerContent.replace(/tags: .*\n/, '')
  21. headerContent = headerContent.replace(/---/, `---\ntags: ${tagsOriginal[1]}`)
  22. }
  23. return headerContent
  24. })
  25. fs.writeFileSync(file, data)
  26. }
  27. }
  28. })