Skip to content

Commit

Permalink
feat: add zhi-env and jest
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Apr 13, 2023
1 parent 1c2591b commit 3081f78
Show file tree
Hide file tree
Showing 28 changed files with 1,904 additions and 85 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module.exports = {
root: true,
// This tells ESLint to load the config from the package `eslint-config-custom`
extends: ["custom"],
}
40 changes: 38 additions & 2 deletions apps/zhi-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

a tool for generating zhi framework related projects

[Read more](https://www.terwer.space/post/use-typescript-to-develop-a-custom-nodejs-frontend-development-scaffold-1i5fne.html)
[Read more about node with cli](https://www.terwer.space/post/use-typescript-to-develop-a-custom-nodejs-frontend-development-scaffold-1i5fne.html)

[Read more about nx vite with cli](https://www.terwer.space/post/use-nrwlnxworkspace-to-create-a-nodejscommand-line-library-1urtj8.html)

[Read more about turbo esbuild with cli](https://www.terwer.space/implement-a-general-environment-variable-settings-tool-2bxmkh.html)

## Usage

Expand All @@ -12,7 +16,7 @@ pnpm set registry https://registry.npmmirror.com
npm i -g zhi-cli
```

Note: these project is under turbo-workspace, you need to create a turbo-workspace first and add the projects inside
Note: this project is under turbo-workspace, you need to create a turbo-workspace first and add the projects inside

a turbo-workspace may like

Expand All @@ -22,6 +26,7 @@ zhi
│   ├── zhi-cli
│   ├── zhi-core
├── packages
│   ├── api-docs
│   ├── eslint-config-custom
│   ├── tsconfig
├── pnpm-worpspace.yaml
Expand All @@ -48,4 +53,35 @@ zhi-cli init my-project ts-esbuild-lib

## ts-vite-react
## zhi-cli init my-project ts-vite-react
```

## Dev

```bash
pnpm dev -F zhi-cli
```

## Build

```bash
pnpm build -F zhi-cli
```

## Api

```bash
pnpm doc -F zhi-cli
pnpm md -F zhi-cli
```

## Test

```bash
pnpm test -F zhi-cli
```

## Publish

```
pnpm publish -F zhi-cli --ver=0.1.0 --tag=latest
```
2 changes: 1 addition & 1 deletion apps/zhi-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zhi-cli",
"version": "1.2.0",
"version": "1.3.0",
"description": "a tool for generating zhi framework related projects",
"type": "module",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion apps/zhi-cli/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import minimist from "minimist"
* zhi 主题构建
*
* @author terwer
* @since 1.0.0
* @since 0.1.0
*/
class ZhiBuild {
/**
Expand Down
26 changes: 26 additions & 0 deletions apps/zhi-core/__mocks__/fileMock.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/

module.exports = "test-file-stub"
26 changes: 26 additions & 0 deletions apps/zhi-core/__mocks__/styleMock.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/

module.exports = {}
15 changes: 15 additions & 0 deletions apps/zhi-core/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const sharedConfig = require("jest-config-custom")

// https://jestjs.io/docs/
module.exports = {
...sharedConfig,
rootDir: "./",
testEnvironment: "node",
// https://github.com/facebook/jest/issues/3094#issuecomment-385164816
// https://jestjs.io/docs/webpack#handling-static-assets
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/fileMock.cjs',
'\\.(css|styl)$': '<rootDir>/__mocks__/styleMock.cjs',
},
}
6 changes: 4 additions & 2 deletions apps/zhi-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zhi-core",
"version": "1.0.0",
"version": "0.1.0",
"type": "module",
"description": "a pluggable siyuan-note theme",
"main": "./dist/theme.js",
Expand All @@ -18,6 +18,7 @@
"scripts": {
"dev": "node --experimental-specifier-resolution=node --loader ts-node/esm scripts/build.ts --watch",
"build": "node --experimental-specifier-resolution=node --loader ts-node/esm scripts/build.ts --production",
"test": "jest",
"lint": "eslint \"src/**/*.{ts,js}\" --fix",
"doc": "../../packages/api-docs/node_modules/.bin/api-extractor run --local --verbose",
"md": "../../packages/api-docs/node_modules/.bin/api-documenter markdown -i temp -o ../zhi-docs/src/api/zhi-core && pnpm mdfix",
Expand All @@ -36,6 +37,7 @@
"minimist": "^1.2.8",
"stylus": "^0.59.0",
"ts-node": "^10.9.1",
"tsconfig": "workspace:*"
"tsconfig": "workspace:*",
"jest-config-custom": "workspace:*"
}
}
2 changes: 1 addition & 1 deletion apps/zhi-core/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import minimist from "minimist"
* zhi 主题构建
*
* @author terwer
* @since 1.0.0
* @since 0.1.0
*/
class ZhiBuild {
/**
Expand Down
33 changes: 33 additions & 0 deletions apps/zhi-core/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { describe, expect, test } from "@jest/globals"
import Zhi from "./index"

describe("zhi-cli", () => {
test("index", () => {
expect(new Zhi()).toBeTruthy()
})
})
2 changes: 1 addition & 1 deletion apps/zhi-core/src/theme/zhi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @public
* @author terwer
* @since 1.0.0
* @since 0.1.0
*/
class Zhi {
/**
Expand Down
2 changes: 1 addition & 1 deletion apps/zhi-docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zhi-docs",
"version": "1.0.0",
"version": "0.1.0",
"description": "a collection of zhi-docs auto build",
"license": "GPL",
"type": "module",
Expand Down
4 changes: 4 additions & 0 deletions apps/zhi-env/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ["custom"],
};
12 changes: 12 additions & 0 deletions apps/zhi-env/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# idea
.idea

# build
dist
lib
node_modules

# api-extractor
etc
temp
tsdoc-metadata.json
102 changes: 102 additions & 0 deletions apps/zhi-env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# zhi-env

a cross-platform env config lib

## Usage

For simple use

```ts
import Env from "zhi-env"

const env = new Env({
"some-key": "some-value",
})
const val = env.getEnv("some-key")
console.log("val=>", val)
```

For `vite`

```ts
import Env from "zhi-env"

const env = new Env(import.meta.env)
const val = env.getEnv("some-key")
console.log("val=>", val)
```

For `Nuxt` framework

```ts
import Env from "zhi-env"

const nuxtEnv = useRuntimeConfig()
const env = new Env(nuxtEnv)

const val = env.getEnv("some-key")
console.log("val=>", val)
```

For Astro framework or other libs

```ts
import Env from "zhi-env"

// https://github.com/vitejs/vite/issues/9539#issuecomment-1206301266
// 1 add modules:esnext to tsconfig.json
// 2 add env.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-common",
...envMeta,
}

const env = new Env(customEnv)

const val = env.getEnv("some-key")
console.log("val=>", val)
```

## Deps

```
## Congregations! zhi-env need no deps, it is just pure js code 🎉
```

## Dev

```bash
pnpm dev -F zhi-env
```

## Build

```bash
pnpm build -F zhi-env
```

## Api

```bash
pnpm doc -F zhi-env
pnpm md -F zhi-env
```

## Test

Execute the unit tests via [jest](https://jestjs.io/docs/getting-started#via-ts-jest)

```bash
pnpm test -F zhi-env
```

## Publish

```
pnpm publish -F zhi-env --ver=0.1.0 --tag=latest
```
8 changes: 8 additions & 0 deletions apps/zhi-env/api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "api-docs/api-extractor-base.json",

"apiReport": {
"enabled": true,
"reportFolder": "../../reports/api/"
}
}
Loading

0 comments on commit 3081f78

Please sign in to comment.