Skip to content

Commit

Permalink
fix: ci
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Apr 9, 2023
1 parent 9e90922 commit d1f99b8
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
142 changes: 142 additions & 0 deletions packages/zhi-server-modules-blog-astro/src/utils/ZhiUtil.ts
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

0 comments on commit d1f99b8

Please sign in to comment.