美文网首页
读取文件匹配文件

读取文件匹配文件

作者: leleo | 来源:发表于2019-09-29 08:39 被阅读0次
    const https = require('https')
    const path = require('path')
    const fs = require('fs')
    
    
    // 读取字符串里所有的js引用路径
    const readUrl = path.join('D:/enterprise/cn/module/ebg-module1.html')
    
    
    // 获取文件里所有需要复制的文件链接
    const getStrRegObj = {
      jsReg1: /src="?'?(.*?\.js)"?'?/gim,
      jsReg2: /require\(\[\"(.*?\.js)"?'?/gim,
      cssReg: /href="?'?(.*?\.css)"?'?/gim,
      cssReg2: /css!(.*?\.css)"?'?/gim,
      pngReg: /src="?'?([^=].[^=]*[^=]?[^=]\.png)"?'?/gim,
      pngReg2: /data-pc-l="?'?([^=].[^=]*[^=]?[^=]\.png)"?'?/gim,
      pngReg3: /data-pc-s="?'?([^=].[^=]*[^=]?[^=]\.png)"?'?/gim,
      pngReg4: /data-ipad-h="?'?([^=].[^=]*[^=]?[^=]\.png)"?'?/gim,
      pngReg5: /data-ipad-v="?'?([^=].[^=]*[^=]?[^=]\.png)"?'?/gim,
      pngReg6: /data-wap="?'?([^=].[^=]*[^=]?[^=]\.png)"?'?/gim,
      jpgReg: /src="?'?([^=].[^=]*[^=]?[^=]\.jpg)"?'?/gim,
      jpgReg2: /data-pc-l="?'?([^=].[^=]*[^=]?[^=]\.jpg)"?'?/gim,
      jpgReg3: /data-pc-s="?'?([^=].[^=]*[^=]?[^=]\.jpg)"?'?/gim,
      jpgReg4: /data-ipad-h="?'?([^=].[^=]*[^=]?[^=]\.jpg)"?'?/gim,
      jpgReg5: /data-ipad-v="?'?([^=].[^=]*[^=]?[^=]\.jpg)"?'?/gim,
      jpgReg6: /data-wap="?'?([^=].[^=]*[^=]?[^=]\.jpg)"?'?/gim
    }
    let newArray = getStrUrl(readUrl, getStrRegObj)
    // 复制文件
    copyFile(newArray)
    
    // 正则替换文件里的指定内容并保存文件
    const replaceStrRegObj = {
      js1: /(src="?'?)(.*?\.js)/gim,
      js2: /(require\(\["?'?)(.*?\.js)/gim,
      css: /(href=')(.*?\.css)/gim,
      css2: /("css!)(.*?\.css)/gim,
      jpg1: /(src=")(.*?\.jpg)/gim,
      jpg2: /(data-pc-l=")(.*?\.jpg)/gim,
      jpg3: /(data-pc-s=")(.*?\.jpg)/gim,
      jpg4: /(data-ipad-h=")(.*?\.jpg)/gim,
      jpg5: /(data-ipad-v=")(.*?\.jpg)/gim,
      jpg6: /(data-wap=")(.*?\.jpg)/gim,
      png1: /(src=")(.*?\.png)/gim,
      png2: /(data-pc-l=")(.*?\.png)/gim,
      png3: /(data-pc-s=")(.*?\.png)/gim,
      png4: /(data-ipad-h=")(.*?\.png)/gim,
      png5: /(data-ipad-v=")(.*?\.png)/gim,
      png6: /(data-wap=")(.*?\.png)/gim
    }
    let newData = replaceStr(readUrl, replaceStrRegObj)
    
    // 将数据追加到文件
    fs.writeFileSync(__dirname + '/moddle.html', newData, 'utf8')
    
    
    // 获取匹配到的文件
    function getStrUrl(readUrl, regObj) {
      // 读取文件
      let data = fs.readFileSync(readUrl, 'utf8')
      let newArray = []
      for (let key in regObj) {
        if (regObj.hasOwnProperty(key)) {
          const newKey = regObj[key]
          // 获取数据
          let jsContext = ''
          while (jsContext = newKey.exec(data)) {
            // 获取所有文件
            newArray.push(jsContext[1])
          }
        }
      }
      // 数组去重并把类数组转换成数组
      newArray = Array.from(new Set(newArray))
      return newArray
    }
    
    // 复制文件到指定目录
    function copyFile(readFileAddress) {
      for (let i = 0; i < readFileAddress.length; i++) {
        // 拼接需要读取的url
        const url = path.join('D:\\enterprise' + readFileAddress[i])
        // 获取文件类型,后缀名
        let extname = path.extname(readFileAddress[i])
        let basename = path.basename(readFileAddress[i])
        let storageUrl = ''
        // 保存路径
        if (extname == '.js') {
          storageUrl = path.join(__dirname + '/js/' + basename)
          copyFile(url, storageUrl)
        }
        if (extname == '.css') {
          storageUrl = path.join(__dirname + '/requireCss/' + basename)
          copyFile(url, storageUrl)
        }
        if (extname == '.jpg' || extname == '.png') {
          storageUrl = path.join(__dirname + '/img/' + basename)
          if (readFileAddress[i].indexOf('e-file') != -1 || readFileAddress[i].indexOf('e.huawei') != -1) {
            getHttpf(readFileAddress[i], storageUrl)
          } else {
            copyFile(url, storageUrl)
          }
        }
    
        function getHttpf(url, storageUrl) {
          let streamImg = ''
          if (url.indexOf('http') == -1) {
            streamImg = 'https:' + url
          } else if (url.indexOf('http') != -1 && url.indexOf('https') == -1) {
            streamImg = url.replace(/http/gim, (m, m1) => {
              return 'https'
            })
          } else {
            streamImg = url
          }
    
          //保存图片
          https.get(streamImg, function (req, res) {
            var imgData = '';
            req.on('data', function (chunk) {
              imgData += chunk;
            })
            req.setEncoding('binary');
            req.on('end', function () {
              fs.writeFile(storageUrl, imgData, 'binary', function (err) {})
            })
          })
        }
    
        function copyFile(url, storageUrl) {
          fs.stat(url, (err, stats) => {
            // 判断文件是否存在
            if (stats) {
              // 复制文件
              fs.copyFile(url, storageUrl, (err) => {
                if (err) throw err
              })
            } else {
              return
            }
          })
        }
      }
    }
    
    // 正则替换文件里的指定内容
    function replaceStr(readUrl, regObj) {
      let getData = fs.readFileSync(readUrl, 'utf8')
      // 去掉注释
      const annotate = /<!--[\u0000-\uFFFF]*?-->/gim
      getData = getData.replace(annotate, (m, m1) => {
        return ''
      })
      for (let key in regObj) {
        if (regObj.hasOwnProperty(key)) {
          const newKey = regObj[key]
          let newData = getData.replace(newKey, (m, m1, m2) => {
            let basename = path.basename(m1)
            let basename2 = path.basename(m2)
            let extname = path.extname(m2)
            if (extname == '.js') {
              let pathname = basename + 'js/' + basename2
              return pathname
            }
            if (extname == '.css') {
              let pathname = basename + 'requireCss/' + basename2
              console.log(pathname)
              return pathname
            }
            if (extname == '.jpg' || extname == '.png') {
              let pathname = basename + 'img/' + basename2
              return pathname
            }
          })
          getData = newData
        }
      }
      return getData
    }
    

    相关文章

      网友评论

          本文标题:读取文件匹配文件

          本文链接:https://www.haomeiwen.com/subject/yqgxpctx.html