Skip to content

Commit

Permalink
fix(文章删除): 修复没有删除本地文件的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyang committed Oct 27, 2022
1 parent 231f237 commit e5ff85d
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions 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 } = this;
const { client, config, postBasicPath } = this;
const articles = await client.getArticles();
if (!Array.isArray(articles.data)) {
throw new Error(
Expand All @@ -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;
Expand All @@ -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++) {
Expand Down

0 comments on commit e5ff85d

Please sign in to comment.