Skip to content

Commit 0624bc0

Browse files
authored
Merge pull request #115 from beimengyeyu/patch-1
fix(文章生成): 修复因为缓存导致的删除的文章还会同步的问题
2 parents 2ec5c9f + e5ff85d commit 0624bc0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/Downloader.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const filenamify = require('filenamify');
99
const YuqueClient = require('./yuque');
1010
const { isPost } = require('../util');
1111
const out = require('./out');
12+
const rimraf = require('rimraf');
1213

1314
const cwd = process.cwd();
1415

@@ -81,7 +82,7 @@ class Downloader {
8182
* @return {Promise} queue
8283
*/
8384
async fetchArticles() {
84-
const { client, config, _cachedArticles } = this;
85+
const { client, config, postBasicPath } = this;
8586
const articles = await client.getArticles();
8687
if (!Array.isArray(articles.data)) {
8788
throw new Error(
@@ -95,6 +96,18 @@ class Downloader {
9596
)
9697
.filter(article => (config.onlyPublic ? !!article.public : true))
9798
.map(article => lodash.pick(article, PICK_PROPERTY));
99+
100+
const deletedArticles = this._cachedArticles.filter(cache => realArticles.findIndex(item => item.slug === cache.slug) === -1);
101+
102+
// 删除本地已存在的但是语雀上面被删除的文章
103+
for (const article of deletedArticles) {
104+
const fileName = filenamify(article[config.mdNameFormat]);
105+
const postPath = path.join(postBasicPath, `${fileName}.md`);
106+
rimraf.sync(postPath);
107+
}
108+
109+
this._cachedArticles = this._cachedArticles.filter(cache => realArticles.findIndex(item => item.slug === cache.slug) !== -1);
110+
98111
const queue = new Queue({ concurrency: config.concurrency });
99112

100113
let article;
@@ -106,6 +119,8 @@ class Downloader {
106119
return item.slug === article.slug;
107120
};
108121

122+
const { _cachedArticles } = this;
123+
109124
for (let i = 0; i < realArticles.length; i++) {
110125
article = realArticles[i];
111126
cacheIndex = _cachedArticles.findIndex(findIndexFn);

0 commit comments

Comments
 (0)