node-复制文件
1 const path = require("path"); 2 const fs = require("fs"); 3 // 获取目录下的所有文件名 4 let fileNameList = fs.readdirSync(path.resolve(__dirname, "./template")) 5 console.log(fileNameList) 6 7 // 文件复制 8 // 1. 管道指引流的方式复制文件 9 let indexFile = fs.createReadStream(path.resolve(__dirname, "./template/index.js")); 10 let indexTo = fs.createWriteStream(path.resolve(process.cwd(), "./to/index.js")) 11 indexFile.pipe(indexTo) 12 13 // 2. 原生的readFile(Sync)和writeFile(Sync) ,参数为path和编码格式(或者内容) 14 // 区分同步和异步 15 let contentSync = fs.readFileSync(path.resolve(__dirname, "./template/render.vue"), "utf-8") 16 fs.writeFileSync(path.resolve(process.cwd(), "./to/render.vue"), contentSync) 17 fs.readFile(path.resolve(__dirname, "./template/render.vue"), "utf-8", function (err, data) { 18 console.log(err, data) 19 fs.writeFileSync(path.resolve(process.cwd(), "./to/render.vue"), data, function (error) { 20 console.log(error) 21 }) 22 }) 23 24 // 3. copyFile(formPath, toPath), 或者copyFileSync 25 copyFileSync(path.resolve(__dirname, "./template/render.vue"), path.resolve(__dirname, "./to/render.vue")) 26 fs.copyFile(path.resolve(__dirname, "./template/render.vue"), path.resolve(__dirname, "./to/render.vue"), 0, () => { 27 console.log("复制完成") 28 })