Skip to content

Commit

Permalink
feat(zhi-lib-common): add common libiary
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 5, 2023
1 parent 0d9eb90 commit 4c553c1
Show file tree
Hide file tree
Showing 38 changed files with 1,918 additions and 15 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@
"vitest": "^0.31.0"
},
"dependencies": {
"ajv": "^8.12.0",
"ansi-colors": "^4.1.3",
"callsites": "^4.0.0",
"compare-versions": "5.0.3",
"kleur": "^4.1.5",
"loglevel": "^1.8.1",
"loglevel-plugin-prefix": "^0.8.4"
"loglevel-plugin-prefix": "^0.8.4",
"showdown": "^2.1.0"
}
}
28 changes: 28 additions & 0 deletions packages/zhi-core/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.
*/

interface ImportMeta {
readonly env: ImportMetaEnv
}
45 changes: 45 additions & 0 deletions packages/zhi-core/src/lib/util/ZhiCoreUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 { Env } from "@siyuan-community/zhi-env"
import { ZhiUtil } from "@siyuan-community/zhi-common"

/**
* 工具类统一入口,每个应用自己实现
*
* @public
* @author terwer
* @since 1.0.0
*/
class ZhiCoreUtil extends ZhiUtil {
public static override zhiEnv(): Env {
if (!this.env) {
this.env = new Env(import.meta.env)
}
return this.env
}
}

export default ZhiCoreUtil
11 changes: 8 additions & 3 deletions packages/zhi-core/src/lib/zhi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
*/

import { DeviceTypeEnum } from "@siyuan-community/zhi-device"
import ZhiCoreUtil from "./util/ZhiCoreUtil"

class Zhi {
private readonly logger

private readonly runAs

/**
Expand All @@ -46,6 +49,8 @@ class Zhi {
* @param runAs - 运行模式
*/
constructor(runAs: DeviceTypeEnum) {
this.logger = ZhiCoreUtil.zhiLog("zhi-core")

this.runAs = runAs ?? DeviceTypeEnum.DeviceType_Node
}

Expand All @@ -54,11 +59,11 @@ class Zhi {
*/
public async init(): Promise<void> {
try {
console.info(`Zhi Theme runAs ${this.runAs}`)
this.logger.info(`Zhi Theme runAs ${this.runAs}`)

console.log("Zhi Theme inited")
this.logger.info("Zhi Theme inited")
} catch (e) {
console.error("Zhi Theme load error=>", e)
this.logger.error("Zhi Theme load error=>", e)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/zhi-core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"target": "esnext",
"module": "esnext",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
Expand Down
18 changes: 18 additions & 0 deletions packages/zhi-lib-common/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
50 changes: 50 additions & 0 deletions packages/zhi-lib-common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# zhi-lib-common

the library base for zhi related projects

## Usage

```ts
import zhiCommon from "@siyuan-community/zhi-common"

const dateUtil = zhiCommon.dateUtil
const now = dateUtil.nowDateZh()
console.log("now=>", now)
```

## Deps

```
├── @siyuan-community/zhi-env
├── @siyuan-community/zhi-log
├── lute
├── showdown
├── compare-versions
├── ajv
```

## Dev

```bash
nx dev zhi-lib-common
```

## Build

```bash
nx build zhi-lib-common
```

## Test

Execute the unit tests via [vitest](https://vitest.dev)

```bash
nx test zhi-lib-common
```

## Publish

```
nx publish zhi-lib-common --ver=0.0.1 --tag=latest
```
17 changes: 17 additions & 0 deletions packages/zhi-lib-common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@siyuan-community/zhi-common",
"version": "1.6.0",
"type": "module",
"description": "the library base for zhi related projects",
"main": "./index.js",
"typings": "./index.d.ts",
"repository": "terwer/zhi",
"homepage": "https://github.com/terwer/zhi/tree/main/packages/zhi-lib-common",
"author": "terwer",
"license": "GPL",
"keywords": [
"zhi",
"common",
"libiary"
]
}
35 changes: 35 additions & 0 deletions packages/zhi-lib-common/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "zhi-lib-common",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/zhi-lib-common/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/vite:build",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/zhi-lib-common"
}
},
"publish": {
"command": "node tools/scripts/publish.mjs zhi-lib-common {args.ver} {args.tag}",
"dependsOn": ["build"]
},
"test": {
"executor": "@nx/vite:test",
"outputs": ["coverage/packages/zhi-lib-common"],
"options": {
"passWithNoTests": true,
"reportsDirectory": "../../coverage/packages/zhi-lib-common"
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/zhi-lib-common/**/*.ts"]
}
}
},
"tags": []
}
28 changes: 28 additions & 0 deletions packages/zhi-lib-common/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.
*/

interface ImportMeta {
readonly env: ImportMetaEnv
}
1 change: 1 addition & 0 deletions packages/zhi-lib-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./lib/zhi-lib-common"
45 changes: 45 additions & 0 deletions packages/zhi-lib-common/src/lib/ZhiCommonUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 { Env } from "@siyuan-community/zhi-env"
import ZhiUtil from "./ZhiUtil"

/**
* Common 模块工具类
*
* @author terwer
* @version 1.4.0
* @since 1.4.0
*/
class ZhiCommonUtil extends ZhiUtil {
public static override zhiEnv(): Env {
if (!this.env) {
this.env = new Env(import.meta.env)
}
return this.env
}
}

export default ZhiCommonUtil
Loading

0 comments on commit 4c553c1

Please sign in to comment.