node之fs模块 生成views router文件

引入fs模块

mkdir创建文件夹

const fs = require('fs');
fs.mkdir("./img",(err)=>{
    console.log(err);
})

rename重命名文件夹   参数1:原来文件夹名字 参数2:新名字

fs.rename("./img","./icon",(err)=>{

    console.log(err);

})

rmdir删除文件夹


fs.rmdir("./img",(err)=>{
    if(err && err.code=="ENOENT"){
        console.log("文件不存在");
    }
})

unlink 删除文件

  异步执行
fs.unlink("./icon/svg.js",err=>{
    console.log(err);
})

删除文件  同步执行   阻塞后面代码执行
fs.unlinkSync("./icon/svg.js",err=>{
    console.log(err);
})

创建文件 写入   writeFile


fs.writeFile("./icon/svg.js","helloword",err=>{
    console.log(err);
})

追加内容不覆盖  \n表示换行   appendFile


fs.appendFile("./icon/svg.js","\n你是",err=>{
    console.log(err);
})

读取文件  readFile

fs.readFile("./icon/svg.js","utf-8",(err,data)=>{
    console.log(err,data);
})

读目录   readdir

fs.readdir("./icon",(err,data)=>{

    console.log(data);

})

可以根据fs模块封装一个自动生成views和router文件的插件


const fs = require("fs").promises;
const vuecompoent = require("./vue.js");
const router = require("./router.js");

//创建文件
function addfiles(path, type, model) {
  fs.mkdir(`${path}`)
    .then(async (data) => {
      await files(`${path}/index.${type}`, model);
    })
    .catch((err) => {
      // console.log(err, "1111");
    });
}
// 写入文件
function files(path, type, model) {
  fs.writeFile(path, type, model).then((data) => {
    // console.log(data);
  });
}

//删除文件
// function deletefile() {
//   fs.rmdir("./route").then((data) => {
//     console.log(data);
//   });
// }

//删除文件
function deletefiles(path) {
  fs.readdir(`./${path}`).then(async (data) => {
    console.log(data);

    await Promise.all(data.map((item) => fs.unlink(`./${path}/${item}`)));
    await fs.rmdir(`./${path}`);
  });
}
//删除
// deletefiles("store")
// 写入
// addfiles("store",vue)

async function processFiles(path1, path2) {
  try {
    var flieA = null;
    var flieB = null;

    fs.readdir(`./${path1}`).then((res) => {
      console.log(res.length, path1);
      flieA = res.length;
      if (flieA >= 0) {
        deletefiles(path1);
      } 
      
    }).catch((err) =>{
      addfiles(path1,"vue",vuecompoent);

    });

    // 读取第二个目录
    fs.readdir(`./${path2}`).then((res) => {
      console.log(res.length, path2);
      // console.log(res.length);
      flieB = res.length;
    if (flieB >= 0) {

      deletefiles(path2);
    }

    }).catch((err) => {
      addfiles(path2,"js",router);


    });
 
  } catch (error) {
    console.log("先创建文件夹,子文件请继续运行");
      addfiles(path1);
  }
}

processFiles("views", "router", vuecompoent, router);

function readFile(name1, name2) {
  fs.readdir(`./${name1}`, `./${name2}`)
    .then((data) => {
      console.log(data);
    })
    .catch((error) => {
      console.log(111);
    });
}
//读取是否存在目录
function readFile(name1, name2) {
  fs.readdir(`./${name1}`).then((res) => {
    console.log(res);
  });

  // 读取第二个目录
  fs.readdir(`./${name2}`).then((res) => {
    console.log(res);
  });
}
//  readFile("views","router")

vue模版

const vuecompoent = `<template>
<div>
  <h1>{{ message }}</h1>
  <h1>{{ message }}</h1>
  <h1>{{ message }}</h1>
  <h1>{{ message }}</h1>
</div>
</template>

<script>
export default {
data() {
  return {
    message: 'Hello, Vue!'
  };
}
};
</script>

<style scoped>
h1 {
color: green;
}
</style>`;

module.exports =vuecompoent

router模版



const router = `import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/Login.vue"),
  },]
const router = new VueRouter({
  mode: "history",
  routes,
  base: process.env.BASE_URL,
});`;
module.exports =router

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值