From e5ff85da04661b1997168edeb725625f99e36379 Mon Sep 17 00:00:00 2001 From: liuyang Date: Thu, 27 Oct 2022 15:36:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=96=87=E7=AB=A0=E5=88=A0=E9=99=A4):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B2=A1=E6=9C=89=E5=88=A0=E9=99=A4=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=96=87=E4=BB=B6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Downloader.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/Downloader.js b/lib/Downloader.js index ceb20d7..cc8c14e 100644 --- a/lib/Downloader.js +++ b/lib/Downloader.js @@ -9,6 +9,7 @@ const filenamify = require('filenamify'); const YuqueClient = require('./yuque'); const { isPost } = require('../util'); const out = require('./out'); +const rimraf = require('rimraf'); const cwd = process.cwd(); @@ -81,7 +82,7 @@ class Downloader { * @return {Promise} queue */ async fetchArticles() { - const { client, config } = this; + const { client, config, postBasicPath } = this; const articles = await client.getArticles(); if (!Array.isArray(articles.data)) { throw new Error( @@ -95,9 +96,18 @@ class Downloader { ) .filter(article => (config.onlyPublic ? !!article.public : true)) .map(article => lodash.pick(article, PICK_PROPERTY)); - - this._cachedArticles = this._cachedArticles.filter(cache => realArticles.findIndex((item)=>item.slug === cache.slug) != -1); - + + const deletedArticles = this._cachedArticles.filter(cache => realArticles.findIndex(item => item.slug === cache.slug) === -1); + + // 删除本地已存在的但是语雀上面被删除的文章 + for (const article of deletedArticles) { + const fileName = filenamify(article[config.mdNameFormat]); + const postPath = path.join(postBasicPath, `${fileName}.md`); + rimraf.sync(postPath); + } + + this._cachedArticles = this._cachedArticles.filter(cache => realArticles.findIndex(item => item.slug === cache.slug) !== -1); + const queue = new Queue({ concurrency: config.concurrency }); let article; @@ -108,7 +118,7 @@ class Downloader { const findIndexFn = function(item) { return item.slug === article.slug; }; - + const { _cachedArticles } = this; for (let i = 0; i < realArticles.length; i++) {