Miconfont SVGアイコンライブラリreact-native-svgを生成するために必要なフォーマット

2252 ワード

const fs = require('fs');
const path = require('path');

const filePath = `${path.resolve('fontsvg')}/build`
// 
const isDirectory = (filePath) =>{
  return new Promise((resolve,reject) => {
    fs.access(filePath, (err) => {
      if(err){
        resolve('faild')
      }
      resolve('ok')
    })
  })
}

// 
const creactMkdir = (filePath) =>{
  return new Promise((resolve,reject) => {
    fs.mkdir(filePath, (err) => {
      if(err){return}
      resolve(true)
    })
  })
}

const writeSvg = (filePath) => {
  console.log(' ...')
  fs.readFile(`${path.resolve('fontsvg')}/iconfont.js`, 'utf8', (err, data) => {
    if (err) {
    } else {
      const svgArr = data.split('');
      svgArr.pop();
      let svgTmp = '';
      let viewBox = '';
      svgArr.forEach((svg) => {
        const tmpPath = svg.split(' x.slice(1).toUpperCase());
        const viewBoxValue = tmpPath[0].match('viewBox=.*"')[0].split('"')[1];
        if (viewBoxValue !== '0 0 1024 1024') {
          viewBox[viewBoxKey] = viewBoxValue;
          viewBox += `    ${viewBoxKey}: '${viewBoxValue}',
`; } const pathArr = []; tmpPath.shift(); tmpPath.forEach((path) => { pathArr.push(`{${path.replace(/=/g, ': ').replace('fill', ', fill').replace('>
', '')}}`); }); svgTmp += ` ${viewBoxKey}: [${pathArr}],
`; }); const svgStr = `export default {
${svgTmp}
}`; fs.writeFile(`${filePath}/iconfont.svg.js`, svgStr, 'utf8',() => { if (err) return console.log(' ') }); } }) } async function WriteFontSvg () { const directory = await isDirectory(filePath) if(directory == 'ok'){ writeSvg(filePath) }else{ const creact= await creactMkdir(filePath) if(creact){ console.log(' ') writeSvg(filePath) } } } WriteFontSvg()