@wwkit/shared 1.0.8 → 1.0.10
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/package.json +1 -1
- package/src/config-loader.js +48 -0
package/package.json
CHANGED
package/src/config-loader.js
CHANGED
|
@@ -73,6 +73,27 @@ function resolvePkgPath(pkgName) {
|
|
|
73
73
|
return ''
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
/**
|
|
77
|
+
* 递归检查 builtin 是否有 user 缺少的 key(嵌套对象也递归比较)。
|
|
78
|
+
* @param {*} builtin
|
|
79
|
+
* @param {*} user
|
|
80
|
+
* @returns {boolean}
|
|
81
|
+
*/
|
|
82
|
+
function hasMissingKeys(builtin, user) {
|
|
83
|
+
if (!builtin || typeof builtin !== 'object' || Array.isArray(builtin)) return false
|
|
84
|
+
if (!user || typeof user !== 'object' || Array.isArray(user)) return false
|
|
85
|
+
for (const key of Object.keys(builtin)) {
|
|
86
|
+
if (!(key in user)) return true
|
|
87
|
+
const bv = builtin[key]
|
|
88
|
+
const uv = user[key]
|
|
89
|
+
if (bv && typeof bv === 'object' && !Array.isArray(bv)
|
|
90
|
+
&& uv && typeof uv === 'object' && !Array.isArray(uv)) {
|
|
91
|
+
if (hasMissingKeys(bv, uv)) return true
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return false
|
|
95
|
+
}
|
|
96
|
+
|
|
76
97
|
/**
|
|
77
98
|
* 递归合并对象,override 优先,嵌套对象深合并(数组/标量直接覆盖)。
|
|
78
99
|
*/
|
|
@@ -189,9 +210,36 @@ export function createConfigLoader({ builtinConfigPath, userConfigDir, envConten
|
|
|
189
210
|
return false
|
|
190
211
|
}
|
|
191
212
|
|
|
213
|
+
/**
|
|
214
|
+
* 合并内置配置到用户配置文件:
|
|
215
|
+
* 用户文件不存在 → 复制 builtin;已存在 → 追加缺失 key(不覆盖已有值)。
|
|
216
|
+
* @returns {boolean} 是否写入了文件
|
|
217
|
+
*/
|
|
218
|
+
function mergeBuiltinConfig() {
|
|
219
|
+
const dir = getUserDir()
|
|
220
|
+
if (!fs.existsSync(dir)) {
|
|
221
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
222
|
+
}
|
|
223
|
+
const userPath = getUserConfigPath()
|
|
224
|
+
if (!fs.existsSync(userPath)) {
|
|
225
|
+
if (fs.existsSync(builtinConfigPath)) {
|
|
226
|
+
fs.copyFileSync(builtinConfigPath, userPath)
|
|
227
|
+
return true
|
|
228
|
+
}
|
|
229
|
+
return false
|
|
230
|
+
}
|
|
231
|
+
const builtin = loadJson5(builtinConfigPath)
|
|
232
|
+
const user = loadJson5(userPath)
|
|
233
|
+
if (!hasMissingKeys(builtin, user)) return false
|
|
234
|
+
const merged = deepMerge(builtin, user)
|
|
235
|
+
fs.writeFileSync(userPath, `${JSON.stringify(merged, null, 2)}\n`, 'utf8')
|
|
236
|
+
return true
|
|
237
|
+
}
|
|
238
|
+
|
|
192
239
|
return {
|
|
193
240
|
getConfig,
|
|
194
241
|
copyBuiltinConfig,
|
|
242
|
+
mergeBuiltinConfig,
|
|
195
243
|
userConfigDir: getUserDir(),
|
|
196
244
|
userConfigPath: getUserConfigPath(),
|
|
197
245
|
}
|