@wwkit/shared 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +35 -0
- package/package.json +40 -0
- package/src/config-loader.js +122 -0
- package/src/index.js +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 bluesliu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# @wwkit/shared
|
|
2
|
+
|
|
3
|
+
Shared utilities for @wwkit packages.
|
|
4
|
+
|
|
5
|
+
## config-loader
|
|
6
|
+
|
|
7
|
+
多层配置加载器:内置 `config.json5` + 环境变量注入内容 + 用户 `config.json5` 合并(用户优先)。
|
|
8
|
+
|
|
9
|
+
**默认行为(无需配置)**:
|
|
10
|
+
- **深合并**(`deepMerge`),嵌套配置只覆盖用户改写的字段,保留其余默认
|
|
11
|
+
- 结果默认解析 `{env:XXX}` 占位符(安全实践:密钥用环境变量引用,不落盘明文)
|
|
12
|
+
|
|
13
|
+
**可选层(通过选项开启)**:
|
|
14
|
+
- `envContentVar`:指定环境变量名,其内容作为"注入配置"这一层参与合并(同样走深合并+占位符解析)
|
|
15
|
+
- `envDirVar`:指定环境变量名,其值覆盖用户配置目录
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import { createConfigLoader, getXdgConfigDir } from '@wwkit/shared'
|
|
19
|
+
|
|
20
|
+
const loader = createConfigLoader({
|
|
21
|
+
builtinConfigPath: path.join(__dirname, 'config.json5'),
|
|
22
|
+
userConfigDir: getXdgConfigDir('cft'),
|
|
23
|
+
envContentVar: 'MY_CONFIG_CONTENT', // 可选
|
|
24
|
+
envDirVar: 'MY_CONFIG_DIR', // 可选
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
loader.copyBuiltinConfig() // 复制内置→用户目录(不覆盖已存在)
|
|
28
|
+
const cfg = loader.getConfig() // 深合并 + 占位符解析
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 导出的工具函数
|
|
32
|
+
|
|
33
|
+
- `deepMerge(base, override)` — 深合并(嵌套对象合并,数组/标量覆盖)
|
|
34
|
+
- `resolveEnvVars(value, env?)` — 递归解析 `{env:XXX}` 占位符
|
|
35
|
+
- `getXdgConfigDir(subdir)` — `~/.config/<subdir>` 或 `$XDG_CONFIG_HOME/<subdir>`
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wwkit/shared",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"author": "bluesliu <langcai163@163.com>",
|
|
5
|
+
"description": "Shared utilities for @wwkit packages — config loader, platform detection",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "src/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"jest": {
|
|
14
|
+
"transform": {},
|
|
15
|
+
"testMatch": [
|
|
16
|
+
"**/tests/unit/**/*.test.js"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"wwkit",
|
|
21
|
+
"shared",
|
|
22
|
+
"config"
|
|
23
|
+
],
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"registry": "https://registry.npmjs.org/",
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"json5": "^2.2.3"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"jest": "^29.7.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --no-cache tests/unit",
|
|
37
|
+
"test:all": "node --experimental-vm-modules node_modules/jest/bin/jest.js --no-cache tests/unit",
|
|
38
|
+
"release": "pnpm version patch && pnpm publish"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import process from 'node:process'
|
|
5
|
+
import JSON5 from 'json5'
|
|
6
|
+
|
|
7
|
+
const ENV_PATTERN = /\{env:(\w+)\}/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 递归合并对象,override 优先,嵌套对象深合并(数组/标量直接覆盖)。
|
|
11
|
+
*/
|
|
12
|
+
export function deepMerge(base, override) {
|
|
13
|
+
const merged = { ...(base ?? {}) }
|
|
14
|
+
for (const [key, value] of Object.entries(override ?? {})) {
|
|
15
|
+
if (value && typeof value === 'object' && !Array.isArray(value)
|
|
16
|
+
&& merged[key] && typeof merged[key] === 'object' && !Array.isArray(merged[key])) {
|
|
17
|
+
merged[key] = deepMerge(merged[key], value)
|
|
18
|
+
} else {
|
|
19
|
+
merged[key] = value
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return merged
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 递归解析字符串中的 {env:XXX} 占位符,替换为环境变量值(未设置则为空串)。
|
|
27
|
+
*/
|
|
28
|
+
export function resolveEnvVars(value, env = process.env) {
|
|
29
|
+
if (typeof value === 'string') {
|
|
30
|
+
return value.replace(ENV_PATTERN, (_, name) => env[name] ?? '')
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
return value.map((v) => resolveEnvVars(v, env))
|
|
34
|
+
}
|
|
35
|
+
if (value && typeof value === 'object') {
|
|
36
|
+
const out = {}
|
|
37
|
+
for (const [k, v] of Object.entries(value)) out[k] = resolveEnvVars(v, env)
|
|
38
|
+
return out
|
|
39
|
+
}
|
|
40
|
+
return value
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 多层配置加载器:内置 config.json5 + 用户 config.json5 + 环境变量注入内容(优先级从低到高)。
|
|
45
|
+
*
|
|
46
|
+
* 默认行为(无需配置):
|
|
47
|
+
* - 深合并(deepMerge),非浅合并
|
|
48
|
+
* - 结果默认解析 {env:XXX} 占位符
|
|
49
|
+
*
|
|
50
|
+
* 可选层(通过选项开启):
|
|
51
|
+
* - envContentVar: 指定环境变量名,其内容作为"注入配置"这一层参与合并(优先级最高,同样走深合并+占位符解析)
|
|
52
|
+
* - envDirVar: 指定环境变量名,其值覆盖用户配置目录
|
|
53
|
+
*
|
|
54
|
+
* 用法:
|
|
55
|
+
* const loader = createConfigLoader({
|
|
56
|
+
* builtinConfigPath: path.join(__dirname, 'config.json5'),
|
|
57
|
+
* userConfigDir: getXdgConfigDir('cft'),
|
|
58
|
+
* })
|
|
59
|
+
* loader.copyBuiltinConfig() // 复制内置→用户目录(不覆盖已存在)
|
|
60
|
+
* const cfg = loader.getConfig()
|
|
61
|
+
*/
|
|
62
|
+
export function createConfigLoader({ builtinConfigPath, userConfigDir, envContentVar = null, envDirVar = null }) {
|
|
63
|
+
const getUserDir = () => (envDirVar ? (process.env[envDirVar] ?? userConfigDir) : userConfigDir)
|
|
64
|
+
const getUserConfigPath = () => path.join(getUserDir(), 'config.json5')
|
|
65
|
+
|
|
66
|
+
function loadJson5(filePath) {
|
|
67
|
+
if (!filePath || !fs.existsSync(filePath)) return {}
|
|
68
|
+
try {
|
|
69
|
+
return JSON5.parse(fs.readFileSync(filePath, 'utf-8'))
|
|
70
|
+
} catch {
|
|
71
|
+
return {}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function loadContent(content) {
|
|
76
|
+
if (!content) return {}
|
|
77
|
+
try {
|
|
78
|
+
return JSON5.parse(content)
|
|
79
|
+
} catch {
|
|
80
|
+
return {}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function getConfig() {
|
|
85
|
+
const builtin = loadJson5(builtinConfigPath)
|
|
86
|
+
const user = loadJson5(getUserConfigPath())
|
|
87
|
+
let merged = deepMerge(builtin, user)
|
|
88
|
+
if (envContentVar) {
|
|
89
|
+
merged = deepMerge(merged, loadContent(process.env[envContentVar]))
|
|
90
|
+
}
|
|
91
|
+
return resolveEnvVars(merged)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function copyBuiltinConfig() {
|
|
95
|
+
const dir = getUserDir()
|
|
96
|
+
if (!fs.existsSync(dir)) {
|
|
97
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
98
|
+
}
|
|
99
|
+
const userPath = getUserConfigPath()
|
|
100
|
+
if (!fs.existsSync(userPath) && fs.existsSync(builtinConfigPath)) {
|
|
101
|
+
fs.copyFileSync(builtinConfigPath, userPath)
|
|
102
|
+
return true
|
|
103
|
+
}
|
|
104
|
+
return false
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
getConfig,
|
|
109
|
+
copyBuiltinConfig,
|
|
110
|
+
userConfigDir: getUserDir(),
|
|
111
|
+
userConfigPath: getUserConfigPath(),
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 获取 XDG 配置目录下的子路径。
|
|
117
|
+
* ~/.config/<subdir> 或 $XDG_CONFIG_HOME/<subdir>
|
|
118
|
+
*/
|
|
119
|
+
export function getXdgConfigDir(subdir) {
|
|
120
|
+
const xdg = process.env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config')
|
|
121
|
+
return path.join(xdg, subdir)
|
|
122
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createConfigLoader, getXdgConfigDir, deepMerge, resolveEnvVars } from './config-loader.js'
|