Skip to content

Commit

Permalink
feat: elog flow
Browse files Browse the repository at this point in the history
  • Loading branch information
LetTTGACO committed Jun 15, 2024
1 parent 1758f5a commit 856f13c
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 19 deletions.
12 changes: 1 addition & 11 deletions packages/elog/src/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -47,14 +46,6 @@ export default class Graph {
writeCache<T>(sortedDocList: SortedDoc<T>[] = []) {
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 })),
Expand All @@ -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}`);
}
Expand All @@ -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) {
// 新增文档
Expand Down
27 changes: 26 additions & 1 deletion packages/elog/src/utils/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
};
}

Expand Down
78 changes: 73 additions & 5 deletions tests/test-elog/elog.config-muti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -28,27 +28,95 @@ 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 },
}),
],
plugins: [
imageLocal({
outputDir: './images2',
outputDir: './images5',
// prefixKey: '../../images',
pathFollowDoc: { enable: true, docOutputDir: './docs2' },
}),
Expand Down
2 changes: 1 addition & 1 deletion tests/test-elog/elog.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion tests/test-elog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit 856f13c

Please sign in to comment.