node 进行文件夹及文件 复制到另一个文件夹 文件内容替换

方法写作的初衷是因为

     项目打包之后  每次都要放到另一个项目里   而且项目文件的层级比较深  每次替换比较麻烦

总结一点  就是    懒  

888888 为按照自己的需求  需要手动更改的地方

以下为代码中所涉及的一些方法

  1. 删除文件夹及子文件
  2. 删除文件
  3. 删除文件夹
  4. 复制文件
  5. 复制文件夹
  6. 文本替换操作
var fs = require('fs');
var path = require('path');

// 根目录
let BASEPATHURL = path.resolve(__dirname, '..')

// 移动目录
let startFileDirectory = BASEPATHURL + "888888"
// 放置目录
let endFileDirectory = BASEPATHURL + "888888"

var startDate = new Date().getTime()

// 删除复制执行
rmDirFile(endFileDirectory, () => {
    console.log("全部删除完成,开始复制")
    copyDir(startFileDirectory, endFileDirectory, (res) => {
        console.log("全部复制完成")
        console.log("修改文件内容")
        replaceText(endFileDirectory)
    })
});

// 文本替换
function replaceText(url) {
    let testHtml = fs.readFileSync(url + "/index.html", 'utf8')
    console.log("修改之前", testHtml)
    fs.writeFileSync(url + '/index.jsp', '888888' + testHtml, 'utf8')
    console.log("新建index.jsp文件完成")
    let newTestHtml = fs.readFileSync(url + "/index.jsp", 'utf8')
    console.log("index.jsp文件内容", newTestHtml)
    console.log("删除index.html文件开始")
    fs.unlinkSync(url + "/index.html");
    console.log("删除index.html文件完成")
    // 耗时
    let endDate = new Date().getTime()

    console.log("本次耗时", (endDate - startDate) , "ms")
}

// 删除
function rmDirFile(path, cb) {
    let files = [];
    console.log("开始删除")
    if (fs.existsSync(path)) {
        var count = 0
        var checkEnd = function () {
            console.log("进度", count)
            ++count == files.length && cb && cb()
        }
        files = fs.readdirSync(path);
        files.forEach(function (file, index) {
            let curPath = path + "/" + file;
            if (fs.statSync(curPath).isDirectory()) {
                console.log("遇到文件夹", curPath)
                rmDirFile(curPath, checkEnd);
            } else {
                fs.unlinkSync(curPath);
                console.log("删除文件完成", curPath)
                checkEnd()
            }
        });
        // 如果删除文件夹为放置文件夹根目录  不执行删除
        if (path == endFileDirectory) {
            console.log("删除文件夹完成", path)
        } else {
            fs.rmdirSync(path);
        }
        //为空时直接回调
        files.length === 0 && cb && cb()
    } else {
        cb && cb()
    }
}

// 复制文件
function copyFile(srcPath, tarPath, cb) {
    var rs = fs.createReadStream(srcPath)
    rs.on('error', function (err) {
        if (err) {
            console.log('read error', srcPath)
        }
        cb && cb(err)
    })

    var ws = fs.createWriteStream(tarPath)
    ws.on('error', function (err) {
        if (err) {
            console.log('write error', tarPath)
        }
        cb && cb(err)
    })

    ws.on('close', function (ex) {
        cb && cb(ex)
    })

    rs.pipe(ws)
    console.log("复制文件完成", srcPath)
}

// 复制文件夹所有
function copyDir(srcDir, tarDir, cb) {
    if (fs.existsSync(tarDir)) {
        fs.readdir(srcDir, function (err, files) {
            var count = 0
            var checkEnd = function () {
                console.log("进度", count)
                ++count == files.length && cb && cb()
            }

            if (err) {
                checkEnd()
                return
            }

            files.forEach(function (file) {
                var srcPath = path.join(srcDir, file)
                var tarPath = path.join(tarDir, file)

                fs.stat(srcPath, function (err, stats) {
                    if (stats.isDirectory()) {
                        fs.mkdir(tarPath, function (err) {
                            if (err) {
                                console.log(err)
                                return
                            }

                            copyDir(srcPath, tarPath, checkEnd)
                            console.log("复制文件完成", srcPath)
                        })
                    } else {
                        copyFile(srcPath, tarPath, checkEnd)
                        console.log("复制文件完成", srcPath)
                    }
                })
            })

            //为空时直接回调
            files.length === 0 && cb && cb()
        })

    } else {
        fs.mkdir(tarDir, function (err) {
            if (err) {
                console.log(err)
                return
            }
            console.log('创建文件夹', tarDir)
            copyDir(srcDir, tarDir, cb)
        })
    }

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值