12345678910111213141516171819202122232425262728293031323334353637383940 |
- const fs = require('fs')
- const path = require('path')
- // 指法文件夹位置
- // const filesDir = path.join(__dirname, './fingering')
- // const filesDir = path.join(__dirname, './pages/detail')
- const filesDir = path.join(__dirname, './images')
- console.log("🚀 ~ filesDir:", filesDir, path.join(filesDir, 'index.json'))
- // 需要处理的文件后缀
- const suffixs = ['png', 'svg']
- const files = fs.readdirSync(path.resolve(filesDir))
- // console.log("🚀 ~ files:", files)
- ;(async function() {
- let i = 0
- const exportJson = {}
- for (const file of files) {
- const suffix = file.slice(file.lastIndexOf('.') + 1)
- // console.log("🚀 ~ suffix:", suffix)
- if (!suffixs.includes(suffix)) continue;
- const dirFullPath = path.join(filesDir, file)
- // console.log("🚀 ~ dirFullPath:", dirFullPath)
- fs.stat(dirFullPath, (err, stat) => {
- if (!err && !stat.isDirectory()) {
- const fileNames = file.split('.')
- const fileBuffer = fs.readFileSync(dirFullPath)
- console.log("🚀 ~ fileBuffer:", fileNames[0])
- const fileType = suffix === 'svg' ? 'svg+xml' : suffix
- const str = `data:image/${fileType};base64,` + Buffer.from(fileBuffer, 'binary').toString('base64')
- exportJson[fileNames[0]] = str
-
- fs.writeFileSync(path.join(filesDir, 'index.json'), JSON.stringify(exportJson, null, 2))
- }
- })
- // if (i === 0) break;
- }
-
- })()
|