tcdona_unilib 1.0.15 → 1.0.16
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/neverthrow.ts +1 -0
- package/package.json +1 -2
- package/staticMeta/file.yml.ts +23 -11
package/neverthrow.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tcdona_unilib",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"description": "Unified dependency aggregation layer for shared libraries",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -74,7 +74,6 @@
|
|
|
74
74
|
"exome": "^2.8.1",
|
|
75
75
|
"hono": "^4.11.3",
|
|
76
76
|
"hotkeys-js": "^4.0.0",
|
|
77
|
-
"js-yaml": "^4.1.1",
|
|
78
77
|
"koka": "^1.0.9",
|
|
79
78
|
"koka-domain": "^1.0.0",
|
|
80
79
|
"magic-regexp": "^0.10.0",
|
package/staticMeta/file.yml.ts
CHANGED
|
@@ -1,9 +1,31 @@
|
|
|
1
1
|
import { fs, YAML } from 'zx'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 深度排序对象的所有键,数组本身不排序但会递归处理数组内的对象
|
|
5
|
+
*/
|
|
6
|
+
export const sortKeys = (value: unknown): unknown => {
|
|
7
|
+
if (value == null || typeof value !== 'object') return value
|
|
8
|
+
if (Array.isArray(value)) return value.map(sortKeys)
|
|
9
|
+
const sorted: Record<string, unknown> = {}
|
|
10
|
+
for (const key of Object.keys(value).sort()) {
|
|
11
|
+
sorted[key] = sortKeys((value as Record<string, unknown>)[key])
|
|
12
|
+
}
|
|
13
|
+
return sorted
|
|
14
|
+
}
|
|
15
|
+
|
|
2
16
|
/**
|
|
3
17
|
* YAML 文件读写:无 data 参数时读取,有 data 参数时写入
|
|
4
18
|
*/
|
|
5
|
-
export function yml(
|
|
19
|
+
export function yml(
|
|
20
|
+
path: string,
|
|
21
|
+
data?: unknown,
|
|
22
|
+
sorted: boolean = true,
|
|
23
|
+
): unknown {
|
|
6
24
|
if (typeof data !== 'undefined') {
|
|
25
|
+
// 针对 data 进行深度遍历,对里面的所有 kv 形式的对象的 key 进行排序
|
|
26
|
+
if (sorted === true) {
|
|
27
|
+
data = sortKeys(data)
|
|
28
|
+
}
|
|
7
29
|
fs.writeFileSync(
|
|
8
30
|
path,
|
|
9
31
|
// @ts-expect-error YAML.stringify 类型定义有误
|
|
@@ -15,13 +37,3 @@ export function yml(path: string, data?: unknown): unknown {
|
|
|
15
37
|
return YAML.parse(ymlContent)
|
|
16
38
|
}
|
|
17
39
|
}
|
|
18
|
-
|
|
19
|
-
import { default as jsYAML } from 'js-yaml'
|
|
20
|
-
export const yml2 = (path: string, data?: unknown): unknown => {
|
|
21
|
-
if (typeof data !== 'undefined') {
|
|
22
|
-
fs.writeFileSync(path, jsYAML.dump(data, { sortKeys: true }), 'utf-8')
|
|
23
|
-
} else {
|
|
24
|
-
const ymlContent = fs.readFileSync(path, 'utf-8')
|
|
25
|
-
return jsYAML.load(ymlContent)
|
|
26
|
-
}
|
|
27
|
-
}
|