image2code.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. const fs = require('fs')
  2. const path = require('path')
  3. // 指法文件夹位置
  4. const filesDir = path.join(__dirname, './fingering')
  5. // const filesDir = path.join(__dirname, './pages/detail')
  6. // const filesDir = path.join(__dirname, './pages/detail/tips/')
  7. // 需要处理的文件后缀
  8. const suffixs = ['png', 'svg']
  9. const dirs = fs.readdirSync(path.resolve(filesDir))
  10. ;(async function() {
  11. for (const dir of dirs) {
  12. const exportJson = {}
  13. const dirFullPath = path.join(filesDir, dir)
  14. // const dirFullPath = path.join(filesDir, './icons')
  15. fs.stat(dirFullPath, (err, stat) => {
  16. if (!err && stat.isDirectory()) {
  17. const files = fs.readdirSync(dirFullPath).filter(file => suffixs.includes(file.split('.').pop()))
  18. for (const file of files) {
  19. const fileNames = file.split('.')
  20. const fileBuffer = fs.readFileSync(path.join(dirFullPath, file))
  21. const fileType = fileNames[1] === 'svg' ? 'svg+xml' : fileNames[1]
  22. const str = `data:image/${fileType};base64,` + Buffer.from(fileBuffer, 'binary').toString('base64')
  23. exportJson[fileNames[0]] = str
  24. }
  25. fs.writeFileSync(path.join(dirFullPath, 'index.json'), JSON.stringify(exportJson, null, 2))
  26. }
  27. })
  28. }
  29. })()