Skip to content

Commit

Permalink
feat(pligun-context): plugin context name
Browse files Browse the repository at this point in the history
  • Loading branch information
LetTTGACO committed Apr 7, 2024
1 parent ca6bee8 commit 6d9e0c4
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 37 deletions.
5 changes: 0 additions & 5 deletions packages/elog/bin/elog.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#! /usr/bin/env node
import { run } from '../dist/index.js';
const debugIndex = process.argv.findIndex((arg) => /^(--debug)$/.test(arg));
if (debugIndex > 0) {
process.env.DEBUG = 'true';
}

void run();
4 changes: 1 addition & 3 deletions packages/elog/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ export async function run() {
program
.command('sync')
.option('-c --config <string>', 'use config with custom, default is elog.config.ts')
.option('-a --cache <string>', 'use cache file name, default is elog.cache.json')
.option('-e, --env <string>', 'use env with custom')
.option('--disable-cache', 'sync doc without cache')
.option('--debug', 'enable debug')
.description('sync doc')
.action((options) => {
try {
void sync(options.config, options.cache, options.env, options.disableCache);
void sync(options.config, options.env, options.debug);
} catch (error: any) {
out.error(error.message);
}
Expand Down
16 changes: 6 additions & 10 deletions packages/elog/src/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ import out from '../utils/logger';
/**
* 同步
* @param customConfigPath
* @param customCachePath
* @param envPath
* @param disableCache 禁用缓存,全量更新文档
* @param enableDebug DEBUG 模式
*/
const sync = async (
customConfigPath?: string,
customCachePath?: string,
envPath?: string,
disableCache?: boolean,
) => {
process.env.ELOG_CACHE_PATH = customCachePath || 'elog.cache.json';
process.env.ELOG_DISABLE_CACHE = disableCache ? 'true' : 'false';
const sync = async (customConfigPath?: string, envPath?: string, enableDebug?: boolean) => {
// 是否开始 DEBUG 模式
if (enableDebug) {
process.env.DEBUG = 'true';
}
const rootDir = process.cwd();
// 加载环境变量
if (envPath) {
Expand Down
8 changes: 4 additions & 4 deletions packages/elog/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { DocDetail } from './doc';
import { LoggingFunction } from './log';
import type request from '../utils/request';
import {
cleanParameter,
generateUniqueId,
cleanUrlParam,
genUniqueIdFromUrl,
getBaseUrl,
getBufferFromUrl,
getFileType,
Expand All @@ -13,10 +13,10 @@ import {
} from '../utils/image';

interface ImageUtilContext {
generateUniqueId: typeof generateUniqueId;
genUniqueIdFromUrl: typeof genUniqueIdFromUrl;
getFileTypeFromUrl: typeof getFileTypeFromUrl;
getFileTypeFromBuffer: typeof getFileTypeFromBuffer;
cleanParameter: typeof cleanParameter;
cleanUrlParam: typeof cleanUrlParam;
getUrlListFromContent: typeof getUrlListFromContent;
getBaseUrl: typeof getBaseUrl;
getFileType: typeof getFileType;
Expand Down
8 changes: 4 additions & 4 deletions packages/elog/src/utils/PluginContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { NormalizeElogOption } from '../types/common';
import out from './logger';
import request from './request';
import {
cleanParameter,
generateUniqueId,
cleanUrlParam,
genUniqueIdFromUrl,
getBaseUrl,
getBufferFromUrl,
getFileType,
Expand All @@ -27,10 +27,10 @@ export function getPluginContext(
info: out.info,
warn: out.warn,
imageUtil: {
generateUniqueId,
genUniqueIdFromUrl,
getFileTypeFromUrl,
getFileTypeFromBuffer,
cleanParameter,
cleanUrlParam,
getUrlListFromContent,
getBaseUrl,
getFileType,
Expand Down
8 changes: 4 additions & 4 deletions packages/elog/src/utils/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const getFileTypeFromBuffer = (buffer: Buffer): FileType | undefined => {
* 去除图片链接中多余的参数
* @param originalUrl
*/
export const cleanParameter = (originalUrl: string) => {
export const cleanUrlParam = (originalUrl: string) => {
let newUrl = originalUrl;
// 去除#号
const indexPoundSign = originalUrl.indexOf('#');
Expand Down Expand Up @@ -86,7 +86,7 @@ export const getUrlListFromContent = (content: string) => {
// 去除#?号
return {
original: url,
url: cleanParameter(url),
url: cleanUrlParam(url),
};
}
return undefined;
Expand All @@ -101,7 +101,7 @@ export const getUrlListFromContent = (content: string) => {
export const getBaseUrl = (url: string) => {
return {
original: url,
url: cleanParameter(url),
url: cleanUrlParam(url),
};
};

Expand All @@ -110,7 +110,7 @@ export const getBaseUrl = (url: string) => {
* @param url
* @param length 长度截取
*/
export const generateUniqueId = (url: string, length?: number) => {
export const genUniqueIdFromUrl = (url: string, length?: number) => {
const hash = createHash('md5').update(url).digest('hex');
if (length) {
return hash.substring(0, length);
Expand Down
4 changes: 1 addition & 3 deletions playground/plugin-from-yuque/src/pwd/YuqueWithPwd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export default class YuqueWithPwd extends Context {
// 删除最后一个斜杠
this.config.baseUrl = this.config.baseUrl.slice(0, -1);
}
this.config.cacheFilePath =
this.config.cacheFilePath || process.env.ELOG_CACHE_PATH || 'elog.cache.json';
this.config.disableCache = process.env.ELOG_DISABLE_CACHE === 'true';
this.config.cacheFilePath = this.config.cacheFilePath || 'elog.cache.json';
// 初始化语雀 api
this.api = new YuqueApi(config, ctx);
this.initIncrementalUpdate();
Expand Down
4 changes: 1 addition & 3 deletions playground/plugin-from-yuque/src/token/YuqueWithToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export default class YuqueWithToken extends Context {
// 删除最后一个斜杠
this.config.baseUrl = this.config.baseUrl.slice(0, -1);
}
this.config.cacheFilePath =
this.config.cacheFilePath || process.env.ELOG_CACHE_PATH || 'elog.cache.json';
this.config.disableCache = process.env.ELOG_DISABLE_CACHE === 'true';
this.config.cacheFilePath = this.config.cacheFilePath || 'elog.cache.json';
// 初始化语雀 api
this.api = new YuqueApi(config, ctx);
this.initIncrementalUpdate();
Expand Down
2 changes: 1 addition & 1 deletion playground/plugin-image-local/src/ImageLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default class ImageLocal {
return await new Promise<ImageSource | undefined>(async (resolve) => {
try {
// 生成文件名
const fileName = this.ctx.imageUtil.generateUniqueId(image.url);
const fileName = this.ctx.imageUtil.genUniqueIdFromUrl(image.url);
// 生成文件名后缀
const fileType = await this.ctx.imageUtil.getFileType(image.url);
if (!fileType) {
Expand Down

0 comments on commit 6d9e0c4

Please sign in to comment.