Skip to content

Commit e5ff85d

Browse files
author
liuyang
committed
fix(文章删除): 修复没有删除本地文件的问题
1 parent 231f237 commit e5ff85d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/Downloader.js

Lines changed: 15 additions & 5 deletions
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 } = this;
85+
const { client, config, postBasicPath } = this;
8586
const articles = await client.getArticles();
8687
if (!Array.isArray(articles.data)) {
8788
throw new Error(
@@ -95,9 +96,18 @@ class Downloader {
9596
)
9697
.filter(article => (config.onlyPublic ? !!article.public : true))
9798
.map(article => lodash.pick(article, PICK_PROPERTY));
98-
99-
this._cachedArticles = this._cachedArticles.filter(cache => realArticles.findIndex((item)=>item.slug === cache.slug) != -1);
100-
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+
101111
const queue = new Queue({ concurrency: config.concurrency });
102112

103113
let article;
@@ -108,7 +118,7 @@ class Downloader {
108118
const findIndexFn = function(item) {
109119
return item.slug === article.slug;
110120
};
111-
121+
112122
const { _cachedArticles } = this;
113123

114124
for (let i = 0; i < realArticles.length; i++) {

0 commit comments

Comments
 (0)