From fa27f6c33fdf65d70a353e904912947e55f7f240 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 30 Dec 2024 23:32:27 +0800 Subject: [PATCH] :art: Remove empty assets dir when exporting PDF https://github.com/siyuan-note/siyuan/issues/13670 --- app/src/boot/onGetConfig.ts | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts index 1f3002e6a..41e9d5f41 100644 --- a/app/src/boot/onGetConfig.ts +++ b/app/src/boot/onGetConfig.ts @@ -322,29 +322,35 @@ ${response.data.replace("%pages", "").replace("%pa path: pdfFilePath, removeAssets: ipcData.removeAssets, watermark: ipcData.watermark - }, () => { + }, async () => { afterExport(pdfFilePath, msgId); if (ipcData.removeAssets) { const removePromise = (dir: string) => { return new Promise(function (resolve) { - //先读文件夹 fs.stat(dir, function (err, stat) { - if (stat) { - if (stat.isDirectory()) { - fs.readdir(dir, function (err, files) { - files = files.map(file => path.join(dir, file)); // a/b a/m - Promise.all(files.map(file => removePromise(file))).then(function () { - fs.rm(dir, resolve); - }); + if (!stat) { + return; + } + + if (stat.isDirectory()) { + fs.readdir(dir, function (err, files) { + files = files.map(file => path.join(dir, file)); // a/b a/m + Promise.all(files.map(file => removePromise(file))).then(function () { + fs.rm(dir, resolve); }); - } else { - fs.unlink(dir, resolve); - } + }); + } else { + fs.unlink(dir, resolve); } }); }); }; - removePromise(path.join(savePath, "assets")); + + const assetsDir = path.join(savePath, "assets"); + await removePromise(assetsDir); + if (1 > fs.readdirSync(assetsDir).length) { + fs.rmdirSync(assetsDir); + } } }); });