Files
iconfont-download/单文件拆分/index.ts
2023-09-12 16:21:46 +08:00

43 lines
1.6 KiB
TypeScript

import { resolve, join } from 'path'
import FileUtil from './src/utils/FileUtil'
const siconUrl = resolve('E:/Desktop/ts')
const staticIconUrl = resolve('E:/Desktop/sicon')
const init = async () => {
await FileUtil.readDirectory(siconUrl, async (filepath: string) => {
if (filepath.endsWith('.ts')) {
const filename =
filepath
.substring(filepath.lastIndexOf('\\') + 1)
.split('.')
.shift() || ''
try {
let fstr = await FileUtil.readFile(filepath)
fstr = fstr.split(`export const ${filename}Name`)[0]
fstr = fstr.split('=')[1]
const ctx = JSON.parse(fstr.replaceAll("'", '"'))
const keys = [] as any[]
const allObj = {} as any
Object.keys(ctx).forEach((key) => {
let _res = JSON.stringify(ctx[key])
if (typeof ctx[key] === 'string') {
_res = _res.substring(1, _res.length - 1)
}
key = key.replaceAll(':', '-')
keys.push(key)
allObj[key] = ctx[key]
FileUtil.writeFile(join(staticIconUrl, `./${filename}/${key}.json`), _res)
})
FileUtil.writeFile(join(staticIconUrl, `./${filename}/_keys.json`), JSON.stringify(keys))
FileUtil.writeFile(join(staticIconUrl, `./${filename}/_all.json`), JSON.stringify(allObj))
} catch {
console.log('处理失败', filename)
}
}
})
}
init()