Skip to content

Commit

Permalink
feat: Unitify env
Browse files Browse the repository at this point in the history
terwer committed Apr 8, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 29bdc49 commit 98105de
Showing 5 changed files with 52 additions and 41 deletions.
21 changes: 16 additions & 5 deletions packages/zhi-core/src/lib/zhi.ts
Original file line number Diff line number Diff line change
@@ -171,19 +171,30 @@ class Zhi {
// 如果有初始化方法,进行初始化
if (lib) {
const libObj = lib
this.logger.debug("Required lib Obj=>", libObj)
this.logger.debug(this.common.strUtil.f("Success {0} lib Obj=>", item.importType), libObj)
if (libObj.init) {
const res = await libObj.init()
if (res) {
this.logger.info("Detected output from required lib=>", res)
this.logger.info(
this.common.strUtil.f(
"Detected output from {0} lib {1}=>",
item.importType,
item.libpath
),
res
)
}
} else {
this.logger.debug(this.common.strUtil.f("No init method for {0}", item.libpath))
this.logger.debug(
this.common.strUtil.f("No init method for {0} {1}", item.importType, item.libpath)
)
}
} else {
this.logger.debug(this.common.strUtil.f("Lib entry is not a function => {0}", item.libpath))
this.logger.debug(
this.common.strUtil.f("Lib entry is not a function => {0} {1}", item.importType, item.libpath)
)
}
this.logger.info(this.common.strUtil.f("loaded {0}", item.libpath))
this.logger.info(this.common.strUtil.f("loaded {0} {1}", item.importType, item.libpath))
}

this.logger.info("Theme inited.")
26 changes: 18 additions & 8 deletions packages/zhi-env/src/lib/EnvConstants.ts
Original file line number Diff line number Diff line change
@@ -31,15 +31,25 @@
* @since 1.0.0
*/
class EnvConstants {
/**
* Node环境
*/
public static NODE_ENV_KEY = "NODE_ENV"
/**
* Node环境
*/
public static NODE_ENV_KEY = "NODE_ENV"

/**
* 是否处于调试模式
*/
public static VITE_DEBUG_MODE_KEY = "VITE_DEBUG_MODE"
/**
* 开发环境
*/
public static NODE_ENV_DEVELOPMENT = "development"

/**
* 生产环境
*/
public static NODE_ENV_PRODUCTION = "production"

/**
* 是否处于调试模式
*/
public static VITE_DEBUG_MODE_KEY = "VITE_DEBUG_MODE"
}

export default EnvConstants
2 changes: 1 addition & 1 deletion packages/zhi-server-modules-middleware/esbuild.mjs
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ esbuild
outdir: outDir,
entryPoints: ["packages/zhi-server-modules-middleware/src/index.ts"],
external: ["esbuild", "fsevents", "express", ...builtinModules],
format: "cjs",
format: "esm",
platform: "node",
mainFields: ["module", "main"],
plugins: [inlineImportPlugin()],
19 changes: 14 additions & 5 deletions packages/zhi-server-modules-middleware/src/lib/ZhiUtil.ts
Original file line number Diff line number Diff line change
@@ -23,10 +23,10 @@
* questions.
*/

import LogFactory from "zhi-log"
import ZhiCommon from "zhi-common"
import Env from "zhi-env"
import { SiyuanKernelApi } from "zhi-siyuan-api"
import LogFactory, { LogConstants, LogLevelEnum } from "zhi-log";
import ZhiCommon from "zhi-common";
import Env, { EnvConstants } from "zhi-env";
import { SiyuanKernelApi } from "zhi-siyuan-api";

/**
* 工具类统一入口,每个应用自己实现
@@ -53,7 +53,16 @@ class ZhiUtil {
// https://github.com/vitejs/vite/issues/9539#issuecomment-1206301266
// 1 add modules:esnext tsconfig.app.json
// 2 add custom.d.ts
ZhiUtil.env = new Env(import.meta.env)
// 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-server-middleware",
}

ZhiUtil.env = new Env(customEnv)
}
return ZhiUtil.env
}
Original file line number Diff line number Diff line change
@@ -23,27 +23,8 @@
* questions.
*/

import ZhiUtil from "./ZhiUtil"
class ZhiServerModulesMiddleware {

/**
* lib入口,如果是 zhi 模块,此方法必须是 init
*
* @param port - 端口
* @author terwer
* @version 1.0.0
* @since 1.0.0
*/
export async function init(port?: number): Promise<string> {
const logger = ZhiUtil.zhiLog("init-blog-middleware")
const common = ZhiUtil.zhiCommon()
const p = port ?? 3000
try {
logger.warn("HTTP server is disabled")
// const zhiBlogMiddleware = new ZhiBlogMiddleware()
// await zhiBlogMiddleware.startServer(p, [markdownMiddleware])
} catch (e) {
logger.error(common.strUtil.f("HTTP server init failed at {0}!Some function may not work", p), e)
}

return "HTTP server started"
}

export default ZhiServerModulesMiddleware

0 comments on commit 98105de

Please sign in to comment.