Skip to content

Commit

Permalink
Merge pull request #115 from beimengyeyu/patch-1
Browse files Browse the repository at this point in the history
fix(文章生成): 修复因为缓存导致的删除的文章还会同步的问题
  • Loading branch information
LetTTGACO authored Oct 27, 2022
2 parents 2ec5c9f + e5ff85d commit 0624bc0
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/Downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -81,7 +82,7 @@ class Downloader {
* @return {Promise} queue
*/
async fetchArticles() {
const { client, config, _cachedArticles } = this;
const { client, config, postBasicPath } = this;
const articles = await client.getArticles();
if (!Array.isArray(articles.data)) {
throw new Error(
Expand All @@ -95,6 +96,18 @@ class Downloader {
)
.filter(article => (config.onlyPublic ? !!article.public : true))
.map(article => lodash.pick(article, PICK_PROPERTY));

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;
Expand All @@ -106,6 +119,8 @@ class Downloader {
return item.slug === article.slug;
};

const { _cachedArticles } = this;

for (let i = 0; i < realArticles.length; i++) {
article = realArticles[i];
cacheIndex = _cachedArticles.findIndex(findIndexFn);
Expand Down

0 comments on commit 0624bc0

Please sign in to comment.