为什么 gulp 任务中文件夹不删除?

问题描述 投票:0回答:1

这个任务工作正常。我使用路径“.wwwroot/css/site.min.css”

var gulp = require("gulp");
const fs = require('fs').promises;

gulp.task('clean', async function (done) {
    try {
        await fs.rm(".wwwroot/js/temp/site.min.js", { recursive: true, force: true });
        done();
    } catch (error) {
        done(error);
    }
});

但是如果我使用路径'.wwwroot/js/temp/'。我收到错误: 以下任务未完成:清洁 您是否忘记发出异步完成信号?

“npm”:“10.8.2”, “节点”:“20.17.0”

问题是在我决定更新到 rimraf 2.2.8 后出现的。建议使用内置的fs库

gulp node.js-fs
1个回答
0
投票
async function removeTempFolder(done) {
    const dirPath = 'wwwroot/js/temp/';

    try {
        const files = await fs.readdir(dirPath);

        if (!files || files.length === 0) {
            console.log('No files to delete.');
        } else {
            for (const file of files) {
                const filePath = path.join(dirPath, file);
                await fs.unlink(filePath);
                console.log(`Deleted file: ${filePath}`);
            }
        }

        await fs.rm(dirPath, { recursive: true, force: true });
        console.log('Directory deleted successfully');
        done();
    } catch (err) {
        if (err.code === 'ENOENT') {
            console.log('Directory does not exist:', dirPath);
        } else {
            console.error('Error deleting files or directory:', err);
        }
        done(err);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.