From 856f13c83f6cdf8b3e9a703309cad232b78f438a Mon Sep 17 00:00:00 2001 From: LetTTGACO <1225751694@qq.com> Date: Sat, 15 Jun 2024 23:23:06 +0800 Subject: [PATCH] feat: elog flow --- packages/elog/src/Graph.ts | 12 +---- packages/elog/src/utils/load.ts | 27 +++++++++- tests/test-elog/elog.config-muti.ts | 78 +++++++++++++++++++++++++++-- tests/test-elog/elog.config.ts | 2 +- tests/test-elog/package.json | 2 +- 5 files changed, 102 insertions(+), 19 deletions(-) diff --git a/packages/elog/src/Graph.ts b/packages/elog/src/Graph.ts index ff7799d3..caf2f4dd 100644 --- a/packages/elog/src/Graph.ts +++ b/packages/elog/src/Graph.ts @@ -14,7 +14,6 @@ export default class Graph { private readonly cachedDocList: DocDetail[] = []; constructor(options: ElogConfig) { this.elogConfig = options; - this.elogConfig.cacheFilePath = this.elogConfig.cacheFilePath || 'elog.cache.json'; this.cachedDocList = this.initCache(options); this.pluginDriver = new PluginDriver(options, this.cachedDocList); } @@ -47,14 +46,6 @@ export default class Graph { writeCache(sortedDocList: SortedDoc[] = []) { try { const { cacheFilePath } = this.elogConfig; - // // 判断cachedDocList列表中对象是否有 body 属性 - // const hasBody = this.cachedDocList?.some((doc) => !!doc.body); - // if (hasBody) { - // out.debug( - // '警告', - // '缓存信息存在 body(文档内容)信息,可能会导致缓存文件过大,如无必要用途建议删除 body 属性', - // ); - // } // 写入缓存 const cacheJson = { cachedDocList: this.cachedDocList.map((item) => ({ ...item, body: undefined })), @@ -63,6 +54,7 @@ export default class Graph { fs.writeFileSync(cacheFilePath!, JSON.stringify(cacheJson, null, 2), { encoding: 'utf8', }); + out.success('任务结束', `同步成功,已生成缓存文件:${cacheFilePath}`); } catch (e) { out.warn('缓存失败', `写入缓存信息失败,请检查,${e.message}`); } @@ -74,9 +66,7 @@ export default class Graph { * @param docStatusMap */ updateCache(docList: DocDetail[], docStatusMap: any) { - console.log('docStatusMap', docStatusMap); for (const doc of docList) { - console.log('doc.id', doc.id); const { _updateIndex, _status } = docStatusMap[doc.id]; if (_status === DocStatus.NEW) { // 新增文档 diff --git a/packages/elog/src/utils/load.ts b/packages/elog/src/utils/load.ts index 266d22d3..b90b9a88 100644 --- a/packages/elog/src/utils/load.ts +++ b/packages/elog/src/utils/load.ts @@ -2,6 +2,7 @@ import path from 'path'; import JoyCon from 'joycon'; import { bundleRequire } from 'bundle-require'; import { defineConfig } from './elog'; +import { ElogConfig } from '../types/common'; export async function loadConfigFromFile( cwd: string, @@ -21,9 +22,33 @@ export async function loadConfigFromFile( const config = await bundleRequire({ filepath: configPath, }); + + let data: ElogConfig | ElogConfig[] = config.mod.default || config.mod; + if (Array.isArray(data)) { + if (data.length > 1) { + data = data.map((item, index) => { + return { + ...item, + cacheFilePath: item.cacheFilePath || `elog.cache${index + 1}.json`, + }; + }); + } else { + data = data.map((item) => { + return { + ...item, + cacheFilePath: item.cacheFilePath || 'elog.cache.json', + }; + }); + } + } else { + data = { + ...data, + cacheFilePath: data.cacheFilePath || 'elog.cache.json', + }; + } return { path: configPath, - data: config.mod.default || config.mod, + data, }; } diff --git a/tests/test-elog/elog.config-muti.ts b/tests/test-elog/elog.config-muti.ts index a97286fb..6a06aa42 100644 --- a/tests/test-elog/elog.config-muti.ts +++ b/tests/test-elog/elog.config-muti.ts @@ -9,7 +9,7 @@ export default defineConfig([ from: fromYuqueToken({ token: process.env.YUQUE_TOKEN, login: process.env.YUQUE_LOGIN, - repo: process.env.YUQUE_REPO, + repo: process.env.YUQUE_REPO1, onlyPublic: false, }), to: [ @@ -28,19 +28,87 @@ export default defineConfig([ }), ], }, + { + from: fromYuqueToken({ + token: process.env.YUQUE_TOKEN, + login: process.env.YUQUE_LOGIN, + repo: process.env.YUQUE_REPO1, + onlyPublic: false, + }), + to: [ + toLocal({ + outputDir: './docs2', + deployByStructure: true, + filename: 'title', + frontMatter: { enable: true }, + }), + ], + plugins: [ + imageLocal({ + outputDir: './images2', + // prefixKey: '../../images', + pathFollowDoc: { enable: true, docOutputDir: './docs1' }, + }), + ], + }, + { + from: fromYuqueToken({ + token: process.env.YUQUE_TOKEN, + login: process.env.YUQUE_LOGIN, + repo: process.env.YUQUE_REPO1, + onlyPublic: false, + }), + to: [ + toLocal({ + outputDir: './docs3', + deployByStructure: true, + filename: 'title', + frontMatter: { enable: true }, + }), + ], + plugins: [ + imageLocal({ + outputDir: './images3', + // prefixKey: '../../images', + pathFollowDoc: { enable: true, docOutputDir: './docs1' }, + }), + ], + }, + { + from: fromYuqueToken({ + token: process.env.YUQUE_TOKEN, + login: process.env.YUQUE_LOGIN, + repo: process.env.YUQUE_REPO1, + onlyPublic: false, + }), + to: [ + toLocal({ + outputDir: './docs4', + deployByStructure: true, + filename: 'title', + frontMatter: { enable: true }, + }), + ], + plugins: [ + imageLocal({ + outputDir: './images4', + // prefixKey: '../../images', + pathFollowDoc: { enable: true, docOutputDir: './docs1' }, + }), + ], + }, { from: fromYuque({ username: process.env.YUQUE_USERNAME, password: process.env.YUQUE_PWD, login: process.env.YUQUE_LOGIN, - repo: process.env.YUQUE_REPO, + repo: process.env.YUQUE_REPO2, onlyPublic: false, linebreak: false, - cacheFilePath: 'elog.cache2.json', }), to: [ toLocal({ - outputDir: './docs2', + outputDir: './docs5', deployByStructure: true, filename: 'title', frontMatter: { enable: true }, @@ -48,7 +116,7 @@ export default defineConfig([ ], plugins: [ imageLocal({ - outputDir: './images2', + outputDir: './images5', // prefixKey: '../../images', pathFollowDoc: { enable: true, docOutputDir: './docs2' }, }), diff --git a/tests/test-elog/elog.config.ts b/tests/test-elog/elog.config.ts index 3e8b79f3..2d5efd19 100644 --- a/tests/test-elog/elog.config.ts +++ b/tests/test-elog/elog.config.ts @@ -7,7 +7,7 @@ export default defineConfig({ from: fromYuque({ token: process.env.YUQUE_TOKEN, login: process.env.YUQUE_LOGIN, - repo: process.env.YUQUE_REPO, + repo: process.env.YUQUE_REPO1, onlyPublic: false, }), to: [ diff --git a/tests/test-elog/package.json b/tests/test-elog/package.json index 093e2b32..08af2d3e 100644 --- a/tests/test-elog/package.json +++ b/tests/test-elog/package.json @@ -8,7 +8,7 @@ "elog:init-templ": "elog init --template yuque-local", "elog:init": "elog init", "elog:sync": "elog sync --env .elog.env", - "elog:sync-muti": "elog sync --env .elog.env --config elog.config-muti.ts --cache elog.cache-muti.json ", + "elog:sync-muti": "elog sync --env .elog.env --config elog.config-muti.ts", "elog:version": "elog --version", "elog": "elog" },