@yarn-tool/pkg-entry-util 3.0.14 → 3.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/CHANGELOG.md +61 -0
- package/README.md +83 -13
- package/lib/field/bin.js +21 -6
- package/lib/field/bin.js.map +1 -1
- package/lib/field/exports.js +35 -12
- package/lib/field/exports.js.map +1 -1
- package/lib/field/fixEmpty.js +22 -0
- package/lib/field/fixEmpty.js.map +1 -1
- package/lib/field/publishConfig.js +13 -10
- package/lib/field/publishConfig.js.map +1 -1
- package/lib/field/scripts.d.ts +46 -1
- package/lib/field/scripts.js +46 -1
- package/lib/field/scripts.js.map +1 -1
- package/lib/preset/scripts/dummy.js +18 -5
- package/lib/preset/scripts/dummy.js.map +1 -1
- package/lib/preset/scripts/pkg-scripts.d.ts +12 -15
- package/lib/preset/scripts/pkg-scripts.js +14 -12
- package/lib/preset/scripts/pkg-scripts.js.map +1 -1
- package/lib/preset/scripts/root-scripts.d.ts +4 -3
- package/lib/preset/scripts/root-scripts.js +4 -1
- package/lib/preset/scripts/root-scripts.js.map +1 -1
- package/lib/preset/scripts/shared-root-scripts.d.ts +2 -0
- package/lib/preset/scripts/shared-root-scripts.js +2 -0
- package/lib/preset/scripts/shared-root-scripts.js.map +1 -1
- package/lib/preset/scripts/ws-root-scripts.d.ts +1 -0
- package/lib/preset/scripts/ws-root-scripts.js +16 -4
- package/lib/preset/scripts/ws-root-scripts.js.map +1 -1
- package/lib/util/scripts/dummy.d.ts +57 -0
- package/lib/util/scripts/dummy.js +57 -0
- package/lib/util/scripts/dummy.js.map +1 -1
- package/package.json +9 -7
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,67 @@
|
|
|
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.16](https://github.com/bluelovers/ws-yarn-workspaces/compare/@yarn-tool/pkg-entry-util@3.0.15...@yarn-tool/pkg-entry-util@3.0.16) (2026-10-12)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### 🔖 Miscellaneous
|
|
11
|
+
|
|
12
|
+
* . ([2ce14fc](https://github.com/bluelovers/ws-yarn-workspaces/commit/2ce14fcf8272584c51c4ba43b87a488f105c2f63))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## [3.0.15](https://github.com/bluelovers/ws-yarn-workspaces/compare/@yarn-tool/pkg-entry-util@3.0.14...@yarn-tool/pkg-entry-util@3.0.15) (2026-10-12)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### BREAKING CHANGES
|
|
20
|
+
|
|
21
|
+
* **deps:** 將套件解析依賴從 @yarn-tool/resolve-package 遷移至 @yarn-tool/require-resolve
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### 🐛 Bug Fixes
|
|
26
|
+
|
|
27
|
+
* **pkg-entry-util:** 擴充 fixEmptyFields 可處理的依賴欄位 ([c78d169](https://github.com/bluelovers/ws-yarn-workspaces/commit/c78d169eda85a0095350b2c1979f1b6f20dbe76f))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
### 📦 Code Refactoring
|
|
31
|
+
|
|
32
|
+
* **core:** 統一靜態檔案控制邏輯並更新 package.json 腳本規範 ([12f5a56](https://github.com/bluelovers/ws-yarn-workspaces/commit/12f5a5623a6f19e33616a326c86a094c548a0436))
|
|
33
|
+
* **deps:** 將套件解析依賴從 @yarn-tool/resolve-package 遷移至 @yarn-tool/require-resolve ([0e146d7](https://github.com/bluelovers/ws-yarn-workspaces/commit/0e146d761964b8777d55483312799941aeceea1a))
|
|
34
|
+
* **pkg-entry-util:** 優化與更新預設 package.json 腳本內容 ([f9bcc9b](https://github.com/bluelovers/ws-yarn-workspaces/commit/f9bcc9b7d2ec3397bcbdb28939bcded09ad3acfd))
|
|
35
|
+
* **require-resolve:** 重構模組載入邏輯並支援非同步導入 ([57f49ad](https://github.com/bluelovers/ws-yarn-workspaces/commit/57f49adf1e3b5d345d7d50aef24a6acb334fb39c))
|
|
36
|
+
* **utils:** 重構測試腳本配置與路徑搜尋工具 ([aeb410e](https://github.com/bluelovers/ws-yarn-workspaces/commit/aeb410e2103f62ce8e012f96115f095a06107ddf))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### 📚 Documentation
|
|
40
|
+
|
|
41
|
+
* add jsdoc ([9cb9b13](https://github.com/bluelovers/ws-yarn-workspaces/commit/9cb9b13eda8c357221ad1b754458aa9916eada22))
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
### 🚨 Tests
|
|
45
|
+
|
|
46
|
+
* **test:** 將測試框架從 jest 遷移至 node:test ([3af9774](https://github.com/bluelovers/ws-yarn-workspaces/commit/3af9774964abc48083ec60b2021c1cec08874803))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
### 🛠 Build System
|
|
50
|
+
|
|
51
|
+
* update build ([3186340](https://github.com/bluelovers/ws-yarn-workspaces/commit/3186340758172427cb1e9b8955f6fd677abac625))
|
|
52
|
+
* **deps:** 更新測試腳本與依賴以支援 node:test ([f9c71c7](https://github.com/bluelovers/ws-yarn-workspaces/commit/f9c71c795c792c5c7ed9d613ce7e9c3c726c39d5))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
### ♻️ Chores
|
|
56
|
+
|
|
57
|
+
* **deps:** 更新各套件依賴版本 ([569f075](https://github.com/bluelovers/ws-yarn-workspaces/commit/569f0756189cdadf69955d8e388ac0a6a8988056))
|
|
58
|
+
* **workspace:** 更新測試指令路徑與新增工作區搜尋選項 ([a80039d](https://github.com/bluelovers/ws-yarn-workspaces/commit/a80039dd8c691e047466966ffc8f8a4e2e31c03f))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
### 🔖 Miscellaneous
|
|
62
|
+
|
|
63
|
+
* . ([e5788f8](https://github.com/bluelovers/ws-yarn-workspaces/commit/e5788f84551b870490a96d0a5d67d8d542481373))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
6
67
|
## [3.0.14](https://github.com/bluelovers/ws-yarn-workspaces/compare/@yarn-tool/pkg-entry-util@3.0.13...@yarn-tool/pkg-entry-util@3.0.14) (2026-09-06)
|
|
7
68
|
|
|
8
69
|
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
Utility library for handling package.json entry-related fields, including fix and verification for bin, exports, and publishConfig fields
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Installation
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
yarn add @yarn-tool/pkg-entry-util
|
|
@@ -31,7 +31,38 @@ npm install @yarn-tool/pkg-entry-util
|
|
|
31
31
|
|
|
32
32
|
This module provides default npm scripts for various package types, supporting monorepo and standalone package development workflows
|
|
33
33
|
|
|
34
|
-
####
|
|
34
|
+
#### 預設腳本分類 / Preset Categories
|
|
35
|
+
|
|
36
|
+
| 函數 / Function | 適用場景 / Use Case | 主要功能 / Main Features |
|
|
37
|
+
|----------------|--------------------|-------------------------|
|
|
38
|
+
| `defaultPkgScripts` | 一般套件 / Regular packages | 測試、覆蓋率、型別檢查 / Test, coverage, type check |
|
|
39
|
+
| `defaultPkgNotOldExists` | 新建立套件 / New packages | 完整開發腳本(建構、lint、review)/ Complete dev scripts (build, lint, review) |
|
|
40
|
+
| `defaultRootScripts` | 獨立套件根目錄 / Standalone package root | 版本遞增、發布、changelog / Version bump, publish, changelog |
|
|
41
|
+
| `defaultWorkspaceRootScripts` | 工作區根目錄 / Workspace root | 跨工作區執行、Lerna 發布 / Cross-workspace execution, Lerna publish |
|
|
42
|
+
| `defaultSharedRootScripts` | 共用根目錄腳本 / Shared root scripts | CI 安裝、依賴清理 / CI install, dedupe |
|
|
43
|
+
| `fillDummyScripts` | 所有套件類型 / All package types | 生命週期腳本佔位符 / Lifecycle script placeholders |
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
### Entry 欄位工具 / Entry Field Utilities
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { fixPkgBinField, fixPublishConfig, pkgExportsAddPJsonEntry, pkgExportsVerify } from '@yarn-tool/pkg-entry-util';
|
|
51
|
+
|
|
52
|
+
// 修復 bin 欄位路徑 / Fix bin field paths
|
|
53
|
+
fixPkgBinField(pkg, root);
|
|
54
|
+
|
|
55
|
+
// 確保 exports 含 ./package.json 入口 / Ensure exports includes ./package.json
|
|
56
|
+
pkgExportsAddPJsonEntry(pkg);
|
|
57
|
+
|
|
58
|
+
// 驗證 exports 路徑存在 / Verify exports paths exist
|
|
59
|
+
pkgExportsVerify(pkg, { cwd });
|
|
60
|
+
|
|
61
|
+
// scoped 套件補上 publishConfig.access / Add publishConfig.access for scoped packages
|
|
62
|
+
fixPublishConfig(pkg);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 腳本預設 / Scripts Presets
|
|
35
66
|
|
|
36
67
|
```typescript
|
|
37
68
|
// 工作區根目錄 (Yarn/Lerna Workspaces Root)
|
|
@@ -68,17 +99,6 @@ fillDummyScripts(scripts, 'my-package');
|
|
|
68
99
|
// Fill echo placeholders for lifecycle scripts
|
|
69
100
|
```
|
|
70
101
|
|
|
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
102
|
#### 腳本預設詳細說明 / Scripts Preset Details
|
|
83
103
|
|
|
84
104
|
##### defaultWorkspaceRootScripts / 工作區根目錄腳本
|
|
@@ -158,6 +178,56 @@ fillDummyScripts(pkg.scripts, 'workspaces');
|
|
|
158
178
|
// }
|
|
159
179
|
```
|
|
160
180
|
|
|
181
|
+
## API Documentation
|
|
182
|
+
|
|
183
|
+
### Entry 欄位工具 / Entry Field Utilities
|
|
184
|
+
|
|
185
|
+
| 函數 / Function | 簡介 / Description |
|
|
186
|
+
|----------------|--------------------|
|
|
187
|
+
| `fixPkgBinField(pkg, root)` | 修正 bin 欄位路徑:檔案不在根目錄但在 `bin/` 子目錄時自動改寫,支援字串與物件兩種格式 / Fix bin field paths, rewriting when the file lives in `bin/` instead of the root; supports string and object formats |
|
|
188
|
+
| `fixBinPath(bin, root)` | 單一路徑的修正版,需修正時回傳新路徑,否則回傳 `null` / Fix a single path, returning the new path or `null` |
|
|
189
|
+
| `pkgExportsAddPJsonEntry(pkg)` | 確保 `exports` 含 `'./package.json'` 入口,讓外部可匯入 package.json / Ensure `exports` includes a `'./package.json'` entry so it can be imported externally |
|
|
190
|
+
| `pkgExportsVerify(pkg, options?)` | 驗證 `exports` 中的檔案路徑是否存在;失敗時拋出附帶詳細清單的錯誤 / Verify that files in `exports` exist; throws an error carrying a detailed list on failure |
|
|
191
|
+
| `fixPublishConfig(pkg)` | 為非私有的 scoped 套件補上 `publishConfig.access = 'public'` / Add `publishConfig.access = 'public'` for non-private scoped packages |
|
|
192
|
+
| `fixEmptyFields(pkg, root)` | 移除 `bin`、`resolutions` 等欄位中的空配置 / Remove empty configurations from fields such as `bin` and `resolutions` |
|
|
193
|
+
| `isEmptyConfig(value)` | 判斷值是否為 `undefined`、`null`、空物件或空字串 / Check whether a value is `undefined`, `null`, an empty object, or an empty string |
|
|
194
|
+
|
|
195
|
+
> `fixEmptyFields` / `isEmptyConfig` 未由套件根目錄匯出,需自 `lib/field/fixEmpty` 引入;
|
|
196
|
+
> `EnumScriptsEntry` / `scriptsEntryIsNoTestSpecified` 則位於 `lib/field/scripts`
|
|
197
|
+
> `fixEmptyFields` / `isEmptyConfig` are not re-exported from the package root; import them from `lib/field/fixEmpty`.
|
|
198
|
+
> `EnumScriptsEntry` / `scriptsEntryIsNoTestSpecified` live in `lib/field/scripts`
|
|
199
|
+
|
|
200
|
+
### 腳本預設 / Scripts Presets
|
|
201
|
+
|
|
202
|
+
| 函數 / Function | 簡介 / Description |
|
|
203
|
+
|----------------|--------------------|
|
|
204
|
+
| `defaultPkgScripts()` | 一般套件的測試與型別檢查腳本 / Test and type-check scripts for regular packages |
|
|
205
|
+
| `defaultPkgNotOldExists()` | 新套件的完整開發腳本(含建構與 lint)/ Complete dev scripts for new packages (build and lint included) |
|
|
206
|
+
| `defaultRootScripts()` | 獨立套件根目錄的版本遞增與發布腳本 / Version bump and publish scripts for standalone roots |
|
|
207
|
+
| `defaultWorkspaceRootScripts()` | 工作區根目錄的跨工作區與 Lerna 發布腳本 / Cross-workspace and Lerna publish scripts for workspace roots |
|
|
208
|
+
| `defaultSharedRootScripts()` | 根目錄共用的 CI 安裝與維護腳本 / Shared CI install and maintenance scripts for roots |
|
|
209
|
+
| `fillDummyScripts(scripts?, prefix?)` | 以 `echo <prefix> <script>` 佔位符填充生命週期腳本 / Fill lifecycle scripts with `echo <prefix> <script>` placeholders |
|
|
210
|
+
| `_runAllOrSince(script)` | 產生 `test:all` / `test:since` 形式的腳本對 / Generate `test:all` / `test:since` style script pairs |
|
|
211
|
+
| `scriptsEntryIsNoTestSpecified(value)` | 判斷腳本是否為「未指定測試」的 echo 佔位 / Detect the "no test specified" echo placeholder |
|
|
212
|
+
|
|
213
|
+
### 常數與列舉 / Constants & Enums
|
|
214
|
+
|
|
215
|
+
| 匯出 | 說明 |
|
|
216
|
+
| --- | --- |
|
|
217
|
+
| `EnumScriptsEntry` | 常用腳本命令字串列舉(Jest、TSC、DTS 建構等)/ Enum of common script command strings (Jest, TSC, DTS build, etc.) |
|
|
218
|
+
| `_defaultCiInstallDeps` | CI 環境需安裝的核心開發工具清單 / Core dev tools installed in CI |
|
|
219
|
+
|
|
220
|
+
## Configuration
|
|
221
|
+
|
|
222
|
+
本套件無設定檔,行為由各函式的參數控制:
|
|
223
|
+
|
|
224
|
+
| 參數 / Parameter | 套用函式 / Used by | 說明 / Description |
|
|
225
|
+
|----------------|--------------------|--------------------|
|
|
226
|
+
| `root` | `fixPkgBinField`、`fixEmptyFields` | 套件根目錄路徑,用以判斷 bin 檔案實際位置 / Package root path, used to locate bin files |
|
|
227
|
+
| `options.cwd` | `pkgExportsVerify` | 查找套件根目錄的起始工作目錄,預設為 `process.cwd()` / Starting directory for locating the package root, defaults to `process.cwd()` |
|
|
228
|
+
| `prefix` | `fillDummyScripts` | echo 佔位符的前綴文字,通常為套件名稱 / Prefix text for echo placeholders, usually the package name |
|
|
229
|
+
| `includeVersion` | - | 本套件不使用;版本處理交由 `npm-package-arg-util` 相關套件 / Not used here; version handling belongs to `npm-package-arg-util` related packages |
|
|
230
|
+
|
|
161
231
|
## 相關模組 / Related Modules
|
|
162
232
|
|
|
163
233
|
- [create-yarn-workspaces](https://www.npmjs.com/package/create-yarn-workspaces) - 使用 `defaultWorkspaceRootScripts` 建立工作區
|
package/lib/field/bin.js
CHANGED
|
@@ -16,8 +16,10 @@ const upath2_1 = require("upath2");
|
|
|
16
16
|
* @returns {string | null} 修正後的路徑或 null(若無需修正)/ Corrected path or null (if no fix needed)
|
|
17
17
|
*/
|
|
18
18
|
function fixBinPath(bin, root) {
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
/**
|
|
20
|
+
* 檢查檔案是否存在於指定路徑,若不存在但存在於 bin/ 子目錄則返回修正路徑
|
|
21
|
+
* Check if file exists at specified path, if not but exists in bin/ subdirectory, return corrected path
|
|
22
|
+
*/
|
|
21
23
|
if (!(0, fs_extra_1.existsSync)((0, upath2_1.join)(root, bin))
|
|
22
24
|
&& (0, fs_extra_1.existsSync)((0, upath2_1.join)(root, 'bin', bin))) {
|
|
23
25
|
return (0, upath2_1.join)('.', 'bin', bin);
|
|
@@ -37,23 +39,36 @@ function fixBinPath(bin, root) {
|
|
|
37
39
|
* @returns {T} 修正後的 package.json 物件 / Corrected package.json object
|
|
38
40
|
*/
|
|
39
41
|
function fixPkgBinField(pkg, root) {
|
|
42
|
+
/** 沒有 bin 欄位時直接回傳,避免無謂的檔案系統檢查 */
|
|
40
43
|
if (pkg.bin) {
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
/**
|
|
45
|
+
* 處理字串格式的 bin 欄位(單一執行檔)
|
|
46
|
+
* Handle string format bin field (single executable)
|
|
47
|
+
*/
|
|
43
48
|
if (typeof pkg.bin === 'string') {
|
|
44
49
|
let bin_new = fixBinPath(pkg.bin, root);
|
|
50
|
+
/** fixBinPath 回傳 null 代表路徑已正確,維持原值不覆寫 */
|
|
45
51
|
if (bin_new) {
|
|
46
52
|
// @ts-ignore
|
|
47
53
|
pkg.bin = bin_new;
|
|
48
54
|
}
|
|
49
55
|
}
|
|
50
|
-
|
|
51
|
-
|
|
56
|
+
/**
|
|
57
|
+
* 處理物件格式的 bin 欄位(多個執行檔)
|
|
58
|
+
* Handle object format bin field (multiple executables)
|
|
59
|
+
*/
|
|
52
60
|
else if (typeof pkg.bin === 'object' && !Array.isArray(pkg.bin)) {
|
|
61
|
+
/**
|
|
62
|
+
* 逐個執行檔名稱修正;僅處理字串值,
|
|
63
|
+
* 其他型別(異常輸入)維持原樣以免破壞資料
|
|
64
|
+
* Fix each executable name; only string values are processed,
|
|
65
|
+
* other types (unexpected input) are left untouched to avoid data loss
|
|
66
|
+
*/
|
|
53
67
|
Object.keys(pkg.bin)
|
|
54
68
|
.forEach(function (key) {
|
|
55
69
|
if (typeof pkg.bin[key] === 'string') {
|
|
56
70
|
let bin_new = fixBinPath(pkg.bin[key], root);
|
|
71
|
+
/** 僅在確實需要修正時才覆寫,保持 package.json 變更最小化 */
|
|
57
72
|
if (bin_new) {
|
|
58
73
|
pkg.bin[key] = bin_new;
|
|
59
74
|
}
|
package/lib/field/bin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bin.js","sourceRoot":"","sources":["bin.ts"],"names":[],"mappings":";;AAmBA,
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["bin.ts"],"names":[],"mappings":";;AAmBA,gCAeC;AAcD,wCAmDC;AA9FD,uCAAsC;AACtC,mCAAqC;AAErC;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CAAC,GAAW,EAAE,IAAY;IAEnD;;;OAGG;IACH,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,iCAAiC;IACjC,IAAI,GAAG,CAAC,GAAG,EACX,CAAC;QACA;;;WAGG;QACH,IAAI,OAAO,GAAG,CAAC,GAAG,KAAK,QAAQ,EAC/B,CAAC;YACA,IAAI,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YAExC,yCAAyC;YACzC,IAAI,OAAO,EACX,CAAC;gBACA,aAAa;gBACb,GAAG,CAAC,GAAG,GAAG,OAAO,CAAC;YACnB,CAAC;QACF,CAAC;QACD;;;WAGG;aACE,IAAI,OAAO,GAAG,CAAC,GAAG,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAC/D,CAAC;YACA;;;;;eAKG;YACH,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,yCAAyC;oBACzC,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/**\n\t * 檢查檔案是否存在於指定路徑,若不存在但存在於 bin/ 子目錄則返回修正路徑\n\t * Check if file exists at specified path, if not but exists in bin/ subdirectory, return corrected path\n\t */\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\t/** 沒有 bin 欄位時直接回傳,避免無謂的檔案系統檢查 */\n\tif (pkg.bin)\n\t{\n\t\t/**\n\t\t * 處理字串格式的 bin 欄位(單一執行檔)\n\t\t * Handle string format bin field (single executable)\n\t\t */\n\t\tif (typeof pkg.bin === 'string')\n\t\t{\n\t\t\tlet bin_new = fixBinPath(pkg.bin, root);\n\n\t\t\t/** fixBinPath 回傳 null 代表路徑已正確,維持原值不覆寫 */\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/**\n\t\t * 處理物件格式的 bin 欄位(多個執行檔)\n\t\t * Handle object format bin field (multiple executables)\n\t\t */\n\t\telse if (typeof pkg.bin === 'object' && !Array.isArray(pkg.bin))\n\t\t{\n\t\t\t/**\n\t\t\t * 逐個執行檔名稱修正;僅處理字串值,\n\t\t\t * 其他型別(異常輸入)維持原樣以免破壞資料\n\t\t\t * Fix each executable name; only string values are processed,\n\t\t\t * other types (unexpected input) are left untouched to avoid data loss\n\t\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\t/** 僅在確實需要修正時才覆寫,保持 package.json 變更最小化 */\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.js
CHANGED
|
@@ -6,7 +6,7 @@ exports._isPackageExportsEntry = _isPackageExportsEntry;
|
|
|
6
6
|
exports.pkgExportsVerify = pkgExportsVerify;
|
|
7
7
|
const fs_extra_1 = require("fs-extra");
|
|
8
8
|
const find_root_1 = require("@yarn-tool/find-root");
|
|
9
|
-
const
|
|
9
|
+
const require_resolve_1 = require("@yarn-tool/require-resolve");
|
|
10
10
|
/**
|
|
11
11
|
* 在 exports 物件中添加 package.json entry 的核心函數
|
|
12
12
|
* Core function to add package.json entry in exports object
|
|
@@ -21,8 +21,10 @@ const resolve_package_1 = require("@yarn-tool/resolve-package");
|
|
|
21
21
|
function _pkgExportsAddPJsonEntryCore(pkgExports) {
|
|
22
22
|
var _a;
|
|
23
23
|
if (typeof pkgExports === 'object') {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
/**
|
|
25
|
+
* 若 './package.json' entry 不存在則添加(`??=` 保留使用者自訂的值)
|
|
26
|
+
* Add './package.json' entry if not exists (`??=` keeps a user-defined value)
|
|
27
|
+
*/
|
|
26
28
|
(_a = pkgExports['./package.json']) !== null && _a !== void 0 ? _a : (pkgExports['./package.json'] = './package.json');
|
|
27
29
|
}
|
|
28
30
|
return pkgExports;
|
|
@@ -69,21 +71,34 @@ function _isPackageExportsEntry(entry, value) {
|
|
|
69
71
|
*/
|
|
70
72
|
function pkgExportsVerify(pkg, options) {
|
|
71
73
|
if (typeof pkg.exports === 'object') {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
/**
|
|
75
|
+
* 尋找套件根目錄;僅在「非工作區且找得到 package.json」時才驗證,
|
|
76
|
+
* 工作區情境下交由各成員套件自行驗證,避免重複或誤報
|
|
77
|
+
* Find the package root; verify only when it is not a workspace and a
|
|
78
|
+
* package.json exists, since workspace members verify themselves
|
|
79
|
+
*/
|
|
74
80
|
const rootData = (0, find_root_1.findRootLazy)({
|
|
75
81
|
cwd: options === null || options === void 0 ? void 0 : options.cwd,
|
|
76
82
|
});
|
|
77
83
|
if (!rootData.isWorkspace && rootData.pkg) {
|
|
78
84
|
const list = [];
|
|
79
|
-
|
|
80
|
-
|
|
85
|
+
/**
|
|
86
|
+
* 遍歷所有 exports entry 檢查路徑
|
|
87
|
+
* Iterate all exports entries to check paths
|
|
88
|
+
*/
|
|
81
89
|
Object.entries(pkg.exports)
|
|
82
90
|
.forEach(([entry, value]) => {
|
|
83
91
|
if (_isPackageExportsEntry(entry, value)) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
92
|
+
/**
|
|
93
|
+
* 以套件根目錄建立解析器,讓 `./xxx` 相對路徑能解析為實際檔案位置
|
|
94
|
+
* Build a resolver from the package root so `./xxx` relative
|
|
95
|
+
* paths can be resolved to their real file locations
|
|
96
|
+
*/
|
|
97
|
+
const _ = (0, require_resolve_1.resolvePackage)(rootData.pkg);
|
|
98
|
+
/**
|
|
99
|
+
* 將值轉換為陣列並檢查每個檔案路徑
|
|
100
|
+
* Convert value to array and check each file path
|
|
101
|
+
*/
|
|
87
102
|
[typeof value === 'string' ? value : Object.values(value)]
|
|
88
103
|
.flat()
|
|
89
104
|
.forEach(file => {
|
|
@@ -96,14 +111,22 @@ function pkgExportsVerify(pkg, options) {
|
|
|
96
111
|
return;
|
|
97
112
|
}
|
|
98
113
|
const bool = (0, fs_extra_1.pathExistsSync)(_.resolveLocation(file));
|
|
114
|
+
/**
|
|
115
|
+
* 先收集所有錯誤再一次性拋出,
|
|
116
|
+
* 讓使用者一次看到全部問題而非逐次修正
|
|
117
|
+
* Collect all errors before throwing so users see
|
|
118
|
+
* every problem at once instead of fixing one by one
|
|
119
|
+
*/
|
|
99
120
|
if (!bool) {
|
|
100
121
|
list.push(`path of '${entry}' does not exist: '${file}'`);
|
|
101
122
|
}
|
|
102
123
|
});
|
|
103
124
|
}
|
|
104
125
|
});
|
|
105
|
-
|
|
106
|
-
|
|
126
|
+
/**
|
|
127
|
+
* 若有錯誤則拋出包含詳細資訊的錯誤
|
|
128
|
+
* Throw error with details if there are any issues
|
|
129
|
+
*/
|
|
107
130
|
if (list.length > 0) {
|
|
108
131
|
let err = new Error(`Invalid package exports: ${rootData.pkg}\n${list.slice().map(v => ` - ${v}`).join('\n')}`);
|
|
109
132
|
// @ts-ignore - 附加錯誤詳細資訊 / Attach error details
|
package/lib/field/exports.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exports.js","sourceRoot":"","sources":["exports.ts"],"names":[],"mappings":";;AAqBA,
|
|
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/require-resolve';\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/**\n\t\t * 若 './package.json' entry 不存在則添加(`??=` 保留使用者自訂的值)\n\t\t * Add './package.json' entry if not exists (`??=` keeps a user-defined value)\n\t\t */\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 * 尋找套件根目錄;僅在「非工作區且找得到 package.json」時才驗證,\n\t\t * 工作區情境下交由各成員套件自行驗證,避免重複或誤報\n\t\t * Find the package root; verify only when it is not a workspace and a\n\t\t * package.json exists, since workspace members verify themselves\n\t\t */\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\t\t\t/**\n\t\t\t * 遍歷所有 exports entry 檢查路徑\n\t\t\t * Iterate all exports entries to check paths\n\t\t\t */\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\t/**\n\t\t\t\t\t\t * 以套件根目錄建立解析器,讓 `./xxx` 相對路徑能解析為實際檔案位置\n\t\t\t\t\t\t * Build a resolver from the package root so `./xxx` relative\n\t\t\t\t\t\t * paths can be resolved to their real file locations\n\t\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 * 將值轉換為陣列並檢查每個檔案路徑\n\t\t\t\t\t\t * Convert value to array and check each file path\n\t\t\t\t\t\t */\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\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 * Collect all errors before throwing so users see\n\t\t\t\t\t\t\t\t * every problem at once instead of fixing one by one\n\t\t\t\t\t\t\t\t */\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 * 若有錯誤則拋出包含詳細資訊的錯誤\n\t\t\t * Throw error with details if there are any issues\n\t\t\t */\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"]}
|
package/lib/field/fixEmpty.js
CHANGED
|
@@ -14,6 +14,14 @@ exports.fixEmptyFields = fixEmptyFields;
|
|
|
14
14
|
* @returns {boolean} 是否為空配置 / Whether it's an empty configuration
|
|
15
15
|
*/
|
|
16
16
|
function isEmptyConfig(value) {
|
|
17
|
+
/**
|
|
18
|
+
* 複雜條件:`&&` 優先於 `||`,因此實際上是四個獨立判斷的聯集——
|
|
19
|
+
* undefined、null、空物件(無自身鍵)、空字串;
|
|
20
|
+
* 物件/字串先以 `&&` 確認型別再檢查長度,避免對非該型別取值出錯
|
|
21
|
+
* Complex condition: `&&` binds tighter than `||`, so this is the union of
|
|
22
|
+
* four checks — undefined, null, empty object (no own keys), empty string;
|
|
23
|
+
* the object/string branches verify the type first to avoid bad access
|
|
24
|
+
*/
|
|
17
25
|
return typeof value === 'undefined' || value === null || typeof value === 'object' && !Object.keys(value).length || typeof value === 'string' && !value.length;
|
|
18
26
|
}
|
|
19
27
|
/**
|
|
@@ -30,6 +38,12 @@ function isEmptyConfig(value) {
|
|
|
30
38
|
* @returns {T} 清理後的 package.json 物件 / Cleaned package.json object
|
|
31
39
|
*/
|
|
32
40
|
function _fixEmptyFieldsCore(pkg, root, keys) {
|
|
41
|
+
/**
|
|
42
|
+
* 逐欄檢查;空配置直接 delete,
|
|
43
|
+
* 讓 package.json 保持精簡並避免空物件影響下游解析
|
|
44
|
+
* Check each key; delete empty configurations to keep package.json
|
|
45
|
+
* tidy and prevent empty objects from breaking downstream parsers
|
|
46
|
+
*/
|
|
33
47
|
for (const key of keys) {
|
|
34
48
|
if (isEmptyConfig(pkg[key])) {
|
|
35
49
|
delete pkg[key];
|
|
@@ -53,6 +67,14 @@ function fixEmptyFields(pkg, root) {
|
|
|
53
67
|
return _fixEmptyFieldsCore(pkg, root, [
|
|
54
68
|
'bin',
|
|
55
69
|
'resolutions',
|
|
70
|
+
'dependencies',
|
|
71
|
+
'devDependencies',
|
|
72
|
+
'dependenciesMeta',
|
|
73
|
+
'peerDependencies',
|
|
74
|
+
'peerDependenciesMeta',
|
|
75
|
+
'optionalDependencies',
|
|
76
|
+
'bundledDependencies',
|
|
77
|
+
'bundleDependencies',
|
|
56
78
|
]);
|
|
57
79
|
}
|
|
58
80
|
//# sourceMappingURL=fixEmpty.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fixEmpty.js","sourceRoot":"","sources":["fixEmpty.ts"],"names":[],"mappings":";;AAgBA,
|
|
1
|
+
{"version":3,"file":"fixEmpty.js","sourceRoot":"","sources":["fixEmpty.ts"],"names":[],"mappings":";;AAgBA,sCAWC;AAeD,kDAiBC;AAcD,wCAeC;AAlFD;;;;;;;;;GASG;AACH,SAAgB,aAAa,CAAC,KAAwC;IAErE;;;;;;;OAOG;IACH,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;;;;;OAKG;IACH,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;QAEb,cAAc;QACd,iBAAiB;QACjB,kBAAkB;QAClB,kBAAkB;QAClB,sBAAsB;QACtB,sBAAsB;QACtB,qBAAqB;QACrB,oBAAoB;KACpB,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\t/**\r\n\t * 複雜條件:`&&` 優先於 `||`,因此實際上是四個獨立判斷的聯集——\r\n\t * undefined、null、空物件(無自身鍵)、空字串;\r\n\t * 物件/字串先以 `&&` 確認型別再檢查長度,避免對非該型別取值出錯\r\n\t * Complex condition: `&&` binds tighter than `||`, so this is the union of\r\n\t * four checks — undefined, null, empty object (no own keys), empty string;\r\n\t * the object/string branches verify the type first to avoid bad access\r\n\t */\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\t/**\r\n\t * 逐欄檢查;空配置直接 delete,\r\n\t * 讓 package.json 保持精簡並避免空物件影響下游解析\r\n\t * Check each key; delete empty configurations to keep package.json\r\n\t * tidy and prevent empty objects from breaking downstream parsers\r\n\t */\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\r\n\t\t'dependencies',\r\n\t\t'devDependencies',\r\n\t\t'dependenciesMeta',\r\n\t\t'peerDependencies',\r\n\t\t'peerDependenciesMeta',\r\n\t\t'optionalDependencies',\r\n\t\t'bundledDependencies',\r\n\t\t'bundleDependencies',\r\n\t])\r\n}\r\n"]}
|
|
@@ -16,16 +16,19 @@ exports.fixPublishConfig = fixPublishConfig;
|
|
|
16
16
|
* @returns {T & { publishConfig: IPackageJsonPublishConfig }} 修正後的 package.json 物件 / Corrected package.json object
|
|
17
17
|
*/
|
|
18
18
|
function fixPublishConfig(pkg) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
19
|
+
/**
|
|
20
|
+
* 檢查是否需要添加 publishConfig:
|
|
21
|
+
* 1. 尚未有 publishConfig
|
|
22
|
+
* 2. 非私有套件
|
|
23
|
+
* 3. 有套件名稱
|
|
24
|
+
* 4. 套件名稱包含 '/'(scoped package)
|
|
25
|
+
*
|
|
26
|
+
* Check if publishConfig needs to be added:
|
|
27
|
+
* 1. No existing publishConfig
|
|
28
|
+
* 2. Not a private package
|
|
29
|
+
* 3. Has package name
|
|
30
|
+
* 4. Package name contains '/' (scoped package)
|
|
31
|
+
*/
|
|
29
32
|
if (!pkg.publishConfig
|
|
30
33
|
&& !pkg.private
|
|
31
34
|
&& pkg.name
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"publishConfig.js","sourceRoot":"","sources":["publishConfig.ts"],"names":[],"mappings":";;AAqBA,
|
|
1
|
+
{"version":3,"file":"publishConfig.js","sourceRoot":"","sources":["publishConfig.ts"],"names":[],"mappings":";;AAqBA,4CA4BC;AA1CD;;;;;;;;;;;;;GAaG;AACH,SAAgB,gBAAgB,CAAyB,GAAM;IAE9D;;;;;;;;;;;;OAYG;IACH,IAAI,CAAC,GAAG,CAAC,aAAa;WAClB,CAAC,GAAG,CAAC,OAAO;WACZ,GAAG,CAAC,IAAI;WACR,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAEvB,CAAC;QACA,GAAG,CAAC,aAAa,GAAG;YACnB,MAAM,EAAE,QAAQ;SAChB,CAAC;IACH,CAAC;IACD,OAAO,GAEN,CAAA;AACF,CAAC","sourcesContent":["/**\n * 處理 package.json publishConfig 欄位的工具模組\n * Utility module for handling package.json publishConfig field\n */\nimport { IPackageJson } from '@ts-type/package-dts/package-json';\nimport { IPackageJsonPublishConfig } from '@ts-type/package-dts/lib/package-json/types';\n\n/**\n * 修復 publishConfig 欄位\n * Fix publishConfig field\n *\n * 為 scoped package(如 @scope/package)自動添加 publishConfig.access = 'public'\n * Automatically add publishConfig.access = 'public' for scoped packages (e.g., @scope/package)\n *\n * 當套件名稱包含 '/' 且非私有套件時,確保 npm 發布時能正確設定存取權限\n * When package name contains '/' and is not private, ensure correct access settings for npm publish\n *\n * @template T - package.json 類型 / package.json type\n * @param {T} pkg - package.json 物件 / package.json object\n * @returns {T & { publishConfig: IPackageJsonPublishConfig }} 修正後的 package.json 物件 / Corrected package.json object\n */\nexport function fixPublishConfig<T extends IPackageJson>(pkg: T)\n{\n\t/**\n\t * 檢查是否需要添加 publishConfig:\n\t * 1. 尚未有 publishConfig\n\t * 2. 非私有套件\n\t * 3. 有套件名稱\n\t * 4. 套件名稱包含 '/'(scoped package)\n\t *\n\t * Check if publishConfig needs to be added:\n\t * 1. No existing publishConfig\n\t * 2. Not a private package\n\t * 3. Has package name\n\t * 4. Package name contains '/' (scoped package)\n\t */\n\tif (!pkg.publishConfig\n\t\t&& !pkg.private\n\t\t&& pkg.name\n\t\t&& /\\//.test(pkg.name)\n\t)\n\t{\n\t\tpkg.publishConfig = {\n\t\t\taccess: \"public\",\n\t\t};\n\t}\n\treturn pkg as T & {\n\t\tpublishConfig: IPackageJsonPublishConfig\n\t}\n}\n"]}
|
package/lib/field/scripts.d.ts
CHANGED
|
@@ -1,8 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 常用腳本命令字串列舉
|
|
3
|
+
* Enum of common script command strings
|
|
4
|
+
*
|
|
5
|
+
* 集中管理預設腳本的命令內容,讓各預設腳本模組引用同一份字串,
|
|
6
|
+
* 避免命令在多處重複定義後逐漸不一致
|
|
7
|
+
* Centralizes the command strings so every preset script module shares a
|
|
8
|
+
* single source, preventing drift caused by duplicated definitions
|
|
9
|
+
*/
|
|
1
10
|
export declare const enum EnumScriptsEntry {
|
|
11
|
+
/** 以 Jest 執行測試,且在沒有測試檔時仍視為成功 / Run Jest tests, passing when no test files exist */
|
|
2
12
|
JEST_TEST = "jest --passWithNoTests",
|
|
13
|
+
/** 以 Node.js 原生 test runner(經 tsx)執行測試並更新快照 / Run tests via Node's native runner (through tsx) and update snapshots */
|
|
14
|
+
NODE_TEST = "tsx --test --test-update-snapshots **/*.node-spec.ts **/*.node-test.ts **/*.spec.ts **/*.test.ts",
|
|
15
|
+
/** 預設測試命令 / Default test command */
|
|
16
|
+
DEFAULT_TEST = "node --run test:node --",
|
|
17
|
+
/**
|
|
18
|
+
* TypeScript 型別檢查:以 `&` 連結 echo,讓檢查完成後仍有成功訊息
|
|
19
|
+
* Type check: chained with `&` so a completion message is printed afterward
|
|
20
|
+
*/
|
|
21
|
+
TSC_TEST = "tsc --noEmit & echo typescript check done",
|
|
22
|
+
/** 未指定測試的預設錯誤訊息(不中斷流程)/ Default "no test specified" message (does not abort) */
|
|
3
23
|
NO_TEST_SPECIFIED = "echo \"Error: no test specified\"",
|
|
24
|
+
/** 同上,但以 `exit 1` 讓腳本以失敗結束 / Same as above, but exits with code 1 to fail the script */
|
|
4
25
|
NO_TEST_SPECIFIED_EXIT = "echo \"Error: no test specified\" && exit 1",
|
|
26
|
+
/** 以 dts-bundle-generator 產生打包型別檔 / Generate a bundled declaration file with dts-bundle-generator */
|
|
5
27
|
BUILD_DTS_BUNDLE = "ynpx @bluelovers/dts-bundle-generator -o ./dist/index.d.ts ./src/index.ts --no-banner --inline-declare-global & echo build:dts:bundle",
|
|
6
|
-
|
|
28
|
+
/** 複製已存在的型別檔到 dist(Windows copy 指令)/ Copy an existing declaration file into dist (Windows copy command) */
|
|
29
|
+
BUILD_DTS_COPY = "copy .\\src\\index.d.ts .\\dist\\index.d.ts & echo build:dts",
|
|
30
|
+
/** 僅輸出型別宣告檔 / Emit declaration files only */
|
|
31
|
+
BUILD_DTS_TSC_EMIT = "tsc --emitDeclarationOnly --declaration --noEmit false",
|
|
32
|
+
/** 完整型別建構:先 emit 再複製 / Full type build: emit first, then copy */
|
|
33
|
+
BUILD_DTS_TSC = "node --run build:dts:tsc:emit & node --run build:dts:copy",
|
|
34
|
+
/** 以 TSDX 建構 JS bundle / Build the JS bundle with TSDX */
|
|
35
|
+
BUILD_TSDX = "ynpx @bluelovers/tsdx build --target node --name index",
|
|
36
|
+
/** 建構 JS 與打包型別檔(兩者依序執行)/ Build JS and bundled declarations (run in sequence) */
|
|
37
|
+
BUILD_TSDX_AND_DTS = "node --run build:tsdx && node --run build:dts:bundle",
|
|
38
|
+
/** 版本遞增前先跑測試,避免發布未經驗證的版本 / Run tests before a version bump so unverified code is never published */
|
|
39
|
+
preversion = "pnpm run test"
|
|
7
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* 判斷腳本命令是否為「未指定測試」的佔位訊息
|
|
43
|
+
* Check whether a script command is the "no test specified" placeholder
|
|
44
|
+
*
|
|
45
|
+
* 同時涵蓋會中斷(`exit 1`)與不會中斷兩種變體,
|
|
46
|
+
* 讓呼叫端不必自行比較兩組字串
|
|
47
|
+
* Covers both the aborting (`exit 1`) and non-aborting variants so callers
|
|
48
|
+
* need not compare two literals themselves
|
|
49
|
+
*
|
|
50
|
+
* @param value - 要檢查的腳本命令 / the script command to check
|
|
51
|
+
* @returns 是否為未指定測試的佔位訊息 / whether it is the "no test specified" placeholder
|
|
52
|
+
*/
|
|
8
53
|
export declare function scriptsEntryIsNoTestSpecified(value: string): value is EnumScriptsEntry.NO_TEST_SPECIFIED_EXIT | EnumScriptsEntry.NO_TEST_SPECIFIED;
|