fs-target-file 1.0.22 → 1.0.25
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 +33 -0
- package/README.md +12 -2
- package/index.d.ts +88 -2
- package/index.js +71 -0
- package/index.js.map +1 -1
- package/package.json +6 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,39 @@
|
|
|
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
|
+
## [1.0.25](https://github.com/bluelovers/ws-iconv/compare/fs-target-file@1.0.24...fs-target-file@1.0.25) (2026-03-02)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### 🛠 Build System
|
|
11
|
+
|
|
12
|
+
* update build ([842c8ed](https://github.com/bluelovers/ws-iconv/commit/842c8edb48b11e163ad688df4f5791ab2e26993f))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## [1.0.24](https://github.com/bluelovers/ws-iconv/compare/fs-target-file@1.0.23...fs-target-file@1.0.24) (2026-03-01)
|
|
17
|
+
|
|
18
|
+
**Note:** Version bump only for package fs-target-file
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## [1.0.23](https://github.com/bluelovers/ws-iconv/compare/fs-target-file@1.0.22...fs-target-file@1.0.23) (2026-03-01)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### 📚 Documentation
|
|
29
|
+
|
|
30
|
+
* 为多个包添加双语 README 和 JSDoc 注释 ([71544eb](https://github.com/bluelovers/ws-iconv/commit/71544eb1347aa061cb4ac71afc8d30e79f1ad3ad))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### ♻️ Chores
|
|
34
|
+
|
|
35
|
+
* Update jest config and add new files for testing ([f370583](https://github.com/bluelovers/ws-iconv/commit/f37058385c7bf24d3d0bc975c752f2f35cfe8356))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
6
39
|
## [1.0.22](https://github.com/bluelovers/ws-iconv/compare/fs-target-file@1.0.21...fs-target-file@1.0.22) (2024-08-29)
|
|
7
40
|
|
|
8
41
|
|
package/README.md
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# fs-target-file - 檔案目標處理工具
|
|
2
2
|
|
|
3
|
+
這個模組提供了檔案目標處理的功能,支援讀取和寫入操作,並可以指定輸入和輸出檔案。
|
|
3
4
|
|
|
5
|
+
## 主要功能
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
- 檔案目標處理
|
|
8
|
+
- 讀取和寫入操作
|
|
9
|
+
- 支援 JSON 檔案
|
|
10
|
+
- 輸入和輸出檔案指定
|
|
11
|
+
- 異步處理
|
|
12
|
+
|
|
13
|
+
## 安裝
|
|
6
14
|
|
|
7
15
|
```bash
|
|
8
16
|
yarn add fs-target-file
|
|
@@ -10,3 +18,5 @@ yarn-tool add fs-target-file
|
|
|
10
18
|
yt add fs-target-file
|
|
11
19
|
```
|
|
12
20
|
|
|
21
|
+
## 快速開始
|
|
22
|
+
|
package/index.d.ts
CHANGED
|
@@ -1,26 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fs-target-file - 檔案目標處理工具
|
|
3
|
+
*
|
|
4
|
+
* 這個模組提供了檔案目標處理的功能,支援讀取和寫入操作,並可以指定輸入和輸出檔案。
|
|
5
|
+
*
|
|
6
|
+
* @module fs-target-file
|
|
7
|
+
* @author bluelovers
|
|
8
|
+
*/
|
|
1
9
|
import { ReadOptions, WriteOptions } from 'fs-extra';
|
|
2
10
|
import { ITSResolvable } from 'ts-type/lib/generic';
|
|
3
11
|
import { ITSRequiredPick } from 'ts-type/lib/type/record';
|
|
12
|
+
/**
|
|
13
|
+
* 檔案方法異步選項
|
|
14
|
+
*
|
|
15
|
+
* @interface IFileMethodAsyncOptions
|
|
16
|
+
* @template RO 讀取選項類型
|
|
17
|
+
* @template WO 寫入選項類型
|
|
18
|
+
* @template D 資料類型
|
|
19
|
+
* @template N 檔案路徑類型
|
|
20
|
+
*/
|
|
4
21
|
export interface IFileMethodAsyncOptions<RO = ReadOptions, WO = WriteOptions, D = unknown, N = string> {
|
|
22
|
+
/** 讀取函數 */
|
|
5
23
|
read?<T = D>(file: N, options?: RO): Promise<T>;
|
|
24
|
+
/** 寫入函數 */
|
|
6
25
|
write?<T = D>(file: N, data: ITSResolvable<T>, options?: RO): Promise<any>;
|
|
26
|
+
/** 讀取選項 */
|
|
7
27
|
readOptions?: RO;
|
|
28
|
+
/** 寫入選項 */
|
|
8
29
|
writeOptions?: WO;
|
|
9
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* 檔案方法異步處理
|
|
33
|
+
*
|
|
34
|
+
* 這個函數會處理檔案的異步讀取和寫入操作,支援自定義函數和選項。
|
|
35
|
+
*
|
|
36
|
+
* @template RO 讀取選項類型
|
|
37
|
+
* @template WO 寫入選項類型
|
|
38
|
+
* @template D 資料類型
|
|
39
|
+
* @template N 檔案路徑類型
|
|
40
|
+
* @param {IFileMethodAsyncOptions<RO, WO, D, N>} [options] 選項
|
|
41
|
+
* @returns {object} 包含 read 和 write 方法的物件
|
|
42
|
+
*/
|
|
10
43
|
export declare function fsFileMethodAsync<RO = ReadOptions, WO = WriteOptions, D = unknown, N = string>(options?: IFileMethodAsyncOptions<RO, WO, D, N>): {
|
|
11
|
-
read: (
|
|
44
|
+
read: ((file: any, options: any) => void) | (<T = D>(file: N, options?: RO) => Promise<T>);
|
|
12
45
|
write: <T = D>(file: N, data: ITSResolvable<T>, options?: RO) => Promise<any>;
|
|
13
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* 檔案目標選項
|
|
49
|
+
*
|
|
50
|
+
* @interface ITargetFileOptions
|
|
51
|
+
* @template N 檔案路徑類型
|
|
52
|
+
*/
|
|
14
53
|
export interface ITargetFileOptions<N = string> {
|
|
54
|
+
/** 輸入檔案路徑 */
|
|
15
55
|
inputFile: N;
|
|
56
|
+
/** 輸出檔案路徑 */
|
|
16
57
|
outputFile?: N;
|
|
17
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* 轉換檔案目標選項
|
|
61
|
+
*
|
|
62
|
+
* 這個函數會將目標選項轉換為標準格式,支援直接傳入檔案路徑或選項物件。
|
|
63
|
+
*
|
|
64
|
+
* @template N 檔案路徑類型
|
|
65
|
+
* @param {N | ITargetFileOptions<N>} targetOptions 目標選項
|
|
66
|
+
* @returns {ITSRequiredPick<ITargetFileOptions<N>>} 標準化後的選項
|
|
67
|
+
*/
|
|
18
68
|
export declare function toTargetFileOptions<N = string>(targetOptions: N | ITargetFileOptions<N>): ITSRequiredPick<ITargetFileOptions<N>>;
|
|
69
|
+
/**
|
|
70
|
+
* 檔案目標處理
|
|
71
|
+
*
|
|
72
|
+
* 這個函數會創建一個檔案目標處理器,支援讀取和寫入操作,並可以指定輸入和輸出檔案。
|
|
73
|
+
*
|
|
74
|
+
* @template D 資料類型
|
|
75
|
+
* @template RO 讀取選項類型
|
|
76
|
+
* @template WO 寫入選項類型
|
|
77
|
+
* @template N 檔案路徑類型
|
|
78
|
+
* @param {N | ITargetFileOptions<N>} targetOptions 目標選項
|
|
79
|
+
* @param {IFileMethodAsyncOptions<RO, WO, D, N>} [options] 選項
|
|
80
|
+
* @returns {object} 檔案目標處理器
|
|
81
|
+
*/
|
|
19
82
|
export declare function fsTargetFile<D = unknown, RO = ReadOptions, WO = WriteOptions, N = string>(targetOptions: N | ITargetFileOptions<N>, options?: IFileMethodAsyncOptions<RO, WO, D, N>): {
|
|
83
|
+
/** 輸入檔案路徑 */
|
|
20
84
|
readonly inputFile: N;
|
|
85
|
+
/** 輸出檔案路徑 */
|
|
21
86
|
readonly outputFile: N;
|
|
87
|
+
/**
|
|
88
|
+
* 讀取檔案
|
|
89
|
+
*
|
|
90
|
+
* @template T 資料類型
|
|
91
|
+
* @param {RO} [options] 選項
|
|
92
|
+
* @returns {Promise<T>} Promise,解析為資料
|
|
93
|
+
*/
|
|
22
94
|
read<T = D>(options?: RO): Promise<T>;
|
|
95
|
+
/**
|
|
96
|
+
* 寫入檔案
|
|
97
|
+
*
|
|
98
|
+
* @template T 資料類型
|
|
99
|
+
* @param {T} data 資料
|
|
100
|
+
* @param {RO} [options] 選項
|
|
101
|
+
* @returns {Promise<unknown>} Promise
|
|
102
|
+
*/
|
|
23
103
|
write<T extends D = D>(data: T, options?: RO): Promise<unknown>;
|
|
24
|
-
|
|
104
|
+
/**
|
|
105
|
+
* 異步處理
|
|
106
|
+
*
|
|
107
|
+
* @returns {Promise<object>} Promise,解析為檔案目標處理器
|
|
108
|
+
*/
|
|
109
|
+
async(): Promise</*elided*/ any>;
|
|
25
110
|
};
|
|
111
|
+
/** 預設導出 - 檔案目標處理函數 */
|
|
26
112
|
export default fsTargetFile;
|
package/index.js
CHANGED
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* fs-target-file - 檔案目標處理工具
|
|
4
|
+
*
|
|
5
|
+
* 這個模組提供了檔案目標處理的功能,支援讀取和寫入操作,並可以指定輸入和輸出檔案。
|
|
6
|
+
*
|
|
7
|
+
* @module fs-target-file
|
|
8
|
+
* @author bluelovers
|
|
9
|
+
*/
|
|
2
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
11
|
exports.fsFileMethodAsync = fsFileMethodAsync;
|
|
4
12
|
exports.toTargetFileOptions = toTargetFileOptions;
|
|
5
13
|
exports.fsTargetFile = fsTargetFile;
|
|
6
14
|
const fs_extra_1 = require("fs-extra");
|
|
15
|
+
/**
|
|
16
|
+
* 檔案方法異步處理
|
|
17
|
+
*
|
|
18
|
+
* 這個函數會處理檔案的異步讀取和寫入操作,支援自定義函數和選項。
|
|
19
|
+
*
|
|
20
|
+
* @template RO 讀取選項類型
|
|
21
|
+
* @template WO 寫入選項類型
|
|
22
|
+
* @template D 資料類型
|
|
23
|
+
* @template N 檔案路徑類型
|
|
24
|
+
* @param {IFileMethodAsyncOptions<RO, WO, D, N>} [options] 選項
|
|
25
|
+
* @returns {object} 包含 read 和 write 方法的物件
|
|
26
|
+
*/
|
|
7
27
|
function fsFileMethodAsync(options) {
|
|
28
|
+
// 設置預設選項
|
|
8
29
|
options !== null && options !== void 0 ? options : (options = {});
|
|
9
30
|
const { readOptions, writeOptions = {
|
|
10
31
|
spaces: 2,
|
|
11
32
|
}, } = options;
|
|
33
|
+
// 設置預設函數
|
|
12
34
|
const { read = (file, options) => (0, fs_extra_1.readJSON)(file, {
|
|
13
35
|
...readOptions,
|
|
14
36
|
...options,
|
|
@@ -21,10 +43,20 @@ function fsFileMethodAsync(options) {
|
|
|
21
43
|
write,
|
|
22
44
|
};
|
|
23
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* 轉換檔案目標選項
|
|
48
|
+
*
|
|
49
|
+
* 這個函數會將目標選項轉換為標準格式,支援直接傳入檔案路徑或選項物件。
|
|
50
|
+
*
|
|
51
|
+
* @template N 檔案路徑類型
|
|
52
|
+
* @param {N | ITargetFileOptions<N>} targetOptions 目標選項
|
|
53
|
+
* @returns {ITSRequiredPick<ITargetFileOptions<N>>} 標準化後的選項
|
|
54
|
+
*/
|
|
24
55
|
function toTargetFileOptions(targetOptions) {
|
|
25
56
|
var _a;
|
|
26
57
|
let inputFile;
|
|
27
58
|
let outputFile;
|
|
59
|
+
// 檢查是否為選項物件
|
|
28
60
|
// @ts-ignore
|
|
29
61
|
if (typeof (targetOptions === null || targetOptions === void 0 ? void 0 : targetOptions.inputFile) !== 'undefined') {
|
|
30
62
|
// @ts-ignore
|
|
@@ -40,27 +72,66 @@ function toTargetFileOptions(targetOptions) {
|
|
|
40
72
|
outputFile,
|
|
41
73
|
};
|
|
42
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* 檔案目標處理
|
|
77
|
+
*
|
|
78
|
+
* 這個函數會創建一個檔案目標處理器,支援讀取和寫入操作,並可以指定輸入和輸出檔案。
|
|
79
|
+
*
|
|
80
|
+
* @template D 資料類型
|
|
81
|
+
* @template RO 讀取選項類型
|
|
82
|
+
* @template WO 寫入選項類型
|
|
83
|
+
* @template N 檔案路徑類型
|
|
84
|
+
* @param {N | ITargetFileOptions<N>} targetOptions 目標選項
|
|
85
|
+
* @param {IFileMethodAsyncOptions<RO, WO, D, N>} [options] 選項
|
|
86
|
+
* @returns {object} 檔案目標處理器
|
|
87
|
+
*/
|
|
43
88
|
function fsTargetFile(targetOptions, options) {
|
|
89
|
+
// 獲取讀取和寫入方法
|
|
44
90
|
const { read, write } = fsFileMethodAsync(options);
|
|
91
|
+
// 獲取標準化後的選項
|
|
45
92
|
const { inputFile, outputFile } = toTargetFileOptions(targetOptions);
|
|
93
|
+
// 創建檔案目標處理器
|
|
46
94
|
const target = {
|
|
95
|
+
/** 輸入檔案路徑 */
|
|
47
96
|
get inputFile() {
|
|
48
97
|
return inputFile;
|
|
49
98
|
},
|
|
99
|
+
/** 輸出檔案路徑 */
|
|
50
100
|
get outputFile() {
|
|
51
101
|
return outputFile;
|
|
52
102
|
},
|
|
103
|
+
/**
|
|
104
|
+
* 讀取檔案
|
|
105
|
+
*
|
|
106
|
+
* @template T 資料類型
|
|
107
|
+
* @param {RO} [options] 選項
|
|
108
|
+
* @returns {Promise<T>} Promise,解析為資料
|
|
109
|
+
*/
|
|
53
110
|
read(options) {
|
|
54
111
|
return read(inputFile, options);
|
|
55
112
|
},
|
|
113
|
+
/**
|
|
114
|
+
* 寫入檔案
|
|
115
|
+
*
|
|
116
|
+
* @template T 資料類型
|
|
117
|
+
* @param {T} data 資料
|
|
118
|
+
* @param {RO} [options] 選項
|
|
119
|
+
* @returns {Promise<unknown>} Promise
|
|
120
|
+
*/
|
|
56
121
|
write(data, options) {
|
|
57
122
|
return write(outputFile, data, options);
|
|
58
123
|
},
|
|
124
|
+
/**
|
|
125
|
+
* 異步處理
|
|
126
|
+
*
|
|
127
|
+
* @returns {Promise<object>} Promise,解析為檔案目標處理器
|
|
128
|
+
*/
|
|
59
129
|
async async() {
|
|
60
130
|
return target;
|
|
61
131
|
}
|
|
62
132
|
};
|
|
63
133
|
return target;
|
|
64
134
|
}
|
|
135
|
+
/** 預設導出 - 檔案目標處理函數 */
|
|
65
136
|
exports.default = fsTargetFile;
|
|
66
137
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AA0CH,8CA4BC;AA0BD,kDAuBC;AAeD,oCA8DC;AAlMD,uCAA2E;AA4B3E;;;;;;;;;;;GAWG;AACH,SAAgB,iBAAiB,CAA+D,OAA+C;IAE9I,SAAS;IACT,OAAO,aAAP,OAAO,cAAP,OAAO,IAAP,OAAO,GAAK,EAAE,EAAC;IAEf,MAAM,EACL,WAAW,EACX,YAAY,GAAG;QACd,MAAM,EAAE,CAAC;KACT,GACD,GAAG,OAAO,CAAC;IAEZ,SAAS;IACT,MAAM,EACL,IAAI,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,IAAA,mBAAQ,EAAC,IAAI,EAAE;QACxC,GAAG,WAAW;QACd,GAAG,OAAO;KACV,CAAC,EACF,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,IAAA,qBAAU,EAAC,IAAI,EAAE,IAAI,EAAE;QACvD,GAAG,YAAY;QACf,GAAG,OAAO;KACV,CAAC,GACF,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC;IAElB,OAAO;QACN,IAAI;QACJ,KAAK;KACL,CAAA;AACF,CAAC;AAiBD;;;;;;;;GAQG;AACH,SAAgB,mBAAmB,CAAa,aAAwC;;IAEvF,IAAI,SAAY,CAAC;IACjB,IAAI,UAAa,CAAC;IAElB,YAAY;IACZ,aAAa;IACb,IAAI,OAAO,CAAA,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,SAAS,CAAA,KAAK,WAAW,EACnD,CAAC;QACA,aAAa;QACb,SAAS,GAAG,aAAa,CAAC,SAAS,CAAC;QACpC,aAAa;QACb,UAAU,GAAG,MAAA,aAAa,CAAC,UAAU,mCAAI,SAAS,CAAC;IACpD,CAAC;SAED,CAAC;QACA,SAAS,GAAG,UAAU,GAAG,aAAkB,CAAC;IAC7C,CAAC;IAED,OAAO;QACN,SAAS;QACT,UAAU;KACV,CAAA;AACF,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,YAAY,CAA+D,aAAwC,EAAE,OAA+C;IAEnL,YAAY;IACZ,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,iBAAiB,CAAe,OAAO,CAAC,CAAC;IAEjE,YAAY;IACZ,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,mBAAmB,CAAC,aAAa,CAAC,CAAC;IAErE,YAAY;IACZ,MAAM,MAAM,GAAG;QAEd,aAAa;QACb,IAAI,SAAS;YAEZ,OAAO,SAAS,CAAA;QACjB,CAAC;QAED,aAAa;QACb,IAAI,UAAU;YAEb,OAAO,UAAU,CAAA;QAClB,CAAC;QAED;;;;;;WAMG;QACH,IAAI,CAAQ,OAAY;YAEvB,OAAO,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA;QAChC,CAAC;QAED;;;;;;;WAOG;QACH,KAAK,CAAkB,IAAO,EAAE,OAAY;YAE3C,OAAO,KAAK,CAAC,UAAU,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;QACxC,CAAC;QAED;;;;WAIG;QACH,KAAK,CAAC,KAAK;YAEV,OAAO,MAAM,CAAA;QACd,CAAC;KAED,CAAC;IAEF,OAAO,MAAM,CAAA;AAEd,CAAC;AAED,sBAAsB;AACtB,kBAAe,YAAY,CAAA","sourcesContent":["/**\n * fs-target-file - 檔案目標處理工具\n * \n * 這個模組提供了檔案目標處理的功能,支援讀取和寫入操作,並可以指定輸入和輸出檔案。\n * \n * @module fs-target-file\n * @author bluelovers\n */\n\nimport { outputJSON, readJSON, ReadOptions, WriteOptions } from 'fs-extra';\nimport { ITSResolvable } from 'ts-type/lib/generic';\nimport { ITSRequiredPick } from 'ts-type/lib/type/record';\n\n/**\n * 檔案方法異步選項\n * \n * @interface IFileMethodAsyncOptions\n * @template RO 讀取選項類型\n * @template WO 寫入選項類型\n * @template D 資料類型\n * @template N 檔案路徑類型\n */\nexport interface IFileMethodAsyncOptions<RO = ReadOptions, WO = WriteOptions, D = unknown, N = string>\n{\n\t/** 讀取函數 */\n\tread?<T = D>(file: N, options?: RO): Promise<T>,\n\n\t/** 寫入函數 */\n\twrite?<T = D>(file: N, data: ITSResolvable<T>, options?: RO): Promise<any>,\n\n\t/** 讀取選項 */\n\treadOptions?: RO,\n\n\t/** 寫入選項 */\n\twriteOptions?: WO,\n}\n\n/**\n * 檔案方法異步處理\n * \n * 這個函數會處理檔案的異步讀取和寫入操作,支援自定義函數和選項。\n * \n * @template RO 讀取選項類型\n * @template WO 寫入選項類型\n * @template D 資料類型\n * @template N 檔案路徑類型\n * @param {IFileMethodAsyncOptions<RO, WO, D, N>} [options] 選項\n * @returns {object} 包含 read 和 write 方法的物件\n */\nexport function fsFileMethodAsync<RO = ReadOptions, WO = WriteOptions, D = unknown, N = string>(options?: IFileMethodAsyncOptions<RO, WO, D, N>)\n{\n\t// 設置預設選項\n\toptions ??= {};\n\n\tconst {\n\t\treadOptions,\n\t\twriteOptions = {\n\t\t\tspaces: 2,\n\t\t},\n\t} = options;\n\n\t// 設置預設函數\n\tconst {\n\t\tread = (file, options) => readJSON(file, {\n\t\t\t...readOptions,\n\t\t\t...options,\n\t\t}),\n\t\twrite = (file, data, options) => outputJSON(file, data, {\n\t\t\t...writeOptions,\n\t\t\t...options,\n\t\t}),\n\t} = options ?? {};\n\n\treturn {\n\t\tread,\n\t\twrite,\n\t}\n}\n\n/**\n * 檔案目標選項\n * \n * @interface ITargetFileOptions\n * @template N 檔案路徑類型\n */\nexport interface ITargetFileOptions<N = string>\n{\n\t/** 輸入檔案路徑 */\n\tinputFile: N,\n\n\t/** 輸出檔案路徑 */\n\toutputFile?: N,\n}\n\n/**\n * 轉換檔案目標選項\n * \n * 這個函數會將目標選項轉換為標準格式,支援直接傳入檔案路徑或選項物件。\n * \n * @template N 檔案路徑類型\n * @param {N | ITargetFileOptions<N>} targetOptions 目標選項\n * @returns {ITSRequiredPick<ITargetFileOptions<N>>} 標準化後的選項\n */\nexport function toTargetFileOptions<N = string>(targetOptions: N | ITargetFileOptions<N>): ITSRequiredPick<ITargetFileOptions<N>>\n{\n\tlet inputFile: N;\n\tlet outputFile: N;\n\n\t// 檢查是否為選項物件\n\t// @ts-ignore\n\tif (typeof targetOptions?.inputFile !== 'undefined')\n\t{\n\t\t// @ts-ignore\n\t\tinputFile = targetOptions.inputFile;\n\t\t// @ts-ignore\n\t\toutputFile = targetOptions.outputFile ?? inputFile;\n\t}\n\telse\n\t{\n\t\tinputFile = outputFile = targetOptions as N;\n\t}\n\n\treturn {\n\t\tinputFile,\n\t\toutputFile,\n\t}\n}\n\n/**\n * 檔案目標處理\n * \n * 這個函數會創建一個檔案目標處理器,支援讀取和寫入操作,並可以指定輸入和輸出檔案。\n * \n * @template D 資料類型\n * @template RO 讀取選項類型\n * @template WO 寫入選項類型\n * @template N 檔案路徑類型\n * @param {N | ITargetFileOptions<N>} targetOptions 目標選項\n * @param {IFileMethodAsyncOptions<RO, WO, D, N>} [options] 選項\n * @returns {object} 檔案目標處理器\n */\nexport function fsTargetFile<D = unknown, RO = ReadOptions, WO = WriteOptions, N = string>(targetOptions: N | ITargetFileOptions<N>, options?: IFileMethodAsyncOptions<RO, WO, D, N>)\n{\n\t// 獲取讀取和寫入方法\n\tconst { read, write } = fsFileMethodAsync<RO, WO, D, N>(options);\n\n\t// 獲取標準化後的選項\n\tconst { inputFile, outputFile } = toTargetFileOptions(targetOptions);\n\n\t// 創建檔案目標處理器\n\tconst target = {\n\n\t\t/** 輸入檔案路徑 */\n\t\tget inputFile()\n\t\t{\n\t\t\treturn inputFile\n\t\t},\n\n\t\t/** 輸出檔案路徑 */\n\t\tget outputFile()\n\t\t{\n\t\t\treturn outputFile\n\t\t},\n\n\t\t/**\n\t\t * 讀取檔案\n\t\t * \n\t\t * @template T 資料類型\n\t\t * @param {RO} [options] 選項\n\t\t * @returns {Promise<T>} Promise,解析為資料\n\t\t */\n\t\tread<T = D>(options?: RO): Promise<T>\n\t\t{\n\t\t\treturn read(inputFile, options)\n\t\t},\n\n\t\t/**\n\t\t * 寫入檔案\n\t\t * \n\t\t * @template T 資料類型\n\t\t * @param {T} data 資料\n\t\t * @param {RO} [options] 選項\n\t\t * @returns {Promise<unknown>} Promise\n\t\t */\n\t\twrite<T extends D = D>(data: T, options?: RO): Promise<unknown>\n\t\t{\n\t\t\treturn write(outputFile, data, options)\n\t\t},\n\n\t\t/**\n\t\t * 異步處理\n\t\t * \n\t\t * @returns {Promise<object>} Promise,解析為檔案目標處理器\n\t\t */\n\t\tasync async()\n\t\t{\n\t\t\treturn target\n\t\t}\n\n\t};\n\n\treturn target\n\n}\n\n/** 預設導出 - 檔案目標處理函數 */\nexport default fsTargetFile\n\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fs-target-file",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.25",
|
|
4
4
|
"description": "",
|
|
5
5
|
"homepage": "https://github.com/bluelovers/ws-iconv/tree/master/packages/fs-target-file#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -15,9 +15,11 @@
|
|
|
15
15
|
"author": "bluelovers",
|
|
16
16
|
"main": "index.js",
|
|
17
17
|
"scripts": {
|
|
18
|
+
"coverage": "yarn run test -- --coverage",
|
|
18
19
|
"lint": "ynpx --quiet eslint -- **/*.ts",
|
|
19
20
|
"test": "echo \"Error: no test specified\"",
|
|
20
21
|
"test:jest": "ynpx --quiet jest -- --coverage --passWithNoTests",
|
|
22
|
+
"test:jest:coverage": "yarn run test:jest -- --coverage",
|
|
21
23
|
"test:jest:snapshot": "yarn run test:jest -- -u",
|
|
22
24
|
"test:mocha": "ynpx --quiet -p ts-node -p mocha mocha -- --require ts-node/register \"!(node_modules)/**/*.{test,spec}.{ts,tsx}\"",
|
|
23
25
|
"test:snapshot": "yarn run test -- -u",
|
|
@@ -27,7 +29,8 @@
|
|
|
27
29
|
"prepublishOnly:check-bin": "ynpx --quiet @yarn-tool/check-pkg-bin",
|
|
28
30
|
"prepublishOnly:update": "yarn run ncu && yarn run sort-package-json",
|
|
29
31
|
"ncu": "yarn-tool ncu -u",
|
|
30
|
-
"sort-package-json": "yarn-tool sort"
|
|
32
|
+
"sort-package-json": "yarn-tool sort",
|
|
33
|
+
"tsc:showConfig": "ynpx get-current-tsconfig -p"
|
|
31
34
|
},
|
|
32
35
|
"dependencies": {
|
|
33
36
|
"fs-extra": "^11.2.0",
|
|
@@ -37,5 +40,5 @@
|
|
|
37
40
|
"devDependencies": {
|
|
38
41
|
"@types/fs-extra": "^11"
|
|
39
42
|
},
|
|
40
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "b3974c1f5bf98e50fca3e403453837fc8e681230"
|
|
41
44
|
}
|