-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
packages/zhi-server-modules-blog-astro/src/utils/ZhiUtil.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright (c) 2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import LogFactory, { LogConstants, LogLevelEnum } from "zhi-log" | ||
import ZhiCommon from "zhi-common" | ||
import Env, { EnvConstants } from "zhi-env" | ||
import { SiYuanApiAdaptor, SiyuanConfig, SiyuanConstants } from "zhi-siyuan-api" | ||
import BlogApi, { BlogConstants, BlogTypeEnum } from "zhi-blog-api" | ||
|
||
/** | ||
* 工具类统一入口,每个应用自己实现 | ||
* | ||
* @public | ||
* @author terwer | ||
* @since 1.0.0 | ||
*/ | ||
class ZhiUtil { | ||
/** | ||
* 按照logger缓存 | ||
* @private | ||
*/ | ||
private static loggerMap: any | ||
private static common: ZhiCommon | ||
private static env: Env | ||
private static bApi: BlogApi | ||
|
||
/** | ||
* 获取 zhi-env 实例 | ||
*/ | ||
public static zhiEnv() { | ||
if (!ZhiUtil.env) { | ||
// https://github.com/vitejs/vite/issues/9539#issuecomment-1206301266 | ||
// 1 add modules:esnext tsconfig.app.json | ||
// 2 add custom.d.ts | ||
const envMeta = import.meta.env | ||
|
||
const customEnv = { | ||
[EnvConstants.NODE_ENV_KEY]: EnvConstants.NODE_ENV_DEVELOPMENT, | ||
[EnvConstants.VITE_DEBUG_MODE_KEY]: false, | ||
[LogConstants.LOG_LEVEL_KEY]: LogLevelEnum.LOG_LEVEL_DEBUG, | ||
[LogConstants.LOG_PREFIX_KEY]: "zhi-blog-astro", | ||
...envMeta, | ||
} | ||
|
||
ZhiUtil.env = new Env(customEnv) | ||
} | ||
return ZhiUtil.env | ||
} | ||
|
||
/** | ||
* 获取 zhi-common 实例 | ||
*/ | ||
public static zhiCommon() { | ||
if (!ZhiUtil.common) { | ||
ZhiUtil.common = new ZhiCommon() | ||
} | ||
return ZhiUtil.common | ||
} | ||
|
||
/** | ||
* 获取 zhi-log 实例 | ||
*/ | ||
public static zhiLog(loggerName: string) { | ||
// 先检测日志Map | ||
if (ZhiUtil.loggerMap) { | ||
// 日志不存在,生成一个新的缓存到Map | ||
if (ZhiUtil.loggerMap[loggerName]) { | ||
ZhiUtil.loggerMap[loggerName].debug("Zhi-log use cache.") | ||
} else { | ||
const env = ZhiUtil.zhiEnv() | ||
ZhiUtil.loggerMap[loggerName] = LogFactory.customSignLogFactory("zhi-blog-astro", env).getLogger( | ||
loggerName | ||
) | ||
ZhiUtil.loggerMap[loggerName].debug("Zhi-log add new logger.") | ||
} | ||
} else { | ||
// 生成新的日志器 | ||
const env = ZhiUtil.zhiEnv() | ||
ZhiUtil.loggerMap = {} | ||
ZhiUtil.loggerMap[loggerName] = LogFactory.customSignLogFactory("zhi-blog-astro", env).getLogger(loggerName) | ||
ZhiUtil.loggerMap[loggerName].debug("Zhi-log inited.") | ||
} | ||
|
||
// 从Map缓存获取日志器 | ||
return ZhiUtil.loggerMap[loggerName] | ||
} | ||
|
||
/** | ||
* 获取 siyuan-kernel-api 实例 | ||
*/ | ||
public static blogApi() { | ||
if (!ZhiUtil.bApi) { | ||
const env = ZhiUtil.zhiEnv() | ||
|
||
let apiAdaptor | ||
const blogType = env.getEnv(BlogConstants.DEFAULT_BLOG_TYPE_KEY) | ||
switch (blogType) { | ||
case BlogTypeEnum.BlogTypeEnum_Wordpress: | ||
break | ||
default: | ||
const apiUrl = env.getEnvOrDefault(SiyuanConstants.VITE_SIYUAN_API_URL_KEY, "http://127.0.0.1:6806") | ||
const token = env.getStringEnv(SiyuanConstants.VITE_SIYUAN_AUTH_TOKEN_KEY) | ||
|
||
const siyuanConfig = new SiyuanConfig(apiUrl, token) | ||
// 显示指定修复标题 | ||
siyuanConfig.fixTitle = true | ||
apiAdaptor = new SiYuanApiAdaptor(siyuanConfig) | ||
break | ||
} | ||
|
||
if (!apiAdaptor) { | ||
throw new Error("ApiAdaptor cannot be null") | ||
} | ||
ZhiUtil.bApi = new BlogApi(apiAdaptor) | ||
} | ||
return ZhiUtil.bApi | ||
} | ||
} | ||
|
||
export default ZhiUtil |