SunXinFei / sunxinfei.github.io

前后端技术相关笔记,已迁移到 Issues 中
https://github.com/SunXinFei/sunxinfei.github.io/issues
32 stars 3 forks source link

仿飞冰项目中的总结 #12

Open SunXinFei opened 5 years ago

SunXinFei commented 5 years ago

Node创建/删除文件夹

原生的创建文件夹方法不支持自动创建,需要封装方法递归创建

/**
 * 递归创建文件夹
 * @param {String} dirpath 创建的文件夹路径
*/
export const mkdirs = (dirpath) => {
  if (!fs.existsSync(path.dirname(dirpath))) {
    mkdirs(path.dirname(dirpath));
  }
  fs.mkdirSync(dirpath);
}

/**
 * 递归删除文件
 * @param {String} dirpath 删除的文件夹路径
*/
export const deleteall = (path) => {
  var files = [];
  if (fs.existsSync(path)) {
    files = fs.readdirSync(path);
    files.forEach(function (file, index) {
      var curPath = path + "/" + file;
      if (fs.statSync(curPath).isDirectory()) { // recurse
        deleteall(curPath);
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
}
//调用
fs.existsSync(realPath) == false && mkdirs(realPath);
SunXinFei commented 5 years ago

下载github中的某个指定文件夹

git没有特别好的方法下载指定文件夹,而svn可以,但是windows会涉及到svn的安装,所以下载并解压是兼容不同系统的比较好的方式 注意:将trunk替换之前的路径即可 svn export https://github.com/SunXinFei/vue-webpack-demo/**trunk**/src/pages

SunXinFei commented 5 years ago

Node 下载并解压压缩包

这里解压是使用的tar的命令或npm包,原因是unzip或者adm-zip之类的虽然也支持解压,但是不支持忽略n层文件夹,也就是说想解压结果不包含最外层文件夹或者aaa/src下的所有文件的话,tar是非常合适的。 tar 压缩命令

tar -cvf  MyCard.tar   ./vue-webpack-demo/src/pages/Home/components/MyCard/
//开始loading
//使用文件流下载模版项目
            const templateZipSrc =
              "https://iivbox.github.io/iivbox/preview/open-source/static/zip/vue-webpack-demo.tar"; //获取图片的url
            const zipFileName = "vue-webpack-demo.tar";
            //采用request模块,向服务器发起一次请求,获取资源
            request.head(templateZipSrc, function(err, res, body) {
              if (err) {
                this.$Notice.error({
                  title: "error",
                  desc: err
                });
                console.log(err);
              }
            });
            //创建writeSteam
            const writeSteam = fs.createWriteStream(
              `${realPath}/${zipFileName}`,
              { autoClose: true }
            );
            //下载资源并写文件流
            request(templateZipSrc)
              .pipe(writeSteam)
              .on("finish", () => {
                //创建读取文件流
                fs.createReadStream(`${realPath}/${zipFileName}`)
                  .pipe(
                    tar.x({
                      strip: 2, //去掉两层目录
                      C: `${realPath}`
                    })
                  )
                  .on("finish", () => {
                    //删除压缩包,windows没有rm指令
                    //shell.exec(
                    //`rm ${realPath}/${zipFileName}`,
                    //  (code, stdout, stderr) => {
                    //    console.log(code, stdout, stderr);
                    //  }
                    // );
                    //使用node删除压缩包,兼容系统   
                    fs.unlink(`${realPath}/${zipFileName}`,(error)=>{
                        if(error){
                            console.log(error);
                            return false;
                        }
                        console.log('删除文件成功');
                    })
                    //路由跳转,业务操作,结束loading
                  });
              });
SunXinFei commented 4 years ago

1.依赖管理页面的version取自node_modules对应的文件夹下的package.json的文件的version 2.页面列表中添加页面会修改menuConfig.js、routerConfig.js、以及创建页面文件夹和组件