npm-package-json-loader 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 +48 -0
- package/README.md +39 -6
- package/index.d.ts +200 -2
- package/index.js +210 -2
- package/index.js.map +1 -1
- package/package.json +9 -11
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,54 @@
|
|
|
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/npm-package-json-loader@3.0.3...npm-package-json-loader@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
|
+
### 📚 Documentation
|
|
24
|
+
|
|
25
|
+
* 為 fix-all、pkg-entry-util、node-package-json-loader 添加完整的 JSDoc 文檔註釋 ([92d17e5](https://github.com/bluelovers/ws-yarn-workspaces/commit/92d17e5866c9330c4ee66e78b34a7318bb7d3203))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### 🛠 Build System
|
|
29
|
+
|
|
30
|
+
* update build ([51d49ad](https://github.com/bluelovers/ws-yarn-workspaces/commit/51d49adaa1f1fbfac4a299007dbf406d7ee7a4c7))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### ♻️ Chores
|
|
34
|
+
|
|
35
|
+
* Add test config and global types for tsdx ([88a8df6](https://github.com/bluelovers/ws-yarn-workspaces/commit/88a8df6eef5dc59d9e0d9268b21706dceb5ab2a1))
|
|
36
|
+
* 更新 Jest 快照連結並增強類型定義與文件 ([b663b97](https://github.com/bluelovers/ws-yarn-workspaces/commit/b663b97d78d223dc8b3927ea2433938350144de7))
|
|
37
|
+
* add __root.ts test utility to workspace packages ([a771727](https://github.com/bluelovers/ws-yarn-workspaces/commit/a7717278883c86d9ed8ab89f42070d3385c173a7))
|
|
38
|
+
* **deps:** 升級路徑與檔案系統相關依賴版本 ([78d395b](https://github.com/bluelovers/ws-yarn-workspaces/commit/78d395b5469feecc85f250ca2d8b4977161e7618))
|
|
39
|
+
* **deps:** 更新依賴版本並將腳本執行從 yarn 遷移至 node --run ([389e581](https://github.com/bluelovers/ws-yarn-workspaces/commit/389e581a07cf803dc34c8402b4d3f69af0016b8c))
|
|
40
|
+
* **deps:** 升級多項依賴套件版本 ([1382602](https://github.com/bluelovers/ws-yarn-workspaces/commit/1382602ca94ff236b251fd1481b6332468de4621))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
### 📌 Dependencies
|
|
44
|
+
|
|
45
|
+
* **release:** 主要版本升級與跨套件相依更新 ([2a24889](https://github.com/bluelovers/ws-yarn-workspaces/commit/2a248893f3667704d1ba0de12544091abf5d341e))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
### 🔖 Miscellaneous
|
|
49
|
+
|
|
50
|
+
* . ([d0a2adc](https://github.com/bluelovers/ws-yarn-workspaces/commit/d0a2adca7732fe14da2977d24e148f42b10bdd1c))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
6
54
|
## [3.0.3](https://github.com/bluelovers/ws-yarn-workspaces/compare/npm-package-json-loader@3.0.2...npm-package-json-loader@3.0.3) (2025-09-07)
|
|
7
55
|
|
|
8
56
|
**Note:** Version bump only for package npm-package-json-loader
|
package/README.md
CHANGED
|
@@ -1,12 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# npm-package-json-loader
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
用於載入、處理和寫入 package.json 的工具類別,支援自動修復、排序與驗證功能 (Utility class for loading, processing, and writing package.json with auto-fix, sorting, and verification features)
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
yarn add
|
|
9
|
-
yarn-tool add
|
|
10
|
-
yt add
|
|
8
|
+
yarn add npm-package-json-loader
|
|
9
|
+
yarn-tool add npm-package-json-loader
|
|
10
|
+
yt add npm-package-json-loader
|
|
11
|
+
|
|
12
|
+
pnpm add npm-package-json-loader
|
|
13
|
+
|
|
14
|
+
npm install npm-package-json-loader
|
|
11
15
|
```
|
|
12
16
|
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- `PackageJsonLoader` - 載入和處理 package.json 的主要類別
|
|
20
|
+
- `autofix()` - 自動修復 bin、exports、publishConfig 欄位
|
|
21
|
+
- `sort()` - 排序 package.json 欄位
|
|
22
|
+
- `write()` - 寫入 package.json 檔案
|
|
23
|
+
- `loadByModuleName()` - 透過模組名稱載入 package.json
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { PackageJsonLoader } from 'npm-package-json-loader';
|
|
29
|
+
|
|
30
|
+
// 從檔案路徑載入
|
|
31
|
+
const pkg = new PackageJsonLoader('/path/to/package.json');
|
|
32
|
+
pkg.read();
|
|
33
|
+
pkg.autofix();
|
|
34
|
+
pkg.sort();
|
|
35
|
+
pkg.write();
|
|
36
|
+
|
|
37
|
+
// 從模組名稱載入
|
|
38
|
+
const pkg2 = PackageJsonLoader.loadByModuleName('lodash');
|
|
39
|
+
|
|
40
|
+
// 使用回調處理
|
|
41
|
+
pkg.use((json) => {
|
|
42
|
+
json.version = '1.0.0';
|
|
43
|
+
});
|
|
44
|
+
pkg.run();
|
|
45
|
+
```
|
package/index.d.ts
CHANGED
|
@@ -2,41 +2,239 @@ import { IPackageJson } from '@ts-type/package-dts';
|
|
|
2
2
|
type IFileOrJson = Buffer | string | object | IPackageJson;
|
|
3
3
|
type IPackageJsonLike<T> = Partial<T> | Record<string, any>;
|
|
4
4
|
type IItemOrItemArray<T> = T | T[];
|
|
5
|
+
/**
|
|
6
|
+
* package.json 載入器類別
|
|
7
|
+
* Package.json loader class
|
|
8
|
+
*
|
|
9
|
+
* 提供載入、修改、排序、驗證和寫入 package.json 的完整功能
|
|
10
|
+
* Provides complete functionality for loading, modifying, sorting, verifying, and writing package.json
|
|
11
|
+
*
|
|
12
|
+
* @template T - package.json 類型參數 / package.json type parameter
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // 從檔案路徑載入 / Load from file path
|
|
16
|
+
* const pkg = new PackageJsonLoader('/path/to/package.json');
|
|
17
|
+
* pkg.read().autofix().sort().write();
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // 從模組名稱載入 / Load from module name
|
|
21
|
+
* const pkg = PackageJsonLoader.loadByModuleName('lodash');
|
|
22
|
+
*/
|
|
5
23
|
export declare class PackageJsonLoader<T extends IPackageJsonLike<IPackageJson> = IPackageJson> {
|
|
24
|
+
/**
|
|
25
|
+
* package.json 檔案路徑
|
|
26
|
+
* Path to package.json file
|
|
27
|
+
*/
|
|
6
28
|
readonly file: string;
|
|
29
|
+
/**
|
|
30
|
+
* 解析後的 JSON 資料
|
|
31
|
+
* Parsed JSON data
|
|
32
|
+
*/
|
|
7
33
|
protected json: T;
|
|
34
|
+
/**
|
|
35
|
+
* 是否已載入資料
|
|
36
|
+
* Whether data has been loaded
|
|
37
|
+
*/
|
|
8
38
|
loaded: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* 自訂處理函式佇列
|
|
41
|
+
* Queue of custom processing functions
|
|
42
|
+
*/
|
|
9
43
|
protected _use: ((json: IPackageJsonLike<T>) => void)[];
|
|
44
|
+
/**
|
|
45
|
+
* 建立 PackageJsonLoader 實例的工廠方法
|
|
46
|
+
* Factory method to create PackageJsonLoader instance
|
|
47
|
+
*
|
|
48
|
+
* @template T - package.json 類型參數 / package.json type parameter
|
|
49
|
+
* @param {IFileOrJson} file - 檔案路徑或 JSON 物件 / File path or JSON object
|
|
50
|
+
* @param {any[]} argv - 額外參數 / Additional arguments
|
|
51
|
+
* @returns {PackageJsonLoader<T>} PackageJsonLoader 實例 / PackageJsonLoader instance
|
|
52
|
+
*/
|
|
10
53
|
static create<T = IPackageJson>(file: IFileOrJson, ...argv: any[]): PackageJsonLoader<T>;
|
|
54
|
+
/**
|
|
55
|
+
* 從 JSON 物件建立實例
|
|
56
|
+
* Create instance from JSON object
|
|
57
|
+
*
|
|
58
|
+
* @template T - package.json 類型參數 / package.json type parameter
|
|
59
|
+
* @param {T} json - JSON 物件 / JSON object
|
|
60
|
+
* @param {any[]} argv - 額外參數 / Additional arguments
|
|
61
|
+
* @returns {PackageJsonLoader<T>} PackageJsonLoader 實例 / PackageJsonLoader instance
|
|
62
|
+
*/
|
|
11
63
|
static createByJson<T = IPackageJson>(json: T, ...argv: any[]): PackageJsonLoader<T>;
|
|
64
|
+
/**
|
|
65
|
+
* 透過模組名稱尋找 package.json 路徑
|
|
66
|
+
* Find package.json path by module name
|
|
67
|
+
*
|
|
68
|
+
* @param {string} name - 模組名稱 / Module name
|
|
69
|
+
* @returns {string} package.json 的絕對路徑 / Absolute path to package.json
|
|
70
|
+
*/
|
|
12
71
|
static findPackageJsonPath(name: string): string;
|
|
72
|
+
/**
|
|
73
|
+
* 透過模組名稱載入 package.json
|
|
74
|
+
* Load package.json by module name
|
|
75
|
+
*
|
|
76
|
+
* @template T - package.json 類型參數 / package.json type parameter
|
|
77
|
+
* @param {string} name - 模組名稱 / Module name
|
|
78
|
+
* @returns {PackageJsonLoader<T>} PackageJsonLoader 實例 / PackageJsonLoader instance
|
|
79
|
+
* @throws {TypeError} 當套件名稱不符時拋出錯誤 / Throws when package name doesn't match
|
|
80
|
+
*/
|
|
13
81
|
static loadByModuleName<T = IPackageJson>(name: string): PackageJsonLoader<T>;
|
|
82
|
+
/**
|
|
83
|
+
* 建構子
|
|
84
|
+
* Constructor
|
|
85
|
+
*
|
|
86
|
+
* @param {IFileOrJson} fileOrJson - 檔案路徑、Buffer 或 JSON 物件 / File path, Buffer, or JSON object
|
|
87
|
+
* @param {any[]} argv - 額外參數 / Additional arguments
|
|
88
|
+
*/
|
|
14
89
|
constructor(fileOrJson: IFileOrJson, ...argv: any[]);
|
|
90
|
+
/**
|
|
91
|
+
* 註冊自訂處理函式
|
|
92
|
+
* Register custom processing function
|
|
93
|
+
*
|
|
94
|
+
* @param {IItemOrItemArray<(json: IPackageJsonLike<T>) => void>} ls - 處理函式或函式陣列 / Processing function or array of functions
|
|
95
|
+
*/
|
|
15
96
|
use(ls: IItemOrItemArray<(json: IPackageJsonLike<T>) => void>): void;
|
|
97
|
+
/**
|
|
98
|
+
* 設定檔案名稱
|
|
99
|
+
* Set filename
|
|
100
|
+
*
|
|
101
|
+
* @param {string} file - 檔案路徑 / File path
|
|
102
|
+
* @returns {this} 当前實例 / Current instance
|
|
103
|
+
*/
|
|
16
104
|
setFilename(file: string): this;
|
|
105
|
+
/**
|
|
106
|
+
* 設定 JSON 資料
|
|
107
|
+
* Set JSON data
|
|
108
|
+
*
|
|
109
|
+
* @param {object | T} json - JSON 物件 / JSON object
|
|
110
|
+
* @returns {this} 当前實例 / Current instance
|
|
111
|
+
*/
|
|
17
112
|
setJson(json: object | T): this;
|
|
113
|
+
/**
|
|
114
|
+
* 讀取 package.json 檔案
|
|
115
|
+
* Read package.json file
|
|
116
|
+
*
|
|
117
|
+
* @param {boolean} reload - 是否強制重新載入 / Whether to force reload
|
|
118
|
+
* @returns {this} 当前實例 / Current instance
|
|
119
|
+
*/
|
|
18
120
|
read(reload?: boolean): this;
|
|
121
|
+
/**
|
|
122
|
+
* 重新載入 package.json
|
|
123
|
+
* Reload package.json
|
|
124
|
+
*
|
|
125
|
+
* @returns {this} 当前實例 / Current instance
|
|
126
|
+
*/
|
|
19
127
|
reload(): this;
|
|
128
|
+
/**
|
|
129
|
+
* 取得 package.json 所在目錄
|
|
130
|
+
* Get directory of package.json
|
|
131
|
+
*
|
|
132
|
+
* @returns {string} 目錄路徑 / Directory path
|
|
133
|
+
*/
|
|
20
134
|
get dir(): string;
|
|
21
135
|
/**
|
|
22
|
-
*
|
|
136
|
+
* 取得跳過 TypeScript 類型檢查的資料
|
|
137
|
+
* Get data without TypeScript type checking
|
|
138
|
+
*
|
|
139
|
+
* 用於需要靈活操作 JSON 資料的場景
|
|
140
|
+
* Use when flexible JSON data manipulation is needed
|
|
23
141
|
*/
|
|
24
142
|
get unsafeTypeData(): IPackageJsonLike<T>;
|
|
25
143
|
/**
|
|
26
|
-
*
|
|
144
|
+
* 設定跳過 TypeScript 類型檢查的資料
|
|
145
|
+
* Set data without TypeScript type checking
|
|
27
146
|
*/
|
|
28
147
|
set unsafeTypeData(json: IPackageJsonLike<T>);
|
|
148
|
+
/**
|
|
149
|
+
* 設定 package.json 資料
|
|
150
|
+
* Set package.json data
|
|
151
|
+
*/
|
|
29
152
|
set data(json: T);
|
|
153
|
+
/**
|
|
154
|
+
* 取得 package.json 資料
|
|
155
|
+
* Get package.json data
|
|
156
|
+
*
|
|
157
|
+
* 若尚未載入會自動讀取檔案
|
|
158
|
+
* Automatically reads file if not loaded yet
|
|
159
|
+
*
|
|
160
|
+
* @returns {T} package.json 資料 / package.json data
|
|
161
|
+
*/
|
|
30
162
|
get data(): T;
|
|
163
|
+
/**
|
|
164
|
+
* 覆寫 JSON 資料
|
|
165
|
+
* Overwrite JSON data
|
|
166
|
+
*
|
|
167
|
+
* @param {object | T} json - 新的 JSON 資料 / New JSON data
|
|
168
|
+
* @returns {this} 当前實例 / Current instance
|
|
169
|
+
*/
|
|
31
170
|
overwrite(json: object | T): this;
|
|
171
|
+
/**
|
|
172
|
+
* 自動修復 package.json 欄位
|
|
173
|
+
* Auto-fix package.json fields
|
|
174
|
+
*
|
|
175
|
+
* 執行以下修復操作:
|
|
176
|
+
* 1. 修復 bin 欄位路徑
|
|
177
|
+
* 2. 修復 publishConfig 欄位
|
|
178
|
+
* 3. 添加 exports 中的 package.json entry
|
|
179
|
+
* 4. 清理空欄位
|
|
180
|
+
*
|
|
181
|
+
* Performs the following fix operations:
|
|
182
|
+
* 1. Fix bin field paths
|
|
183
|
+
* 2. Fix publishConfig field
|
|
184
|
+
* 3. Add package.json entry in exports
|
|
185
|
+
* 4. Clean empty fields
|
|
186
|
+
*/
|
|
32
187
|
autofix(): void;
|
|
188
|
+
/**
|
|
189
|
+
* 執行處理流程
|
|
190
|
+
* Run processing pipeline
|
|
191
|
+
*
|
|
192
|
+
* @param {object} options - 選項 / Options
|
|
193
|
+
* @param {boolean} options.autofix - 是否執行自動修復 / Whether to run auto-fix
|
|
194
|
+
* @returns {this} 当前實例 / Current instance
|
|
195
|
+
*/
|
|
33
196
|
run(options?: {
|
|
34
197
|
autofix?: boolean;
|
|
35
198
|
}): this;
|
|
199
|
+
/**
|
|
200
|
+
* 檢查檔案是否存在
|
|
201
|
+
* Check if file exists
|
|
202
|
+
*
|
|
203
|
+
* @returns {boolean} 檔案是否存在 / Whether file exists
|
|
204
|
+
*/
|
|
36
205
|
exists(): boolean;
|
|
206
|
+
/**
|
|
207
|
+
* 將 JSON 資料轉換為字串
|
|
208
|
+
* Convert JSON data to string
|
|
209
|
+
*
|
|
210
|
+
* @returns {string} 格式化的 JSON 字串 / Formatted JSON string
|
|
211
|
+
*/
|
|
37
212
|
stringify(): string;
|
|
213
|
+
/**
|
|
214
|
+
* 排序 package.json 欄位
|
|
215
|
+
* Sort package.json fields
|
|
216
|
+
*
|
|
217
|
+
* 使用 sort-package-json3 套件進行標準化排序
|
|
218
|
+
* Use sort-package-json3 package for standardized sorting
|
|
219
|
+
*
|
|
220
|
+
* @returns {this} 当前實例 / Current instance
|
|
221
|
+
* @throws {Error} 當資料為 undefined 或 null 時拋出錯誤 / Throws when data is undefined or null
|
|
222
|
+
*/
|
|
38
223
|
sort(): this;
|
|
224
|
+
/**
|
|
225
|
+
* 寫入 package.json 檔案
|
|
226
|
+
* Write package.json file
|
|
227
|
+
*
|
|
228
|
+
* @returns {this} 当前實例 / Current instance
|
|
229
|
+
* @throws {Error} 當檔案路徑未設定時拋出錯誤 / Throws when file path is not set
|
|
230
|
+
*/
|
|
39
231
|
write(): this;
|
|
232
|
+
/**
|
|
233
|
+
* 僅在已載入時寫入
|
|
234
|
+
* Write only when loaded
|
|
235
|
+
*
|
|
236
|
+
* @returns {boolean} 是否已寫入 / Whether written
|
|
237
|
+
*/
|
|
40
238
|
writeOnlyWhenLoaded(): boolean;
|
|
41
239
|
}
|
|
42
240
|
export default PackageJsonLoader;
|
package/index.js
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PackageJsonLoader = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
/**
|
|
6
|
+
* npm-package-json-loader
|
|
7
|
+
*
|
|
8
|
+
* 用於載入、處理和寫入 package.json 的工具類別
|
|
9
|
+
* Utility class for loading, processing, and writing package.json
|
|
10
|
+
*
|
|
11
|
+
* 支援自動修復、排序與驗證功能
|
|
12
|
+
* Supports auto-fix, sorting, and verification features
|
|
13
|
+
*/
|
|
5
14
|
const fs_extra_1 = require("fs-extra");
|
|
6
15
|
const sort_package_json3_1 = require("sort-package-json3");
|
|
7
16
|
const bind_decorator_1 = tslib_1.__importDefault(require("bind-decorator"));
|
|
@@ -12,26 +21,93 @@ const fs_json_1 = require("@bluelovers/fs-json");
|
|
|
12
21
|
const publishConfig_1 = require("@yarn-tool/pkg-entry-util/lib/field/publishConfig");
|
|
13
22
|
const bin_1 = require("@yarn-tool/pkg-entry-util/lib/field/bin");
|
|
14
23
|
const write_package_json_1 = require("@yarn-tool/write-package-json");
|
|
24
|
+
const fixEmpty_1 = require("@yarn-tool/pkg-entry-util/lib/field/fixEmpty");
|
|
25
|
+
/**
|
|
26
|
+
* package.json 載入器類別
|
|
27
|
+
* Package.json loader class
|
|
28
|
+
*
|
|
29
|
+
* 提供載入、修改、排序、驗證和寫入 package.json 的完整功能
|
|
30
|
+
* Provides complete functionality for loading, modifying, sorting, verifying, and writing package.json
|
|
31
|
+
*
|
|
32
|
+
* @template T - package.json 類型參數 / package.json type parameter
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // 從檔案路徑載入 / Load from file path
|
|
36
|
+
* const pkg = new PackageJsonLoader('/path/to/package.json');
|
|
37
|
+
* pkg.read().autofix().sort().write();
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* // 從模組名稱載入 / Load from module name
|
|
41
|
+
* const pkg = PackageJsonLoader.loadByModuleName('lodash');
|
|
42
|
+
*/
|
|
15
43
|
class PackageJsonLoader {
|
|
44
|
+
/**
|
|
45
|
+
* 建立 PackageJsonLoader 實例的工廠方法
|
|
46
|
+
* Factory method to create PackageJsonLoader instance
|
|
47
|
+
*
|
|
48
|
+
* @template T - package.json 類型參數 / package.json type parameter
|
|
49
|
+
* @param {IFileOrJson} file - 檔案路徑或 JSON 物件 / File path or JSON object
|
|
50
|
+
* @param {any[]} argv - 額外參數 / Additional arguments
|
|
51
|
+
* @returns {PackageJsonLoader<T>} PackageJsonLoader 實例 / PackageJsonLoader instance
|
|
52
|
+
*/
|
|
16
53
|
static create(file, ...argv) {
|
|
17
54
|
return new this(file, ...argv);
|
|
18
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* 從 JSON 物件建立實例
|
|
58
|
+
* Create instance from JSON object
|
|
59
|
+
*
|
|
60
|
+
* @template T - package.json 類型參數 / package.json type parameter
|
|
61
|
+
* @param {T} json - JSON 物件 / JSON object
|
|
62
|
+
* @param {any[]} argv - 額外參數 / Additional arguments
|
|
63
|
+
* @returns {PackageJsonLoader<T>} PackageJsonLoader 實例 / PackageJsonLoader instance
|
|
64
|
+
*/
|
|
19
65
|
static createByJson(json, ...argv) {
|
|
20
66
|
return new this(json, ...argv);
|
|
21
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* 透過模組名稱尋找 package.json 路徑
|
|
70
|
+
* Find package.json path by module name
|
|
71
|
+
*
|
|
72
|
+
* @param {string} name - 模組名稱 / Module name
|
|
73
|
+
* @returns {string} package.json 的絕對路徑 / Absolute path to package.json
|
|
74
|
+
*/
|
|
22
75
|
static findPackageJsonPath(name) {
|
|
23
76
|
return (0, resolve_package_1.resolvePackageJsonLocation)(name);
|
|
24
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* 透過模組名稱載入 package.json
|
|
80
|
+
* Load package.json by module name
|
|
81
|
+
*
|
|
82
|
+
* @template T - package.json 類型參數 / package.json type parameter
|
|
83
|
+
* @param {string} name - 模組名稱 / Module name
|
|
84
|
+
* @returns {PackageJsonLoader<T>} PackageJsonLoader 實例 / PackageJsonLoader instance
|
|
85
|
+
* @throws {TypeError} 當套件名稱不符時拋出錯誤 / Throws when package name doesn't match
|
|
86
|
+
*/
|
|
25
87
|
static loadByModuleName(name) {
|
|
26
88
|
let file = this.findPackageJsonPath(name);
|
|
27
89
|
let pkg = this.create(file);
|
|
90
|
+
// 驗證載入的套件名稱是否與請求的名稱相符
|
|
91
|
+
// Verify loaded package name matches requested name
|
|
28
92
|
if (pkg.data.name !== name) {
|
|
29
93
|
throw new TypeError(`package name not match, '${pkg.data.name}' != '${name}'`);
|
|
30
94
|
}
|
|
31
95
|
return pkg;
|
|
32
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* 建構子
|
|
99
|
+
* Constructor
|
|
100
|
+
*
|
|
101
|
+
* @param {IFileOrJson} fileOrJson - 檔案路徑、Buffer 或 JSON 物件 / File path, Buffer, or JSON object
|
|
102
|
+
* @param {any[]} argv - 額外參數 / Additional arguments
|
|
103
|
+
*/
|
|
33
104
|
constructor(fileOrJson, ...argv) {
|
|
105
|
+
/**
|
|
106
|
+
* 自訂處理函式佇列
|
|
107
|
+
* Queue of custom processing functions
|
|
108
|
+
*/
|
|
34
109
|
this._use = [];
|
|
110
|
+
// 根據輸入類型初始化實例 / Initialize instance based on input type
|
|
35
111
|
if (typeof fileOrJson === 'string') {
|
|
36
112
|
this.setFilename(fileOrJson);
|
|
37
113
|
}
|
|
@@ -45,6 +121,12 @@ class PackageJsonLoader {
|
|
|
45
121
|
throw new TypeError(`fileOrJson is not valid`);
|
|
46
122
|
}
|
|
47
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* 註冊自訂處理函式
|
|
126
|
+
* Register custom processing function
|
|
127
|
+
*
|
|
128
|
+
* @param {IItemOrItemArray<(json: IPackageJsonLike<T>) => void>} ls - 處理函式或函式陣列 / Processing function or array of functions
|
|
129
|
+
*/
|
|
48
130
|
use(ls) {
|
|
49
131
|
if (Array.isArray(ls)) {
|
|
50
132
|
this._use.push(...ls);
|
|
@@ -53,16 +135,37 @@ class PackageJsonLoader {
|
|
|
53
135
|
this._use.push(ls);
|
|
54
136
|
}
|
|
55
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* 設定檔案名稱
|
|
140
|
+
* Set filename
|
|
141
|
+
*
|
|
142
|
+
* @param {string} file - 檔案路徑 / File path
|
|
143
|
+
* @returns {this} 当前實例 / Current instance
|
|
144
|
+
*/
|
|
56
145
|
setFilename(file) {
|
|
57
146
|
// @ts-ignore
|
|
58
147
|
this.file = file;
|
|
59
148
|
return this;
|
|
60
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* 設定 JSON 資料
|
|
152
|
+
* Set JSON data
|
|
153
|
+
*
|
|
154
|
+
* @param {object | T} json - JSON 物件 / JSON object
|
|
155
|
+
* @returns {this} 当前實例 / Current instance
|
|
156
|
+
*/
|
|
61
157
|
setJson(json) {
|
|
62
158
|
this.loaded = true;
|
|
63
159
|
this.json = json;
|
|
64
160
|
return this;
|
|
65
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* 讀取 package.json 檔案
|
|
164
|
+
* Read package.json file
|
|
165
|
+
*
|
|
166
|
+
* @param {boolean} reload - 是否強制重新載入 / Whether to force reload
|
|
167
|
+
* @returns {this} 当前實例 / Current instance
|
|
168
|
+
*/
|
|
66
169
|
read(reload) {
|
|
67
170
|
if (!this.loaded || reload) {
|
|
68
171
|
this.json = (0, fs_json_1.readJSONSync)(this.file);
|
|
@@ -70,63 +173,155 @@ class PackageJsonLoader {
|
|
|
70
173
|
this.loaded = true;
|
|
71
174
|
return this;
|
|
72
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* 重新載入 package.json
|
|
178
|
+
* Reload package.json
|
|
179
|
+
*
|
|
180
|
+
* @returns {this} 当前實例 / Current instance
|
|
181
|
+
*/
|
|
73
182
|
reload() {
|
|
74
183
|
return this.read(true);
|
|
75
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* 取得 package.json 所在目錄
|
|
187
|
+
* Get directory of package.json
|
|
188
|
+
*
|
|
189
|
+
* @returns {string} 目錄路徑 / Directory path
|
|
190
|
+
*/
|
|
76
191
|
get dir() {
|
|
77
192
|
return path_1.default.dirname(this.file);
|
|
78
193
|
}
|
|
79
194
|
/**
|
|
80
|
-
*
|
|
195
|
+
* 取得跳過 TypeScript 類型檢查的資料
|
|
196
|
+
* Get data without TypeScript type checking
|
|
197
|
+
*
|
|
198
|
+
* 用於需要靈活操作 JSON 資料的場景
|
|
199
|
+
* Use when flexible JSON data manipulation is needed
|
|
81
200
|
*/
|
|
82
201
|
get unsafeTypeData() {
|
|
83
202
|
return this.data;
|
|
84
203
|
}
|
|
85
204
|
/**
|
|
86
|
-
*
|
|
205
|
+
* 設定跳過 TypeScript 類型檢查的資料
|
|
206
|
+
* Set data without TypeScript type checking
|
|
87
207
|
*/
|
|
88
208
|
set unsafeTypeData(json) {
|
|
89
209
|
this.data = json;
|
|
90
210
|
}
|
|
211
|
+
/**
|
|
212
|
+
* 設定 package.json 資料
|
|
213
|
+
* Set package.json data
|
|
214
|
+
*/
|
|
91
215
|
set data(json) {
|
|
92
216
|
this.overwrite(json);
|
|
93
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* 取得 package.json 資料
|
|
220
|
+
* Get package.json data
|
|
221
|
+
*
|
|
222
|
+
* 若尚未載入會自動讀取檔案
|
|
223
|
+
* Automatically reads file if not loaded yet
|
|
224
|
+
*
|
|
225
|
+
* @returns {T} package.json 資料 / package.json data
|
|
226
|
+
*/
|
|
94
227
|
get data() {
|
|
95
228
|
if (!this.loaded && this.file) {
|
|
96
229
|
this.read();
|
|
97
230
|
}
|
|
98
231
|
return this.json;
|
|
99
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* 覆寫 JSON 資料
|
|
235
|
+
* Overwrite JSON data
|
|
236
|
+
*
|
|
237
|
+
* @param {object | T} json - 新的 JSON 資料 / New JSON data
|
|
238
|
+
* @returns {this} 当前實例 / Current instance
|
|
239
|
+
*/
|
|
100
240
|
overwrite(json) {
|
|
101
241
|
this.loaded = true;
|
|
102
242
|
this.json = json;
|
|
103
243
|
return this;
|
|
104
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* 自動修復 package.json 欄位
|
|
247
|
+
* Auto-fix package.json fields
|
|
248
|
+
*
|
|
249
|
+
* 執行以下修復操作:
|
|
250
|
+
* 1. 修復 bin 欄位路徑
|
|
251
|
+
* 2. 修復 publishConfig 欄位
|
|
252
|
+
* 3. 添加 exports 中的 package.json entry
|
|
253
|
+
* 4. 清理空欄位
|
|
254
|
+
*
|
|
255
|
+
* Performs the following fix operations:
|
|
256
|
+
* 1. Fix bin field paths
|
|
257
|
+
* 2. Fix publishConfig field
|
|
258
|
+
* 3. Add package.json entry in exports
|
|
259
|
+
* 4. Clean empty fields
|
|
260
|
+
*/
|
|
105
261
|
autofix() {
|
|
106
262
|
var _a;
|
|
107
263
|
let self = this;
|
|
108
264
|
let dir;
|
|
265
|
+
// 檢查檔案和目錄是否存在
|
|
266
|
+
// Check if file and directory exist
|
|
109
267
|
if (self.file && (0, fs_extra_1.pathExistsSync)(dir = self.dir)) {
|
|
110
268
|
if (self.data) {
|
|
269
|
+
// 修復 bin 欄位路徑 / Fix bin field paths
|
|
111
270
|
(0, bin_1.fixPkgBinField)(self.data, dir);
|
|
271
|
+
// 修復 publishConfig 欄位 / Fix publishConfig field
|
|
112
272
|
(0, publishConfig_1.fixPublishConfig)(self.data);
|
|
113
273
|
}
|
|
114
274
|
}
|
|
275
|
+
// 在 exports 中添加 package.json entry / Add package.json entry in exports
|
|
115
276
|
(0, exports_1._pkgExportsAddPJsonEntryCore)((_a = self.data) === null || _a === void 0 ? void 0 : _a.exports);
|
|
277
|
+
// 清理空欄位 / Clean empty fields
|
|
278
|
+
(0, fixEmpty_1.fixEmptyFields)(self.data, dir);
|
|
116
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* 執行處理流程
|
|
282
|
+
* Run processing pipeline
|
|
283
|
+
*
|
|
284
|
+
* @param {object} options - 選項 / Options
|
|
285
|
+
* @param {boolean} options.autofix - 是否執行自動修復 / Whether to run auto-fix
|
|
286
|
+
* @returns {this} 当前實例 / Current instance
|
|
287
|
+
*/
|
|
117
288
|
run(options = {}) {
|
|
289
|
+
// 預設執行自動修復 / Default to run auto-fix
|
|
118
290
|
if (options.autofix == null || options.autofix) {
|
|
119
291
|
this.autofix();
|
|
120
292
|
}
|
|
293
|
+
// 執行所有註冊的處理函式 / Execute all registered processing functions
|
|
121
294
|
this._use.forEach(fn => fn.call(this, this.data));
|
|
122
295
|
return this;
|
|
123
296
|
}
|
|
297
|
+
/**
|
|
298
|
+
* 檢查檔案是否存在
|
|
299
|
+
* Check if file exists
|
|
300
|
+
*
|
|
301
|
+
* @returns {boolean} 檔案是否存在 / Whether file exists
|
|
302
|
+
*/
|
|
124
303
|
exists() {
|
|
125
304
|
return (0, fs_extra_1.pathExistsSync)(this.file);
|
|
126
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* 將 JSON 資料轉換為字串
|
|
308
|
+
* Convert JSON data to string
|
|
309
|
+
*
|
|
310
|
+
* @returns {string} 格式化的 JSON 字串 / Formatted JSON string
|
|
311
|
+
*/
|
|
127
312
|
stringify() {
|
|
128
313
|
return JSON.stringify(this.json, null, 2);
|
|
129
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* 排序 package.json 欄位
|
|
317
|
+
* Sort package.json fields
|
|
318
|
+
*
|
|
319
|
+
* 使用 sort-package-json3 套件進行標準化排序
|
|
320
|
+
* Use sort-package-json3 package for standardized sorting
|
|
321
|
+
*
|
|
322
|
+
* @returns {this} 当前實例 / Current instance
|
|
323
|
+
* @throws {Error} 當資料為 undefined 或 null 時拋出錯誤 / Throws when data is undefined or null
|
|
324
|
+
*/
|
|
130
325
|
sort() {
|
|
131
326
|
if (typeof this.data === 'undefined' || this.data === null) {
|
|
132
327
|
throw new Error(`data is undefined`);
|
|
@@ -134,6 +329,13 @@ class PackageJsonLoader {
|
|
|
134
329
|
this.data = (0, sort_package_json3_1.sortPackageJson)(this.data);
|
|
135
330
|
return this;
|
|
136
331
|
}
|
|
332
|
+
/**
|
|
333
|
+
* 寫入 package.json 檔案
|
|
334
|
+
* Write package.json file
|
|
335
|
+
*
|
|
336
|
+
* @returns {this} 当前實例 / Current instance
|
|
337
|
+
* @throws {Error} 當檔案路徑未設定時拋出錯誤 / Throws when file path is not set
|
|
338
|
+
*/
|
|
137
339
|
write() {
|
|
138
340
|
if (!this.file) {
|
|
139
341
|
throw new Error(`file is undefined`);
|
|
@@ -141,6 +343,12 @@ class PackageJsonLoader {
|
|
|
141
343
|
(0, write_package_json_1.outputPackageJSONSync)(this.file, this.json);
|
|
142
344
|
return this;
|
|
143
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* 僅在已載入時寫入
|
|
348
|
+
* Write only when loaded
|
|
349
|
+
*
|
|
350
|
+
* @returns {boolean} 是否已寫入 / Whether written
|
|
351
|
+
*/
|
|
144
352
|
writeOnlyWhenLoaded() {
|
|
145
353
|
if (this.loaded) {
|
|
146
354
|
this.write();
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;AAAA,uCAAyD;AACzD,2DAAqD;AACrD,4EAAkC;AAClC,wDAAwB;AAExB,gEAAwE;AACxE,yEAA2F;AAC3F,iDAAmD;AACnD,qFAAqF;AACrF,iEAAqF;AACrF,sEAAsE;AAQtE,MAAa,iBAAiB;IAStB,AAAP,MAAM,CAAC,MAAM,CAAmB,IAAiB,EAAE,GAAG,IAAI;QAEzD,OAAO,IAAI,IAAI,CAAI,IAAI,EAAE,GAAG,IAAI,CAAC,CAAA;IAClC,CAAC;IAED,MAAM,CAAC,YAAY,CAAmB,IAAO,EAAE,GAAG,IAAI;QAErD,OAAO,IAAI,IAAI,CAAI,IAAW,EAAE,GAAG,IAAI,CAAC,CAAA;IACzC,CAAC;IAED,MAAM,CAAC,mBAAmB,CAAC,IAAY;QAEtC,OAAO,IAAA,4CAA0B,EAAC,IAAI,CAAC,CAAC;IACzC,CAAC;IAGM,AAAP,MAAM,CAAC,gBAAgB,CAAmB,IAAY;QAErD,IAAI,IAAI,GAAG,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC;QAE1C,IAAI,GAAG,GAAG,IAAI,CAAC,MAAM,CAAI,IAAI,CAAC,CAAC;QAE/B,IAAK,GAAG,CAAC,IAAY,CAAC,IAAI,KAAK,IAAI,EACnC,CAAC;YACA,MAAM,IAAI,SAAS,CAAC,4BAA6B,GAAG,CAAC,IAAY,CAAC,IAAI,SAAS,IAAI,GAAG,CAAC,CAAC;QACzF,CAAC;QAED,OAAO,GAAG,CAAC;IACZ,CAAC;IAED,YAAY,UAAuB,EAAE,GAAG,IAAI;QAjClC,SAAI,GAA4C,EAAE,CAAC;QAmC5D,IAAI,OAAO,UAAU,KAAK,QAAQ,EAClC,CAAC;YACA,IAAI,CAAC,WAAW,CAAC,UAAU,CAA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;IADN,wBAAI;;;;+CAaJ;AA2LF,kBAAe,iBAAiB,CAAA","sourcesContent":["import { pathExistsSync, writeFileSync } from 'fs-extra';\nimport { sortPackageJson } from 'sort-package-json3';\nimport bind from 'bind-decorator';\nimport path from 'path';\nimport { IPackageJson } from '@ts-type/package-dts';\nimport { resolvePackageJsonLocation } from '@yarn-tool/resolve-package';\nimport { _pkgExportsAddPJsonEntryCore } from '@yarn-tool/pkg-entry-util/lib/field/exports';\nimport { readJSONSync } from '@bluelovers/fs-json';\nimport { fixPublishConfig } from '@yarn-tool/pkg-entry-util/lib/field/publishConfig';\nimport { fixBinPath, fixPkgBinField } from '@yarn-tool/pkg-entry-util/lib/field/bin';\nimport { outputPackageJSONSync } from '@yarn-tool/write-package-json';\n\ntype IFileOrJson = Buffer | string | object | IPackageJson\n\ntype IPackageJsonLike<T> = Partial<T> | Record<string, any>;\n\ntype IItemOrItemArray<T> = T | T[];\n\nexport class PackageJsonLoader<T extends IPackageJsonLike<IPackageJson> = IPackageJson>\n{\n\treadonly file: string;\n\tprotected json: T;\n\tloaded: boolean;\n\n\tprotected _use: ((json: IPackageJsonLike<T>) => void)[] = [];\n\n\t@bind\n\tstatic create<T = IPackageJson>(file: IFileOrJson, ...argv)\n\t{\n\t\treturn new this<T>(file, ...argv)\n\t}\n\n\tstatic createByJson<T = IPackageJson>(json: T, ...argv)\n\t{\n\t\treturn new this<T>(json as any, ...argv)\n\t}\n\n\tstatic findPackageJsonPath(name: string): string\n\t{\n\t\treturn resolvePackageJsonLocation(name);\n\t}\n\n\t@bind\n\tstatic loadByModuleName<T = IPackageJson>(name: string)\n\t{\n\t\tlet file = this.findPackageJsonPath(name);\n\n\t\tlet pkg = this.create<T>(file);\n\n\t\tif ((pkg.data as any).name !== name)\n\t\t{\n\t\t\tthrow new TypeError(`package name not match, '${(pkg.data as any).name}' != '${name}'`);\n\t\t}\n\n\t\treturn pkg;\n\t}\n\n\tconstructor(fileOrJson: IFileOrJson, ...argv)\n\t{\n\t\tif (typeof fileOrJson === 'string')\n\t\t{\n\t\t\tthis.setFilename(fileOrJson)\n\t\t}\n\t\telse if (Buffer.isBuffer(fileOrJson))\n\t\t{\n\t\t\tthis.setJson(JSON.parse(fileOrJson.toString()))\n\t\t}\n\t\telse if (typeof fileOrJson === 'object')\n\t\t{\n\t\t\tthis.setJson(JSON.parse(fileOrJson.toString()))\n\t\t}\n\t\telse if (fileOrJson != null)\n\t\t{\n\t\t\tthrow new TypeError(`fileOrJson is not valid`)\n\t\t}\n\t}\n\n\tuse(ls: IItemOrItemArray<(json: IPackageJsonLike<T>) => void>)\n\t{\n\t\tif (Array.isArray(ls))\n\t\t{\n\t\t\tthis._use.push(...ls);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tthis._use.push(ls);\n\t\t}\n\t}\n\n\tsetFilename(file: string)\n\t{\n\t\t// @ts-ignore\n\t\tthis.file = file;\n\n\t\treturn this;\n\t}\n\n\tsetJson(json: object | T)\n\t{\n\t\tthis.loaded = true;\n\t\tthis.json = json as T;\n\n\t\treturn this;\n\t}\n\n\tread(reload?: boolean)\n\t{\n\t\tif (!this.loaded || reload)\n\t\t{\n\t\t\tthis.json = readJSONSync(this.file);\n\t\t}\n\n\t\tthis.loaded = true;\n\n\t\treturn this;\n\t}\n\n\treload()\n\t{\n\t\treturn this.read(true);\n\t}\n\n\tget dir()\n\t{\n\t\treturn path.dirname(this.file)\n\t}\n\n\t/**\n\t * skip typescript type check\n\t */\n\tget unsafeTypeData(): IPackageJsonLike<T>\n\t{\n\t\treturn this.data as any;\n\t}\n\n\t/**\n\t * skip typescript type check\n\t */\n\tset unsafeTypeData(json)\n\t{\n\t\tthis.data = json as any;\n\t}\n\n\tset data(json: T)\n\t{\n\t\tthis.overwrite(json);\n\t}\n\n\tget data(): T\n\t{\n\t\tif (!this.loaded && this.file)\n\t\t{\n\t\t\tthis.read();\n\t\t}\n\n\t\treturn this.json;\n\t}\n\n\toverwrite(json: object | T)\n\t{\n\t\tthis.loaded = true;\n\t\tthis.json = json as T;\n\n\t\treturn this;\n\t}\n\n\tautofix()\n\t{\n\t\tlet self = this;\n\t\tlet dir: string;\n\n\t\tif (self.file && pathExistsSync(dir = self.dir))\n\t\t{\n\t\t\tif (self.data)\n\t\t\t{\n\t\t\t\tfixPkgBinField(self.data, dir);\n\t\t\t\tfixPublishConfig(self.data);\n\t\t\t}\n\t\t}\n\n\t\t_pkgExportsAddPJsonEntryCore(self.data?.exports);\n\t}\n\n\trun(options: {\n\t\tautofix?: boolean\n\t} = {})\n\t{\n\t\tif (options.autofix == null || options.autofix)\n\t\t{\n\t\t\tthis.autofix();\n\t\t}\n\n\t\tthis._use.forEach(fn => fn.call(this, this.data));\n\n\t\treturn this;\n\t}\n\n\texists()\n\t{\n\t\treturn pathExistsSync(this.file)\n\t}\n\n\tstringify()\n\t{\n\t\treturn JSON.stringify(this.json, null, 2)\n\t}\n\n\tsort()\n\t{\n\t\tif (typeof this.data === 'undefined' || this.data === null)\n\t\t{\n\t\t\tthrow new Error(`data is undefined`)\n\t\t}\n\n\t\tthis.data = sortPackageJson(this.data);\n\n\t\treturn this;\n\t}\n\n\twrite()\n\t{\n\t\tif (!this.file)\n\t\t{\n\t\t\tthrow new Error(`file is undefined`)\n\t\t}\n\n\t\toutputPackageJSONSync(this.file, this.json);\n\n\t\treturn this;\n\t}\n\n\twriteOnlyWhenLoaded()\n\t{\n\t\tif (this.loaded)\n\t\t{\n\t\t\tthis.write();\n\t\t}\n\n\t\treturn this.loaded;\n\t}\n}\n\nexport default PackageJsonLoader\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;AAAA;;;;;;;;GAQG;AACH,uCAAyD;AACzD,2DAAqD;AACrD,4EAAkC;AAClC,wDAAwB;AAExB,gEAAwE;AACxE,yEAA2F;AAC3F,iDAAmD;AACnD,qFAAqF;AACrF,iEAAqF;AACrF,sEAAsE;AACtE,2EAA8E;AAW9E;;;;;;;;;;;;;;;;;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C,CAAC;QAED,IAAA,0CAAqB,EAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QAE5C,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;;;;OAKG;IACH,mBAAmB;QAElB,IAAI,IAAI,CAAC,MAAM,EACf,CAAC;YACA,IAAI,CAAC,KAAK,EAAE,CAAC;QACd,CAAC;QAED,OAAO,IAAI,CAAC,MAAM,CAAC;IACpB,CAAC;CACD;AAjaD,8CAiaC;AA7XO;IADN,wBAAI;;;;qCAIJ;AAsCM;IADN,wBAAI;;;;+CAeJ;AAwUF,kBAAe,iBAAiB,CAAA","sourcesContent":["/**\n * npm-package-json-loader\n *\n * 用於載入、處理和寫入 package.json 的工具類別\n * Utility class for loading, processing, and writing package.json\n *\n * 支援自動修復、排序與驗證功能\n * Supports auto-fix, sorting, and verification features\n */\nimport { pathExistsSync, writeFileSync } from 'fs-extra';\nimport { sortPackageJson } from 'sort-package-json3';\nimport bind from 'bind-decorator';\nimport path from 'path';\nimport { IPackageJson } from '@ts-type/package-dts';\nimport { resolvePackageJsonLocation } from '@yarn-tool/resolve-package';\nimport { _pkgExportsAddPJsonEntryCore } from '@yarn-tool/pkg-entry-util/lib/field/exports';\nimport { readJSONSync } from '@bluelovers/fs-json';\nimport { fixPublishConfig } from '@yarn-tool/pkg-entry-util/lib/field/publishConfig';\nimport { fixBinPath, fixPkgBinField } from '@yarn-tool/pkg-entry-util/lib/field/bin';\nimport { outputPackageJSONSync } from '@yarn-tool/write-package-json';\nimport { fixEmptyFields } from '@yarn-tool/pkg-entry-util/lib/field/fixEmpty';\n\n// 檔案路徑或 JSON 物件的聯合類型 / Union type for file path or JSON object\ntype IFileOrJson = Buffer | string | object | IPackageJson\n\n// 類似 package.json 的物件類型 / Package.json-like object type\ntype IPackageJsonLike<T> = Partial<T> | Record<string, any>;\n\n// 單一項目或項目陣列的類型 / Type for single item or array of items\ntype IItemOrItemArray<T> = T | T[];\n\n/**\n * package.json 載入器類別\n * Package.json loader class\n *\n * 提供載入、修改、排序、驗證和寫入 package.json 的完整功能\n * Provides complete functionality for loading, modifying, sorting, verifying, and writing package.json\n *\n * @template T - package.json 類型參數 / package.json type parameter\n *\n * @example\n * // 從檔案路徑載入 / Load from file path\n * const pkg = new PackageJsonLoader('/path/to/package.json');\n * pkg.read().autofix().sort().write();\n *\n * @example\n * // 從模組名稱載入 / Load from module name\n * const pkg = PackageJsonLoader.loadByModuleName('lodash');\n */\nexport class PackageJsonLoader<T extends IPackageJsonLike<IPackageJson> = IPackageJson>\n{\n\t/**\n\t * package.json 檔案路徑\n\t * Path to package.json file\n\t */\n\treadonly file: string;\n\n\t/**\n\t * 解析後的 JSON 資料\n\t * Parsed JSON data\n\t */\n\tprotected json: T;\n\n\t/**\n\t * 是否已載入資料\n\t * Whether data has been loaded\n\t */\n\tloaded: boolean;\n\n\t/**\n\t * 自訂處理函式佇列\n\t * Queue of custom processing functions\n\t */\n\tprotected _use: ((json: IPackageJsonLike<T>) => void)[] = [];\n\n\t/**\n\t * 建立 PackageJsonLoader 實例的工廠方法\n\t * Factory method to create PackageJsonLoader instance\n\t *\n\t * @template T - package.json 類型參數 / package.json type parameter\n\t * @param {IFileOrJson} file - 檔案路徑或 JSON 物件 / File path or JSON object\n\t * @param {any[]} argv - 額外參數 / Additional arguments\n\t * @returns {PackageJsonLoader<T>} PackageJsonLoader 實例 / PackageJsonLoader instance\n\t */\n\t@bind\n\tstatic create<T = IPackageJson>(file: IFileOrJson, ...argv)\n\t{\n\t\treturn new this<T>(file, ...argv)\n\t}\n\n\t/**\n\t * 從 JSON 物件建立實例\n\t * Create instance from JSON object\n\t *\n\t * @template T - package.json 類型參數 / package.json type parameter\n\t * @param {T} json - JSON 物件 / JSON object\n\t * @param {any[]} argv - 額外參數 / Additional arguments\n\t * @returns {PackageJsonLoader<T>} PackageJsonLoader 實例 / PackageJsonLoader instance\n\t */\n\tstatic createByJson<T = IPackageJson>(json: T, ...argv)\n\t{\n\t\treturn new this<T>(json as any, ...argv)\n\t}\n\n\t/**\n\t * 透過模組名稱尋找 package.json 路徑\n\t * Find package.json path by module name\n\t *\n\t * @param {string} name - 模組名稱 / Module name\n\t * @returns {string} package.json 的絕對路徑 / Absolute path to package.json\n\t */\n\tstatic findPackageJsonPath(name: string): string\n\t{\n\t\treturn resolvePackageJsonLocation(name);\n\t}\n\n\t/**\n\t * 透過模組名稱載入 package.json\n\t * Load package.json by module name\n\t *\n\t * @template T - package.json 類型參數 / package.json type parameter\n\t * @param {string} name - 模組名稱 / Module name\n\t * @returns {PackageJsonLoader<T>} PackageJsonLoader 實例 / PackageJsonLoader instance\n\t * @throws {TypeError} 當套件名稱不符時拋出錯誤 / Throws when package name doesn't match\n\t */\n\t@bind\n\tstatic loadByModuleName<T = IPackageJson>(name: string)\n\t{\n\t\tlet file = this.findPackageJsonPath(name);\n\n\t\tlet pkg = this.create<T>(file);\n\n\t\t// 驗證載入的套件名稱是否與請求的名稱相符\n\t\t// Verify loaded package name matches requested name\n\t\tif ((pkg.data as any).name !== name)\n\t\t{\n\t\t\tthrow new TypeError(`package name not match, '${(pkg.data as any).name}' != '${name}'`);\n\t\t}\n\n\t\treturn pkg;\n\t}\n\n\t/**\n\t * 建構子\n\t * Constructor\n\t *\n\t * @param {IFileOrJson} fileOrJson - 檔案路徑、Buffer 或 JSON 物件 / File path, Buffer, or JSON object\n\t * @param {any[]} argv - 額外參數 / Additional arguments\n\t */\n\tconstructor(fileOrJson: IFileOrJson, ...argv)\n\t{\n\t\t// 根據輸入類型初始化實例 / Initialize instance based on input type\n\t\tif (typeof fileOrJson === 'string')\n\t\t{\n\t\t\tthis.setFilename(fileOrJson)\n\t\t}\n\t\telse if (Buffer.isBuffer(fileOrJson))\n\t\t{\n\t\t\tthis.setJson(JSON.parse(fileOrJson.toString()))\n\t\t}\n\t\telse if (typeof fileOrJson === 'object')\n\t\t{\n\t\t\tthis.setJson(JSON.parse(fileOrJson.toString()))\n\t\t}\n\t\telse if (fileOrJson != null)\n\t\t{\n\t\t\tthrow new TypeError(`fileOrJson is not valid`)\n\t\t}\n\t}\n\n\t/**\n\t * 註冊自訂處理函式\n\t * Register custom processing function\n\t *\n\t * @param {IItemOrItemArray<(json: IPackageJsonLike<T>) => void>} ls - 處理函式或函式陣列 / Processing function or array of functions\n\t */\n\tuse(ls: IItemOrItemArray<(json: IPackageJsonLike<T>) => void>)\n\t{\n\t\tif (Array.isArray(ls))\n\t\t{\n\t\t\tthis._use.push(...ls);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tthis._use.push(ls);\n\t\t}\n\t}\n\n\t/**\n\t * 設定檔案名稱\n\t * Set filename\n\t *\n\t * @param {string} file - 檔案路徑 / File path\n\t * @returns {this} 当前實例 / Current instance\n\t */\n\tsetFilename(file: string)\n\t{\n\t\t// @ts-ignore\n\t\tthis.file = file;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * 設定 JSON 資料\n\t * Set JSON data\n\t *\n\t * @param {object | T} json - JSON 物件 / JSON object\n\t * @returns {this} 当前實例 / Current instance\n\t */\n\tsetJson(json: object | T)\n\t{\n\t\tthis.loaded = true;\n\t\tthis.json = json as T;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * 讀取 package.json 檔案\n\t * Read package.json file\n\t *\n\t * @param {boolean} reload - 是否強制重新載入 / Whether to force reload\n\t * @returns {this} 当前實例 / Current instance\n\t */\n\tread(reload?: boolean)\n\t{\n\t\tif (!this.loaded || reload)\n\t\t{\n\t\t\tthis.json = readJSONSync(this.file);\n\t\t}\n\n\t\tthis.loaded = true;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * 重新載入 package.json\n\t * Reload package.json\n\t *\n\t * @returns {this} 当前實例 / Current instance\n\t */\n\treload()\n\t{\n\t\treturn this.read(true);\n\t}\n\n\t/**\n\t * 取得 package.json 所在目錄\n\t * Get directory of package.json\n\t *\n\t * @returns {string} 目錄路徑 / Directory path\n\t */\n\tget dir()\n\t{\n\t\treturn path.dirname(this.file)\n\t}\n\n\t/**\n\t * 取得跳過 TypeScript 類型檢查的資料\n\t * Get data without TypeScript type checking\n\t *\n\t * 用於需要靈活操作 JSON 資料的場景\n\t * Use when flexible JSON data manipulation is needed\n\t */\n\tget unsafeTypeData(): IPackageJsonLike<T>\n\t{\n\t\treturn this.data as any;\n\t}\n\n\t/**\n\t * 設定跳過 TypeScript 類型檢查的資料\n\t * Set data without TypeScript type checking\n\t */\n\tset unsafeTypeData(json)\n\t{\n\t\tthis.data = json as any;\n\t}\n\n\t/**\n\t * 設定 package.json 資料\n\t * Set package.json data\n\t */\n\tset data(json: T)\n\t{\n\t\tthis.overwrite(json);\n\t}\n\n\t/**\n\t * 取得 package.json 資料\n\t * Get package.json data\n\t *\n\t * 若尚未載入會自動讀取檔案\n\t * Automatically reads file if not loaded yet\n\t *\n\t * @returns {T} package.json 資料 / package.json data\n\t */\n\tget data(): T\n\t{\n\t\tif (!this.loaded && this.file)\n\t\t{\n\t\t\tthis.read();\n\t\t}\n\n\t\treturn this.json;\n\t}\n\n\t/**\n\t * 覆寫 JSON 資料\n\t * Overwrite JSON data\n\t *\n\t * @param {object | T} json - 新的 JSON 資料 / New JSON data\n\t * @returns {this} 当前實例 / Current instance\n\t */\n\toverwrite(json: object | T)\n\t{\n\t\tthis.loaded = true;\n\t\tthis.json = json as T;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * 自動修復 package.json 欄位\n\t * Auto-fix package.json fields\n\t *\n\t * 執行以下修復操作:\n\t * 1. 修復 bin 欄位路徑\n\t * 2. 修復 publishConfig 欄位\n\t * 3. 添加 exports 中的 package.json entry\n\t * 4. 清理空欄位\n\t *\n\t * Performs the following fix operations:\n\t * 1. Fix bin field paths\n\t * 2. Fix publishConfig field\n\t * 3. Add package.json entry in exports\n\t * 4. Clean empty fields\n\t */\n\tautofix()\n\t{\n\t\tlet self = this;\n\t\tlet dir: string;\n\n\t\t// 檢查檔案和目錄是否存在\n\t\t// Check if file and directory exist\n\t\tif (self.file && pathExistsSync(dir = self.dir))\n\t\t{\n\t\t\tif (self.data)\n\t\t\t{\n\t\t\t\t// 修復 bin 欄位路徑 / Fix bin field paths\n\t\t\t\tfixPkgBinField(self.data, dir);\n\t\t\t\t// 修復 publishConfig 欄位 / Fix publishConfig field\n\t\t\t\tfixPublishConfig(self.data);\n\t\t\t}\n\t\t}\n\n\t\t// 在 exports 中添加 package.json entry / Add package.json entry in exports\n\t\t_pkgExportsAddPJsonEntryCore(self.data?.exports);\n\n\t\t// 清理空欄位 / Clean empty fields\n\t\tfixEmptyFields(self.data, dir);\n\t}\n\n\t/**\n\t * 執行處理流程\n\t * Run processing pipeline\n\t *\n\t * @param {object} options - 選項 / Options\n\t * @param {boolean} options.autofix - 是否執行自動修復 / Whether to run auto-fix\n\t * @returns {this} 当前實例 / Current instance\n\t */\n\trun(options: {\n\t\tautofix?: boolean\n\t} = {})\n\t{\n\t\t// 預設執行自動修復 / Default to run auto-fix\n\t\tif (options.autofix == null || options.autofix)\n\t\t{\n\t\t\tthis.autofix();\n\t\t}\n\n\t\t// 執行所有註冊的處理函式 / Execute all registered processing functions\n\t\tthis._use.forEach(fn => fn.call(this, this.data));\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * 檢查檔案是否存在\n\t * Check if file exists\n\t *\n\t * @returns {boolean} 檔案是否存在 / Whether file exists\n\t */\n\texists()\n\t{\n\t\treturn pathExistsSync(this.file)\n\t}\n\n\t/**\n\t * 將 JSON 資料轉換為字串\n\t * Convert JSON data to string\n\t *\n\t * @returns {string} 格式化的 JSON 字串 / Formatted JSON string\n\t */\n\tstringify()\n\t{\n\t\treturn JSON.stringify(this.json, null, 2)\n\t}\n\n\t/**\n\t * 排序 package.json 欄位\n\t * Sort package.json fields\n\t *\n\t * 使用 sort-package-json3 套件進行標準化排序\n\t * Use sort-package-json3 package for standardized sorting\n\t *\n\t * @returns {this} 当前實例 / Current instance\n\t * @throws {Error} 當資料為 undefined 或 null 時拋出錯誤 / Throws when data is undefined or null\n\t */\n\tsort()\n\t{\n\t\tif (typeof this.data === 'undefined' || this.data === null)\n\t\t{\n\t\t\tthrow new Error(`data is undefined`)\n\t\t}\n\n\t\tthis.data = sortPackageJson(this.data);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * 寫入 package.json 檔案\n\t * Write package.json file\n\t *\n\t * @returns {this} 当前實例 / Current instance\n\t * @throws {Error} 當檔案路徑未設定時拋出錯誤 / Throws when file path is not set\n\t */\n\twrite()\n\t{\n\t\tif (!this.file)\n\t\t{\n\t\t\tthrow new Error(`file is undefined`)\n\t\t}\n\n\t\toutputPackageJSONSync(this.file, this.json);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * 僅在已載入時寫入\n\t * Write only when loaded\n\t *\n\t * @returns {boolean} 是否已寫入 / Whether written\n\t */\n\twriteOnlyWhenLoaded()\n\t{\n\t\tif (this.loaded)\n\t\t{\n\t\t\tthis.write();\n\t\t}\n\n\t\treturn this.loaded;\n\t}\n}\n\nexport default PackageJsonLoader\n"]}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "npm-package-json-loader",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"private": false,
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "用於載入、處理和寫入 package.json 的工具類別,支援自動修復、排序與驗證功能",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"conf",
|
|
8
8
|
"config",
|
|
@@ -30,7 +30,6 @@
|
|
|
30
30
|
"author": "",
|
|
31
31
|
"contributors": [],
|
|
32
32
|
"main": "index.js",
|
|
33
|
-
"bin": {},
|
|
34
33
|
"directories": {
|
|
35
34
|
"test": "test"
|
|
36
35
|
},
|
|
@@ -51,21 +50,20 @@
|
|
|
51
50
|
"tsc:showConfig": "ynpx get-current-tsconfig -p"
|
|
52
51
|
},
|
|
53
52
|
"config": {},
|
|
54
|
-
"resolutions": {},
|
|
55
53
|
"dependencies": {
|
|
56
|
-
"@bluelovers/fs-json": "^1.0.
|
|
54
|
+
"@bluelovers/fs-json": "^1.0.17",
|
|
57
55
|
"@ts-type/package-dts": "^2.0.8",
|
|
58
|
-
"@yarn-tool/pkg-entry-util": "^3.0.
|
|
59
|
-
"@yarn-tool/resolve-package": "^
|
|
60
|
-
"@yarn-tool/write-package-json": "^1.0.
|
|
56
|
+
"@yarn-tool/pkg-entry-util": "^3.0.4",
|
|
57
|
+
"@yarn-tool/resolve-package": "^3.0.1",
|
|
58
|
+
"@yarn-tool/write-package-json": "^1.0.14",
|
|
61
59
|
"bind-decorator": "^1.0.11",
|
|
62
|
-
"fs-extra": "^11.
|
|
63
|
-
"sort-package-json3": "^3.0.
|
|
60
|
+
"fs-extra": "^11.3.3",
|
|
61
|
+
"sort-package-json3": "^3.0.4",
|
|
64
62
|
"ts-type": "^3.0.1",
|
|
65
63
|
"tslib": ">=2"
|
|
66
64
|
},
|
|
67
65
|
"bundleDependencies": [],
|
|
68
66
|
"preferGlobal": true,
|
|
69
67
|
"publishConfig": {},
|
|
70
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "ae9b0817b25b1019ba68c88e541bbffc7d84047c"
|
|
71
69
|
}
|