@yarn-tool/pkg-entry-util 3.0.3 → 3.0.4
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/CHANGELOG.md +50 -0
- package/README.md +155 -3
- package/lib/field/bin.d.ts +27 -0
- package/lib/field/bin.js +29 -0
- package/lib/field/bin.js.map +1 -1
- package/lib/field/exports.d.ts +48 -0
- package/lib/field/exports.js +59 -4
- package/lib/field/exports.js.map +1 -1
- package/lib/field/fixEmpty.d.ts +43 -0
- package/lib/field/fixEmpty.js +58 -0
- package/lib/field/fixEmpty.js.map +1 -0
- package/lib/field/publishConfig.d.ts +18 -0
- package/lib/field/publishConfig.js +24 -0
- package/lib/field/publishConfig.js.map +1 -1
- package/lib/index.d.ts +30 -0
- package/lib/index.js +30 -0
- package/lib/index.js.map +1 -1
- package/lib/preset/scripts/dummy.d.ts +58 -0
- package/lib/preset/scripts/dummy.js +58 -0
- package/lib/preset/scripts/dummy.js.map +1 -1
- package/lib/preset/scripts/index.d.ts +56 -0
- package/lib/preset/scripts/index.js +68 -0
- package/lib/preset/scripts/index.js.map +1 -0
- package/lib/preset/scripts/pkg-scripts.d.ts +50 -0
- package/lib/preset/scripts/pkg-scripts.js +56 -6
- package/lib/preset/scripts/pkg-scripts.js.map +1 -1
- package/lib/preset/scripts/root-scripts.d.ts +39 -1
- package/lib/preset/scripts/root-scripts.js +48 -4
- package/lib/preset/scripts/root-scripts.js.map +1 -1
- package/lib/preset/scripts/shared-root-scripts.d.ts +36 -0
- package/lib/preset/scripts/shared-root-scripts.js +38 -1
- package/lib/preset/scripts/shared-root-scripts.js.map +1 -1
- package/lib/preset/scripts/ws-root-scripts.d.ts +76 -0
- package/lib/preset/scripts/ws-root-scripts.js +93 -8
- package/lib/preset/scripts/ws-root-scripts.js.map +1 -1
- package/package.json +18 -8
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,56 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [3.0.4](https://github.com/bluelovers/ws-yarn-workspaces/compare/@yarn-tool/pkg-entry-util@3.0.3...@yarn-tool/pkg-entry-util@3.0.4) (2026-03-02)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### BREAKING CHANGES
|
|
10
|
+
|
|
11
|
+
* **release:** 核心工具鏈套件升級
|
|
12
|
+
- @lazy-node/semver-simple-parse v4.0.0
|
|
13
|
+
- @yarn-tool/require-resolve v4.0.0
|
|
14
|
+
- @yarn-tool/resolve-package v3.0.0
|
|
15
|
+
- @yarn-tool/get-pkg-bin v3.0.0
|
|
16
|
+
- @yarn-tool/check-pkg-bin v4.0.0
|
|
17
|
+
|
|
18
|
+
更新所有工作區套件以使用相容的主要版本,並重構
|
|
19
|
+
npm-package-arg-util 以抽離 _parsePackageNameCore 核心函式。
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### ✨ Features
|
|
24
|
+
|
|
25
|
+
* **pkg-entry-util:** 將預設腳本執行從 yarn run 遷移至 node --run 並新增 pnpm 支援 ([9a414f0](https://github.com/bluelovers/ws-yarn-workspaces/commit/9a414f0b9b8f2970b539ee6ef45708285af29e13))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### 📚 Documentation
|
|
29
|
+
|
|
30
|
+
* 為 fix-all、pkg-entry-util、node-package-json-loader 添加完整的 JSDoc 文檔註釋 ([92d17e5](https://github.com/bluelovers/ws-yarn-workspaces/commit/92d17e5866c9330c4ee66e78b34a7318bb7d3203))
|
|
31
|
+
* **pkg-entry-util,create-yarn-workspaces,npm-init2:** 新增完整雙語文件與 JSDoc 註解 ([c1786e6](https://github.com/bluelovers/ws-yarn-workspaces/commit/c1786e6785abe484c95ee3dee8b8a16358c3904b))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
### 🛠 Build System
|
|
35
|
+
|
|
36
|
+
* update build ([51d49ad](https://github.com/bluelovers/ws-yarn-workspaces/commit/51d49adaa1f1fbfac4a299007dbf406d7ee7a4c7))
|
|
37
|
+
* **pkg-entry-util:** 將腳本執行從 yarn run 遷移至 node --run 並新增 pnpm 支援 ([a2e0302](https://github.com/bluelovers/ws-yarn-workspaces/commit/a2e0302758049af2067e4d0f67f7cc8792f56df2))
|
|
38
|
+
* **pkg-entry-util,create-yarn-workspaces,npm-init2:** 新增完整雙語文件與 JSDoc 註解 ([087d6d6](https://github.com/bluelovers/ws-yarn-workspaces/commit/087d6d6e560c70ed68a79ad73c8eb05868ec4203))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### ♻️ Chores
|
|
42
|
+
|
|
43
|
+
* Add test config and global types for tsdx ([88a8df6](https://github.com/bluelovers/ws-yarn-workspaces/commit/88a8df6eef5dc59d9e0d9268b21706dceb5ab2a1))
|
|
44
|
+
* add __root.ts test utility to workspace packages ([a771727](https://github.com/bluelovers/ws-yarn-workspaces/commit/a7717278883c86d9ed8ab89f42070d3385c173a7))
|
|
45
|
+
* **deps:** 升級路徑與檔案系統相關依賴版本 ([78d395b](https://github.com/bluelovers/ws-yarn-workspaces/commit/78d395b5469feecc85f250ca2d8b4977161e7618))
|
|
46
|
+
* **deps:** 更新依賴版本並將腳本執行從 yarn 遷移至 node --run ([389e581](https://github.com/bluelovers/ws-yarn-workspaces/commit/389e581a07cf803dc34c8402b4d3f69af0016b8c))
|
|
47
|
+
* **deps:** 升級多項依賴套件版本 ([1382602](https://github.com/bluelovers/ws-yarn-workspaces/commit/1382602ca94ff236b251fd1481b6332468de4621))
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### 📌 Dependencies
|
|
51
|
+
|
|
52
|
+
* **release:** 主要版本升級與跨套件相依更新 ([2a24889](https://github.com/bluelovers/ws-yarn-workspaces/commit/2a248893f3667704d1ba0de12544091abf5d341e))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
6
56
|
## [3.0.3](https://github.com/bluelovers/ws-yarn-workspaces/compare/@yarn-tool/pkg-entry-util@3.0.2...@yarn-tool/pkg-entry-util@3.0.3) (2025-09-07)
|
|
7
57
|
|
|
8
58
|
**Note:** Version bump only for package @yarn-tool/pkg-entry-util
|
package/README.md
CHANGED
|
@@ -1,12 +1,164 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yarn-tool/pkg-entry-util
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
用於處理 package.json entry 相關欄位的工具庫,包含 bin、exports、publishConfig 欄位的修復與驗證功能
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Utility library for handling package.json entry-related fields, including fix and verification for bin, exports, and publishConfig fields
|
|
6
|
+
|
|
7
|
+
## Install
|
|
6
8
|
|
|
7
9
|
```bash
|
|
8
10
|
yarn add @yarn-tool/pkg-entry-util
|
|
9
11
|
yarn-tool add @yarn-tool/pkg-entry-util
|
|
10
12
|
yt add @yarn-tool/pkg-entry-util
|
|
13
|
+
|
|
14
|
+
pnpm add @yarn-tool/pkg-entry-util
|
|
15
|
+
|
|
16
|
+
npm install @yarn-tool/pkg-entry-util
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
### Entry Field Utilities / Entry 欄位工具
|
|
22
|
+
|
|
23
|
+
- `fixPkgBinField` - 修復 package.json 的 bin 欄位路徑 / Fix package.json bin field paths
|
|
24
|
+
- `pkgExportsAddPJsonEntry` - 在 exports 欄位中添加 package.json entry / Add package.json entry to exports field
|
|
25
|
+
- `pkgExportsVerify` - 驗證 package exports 路徑是否存在 / Verify package exports paths exist
|
|
26
|
+
- `fixPublishConfig` - 修復 publishConfig 欄位 / Fix publishConfig field
|
|
27
|
+
|
|
28
|
+
### Scripts Presets / 腳本預設
|
|
29
|
+
|
|
30
|
+
此模組提供各類套件的預設 npm scripts,支援 monorepo 與獨立套件開發流程
|
|
31
|
+
|
|
32
|
+
This module provides default npm scripts for various package types, supporting monorepo and standalone package development workflows
|
|
33
|
+
|
|
34
|
+
#### 使用方式 / Usage
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
// 工作區根目錄 (Yarn/Lerna Workspaces Root)
|
|
38
|
+
import { defaultWorkspaceRootScripts } from '@yarn-tool/pkg-entry-util/lib/preset/scripts/ws-root-scripts';
|
|
39
|
+
|
|
40
|
+
const wsScripts = defaultWorkspaceRootScripts();
|
|
41
|
+
// 包含跨工作區測試、建構、發布等腳本
|
|
42
|
+
// Includes cross-workspace test, build, publish scripts
|
|
43
|
+
|
|
44
|
+
// 一般套件 (Regular Package)
|
|
45
|
+
import { defaultPkgScripts, defaultPkgNotOldExists } from '@yarn-tool/pkg-entry-util/lib/preset/scripts/pkg-scripts';
|
|
46
|
+
|
|
47
|
+
const pkgScripts = defaultPkgScripts();
|
|
48
|
+
// 基本測試腳本:test, test:jest, coverage, test:tsd
|
|
49
|
+
// Basic test scripts: test, test:jest, coverage, test:tsd
|
|
50
|
+
|
|
51
|
+
const newPkgScripts = defaultPkgNotOldExists();
|
|
52
|
+
// 新套件完整腳本:包含建構、lint、review 等
|
|
53
|
+
// Complete scripts for new packages: includes build, lint, review, etc.
|
|
54
|
+
|
|
55
|
+
// 根目錄套件 (Root Package - Non-workspace)
|
|
56
|
+
import { defaultRootScripts } from '@yarn-tool/pkg-entry-util/lib/preset/scripts/root-scripts';
|
|
57
|
+
|
|
58
|
+
const rootScripts = defaultRootScripts();
|
|
59
|
+
// 版本管理與發布腳本:version:bump, npm:publish, postpublish 等
|
|
60
|
+
// Version management and publishing scripts: version:bump, npm:publish, postpublish, etc.
|
|
61
|
+
|
|
62
|
+
// 填充虛擬腳本 (Fill Dummy Scripts)
|
|
63
|
+
import { fillDummyScripts } from '@yarn-tool/pkg-entry-util/lib/preset/scripts/dummy';
|
|
64
|
+
|
|
65
|
+
const scripts = {};
|
|
66
|
+
fillDummyScripts(scripts, 'my-package');
|
|
67
|
+
// 為生命週期腳本填充 echo 佔位符
|
|
68
|
+
// Fill echo placeholders for lifecycle scripts
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### 預設腳本分類 / Preset Categories
|
|
72
|
+
|
|
73
|
+
| 函數 / Function | 適用場景 / Use Case | 主要功能 / Main Features |
|
|
74
|
+
|----------------|--------------------|-------------------------|
|
|
75
|
+
| `defaultPkgScripts` | 一般套件 / Regular packages | 測試、覆蓋率、型別檢查 / Test, coverage, type check |
|
|
76
|
+
| `defaultPkgNotOldExists` | 新建立套件 / New packages | 完整開發腳本(建構、lint、review)/ Complete dev scripts (build, lint, review) |
|
|
77
|
+
| `defaultRootScripts` | 獨立套件根目錄 / Standalone package root | 版本遞增、發布、changelog / Version bump, publish, changelog |
|
|
78
|
+
| `defaultWorkspaceRootScripts` | 工作區根目錄 / Workspace root | 跨工作區執行、Lerna 發布 / Cross-workspace execution, Lerna publish |
|
|
79
|
+
| `defaultSharedRootScripts` | 共用根目錄腳本 / Shared root scripts | CI 安裝、依賴清理 / CI install, dedupe |
|
|
80
|
+
| `fillDummyScripts` | 所有套件類型 / All package types | 生命週期腳本佔位符 / Lifecycle script placeholders |
|
|
81
|
+
|
|
82
|
+
#### 腳本預設詳細說明 / Scripts Preset Details
|
|
83
|
+
|
|
84
|
+
##### defaultWorkspaceRootScripts / 工作區根目錄腳本
|
|
85
|
+
|
|
86
|
+
適用於 `create-yarn-workspaces` 建立的工作區根目錄
|
|
87
|
+
|
|
88
|
+
For workspace root created by `create-yarn-workspaces`
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { defaultWorkspaceRootScripts } from '@yarn-tool/pkg-entry-util/lib/preset/scripts/ws-root-scripts';
|
|
92
|
+
|
|
93
|
+
const scripts = defaultWorkspaceRootScripts();
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
包含腳本 / Includes:
|
|
97
|
+
- **測試相關 / Test**: `test`, `test:all`, `test:since`, `test:tsd:all`, `test:tsd:since`
|
|
98
|
+
- **建構相關 / Build**: `build:all`, `build:since`
|
|
99
|
+
- **程式碼品質 / Quality**: `lint:all`, `lint:since`, `review:all`, `review:since`, `coverage:all`, `coverage:since`
|
|
100
|
+
- **發布相關 / Publish**: `lerna:publish`, `lerna:publish:yes:patch\|minor\|major`, `prepublishOnly:root`
|
|
101
|
+
- **維護相關 / Maintenance**: `ncu:root`, `ncu:ws`, `sort-package-json`, `prepare:fix-ws-links`
|
|
102
|
+
|
|
103
|
+
##### defaultRootScripts / 根目錄腳本
|
|
104
|
+
|
|
105
|
+
適用於獨立套件或 monorepo 根目錄(非工作區)
|
|
106
|
+
|
|
107
|
+
For standalone packages or monorepo root (non-workspace)
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { defaultRootScripts } from '@yarn-tool/pkg-entry-util/lib/preset/scripts/root-scripts';
|
|
111
|
+
|
|
112
|
+
const scripts = defaultRootScripts();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
包含腳本 / Includes:
|
|
116
|
+
- **版本管理 / Version**: `version:bump`, `version:bump:patch\|minor\|major\|prerelease`
|
|
117
|
+
- **發布流程 / Publishing**: `npm:publish`, `npm:publish:bump`, `prepublishOnly:check-bin`
|
|
118
|
+
- **發布後自動化 / Post-publish**: `postpublish`, `postpublish:git:commit`, `postpublish:git:tag`, `postpublish:changelog`
|
|
119
|
+
- **依賴更新 / Dependencies**: `ncu`, `pnpm:dedupe`, `ncu:pnpm`
|
|
120
|
+
|
|
121
|
+
##### defaultPkgScripts / 套件腳本
|
|
122
|
+
|
|
123
|
+
適用於一般套件專案
|
|
124
|
+
|
|
125
|
+
For general package projects
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { defaultPkgScripts } from '@yarn-tool/pkg-entry-util/lib/preset/scripts/pkg-scripts';
|
|
129
|
+
|
|
130
|
+
const scripts = defaultPkgScripts();
|
|
11
131
|
```
|
|
12
132
|
|
|
133
|
+
包含腳本 / Includes:
|
|
134
|
+
- `test` / `test:jest` - 執行 Jest 測試 / Run Jest tests
|
|
135
|
+
- `coverage` - 測試含覆蓋率報告 / Test with coverage report
|
|
136
|
+
- `test:snapshot` / `test:jest:snapshot` - 更新測試快照 / Update test snapshots
|
|
137
|
+
- `test:tsd` - TypeScript 型別檢查 / TypeScript type checking
|
|
138
|
+
- `tsc:showConfig` - 顯示 TypeScript 設定 / Show TypeScript config
|
|
139
|
+
|
|
140
|
+
##### fillDummyScripts / 填充虛擬腳本
|
|
141
|
+
|
|
142
|
+
為 package.json 的生命週期腳本填充 echo 佔位符
|
|
143
|
+
|
|
144
|
+
Fill echo placeholders for package.json lifecycle scripts
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
import { fillDummyScripts } from '@yarn-tool/pkg-entry-util/lib/preset/scripts/dummy';
|
|
148
|
+
|
|
149
|
+
const pkg = { scripts: {} };
|
|
150
|
+
fillDummyScripts(pkg.scripts, 'workspaces');
|
|
151
|
+
|
|
152
|
+
// 結果 / Result:
|
|
153
|
+
// pkg.scripts = {
|
|
154
|
+
// preversion: 'echo workspaces preversion',
|
|
155
|
+
// version: 'echo workspaces version',
|
|
156
|
+
// prepublishOnly: 'echo workspaces prepublishOnly',
|
|
157
|
+
// // ... 其他生命週期腳本 / other lifecycle scripts
|
|
158
|
+
// }
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## 相關模組 / Related Modules
|
|
162
|
+
|
|
163
|
+
- [create-yarn-workspaces](https://www.npmjs.com/package/create-yarn-workspaces) - 使用 `defaultWorkspaceRootScripts` 建立工作區
|
|
164
|
+
- [npm-init2](https://www.npmjs.com/package/npm-init2) - 使用 `fillDummyScripts` 和 `defaultRootScripts` 初始化套件
|
package/lib/field/bin.d.ts
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 處理 package.json entry 相關欄位的工具模組
|
|
3
|
+
* Utility module for handling package.json entry-related fields
|
|
4
|
+
*/
|
|
1
5
|
import { IPackageJson } from '@ts-type/package-dts/package-json';
|
|
6
|
+
/**
|
|
7
|
+
* 修復 bin 欄位的路徑
|
|
8
|
+
* Fix the path of bin field
|
|
9
|
+
*
|
|
10
|
+
* 檢查 bin 檔案是否存在於根目錄,若不存在則檢查是否在 bin/ 子目錄中
|
|
11
|
+
* Check if bin file exists in root directory, if not, check if it's in bin/ subdirectory
|
|
12
|
+
*
|
|
13
|
+
* @param {string} bin - bin 欄位的原始路徑 / Original path from bin field
|
|
14
|
+
* @param {string} root - 套件根目錄路徑 / Package root directory path
|
|
15
|
+
* @returns {string | null} 修正後的路徑或 null(若無需修正)/ Corrected path or null (if no fix needed)
|
|
16
|
+
*/
|
|
2
17
|
export declare function fixBinPath(bin: string, root: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* 修復 package.json 的 bin 欄位路徑
|
|
20
|
+
* Fix bin field paths in package.json
|
|
21
|
+
*
|
|
22
|
+
* 支援字串格式與物件格式的 bin 欄位,自動修正不正確的路徑
|
|
23
|
+
* Supports both string and object format bin fields, automatically corrects incorrect paths
|
|
24
|
+
*
|
|
25
|
+
* @template T - package.json 類型 / package.json type
|
|
26
|
+
* @param {T} pkg - package.json 物件 / package.json object
|
|
27
|
+
* @param {string} root - 套件根目錄路徑 / Package root directory path
|
|
28
|
+
* @returns {T} 修正後的 package.json 物件 / Corrected package.json object
|
|
29
|
+
*/
|
|
3
30
|
export declare function fixPkgBinField<T extends IPackageJson>(pkg: T, root: string): T;
|
package/lib/field/bin.js
CHANGED
|
@@ -4,15 +4,42 @@ exports.fixBinPath = fixBinPath;
|
|
|
4
4
|
exports.fixPkgBinField = fixPkgBinField;
|
|
5
5
|
const fs_extra_1 = require("fs-extra");
|
|
6
6
|
const upath2_1 = require("upath2");
|
|
7
|
+
/**
|
|
8
|
+
* 修復 bin 欄位的路徑
|
|
9
|
+
* Fix the path of bin field
|
|
10
|
+
*
|
|
11
|
+
* 檢查 bin 檔案是否存在於根目錄,若不存在則檢查是否在 bin/ 子目錄中
|
|
12
|
+
* Check if bin file exists in root directory, if not, check if it's in bin/ subdirectory
|
|
13
|
+
*
|
|
14
|
+
* @param {string} bin - bin 欄位的原始路徑 / Original path from bin field
|
|
15
|
+
* @param {string} root - 套件根目錄路徑 / Package root directory path
|
|
16
|
+
* @returns {string | null} 修正後的路徑或 null(若無需修正)/ Corrected path or null (if no fix needed)
|
|
17
|
+
*/
|
|
7
18
|
function fixBinPath(bin, root) {
|
|
19
|
+
// 檢查檔案是否存在於指定路徑,若不存在但存在於 bin/ 子目錄則返回修正路徑
|
|
20
|
+
// Check if file exists at specified path, if not but exists in bin/ subdirectory, return corrected path
|
|
8
21
|
if (!(0, fs_extra_1.existsSync)((0, upath2_1.join)(root, bin))
|
|
9
22
|
&& (0, fs_extra_1.existsSync)((0, upath2_1.join)(root, 'bin', bin))) {
|
|
10
23
|
return (0, upath2_1.join)('.', 'bin', bin);
|
|
11
24
|
}
|
|
12
25
|
return null;
|
|
13
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* 修復 package.json 的 bin 欄位路徑
|
|
29
|
+
* Fix bin field paths in package.json
|
|
30
|
+
*
|
|
31
|
+
* 支援字串格式與物件格式的 bin 欄位,自動修正不正確的路徑
|
|
32
|
+
* Supports both string and object format bin fields, automatically corrects incorrect paths
|
|
33
|
+
*
|
|
34
|
+
* @template T - package.json 類型 / package.json type
|
|
35
|
+
* @param {T} pkg - package.json 物件 / package.json object
|
|
36
|
+
* @param {string} root - 套件根目錄路徑 / Package root directory path
|
|
37
|
+
* @returns {T} 修正後的 package.json 物件 / Corrected package.json object
|
|
38
|
+
*/
|
|
14
39
|
function fixPkgBinField(pkg, root) {
|
|
15
40
|
if (pkg.bin) {
|
|
41
|
+
// 處理字串格式的 bin 欄位
|
|
42
|
+
// Handle string format bin field
|
|
16
43
|
if (typeof pkg.bin === 'string') {
|
|
17
44
|
let bin_new = fixBinPath(pkg.bin, root);
|
|
18
45
|
if (bin_new) {
|
|
@@ -20,6 +47,8 @@ function fixPkgBinField(pkg, root) {
|
|
|
20
47
|
pkg.bin = bin_new;
|
|
21
48
|
}
|
|
22
49
|
}
|
|
50
|
+
// 處理物件格式的 bin 欄位(多個執行檔)
|
|
51
|
+
// Handle object format bin field (multiple executables)
|
|
23
52
|
else if (typeof pkg.bin === 'object' && !Array.isArray(pkg.bin)) {
|
|
24
53
|
Object.keys(pkg.bin)
|
|
25
54
|
.forEach(function (key) {
|
package/lib/field/bin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bin.js","sourceRoot":"","sources":["bin.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["bin.ts"],"names":[],"mappings":";;AAmBA,gCAaC;AAcD,wCAsCC;AA/ED,uCAAsC;AACtC,mCAAqC;AAErC;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CAAC,GAAW,EAAE,IAAY;IAEnD,yCAAyC;IACzC,wGAAwG;IACxG,IACC,CAAC,IAAA,qBAAU,EAAC,IAAA,aAAI,EAAC,IAAI,EAAE,GAAG,CAAC,CAAC;WACzB,IAAA,qBAAU,EAAC,IAAA,aAAI,EAAC,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC,EAEtC,CAAC;QACA,OAAO,IAAA,aAAI,EAAC,GAAG,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;IAC9B,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,cAAc,CAAyB,GAAM,EAAE,IAAY;IAE1E,IAAI,GAAG,CAAC,GAAG,EACX,CAAC;QACA,iBAAiB;QACjB,iCAAiC;QACjC,IAAI,OAAO,GAAG,CAAC,GAAG,KAAK,QAAQ,EAC/B,CAAC;YACA,IAAI,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YAExC,IAAI,OAAO,EACX,CAAC;gBACA,aAAa;gBACb,GAAG,CAAC,GAAG,GAAG,OAAO,CAAC;YACnB,CAAC;QACF,CAAC;QACD,wBAAwB;QACxB,wDAAwD;aACnD,IAAI,OAAO,GAAG,CAAC,GAAG,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAC/D,CAAC;YACA,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;iBAClB,OAAO,CAAC,UAAU,GAAG;gBAErB,IAAI,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,QAAQ,EACpC,CAAC;oBACA,IAAI,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;oBAE7C,IAAI,OAAO,EACX,CAAC;wBACA,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC;oBACxB,CAAC;gBACF,CAAC;YACF,CAAC,CAAC,CACF;QACF,CAAC;IACF,CAAC;IAED,OAAO,GAAG,CAAA;AACX,CAAC","sourcesContent":["/**\n * 處理 package.json entry 相關欄位的工具模組\n * Utility module for handling package.json entry-related fields\n */\nimport { IPackageJson } from '@ts-type/package-dts/package-json';\nimport { existsSync } from 'fs-extra';\nimport { join, posix } from 'upath2';\n\n/**\n * 修復 bin 欄位的路徑\n * Fix the path of bin field\n *\n * 檢查 bin 檔案是否存在於根目錄,若不存在則檢查是否在 bin/ 子目錄中\n * Check if bin file exists in root directory, if not, check if it's in bin/ subdirectory\n *\n * @param {string} bin - bin 欄位的原始路徑 / Original path from bin field\n * @param {string} root - 套件根目錄路徑 / Package root directory path\n * @returns {string | null} 修正後的路徑或 null(若無需修正)/ Corrected path or null (if no fix needed)\n */\nexport function fixBinPath(bin: string, root: string)\n{\n\t// 檢查檔案是否存在於指定路徑,若不存在但存在於 bin/ 子目錄則返回修正路徑\n\t// Check if file exists at specified path, if not but exists in bin/ subdirectory, return corrected path\n\tif (\n\t\t!existsSync(join(root, bin))\n\t\t&& existsSync(join(root, 'bin', bin))\n\t)\n\t{\n\t\treturn join('.', 'bin', bin);\n\t}\n\n\treturn null;\n}\n\n/**\n * 修復 package.json 的 bin 欄位路徑\n * Fix bin field paths in package.json\n *\n * 支援字串格式與物件格式的 bin 欄位,自動修正不正確的路徑\n * Supports both string and object format bin fields, automatically corrects incorrect paths\n *\n * @template T - package.json 類型 / package.json type\n * @param {T} pkg - package.json 物件 / package.json object\n * @param {string} root - 套件根目錄路徑 / Package root directory path\n * @returns {T} 修正後的 package.json 物件 / Corrected package.json object\n */\nexport function fixPkgBinField<T extends IPackageJson>(pkg: T, root: string)\n{\n\tif (pkg.bin)\n\t{\n\t\t// 處理字串格式的 bin 欄位\n\t\t// Handle string format bin field\n\t\tif (typeof pkg.bin === 'string')\n\t\t{\n\t\t\tlet bin_new = fixBinPath(pkg.bin, root);\n\n\t\t\tif (bin_new)\n\t\t\t{\n\t\t\t\t// @ts-ignore\n\t\t\t\tpkg.bin = bin_new;\n\t\t\t}\n\t\t}\n\t\t// 處理物件格式的 bin 欄位(多個執行檔)\n\t\t// Handle object format bin field (multiple executables)\n\t\telse if (typeof pkg.bin === 'object' && !Array.isArray(pkg.bin))\n\t\t{\n\t\t\tObject.keys(pkg.bin)\n\t\t\t\t.forEach(function (key)\n\t\t\t\t{\n\t\t\t\t\tif (typeof pkg.bin[key] === 'string')\n\t\t\t\t\t{\n\t\t\t\t\t\tlet bin_new = fixBinPath(pkg.bin[key], root);\n\n\t\t\t\t\t\tif (bin_new)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpkg.bin[key] = bin_new;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t;\n\t\t}\n\t}\n\n\treturn pkg\n}\n"]}
|
package/lib/field/exports.d.ts
CHANGED
|
@@ -1,8 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 處理 package.json exports 欄位的工具模組
|
|
3
|
+
* Utility module for handling package.json exports field
|
|
4
|
+
*/
|
|
1
5
|
import { IPackageJson } from '@ts-type/package-dts/package-json';
|
|
2
6
|
import { PackageExportsEntry, PackageExportsFallback } from '@ts-type/package-dts/types/package.json';
|
|
7
|
+
/**
|
|
8
|
+
* 在 exports 物件中添加 package.json entry 的核心函數
|
|
9
|
+
* Core function to add package.json entry in exports object
|
|
10
|
+
*
|
|
11
|
+
* 確保 exports 包含 './package.json' 入口點,讓外部可以匯入 package.json
|
|
12
|
+
* Ensure exports includes './package.json' entry point for external package.json import
|
|
13
|
+
*
|
|
14
|
+
* @template T - exports 欄位類型 / exports field type
|
|
15
|
+
* @param {T} pkgExports - package.json 的 exports 物件 / exports object from package.json
|
|
16
|
+
* @returns {T} 修正後的 exports 物件 / Corrected exports object
|
|
17
|
+
*/
|
|
3
18
|
export declare function _pkgExportsAddPJsonEntryCore<T extends IPackageJson["exports"]>(pkgExports: T): T;
|
|
19
|
+
/**
|
|
20
|
+
* 在 package.json 的 exports 欄位中添加 package.json entry
|
|
21
|
+
* Add package.json entry to exports field in package.json
|
|
22
|
+
*
|
|
23
|
+
* @template T - package.json 類型 / package.json type
|
|
24
|
+
* @param {T} pkg - package.json 物件 / package.json object
|
|
25
|
+
* @returns {T} 修正後的 package.json 物件 / Corrected package.json object
|
|
26
|
+
*/
|
|
4
27
|
export declare function pkgExportsAddPJsonEntry<T extends IPackageJson>(pkg: T): T;
|
|
28
|
+
/**
|
|
29
|
+
* 判斷是否為有效的 package exports entry
|
|
30
|
+
* Determine if it's a valid package exports entry
|
|
31
|
+
*
|
|
32
|
+
* 檢查 entry 是否以 './' 開頭且值不為空
|
|
33
|
+
* Check if entry starts with './' and value is not empty
|
|
34
|
+
*
|
|
35
|
+
* @param {string} entry - exports 的鍵名 / exports key name
|
|
36
|
+
* @param {PackageExportsEntry | PackageExportsFallback} value - exports 的值 / exports value
|
|
37
|
+
* @returns {boolean} 是否為有效的 PackageExportsEntry / Whether it's a valid PackageExportsEntry
|
|
38
|
+
*/
|
|
5
39
|
export declare function _isPackageExportsEntry(entry: string | keyof keyof IPackageJson["exports"], value: PackageExportsEntry | PackageExportsFallback): value is PackageExportsEntry;
|
|
40
|
+
/**
|
|
41
|
+
* 驗證 package exports 路徑是否存在
|
|
42
|
+
* Verify if package exports paths exist
|
|
43
|
+
*
|
|
44
|
+
* 檢查 exports 中定義的所有檔案路徑是否存在於檔案系統中
|
|
45
|
+
* Check if all file paths defined in exports exist in the file system
|
|
46
|
+
*
|
|
47
|
+
* @template T - package.json 類型 / package.json type
|
|
48
|
+
* @param {T} pkg - package.json 物件 / package.json object
|
|
49
|
+
* @param {object} options - 選項 / Options
|
|
50
|
+
* @param {string} options.cwd - 當前工作目錄 / Current working directory
|
|
51
|
+
* @returns {null} 無返回值 / No return value
|
|
52
|
+
* @throws {Error} 當 exports 路徑不存在時拋出錯誤 / Throws error when exports paths don't exist
|
|
53
|
+
*/
|
|
6
54
|
export declare function pkgExportsVerify<T extends IPackageJson>(pkg: T, options?: {
|
|
7
55
|
cwd?: string;
|
|
8
56
|
}): null;
|
package/lib/field/exports.js
CHANGED
|
@@ -7,37 +7,90 @@ exports.pkgExportsVerify = pkgExportsVerify;
|
|
|
7
7
|
const fs_extra_1 = require("fs-extra");
|
|
8
8
|
const find_root_1 = require("@yarn-tool/find-root");
|
|
9
9
|
const resolve_package_1 = require("@yarn-tool/resolve-package");
|
|
10
|
+
/**
|
|
11
|
+
* 在 exports 物件中添加 package.json entry 的核心函數
|
|
12
|
+
* Core function to add package.json entry in exports object
|
|
13
|
+
*
|
|
14
|
+
* 確保 exports 包含 './package.json' 入口點,讓外部可以匯入 package.json
|
|
15
|
+
* Ensure exports includes './package.json' entry point for external package.json import
|
|
16
|
+
*
|
|
17
|
+
* @template T - exports 欄位類型 / exports field type
|
|
18
|
+
* @param {T} pkgExports - package.json 的 exports 物件 / exports object from package.json
|
|
19
|
+
* @returns {T} 修正後的 exports 物件 / Corrected exports object
|
|
20
|
+
*/
|
|
10
21
|
function _pkgExportsAddPJsonEntryCore(pkgExports) {
|
|
11
22
|
var _a;
|
|
12
23
|
if (typeof pkgExports === 'object') {
|
|
24
|
+
// 若 './package.json' entry 不存在則添加
|
|
25
|
+
// Add './package.json' entry if not exists
|
|
13
26
|
(_a = pkgExports['./package.json']) !== null && _a !== void 0 ? _a : (pkgExports['./package.json'] = './package.json');
|
|
14
27
|
}
|
|
15
28
|
return pkgExports;
|
|
16
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* 在 package.json 的 exports 欄位中添加 package.json entry
|
|
32
|
+
* Add package.json entry to exports field in package.json
|
|
33
|
+
*
|
|
34
|
+
* @template T - package.json 類型 / package.json type
|
|
35
|
+
* @param {T} pkg - package.json 物件 / package.json object
|
|
36
|
+
* @returns {T} 修正後的 package.json 物件 / Corrected package.json object
|
|
37
|
+
*/
|
|
17
38
|
function pkgExportsAddPJsonEntry(pkg) {
|
|
18
39
|
_pkgExportsAddPJsonEntryCore(pkg.exports);
|
|
19
40
|
return pkg;
|
|
20
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* 判斷是否為有效的 package exports entry
|
|
44
|
+
* Determine if it's a valid package exports entry
|
|
45
|
+
*
|
|
46
|
+
* 檢查 entry 是否以 './' 開頭且值不為空
|
|
47
|
+
* Check if entry starts with './' and value is not empty
|
|
48
|
+
*
|
|
49
|
+
* @param {string} entry - exports 的鍵名 / exports key name
|
|
50
|
+
* @param {PackageExportsEntry | PackageExportsFallback} value - exports 的值 / exports value
|
|
51
|
+
* @returns {boolean} 是否為有效的 PackageExportsEntry / Whether it's a valid PackageExportsEntry
|
|
52
|
+
*/
|
|
21
53
|
function _isPackageExportsEntry(entry, value) {
|
|
22
54
|
return entry.startsWith('./') && !!(value !== null && value !== void 0 ? value : false);
|
|
23
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* 驗證 package exports 路徑是否存在
|
|
58
|
+
* Verify if package exports paths exist
|
|
59
|
+
*
|
|
60
|
+
* 檢查 exports 中定義的所有檔案路徑是否存在於檔案系統中
|
|
61
|
+
* Check if all file paths defined in exports exist in the file system
|
|
62
|
+
*
|
|
63
|
+
* @template T - package.json 類型 / package.json type
|
|
64
|
+
* @param {T} pkg - package.json 物件 / package.json object
|
|
65
|
+
* @param {object} options - 選項 / Options
|
|
66
|
+
* @param {string} options.cwd - 當前工作目錄 / Current working directory
|
|
67
|
+
* @returns {null} 無返回值 / No return value
|
|
68
|
+
* @throws {Error} 當 exports 路徑不存在時拋出錯誤 / Throws error when exports paths don't exist
|
|
69
|
+
*/
|
|
24
70
|
function pkgExportsVerify(pkg, options) {
|
|
25
71
|
if (typeof pkg.exports === 'object') {
|
|
72
|
+
// 尋找套件根目錄
|
|
73
|
+
// Find package root directory
|
|
26
74
|
const rootData = (0, find_root_1.findRootLazy)({
|
|
27
75
|
cwd: options === null || options === void 0 ? void 0 : options.cwd,
|
|
28
76
|
});
|
|
29
77
|
if (!rootData.isWorkspace && rootData.pkg) {
|
|
30
78
|
const list = [];
|
|
79
|
+
// 遍歷所有 exports entry 檢查路徑
|
|
80
|
+
// Iterate all exports entries to check paths
|
|
31
81
|
Object.entries(pkg.exports)
|
|
32
82
|
.forEach(([entry, value]) => {
|
|
33
83
|
if (_isPackageExportsEntry(entry, value)) {
|
|
34
84
|
const _ = (0, resolve_package_1.resolvePackage)(rootData.pkg);
|
|
85
|
+
// 將值轉換為陣列並檢查每個檔案路徑
|
|
86
|
+
// Convert value to array and check each file path
|
|
35
87
|
[typeof value === 'string' ? value : Object.values(value)]
|
|
36
88
|
.flat()
|
|
37
89
|
.forEach(file => {
|
|
38
90
|
var _a;
|
|
39
91
|
/**
|
|
40
|
-
*
|
|
92
|
+
* 跳過包含萬用字元 '*' 的路徑檢查
|
|
93
|
+
* Skip path check for wildcard '*' patterns
|
|
41
94
|
*/
|
|
42
95
|
if ((_a = file.includes) === null || _a === void 0 ? void 0 : _a.call(file, '*')) {
|
|
43
96
|
return;
|
|
@@ -49,13 +102,15 @@ function pkgExportsVerify(pkg, options) {
|
|
|
49
102
|
});
|
|
50
103
|
}
|
|
51
104
|
});
|
|
105
|
+
// 若有錯誤則拋出包含詳細資訊的錯誤
|
|
106
|
+
// Throw error with details if there are any issues
|
|
52
107
|
if (list.length > 0) {
|
|
53
108
|
let err = new Error(`Invalid package exports: ${rootData.pkg}\n${list.slice().map(v => ` - ${v}`).join('\n')}`);
|
|
54
|
-
// @ts-ignore
|
|
109
|
+
// @ts-ignore - 附加錯誤詳細資訊 / Attach error details
|
|
55
110
|
err.list = list;
|
|
56
|
-
// @ts-ignore
|
|
111
|
+
// @ts-ignore - 附加根目錄資訊 / Attach root data
|
|
57
112
|
err.rootData = rootData;
|
|
58
|
-
// @ts-ignore
|
|
113
|
+
// @ts-ignore - 附加 exports 資訊 / Attach exports info
|
|
59
114
|
err.pkgExports = pkg.exports;
|
|
60
115
|
throw err;
|
|
61
116
|
}
|
package/lib/field/exports.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exports.js","sourceRoot":"","sources":["exports.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"exports.js","sourceRoot":"","sources":["exports.ts"],"names":[],"mappings":"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","sourcesContent":["/**\n * 處理 package.json exports 欄位的工具模組\n * Utility module for handling package.json exports field\n */\nimport { IPackageJson } from '@ts-type/package-dts/package-json';\nimport { PackageExportsEntry, PackageExportsFallback } from '@ts-type/package-dts/types/package.json';\nimport { pathExistsSync } from 'fs-extra';\nimport { findRootLazy } from '@yarn-tool/find-root';\nimport { resolvePackage, resolvePackageRoot } from '@yarn-tool/resolve-package';\n\n/**\n * 在 exports 物件中添加 package.json entry 的核心函數\n * Core function to add package.json entry in exports object\n *\n * 確保 exports 包含 './package.json' 入口點,讓外部可以匯入 package.json\n * Ensure exports includes './package.json' entry point for external package.json import\n *\n * @template T - exports 欄位類型 / exports field type\n * @param {T} pkgExports - package.json 的 exports 物件 / exports object from package.json\n * @returns {T} 修正後的 exports 物件 / Corrected exports object\n */\nexport function _pkgExportsAddPJsonEntryCore<T extends IPackageJson[\"exports\"]>(pkgExports: T)\n{\n\tif (typeof pkgExports === 'object')\n\t{\n\t\t// 若 './package.json' entry 不存在則添加\n\t\t// Add './package.json' entry if not exists\n\t\tpkgExports['./package.json'] ??= './package.json';\n\t}\n\n\treturn pkgExports\n}\n\n/**\n * 在 package.json 的 exports 欄位中添加 package.json entry\n * Add package.json entry to exports field in package.json\n *\n * @template T - package.json 類型 / package.json type\n * @param {T} pkg - package.json 物件 / package.json object\n * @returns {T} 修正後的 package.json 物件 / Corrected package.json object\n */\nexport function pkgExportsAddPJsonEntry<T extends IPackageJson>(pkg: T)\n{\n\t_pkgExportsAddPJsonEntryCore(pkg.exports);\n\n\treturn pkg\n}\n\n/**\n * 判斷是否為有效的 package exports entry\n * Determine if it's a valid package exports entry\n *\n * 檢查 entry 是否以 './' 開頭且值不為空\n * Check if entry starts with './' and value is not empty\n *\n * @param {string} entry - exports 的鍵名 / exports key name\n * @param {PackageExportsEntry | PackageExportsFallback} value - exports 的值 / exports value\n * @returns {boolean} 是否為有效的 PackageExportsEntry / Whether it's a valid PackageExportsEntry\n */\nexport function _isPackageExportsEntry(entry: string | keyof keyof IPackageJson[\"exports\"],\n\tvalue: PackageExportsEntry | PackageExportsFallback,\n): value is PackageExportsEntry\n{\n\treturn entry.startsWith('./') && !!(value ?? false)\n}\n\n/**\n * 驗證 package exports 路徑是否存在\n * Verify if package exports paths exist\n *\n * 檢查 exports 中定義的所有檔案路徑是否存在於檔案系統中\n * Check if all file paths defined in exports exist in the file system\n *\n * @template T - package.json 類型 / package.json type\n * @param {T} pkg - package.json 物件 / package.json object\n * @param {object} options - 選項 / Options\n * @param {string} options.cwd - 當前工作目錄 / Current working directory\n * @returns {null} 無返回值 / No return value\n * @throws {Error} 當 exports 路徑不存在時拋出錯誤 / Throws error when exports paths don't exist\n */\nexport function pkgExportsVerify<T extends IPackageJson>(pkg: T, options?: {\n\tcwd?: string,\n})\n{\n\tif (typeof pkg.exports === 'object')\n\t{\n\t\t// 尋找套件根目錄\n\t\t// Find package root directory\n\t\tconst rootData = findRootLazy({\n\t\t\tcwd: options?.cwd,\n\t\t});\n\n\t\tif (!rootData.isWorkspace && rootData.pkg)\n\t\t{\n\t\t\tconst list: string[] = [];\n\n\t\t\t// 遍歷所有 exports entry 檢查路徑\n\t\t\t// Iterate all exports entries to check paths\n\t\t\tObject.entries(pkg.exports)\n\t\t\t\t.forEach(([entry, value]) =>\n\t\t\t\t{\n\t\t\t\t\tif (_isPackageExportsEntry(entry, value))\n\t\t\t\t\t{\n\t\t\t\t\t\tconst _ = resolvePackage(rootData.pkg);\n\n\t\t\t\t\t\t// 將值轉換為陣列並檢查每個檔案路徑\n\t\t\t\t\t\t// Convert value to array and check each file path\n\t\t\t\t\t\t([typeof value === 'string' ? value : Object.values(value)] as string[])\n\t\t\t\t\t\t\t.flat()\n\t\t\t\t\t\t\t.forEach(file =>\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t/**\n\t\t\t\t\t\t\t\t * 跳過包含萬用字元 '*' 的路徑檢查\n\t\t\t\t\t\t\t\t * Skip path check for wildcard '*' patterns\n\t\t\t\t\t\t\t\t */\n\t\t\t\t\t\t\t\tif (file.includes?.('*'))\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tconst bool = pathExistsSync(_.resolveLocation(file));\n\n\t\t\t\t\t\t\t\tif (!bool)\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tlist.push(`path of '${entry}' does not exist: '${file}'`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t;\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t;\n\n\t\t\t// 若有錯誤則拋出包含詳細資訊的錯誤\n\t\t\t// Throw error with details if there are any issues\n\t\t\tif (list.length > 0)\n\t\t\t{\n\t\t\t\tlet err = new Error(`Invalid package exports: ${rootData.pkg}\\n${list.slice().map(v => ` - ${v}`).join('\\n')}`);\n\n\t\t\t\t// @ts-ignore - 附加錯誤詳細資訊 / Attach error details\n\t\t\t\terr.list = list;\n\t\t\t\t// @ts-ignore - 附加根目錄資訊 / Attach root data\n\t\t\t\terr.rootData = rootData;\n\t\t\t\t// @ts-ignore - 附加 exports 資訊 / Attach exports info\n\t\t\t\terr.pkgExports = pkg.exports;\n\n\t\t\t\tthrow err;\n\t\t\t}\n\t\t}\n\n\t}\n\n\treturn null as null\n}\n"]}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 清理 package.json 空欄位的工具模組
|
|
3
|
+
* Utility module for cleaning empty fields in package.json
|
|
4
|
+
*/
|
|
5
|
+
import { IPackageJson } from '@ts-type/package-dts/package-json';
|
|
6
|
+
/**
|
|
7
|
+
* 判斷值是否為空配置
|
|
8
|
+
* Determine if value is an empty configuration
|
|
9
|
+
*
|
|
10
|
+
* 檢查值是否為 undefined、null、空物件或空字串
|
|
11
|
+
* Check if value is undefined, null, empty object, or empty string
|
|
12
|
+
*
|
|
13
|
+
* @param {any[] | object | string | unknown} value - 要檢查的值 / Value to check
|
|
14
|
+
* @returns {boolean} 是否為空配置 / Whether it's an empty configuration
|
|
15
|
+
*/
|
|
16
|
+
export declare function isEmptyConfig(value: any[] | object | string | unknown): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* 清理空欄位的核心函數
|
|
19
|
+
* Core function for cleaning empty fields
|
|
20
|
+
*
|
|
21
|
+
* 移除 package.json 中指定的空欄位
|
|
22
|
+
* Remove specified empty fields from package.json
|
|
23
|
+
*
|
|
24
|
+
* @template T - package.json 類型 / package.json type
|
|
25
|
+
* @param {T} pkg - package.json 物件 / package.json object
|
|
26
|
+
* @param {string} root - 套件根目錄路徑 / Package root directory path
|
|
27
|
+
* @param {(keyof T)[]} keys - 要檢查的欄位名稱陣列 / Array of field names to check
|
|
28
|
+
* @returns {T} 清理後的 package.json 物件 / Cleaned package.json object
|
|
29
|
+
*/
|
|
30
|
+
export declare function _fixEmptyFieldsCore<T extends IPackageJson>(pkg: T, root: string, keys: (keyof T)[]): T;
|
|
31
|
+
/**
|
|
32
|
+
* 修復 package.json 中的空欄位
|
|
33
|
+
* Fix empty fields in package.json
|
|
34
|
+
*
|
|
35
|
+
* 移除 bin 和 resolutions 欄位中的空值
|
|
36
|
+
* Remove empty values from bin and resolutions fields
|
|
37
|
+
*
|
|
38
|
+
* @template T - package.json 類型 / package.json type
|
|
39
|
+
* @param {T} pkg - package.json 物件 / package.json object
|
|
40
|
+
* @param {string} root - 套件根目錄路徑 / Package root directory path
|
|
41
|
+
* @returns {T} 清理後的 package.json 物件 / Cleaned package.json object
|
|
42
|
+
*/
|
|
43
|
+
export declare function fixEmptyFields<T extends IPackageJson>(pkg: T, root: string): T;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isEmptyConfig = isEmptyConfig;
|
|
4
|
+
exports._fixEmptyFieldsCore = _fixEmptyFieldsCore;
|
|
5
|
+
exports.fixEmptyFields = fixEmptyFields;
|
|
6
|
+
/**
|
|
7
|
+
* 判斷值是否為空配置
|
|
8
|
+
* Determine if value is an empty configuration
|
|
9
|
+
*
|
|
10
|
+
* 檢查值是否為 undefined、null、空物件或空字串
|
|
11
|
+
* Check if value is undefined, null, empty object, or empty string
|
|
12
|
+
*
|
|
13
|
+
* @param {any[] | object | string | unknown} value - 要檢查的值 / Value to check
|
|
14
|
+
* @returns {boolean} 是否為空配置 / Whether it's an empty configuration
|
|
15
|
+
*/
|
|
16
|
+
function isEmptyConfig(value) {
|
|
17
|
+
return typeof value === 'undefined' || value === null || typeof value === 'object' && !Object.keys(value).length || typeof value === 'string' && !value.length;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 清理空欄位的核心函數
|
|
21
|
+
* Core function for cleaning empty fields
|
|
22
|
+
*
|
|
23
|
+
* 移除 package.json 中指定的空欄位
|
|
24
|
+
* Remove specified empty fields from package.json
|
|
25
|
+
*
|
|
26
|
+
* @template T - package.json 類型 / package.json type
|
|
27
|
+
* @param {T} pkg - package.json 物件 / package.json object
|
|
28
|
+
* @param {string} root - 套件根目錄路徑 / Package root directory path
|
|
29
|
+
* @param {(keyof T)[]} keys - 要檢查的欄位名稱陣列 / Array of field names to check
|
|
30
|
+
* @returns {T} 清理後的 package.json 物件 / Cleaned package.json object
|
|
31
|
+
*/
|
|
32
|
+
function _fixEmptyFieldsCore(pkg, root, keys) {
|
|
33
|
+
for (const key of keys) {
|
|
34
|
+
if (isEmptyConfig(pkg[key])) {
|
|
35
|
+
delete pkg[key];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return pkg;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 修復 package.json 中的空欄位
|
|
42
|
+
* Fix empty fields in package.json
|
|
43
|
+
*
|
|
44
|
+
* 移除 bin 和 resolutions 欄位中的空值
|
|
45
|
+
* Remove empty values from bin and resolutions fields
|
|
46
|
+
*
|
|
47
|
+
* @template T - package.json 類型 / package.json type
|
|
48
|
+
* @param {T} pkg - package.json 物件 / package.json object
|
|
49
|
+
* @param {string} root - 套件根目錄路徑 / Package root directory path
|
|
50
|
+
* @returns {T} 清理後的 package.json 物件 / Cleaned package.json object
|
|
51
|
+
*/
|
|
52
|
+
function fixEmptyFields(pkg, root) {
|
|
53
|
+
return _fixEmptyFieldsCore(pkg, root, [
|
|
54
|
+
'bin',
|
|
55
|
+
'resolutions',
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=fixEmpty.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fixEmpty.js","sourceRoot":"","sources":["fixEmpty.ts"],"names":[],"mappings":";;AAgBA,sCAGC;AAeD,kDAWC;AAcD,wCAMC;AA3DD;;;;;;;;;GASG;AACH,SAAgB,aAAa,CAAC,KAAwC;IAErE,OAAO,OAAO,KAAK,KAAK,WAAW,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;AAChK,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,mBAAmB,CAAyB,GAAM,EAAE,IAAY,EAAE,IAAiB;IAElG,KAAK,MAAM,GAAG,IAAI,IAAI,EACtB,CAAC;QACA,IAAI,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAC3B,CAAC;YACA,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC;QACjB,CAAC;IACF,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,cAAc,CAAyB,GAAM,EAAE,IAAY;IAE1E,OAAO,mBAAmB,CAAC,GAAG,EAAE,IAAI,EAAE;QACrC,KAAK;QACL,aAAa;KACb,CAAC,CAAA;AACH,CAAC","sourcesContent":["/**\r\n * 清理 package.json 空欄位的工具模組\r\n * Utility module for cleaning empty fields in package.json\r\n */\r\nimport { IPackageJson } from '@ts-type/package-dts/package-json';\r\n\r\n/**\r\n * 判斷值是否為空配置\r\n * Determine if value is an empty configuration\r\n *\r\n * 檢查值是否為 undefined、null、空物件或空字串\r\n * Check if value is undefined, null, empty object, or empty string\r\n *\r\n * @param {any[] | object | string | unknown} value - 要檢查的值 / Value to check\r\n * @returns {boolean} 是否為空配置 / Whether it's an empty configuration\r\n */\r\nexport function isEmptyConfig(value: any[] | object | string | unknown)\r\n{\r\n\treturn typeof value === 'undefined' || value === null || typeof value === 'object' && !Object.keys(value).length || typeof value === 'string' && !value.length;\r\n}\r\n\r\n/**\r\n * 清理空欄位的核心函數\r\n * Core function for cleaning empty fields\r\n *\r\n * 移除 package.json 中指定的空欄位\r\n * Remove specified empty fields from package.json\r\n *\r\n * @template T - package.json 類型 / package.json type\r\n * @param {T} pkg - package.json 物件 / package.json object\r\n * @param {string} root - 套件根目錄路徑 / Package root directory path\r\n * @param {(keyof T)[]} keys - 要檢查的欄位名稱陣列 / Array of field names to check\r\n * @returns {T} 清理後的 package.json 物件 / Cleaned package.json object\r\n */\r\nexport function _fixEmptyFieldsCore<T extends IPackageJson>(pkg: T, root: string, keys: (keyof T)[])\r\n{\r\n\tfor (const key of keys)\r\n\t{\r\n\t\tif (isEmptyConfig(pkg[key]))\r\n\t\t{\r\n\t\t\tdelete pkg[key];\r\n\t\t}\r\n\t}\r\n\r\n\treturn pkg;\r\n}\r\n\r\n/**\r\n * 修復 package.json 中的空欄位\r\n * Fix empty fields in package.json\r\n *\r\n * 移除 bin 和 resolutions 欄位中的空值\r\n * Remove empty values from bin and resolutions fields\r\n *\r\n * @template T - package.json 類型 / package.json type\r\n * @param {T} pkg - package.json 物件 / package.json object\r\n * @param {string} root - 套件根目錄路徑 / Package root directory path\r\n * @returns {T} 清理後的 package.json 物件 / Cleaned package.json object\r\n */\r\nexport function fixEmptyFields<T extends IPackageJson>(pkg: T, root: string)\r\n{\r\n\treturn _fixEmptyFieldsCore(pkg, root, [\r\n\t\t'bin',\r\n\t\t'resolutions',\r\n\t])\r\n}\r\n"]}
|