UNPKG

bixi

Version:

企业级中后台前端解决方案

66 lines (55 loc) 1.89 kB
const fs = require('fs'); const fse = require('fs-extra'); const { PATH } = require('../util'); const excluded = ['.DS_Store']; // const { scanAntdIcons } = require('./scan-antd'); function scanIconsFolder() { // const antdIcons = scanAntdIcons(); const iconsPath = `${PATH.assets}/icons/bixi`; const iconsGenPath = `${PATH.assets}/icon-data.json`; const ret = {}; // type fs.readdirSync(iconsPath).forEach(type => { ret[type] = {}; // category fs.readdirSync(`${iconsPath}/${type}`).forEach(cate => { if (excluded.includes(cate)) return; ret[type][cate] = []; fs.readdirSync(`${iconsPath}/${type}/${cate}`).forEach(icon => { const source = `${iconsPath}/${type}/${cate}/${icon}`; const target = `${iconsPath}/${type}/${cate}/${formatIconName(icon)}`; // 重命名 if (source !== target) { console.log(`重命名 icon: ${icon} --> ${formatIconName(icon)}`) fse.moveSync(source, target) }; if (!excluded.includes(icon)) { ret[type][cate].push(formatIconName(icon)); } }); }); }); const jsonRet = { data: ret }; fse.writeFileSync(iconsGenPath, JSON.stringify(jsonRet, null, 2)); } /** * 格式化图标名称: * 去除中间空格 * 去除末尾空格+数字 * @param { string } origin */ function formatIconName(origin) { if (/^[a-z][a-z0-9]*((-[a-z0-9]+)*|[a-z0-9]*)$/.test(origin)) return origin; return origin .replace(/\s\d+(.svg)$/, '.svg') // 去除 iconfont 的末尾空格与数字 .replace(/(-[A-Z])/g, i => i.toLowerCase()) // 匹配 foo-Bar --> foo-bar .replace(/([A-Z][a-z])/g, i => '-' + i.toLowerCase()) // 匹配 FooBar --> -foo-bar .replace(/^-/, '') // 匹配 -foo-bar --> foo-bar .toLowerCase(); // 匹配 FB --> fb } module.exports = { scanIconsFolder }; scanIconsFolder();