标签:读取文件 options win 监听 需要 require src 权限 mode
const fs = require("fs"); const removeDir = (dir)=>{ let files = fs.readdirSync(dir);//一个数组 files.forEach((item) => { let file = dir +"/" + item; if(fs.statSync(file).isFile()){ fs.unlinkSync(file); }else{ removeDir(file); } }); fs.rmdirSync(dir); }; removeDir("./project");
const fs = require("fs"); const config = { name: "blog", data:[ { name: "css", type: "dir" }, { name: "js", type: "dir" }, { name: "images", type: "dir" }, { name: "index.html", type: "file", content : ‘<!DOCTYPE html>\n<html lang="en">\n<head>\n<meta charset="UTF-8">\n<title>Title</title>\n</head>\n <body>\n</body>\n</html>‘ } ] }; //根据配置,自动生成项目目录 if(config.name){ //第一步创建根目录 fs.mkdir(config.name,(err) => { config.data.forEach((item) => { //拼接路径 let file = config.name + "/" +item.name; switch(item.type){ case "dir": fs.mkdirSync(file); break; case "file": fs.writeFileSync(file,item.content,"utf-8"); break; default: break; } }); }); }
const fs = require(‘fs‘); const dir = "./blog/js/src"; const subject = ‘./blog/js/dest/app.js‘; //监听dir这个目录 fs.watch(dir,(evt, filename) =>{ let data = "" ; //保存读取的内容 //遍历dir目录 fs.readdir(dir, (err,files) => { if (err) throw err; //得到的files是一个数组,遍历这个数组 files.forEach( (item) => { //需要拼接,得到文件的完整的路径 let file = dir + "/" + item; //读取文件 data += fs.readFileSync(file,‘utf8‘); }); //遍历完毕,就可以将data写入目标文件 fs.writeFileSync(subject,data,‘utf8‘); }); });
标签:读取文件 options win 监听 需要 require src 权限 mode
原文地址:http://www.cnblogs.com/clown3/p/6181415.html