diff --git a/lib/Downloader.js b/lib/Downloader.js index 2ca45da..e6fca13 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, _cachedArticles } = this; + const { client, config, postBasicPath } = this; const articles = await client.getArticles(); if (!Array.isArray(articles.data)) { throw new Error( @@ -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; @@ -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);