nodejs 使用经验

发布时间 2023-04-17 09:04:59作者: lvye1221

路径操作

    var dirName = path.dirname(filePath);
    var fileName = path.basename(filePath);


    var newFilePath = dirName + "/min-" + fileName;



function walkSync(currentDirPath, callback) {
   fs.readdirSync(currentDirPath, { withFileTypes: true }).forEach(function(dirent) {
        var filePath = path.join(currentDirPath, dirent.name);
        if (dirent.isFile()) {
            callback(filePath, dirent);
        } else if (dirent.isDirectory()) {
            walkSync(filePath, callback);
        }
   });
}

// 遍历所有文件
walkSync(resDir, function(filePath, stat) {

});

执行批处理命令


    let cmd = obj.cmd;
    let oldFilePath = obj.oldFilePath;
    exec(cmd, function(error, stdout, stderr) {
        if(error){
            console.error(error);
        } else {
            var msg = cmd + " success!";
            console.log(msg);

            // 删除文件
            fs.unlinkSync(oldFilePath);
        }
    });