@wwkit/shared 1.0.30 → 1.0.31

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 CHANGED
@@ -1,21 +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
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
21
  SOFTWARE.
package/README.md CHANGED
@@ -1,35 +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}` 占位符
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
35
  - `getXdgConfigDir(subdir)` — `~/.config/<subdir>` 或 `$XDG_CONFIG_HOME/<subdir>`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wwkit/shared",
3
- "version": "1.0.30",
3
+ "version": "1.0.31",
4
4
  "author": "bluesliu <langcai163@163.com>",
5
5
  "description": "Shared utilities for @wwkit packages — config loader, platform detection",
6
6
  "type": "module",
package/src/args.js CHANGED
@@ -1,79 +1,79 @@
1
- /**
2
- * 共享命令行参数解析器 — @wwkit/shared 统一参数解析
3
- *
4
- * 支持以下格式的参数:
5
- * --flag value 长标志(空格分隔)
6
- * --flag=value 长标志(等号形式)
7
- * -f value 短标志(空格分隔)
8
- * -f=value 短标志(等号形式)
9
- * positional 位置参数(不以 - 开头)
10
- *
11
- * 布尔标志(见 DEFAULT_BOOLEAN_FLAGS 与 opts.booleans)不消费下一个参数:
12
- * --json、-g、-y 等后跟普通 token 时,token 保持为位置参数/值参数,
13
- * 避免 `opm npm install -g typescript` 将 typescript 吞为 -g 的值。
14
- * 上下文相关的布尔标志(如 proc 的 -f/--force)由
15
- * 调用方通过 opts.booleans 声明。
16
- *
17
- * @param {string[]} argv - 原始参数数组
18
- * @param {{ booleans?: string[] }} [opts] - 上下文布尔标志名(不含 -/-- 前缀)
19
- * @returns {{ flags: Record<string, string>, positional: string[] }}
20
- */
21
- export const DEFAULT_BOOLEAN_FLAGS = new Set([
22
- 'h', 'help',
23
- 'g', 'global',
24
- 'y', 'yes',
25
- 'a', 'all',
26
- 'j', 'json',
27
- 'exact',
28
- 'dry-run',
29
- 'backup',
30
- 'delete',
31
- ])
32
-
33
- export function parseFlags(argv, opts = {}) {
34
- const booleans = new Set([...DEFAULT_BOOLEAN_FLAGS, ...(opts.booleans || [])])
35
- const flags = {}
36
- const positional = []
37
-
38
- for (let i = 0; i < argv.length; i++) {
39
- const arg = argv[i]
40
-
41
- if (arg.startsWith('--') && arg.includes('=')) {
42
- const idx = arg.indexOf('=')
43
- flags[arg.slice(2, idx)] = arg.slice(idx + 1)
44
- } else if (arg.startsWith('--')) {
45
- const name = arg.slice(2)
46
- if (booleans.has(name)) {
47
- flags[name] = 'true'
48
- } else {
49
- const next = argv[i + 1]
50
- if (next !== undefined && !next.startsWith('-')) {
51
- flags[name] = next
52
- i++
53
- } else {
54
- flags[name] = 'true'
55
- }
56
- }
57
- } else if (arg.startsWith('-') && arg.includes('=')) {
58
- const idx = arg.indexOf('=')
59
- flags[arg.slice(1, idx)] = arg.slice(idx + 1)
60
- } else if (arg.startsWith('-') && arg.length > 1) {
61
- const name = arg.slice(1)
62
- if (booleans.has(name)) {
63
- flags[name] = 'true'
64
- } else {
65
- const next = argv[i + 1]
66
- if (next !== undefined && !next.startsWith('-')) {
67
- flags[name] = next
68
- i++
69
- } else {
70
- flags[name] = 'true'
71
- }
72
- }
73
- } else {
74
- positional.push(arg)
75
- }
76
- }
77
-
78
- return { flags, positional }
79
- }
1
+ /**
2
+ * 共享命令行参数解析器 — @wwkit/shared 统一参数解析
3
+ *
4
+ * 支持以下格式的参数:
5
+ * --flag value 长标志(空格分隔)
6
+ * --flag=value 长标志(等号形式)
7
+ * -f value 短标志(空格分隔)
8
+ * -f=value 短标志(等号形式)
9
+ * positional 位置参数(不以 - 开头)
10
+ *
11
+ * 布尔标志(见 DEFAULT_BOOLEAN_FLAGS 与 opts.booleans)不消费下一个参数:
12
+ * --json、-g、-y 等后跟普通 token 时,token 保持为位置参数/值参数,
13
+ * 避免 `opm npm install -g typescript` 将 typescript 吞为 -g 的值。
14
+ * 上下文相关的布尔标志(如 proc 的 -f/--force)由
15
+ * 调用方通过 opts.booleans 声明。
16
+ *
17
+ * @param {string[]} argv - 原始参数数组
18
+ * @param {{ booleans?: string[] }} [opts] - 上下文布尔标志名(不含 -/-- 前缀)
19
+ * @returns {{ flags: Record<string, string>, positional: string[] }}
20
+ */
21
+ export const DEFAULT_BOOLEAN_FLAGS = new Set([
22
+ 'h', 'help',
23
+ 'g', 'global',
24
+ 'y', 'yes',
25
+ 'a', 'all',
26
+ 'j', 'json',
27
+ 'exact',
28
+ 'dry-run',
29
+ 'backup',
30
+ 'delete',
31
+ ])
32
+
33
+ export function parseFlags(argv, opts = {}) {
34
+ const booleans = new Set([...DEFAULT_BOOLEAN_FLAGS, ...(opts.booleans || [])])
35
+ const flags = {}
36
+ const positional = []
37
+
38
+ for (let i = 0; i < argv.length; i++) {
39
+ const arg = argv[i]
40
+
41
+ if (arg.startsWith('--') && arg.includes('=')) {
42
+ const idx = arg.indexOf('=')
43
+ flags[arg.slice(2, idx)] = arg.slice(idx + 1)
44
+ } else if (arg.startsWith('--')) {
45
+ const name = arg.slice(2)
46
+ if (booleans.has(name)) {
47
+ flags[name] = 'true'
48
+ } else {
49
+ const next = argv[i + 1]
50
+ if (next !== undefined && !next.startsWith('-')) {
51
+ flags[name] = next
52
+ i++
53
+ } else {
54
+ flags[name] = 'true'
55
+ }
56
+ }
57
+ } else if (arg.startsWith('-') && arg.includes('=')) {
58
+ const idx = arg.indexOf('=')
59
+ flags[arg.slice(1, idx)] = arg.slice(idx + 1)
60
+ } else if (arg.startsWith('-') && arg.length > 1) {
61
+ const name = arg.slice(1)
62
+ if (booleans.has(name)) {
63
+ flags[name] = 'true'
64
+ } else {
65
+ const next = argv[i + 1]
66
+ if (next !== undefined && !next.startsWith('-')) {
67
+ flags[name] = next
68
+ i++
69
+ } else {
70
+ flags[name] = 'true'
71
+ }
72
+ }
73
+ } else {
74
+ positional.push(arg)
75
+ }
76
+ }
77
+
78
+ return { flags, positional }
79
+ }