image2codev2.js 1.3 KB

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