From d1f99b891f58ff3d550ab706a7af82536e74bf1e Mon Sep 17 00:00:00 2001 From: terwer Date: Mon, 10 Apr 2023 02:03:13 +0800 Subject: [PATCH] fix: ci --- .github/workflows/ci.yml | 2 +- package.json | 1 + .../src/utils/ZhiUtil.ts | 142 ++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 packages/zhi-server-modules-blog-astro/src/utils/ZhiUtil.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f0093aed..ded86c98 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,5 +43,5 @@ jobs: run: pnpm i --registry=https://registry.npmmirror.com - name: build for production - run: nx reset && pnpm build + run: pnpm reset && pnpm build diff --git a/package.json b/package.json index 8f56dec4..e28b1360 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "license": "GPL", "scripts": { + "reset": "nx reset", "build": "nx run-many --target=build --exclude=zhi-docs", "zhi-build": "nx zhi-build zhi-core", "test": "nx run-many --target=test --exclude=zhi-docs", diff --git a/packages/zhi-server-modules-blog-astro/src/utils/ZhiUtil.ts b/packages/zhi-server-modules-blog-astro/src/utils/ZhiUtil.ts new file mode 100644 index 00000000..fd370a63 --- /dev/null +++ b/packages/zhi-server-modules-blog-astro/src/utils/ZhiUtil.ts @@ -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