美文网首页
node 基础函数

node 基础函数

作者: leleo | 来源:发表于2019-10-10 11:49 被阅读0次
// 同步读取某个目录下的所有文件
function getDirFile(readFilePath) {
  let saveFilePath = []
  let files = fs.readdirSync(readFilePath);
  //遍历读取到的文件列表
  files.forEach(function (filename) {
    //获取当前文件的绝对路径
    var filedir = path.join(readFilePath, filename);
    //根据文件路径获取文件信息,返回一个fs.Stats对象
    let stats = fs.statSync(filedir)
    var isFile = stats.isFile(); //是文件
    var isDir = stats.isDirectory(); //是文件夹
    if (isFile) {
      saveFilePath.push(filedir)
    }
    if (isDir) {
      getDirFile(filedir); //递归,如果是文件夹,就继续遍历该文件夹下面的文件
    }
  })
  return saveFilePath
}




// 路径是否存在,不存在则创建
function createDirPath(dirPath) {
  fs.mkdir(dirPath, {
    recursive: true
  }, (err) => {
    if (err) throw err;
  });
}



// 路径是否存在
function findDirExists(dirPath) {
  let stat = fs.statSync(dirPath)
  if (stat) {
    return true
  } else {
    return false
  }
}



function copyFileV2(filePath, saveFilePath) {
  // 获取文件名后缀
  let extname = path.extname(filePath)
  // 获取完整路径
  let basename = path.basename(filePath)
  // 如果是js文件
  if (extname == '.js') {
    let newDir = saveFilePath + 'js/'
    let newFilePath = saveFilePath + 'js/' + basename
    // 路径是否存在,不存在则创建
    createDirPath(newDir)
    // 复制文件
    fs.copyFile(filePath, newFilePath, (err) => {
      if (err) throw err
    })
  }
  // 如果是css文件
  if (extname == '.css') {
    let newDir = saveFilePath + 'css/'
    let newFilePath = saveFilePath + 'css/' + basename
    // 路径是否存在,不存在则创建
    createDirPath(newDir)
    // 复制文件
    fs.copyFile(filePath, newFilePath, (err) => {
      if (err) throw err
    })
  }
  // 如果是js文件
  if (extname == '.jpg' || extname == '.png') {
    let newDir = saveFilePath + 'img/'
    let newFilePath = saveFilePath + 'img/' + basename
    // 路径是否存在,不存在则创建
    createDirPath(newDir)
    // 复制文件
    fs.copyFile(filePath, newFilePath, (err) => {
      if (err) throw err
    })
  }
}



// 需要保存的文件夹路径
let saveFilePath = 'D:/template-editor/demo/html/aaa/'
// 解析需要遍历的文件夹路径
let readFilePath = 'D:/template-editor/demo/.nuxt/dist/client/'
//调用文件遍历方法
getDirFile(readFilePath, saveFilePath);

// 读取某个目录下的所有文件
function getDirFile(readFilePath, saveFilePath) {
  fs.readdir(readFilePath, function (err, files) {
    if (err) {
      console.log(err)
    } else {
      //遍历读取到的文件列表
      files.forEach(function (filename) {
        //获取当前文件的绝对路径
        var filedir = path.join(readFilePath, filename);
        //根据文件路径获取文件信息,返回一个fs.Stats对象
        fs.stat(filedir, function (eror, stats) {
          if (eror) {
            console.log('获取文件stats失败');
          } else {
            var isFile = stats.isFile(); //是文件
            var isDir = stats.isDirectory(); //是文件夹
            if (isFile) {
              // console.log(filedir)
              copyFileV2(filedir, saveFilePath)
            }
            if (isDir) {
              getDirFile(filedir); //递归,如果是文件夹,就继续遍历该文件夹下面的文件
            }
          }
        })
      });
    }
  });
}



// 去掉注释
function annotate(data) {
  const annotate = /<!--[\u0000-\uFFFF]*?-->/gim
  data = data.replace(annotate, (m, m1) => {
    return ''
  })
  return data
}



// 拼接html,创建html文件
function getHtml(req) {
  // 拼接HTML文件
  let html = buildHtml(req.body.html)
  // 保存路径
  let url = savePath
  // 创建 /tmp/a/apple 目录,无论是否存在 /tmp 和 /tmp/a 目录。
  fs.mkdir(url, {
    recursive: true
  }, (err) => {
    if (err) throw err;
    // 写入html文件
    fs.writeFileSync(url + '/index.html', html)
  });

  function buildHtml(txt) {
    var header = '<title>demo</title>';
    var body = txt;
    return '<!DOCTYPE html>' +
      '<html><header>' + header + '</header><body>' + body + '</body></html>';
  };
}


// 获取url指定参数
function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
}

相关文章

  • node 基础函数

  • 前端面试题(持续补充)

    js,node.js基础: 闭包 闭包是能够读取其他函数内部变量的函数。在js中,只有函数内部的子函数可以访问内部...

  • Vue学习第一天

    基础知识 node 安装 Node(傻瓜式安装) npm基础 npm 之于 Node.js ,就像 pip 之于 ...

  • node 函数

    函数 functionsay(data){ console.log('我想说'+data); } function...

  • nodejs笔记2(回调函数和事件循环)

    回调函数 Node.js 异步编程的直接体现就是回调。Node 使用了大量的回调函数,Node 所有 API 都支...

  • node函数 node路由

    node函数 js中一个函数可以作为另一个函数的参数,即先定义一个函数,然后传递 匿名函数 这个学过,过 node...

  • 理解jQuery过程

    1.封装函数,命名空间 2.封装函数 node 放在前面,参考jQuery习惯,node.getSiblings(...

  • nodejs

    回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都支持回调函数。 NPM ...

  • 前端Node.js 基础

    一 .Node.js 基础 目录 Node开发概述Node运行环境搭建Node.js快速入门 1. Node开发概...

  • Node学习(7)--函数,全局对象,常用工具

    Node.js 函数 Node.js中函数的使用与Javascript类似,举例来说,你可以这样做: 以上代码中,...

网友评论

      本文标题:node 基础函数

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