xshell 0.0.63 → 0.0.65

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/file.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  /// <reference types="node" resolution-mode="require"/>
3
3
  /// <reference types="node" resolution-mode="require"/>
4
4
  import { promises as fsp, default as fs } from 'fs';
5
- declare type FileHandle = fsp.FileHandle & {
5
+ type FileHandle = fsp.FileHandle & {
6
6
  fp: string;
7
7
  };
8
8
  import path from 'upath';
@@ -15,7 +15,7 @@ declare module 'memfs' {
15
15
  }
16
16
  export * from './ufs.js';
17
17
  export { MFS };
18
- export declare type Encoding = 'utf-8' | 'gb18030' | 'shift-jis' | 'binary';
18
+ export type Encoding = 'utf-8' | 'gb18030' | 'shift-jis' | 'binary';
19
19
  /** Does the file/folder pointed to by fp exist? */
20
20
  export declare function fexists(fp: string, { print }?: {
21
21
  print?: boolean;
@@ -28,8 +28,7 @@ export declare function fexists(fp: string, { print }?: {
28
28
 
29
29
  - flags: `'r'`
30
30
  - options?:
31
- - mode?: `'0o666'` Sets the file mode (permission and sticky bits) if the file is created.
32
- */
31
+ - mode?: `'0o666'` Sets the file mode (permission and sticky bits) if the file is created. */
33
32
  export declare function fopen(fp: string, flags: string | number, { mode, print }?: {
34
33
  mode?: fs.Mode;
35
34
  print?: boolean;
@@ -82,8 +81,7 @@ export declare function fappend(fp: string, data: any, { dir, print }?: {
82
81
  - absolute?: `false` Return, print full path instead of relative path
83
82
  - print?: `true`
84
83
  - filter?: `true` RegExp | (fp: string) => any, Note that when deep = true,
85
- directories and files in directories that are filtered out by the filter will not be included in the results
86
- */
84
+ directories and files in directories that are filtered out by the filter will not be included in the results */
87
85
  export declare function flist(fpd: string, options?: {
88
86
  filter?: RegExp | ((fp: string) => any);
89
87
  deep?: boolean;
@@ -96,8 +94,7 @@ export declare function fstat(fp: string): Promise<fs.BigIntStats>;
96
94
  - options?:
97
95
  - print?: `true`
98
96
 
99
- Returns whether the delete operation actually took place
100
- */
97
+ Returns whether the delete operation actually took place */
101
98
  export declare function fdelete(fp: string, { print }?: {
102
99
  print?: boolean;
103
100
  }): Promise<boolean>;
@@ -105,8 +102,7 @@ export declare function fdelete(fp: string, { print }?: {
105
102
  - src: src file/directory absolute path
106
103
  - dst: dst file/directory absolute path
107
104
  @example
108
- fcopy('d:/temp/camera/', 'd:/camera/')
109
- */
105
+ fcopy('d:/temp/camera/', 'd:/camera/') */
110
106
  export declare function fcopy(fp_src: string, fp_dst: string, { print, overwrite, }?: {
111
107
  print?: boolean;
112
108
  overwrite?: boolean;
@@ -115,8 +111,7 @@ export declare function fcopy(fp_src: string, fp_dst: string, { print, overwrite
115
111
  - src: src file/directory absolute path
116
112
  - dst: dst file/directory absolute path
117
113
  @example
118
- fmove('d:/temp/camera/', 'd:/camera/')
119
- */
114
+ fmove('d:/temp/camera/', 'd:/camera/') */
120
115
  export declare function fmove(src: string, dst: string, { overwrite, print }?: {
121
116
  overwrite?: boolean;
122
117
  print?: boolean;
@@ -127,8 +122,7 @@ export declare function fmove(src: string, dst: string, { overwrite, print }?: {
127
122
  - options?:
128
123
  - fpd?: fp and fp_ is in same directory
129
124
  - print?: `true`
130
- - overwrite?: `true` better performance without check
131
- */
125
+ - overwrite?: `true` better performance without check */
132
126
  export declare function frename(fp: string, fp_: string, { fpd, print, overwrite }?: {
133
127
  fpd?: string;
134
128
  print?: boolean;
@@ -141,17 +135,15 @@ export declare function frename(fp: string, fp_: string, { fpd, print, overwrite
141
135
  - fpd: Folder full path
142
136
  - options?:
143
137
  - print?: `true`
144
- - mode?: `'0o777'`
145
- */
138
+ - mode?: `'0o777'` */
146
139
  export declare function fmkdir(fpd: string, { print, mode, }?: {
147
140
  print?: boolean;
148
141
  /** `0o777` A file mode. If a string is passed, it is parsed as an octal integer. */
149
142
  mode?: string | number;
150
143
  }): Promise<string>;
151
- /**
144
+ /** Create soft links
152
145
  - fp_real: current real file/directory path
153
- - fp_link: target file/directory path
154
- */
146
+ - fp_link: target file/directory path */
155
147
  export declare function flink(fp_real: string, fp_link: string, { junction, print }?: {
156
148
  junction?: boolean;
157
149
  print?: boolean;
@@ -169,9 +161,7 @@ export declare let fwatchers: Record<string, fs.FSWatcher>;
169
161
  event is either 'rename' or 'change', and filename is the name of the file which triggered the event.
170
162
  On most platforms, 'rename' is emitted whenever a filename appears or disappears in the directory.
171
163
 
172
- The listener callback is attached to the 'change' event fired by fs.FSWatcher, but it is not the same thing as the 'change' value of event.
173
-
174
- */
164
+ The listener callback is attached to the 'change' event fired by fs.FSWatcher, but it is not the same thing as the 'change' value of event. */
175
165
  export declare function fwatch(fp: string, onchange: (event: string, fname: string) => any, { exec }?: {
176
166
  exec?: boolean;
177
167
  }): Promise<fs.FSWatcher>;
@@ -181,8 +171,7 @@ export declare function freplace(fp: string, pattern: string | RegExp, replaceme
181
171
  - fp: file absolute path
182
172
  - options?:
183
173
  - dryrun?: `true`
184
- - encoding?: `'auto'`
185
- */
174
+ - encoding?: `'auto'` */
186
175
  export declare function f2utf8(fp: string, { dryrun, encoding, }?: {
187
176
  dryrun?: boolean;
188
177
  encoding?: Encoding | 'auto';
package/file.js CHANGED
@@ -22,16 +22,11 @@ export function fexists(fp, { print = true } = {}) {
22
22
 
23
23
  - flags: `'r'`
24
24
  - options?:
25
- - mode?: `'0o666'` Sets the file mode (permission and sticky bits) if the file is created.
26
- */
25
+ - mode?: `'0o666'` Sets the file mode (permission and sticky bits) if the file is created. */
27
26
  export async function fopen(fp, flags, { mode, print } = {}) {
28
27
  if (print)
29
- console.log('fopen:', fp);
30
- return Object.assign(await fsp.open(fp, flags, mode), {
31
- fp,
32
- flags,
33
- mode
34
- });
28
+ console.log('open file:', fp);
29
+ return Object.assign(await fsp.open(fp, flags, mode), { fp, flags, mode });
35
30
  }
36
31
  export function create_mfs() {
37
32
  let mfs = MFS.createFsFromVolume(new MFS.Volume());
@@ -43,21 +38,25 @@ export async function fread(fp, { dir, encoding = 'utf-8', print = true } = {})
43
38
  if (dir)
44
39
  fp = path.join(dir, fp);
45
40
  else if (!path.isAbsolute(fp))
46
- throw new Error('fp must be absolute path, or pass in \'dir\' parameter');
41
+ throw new Error(`fp must be absolute path, or pass in 'dir' parameter: ${fp}`);
47
42
  if (print)
48
43
  console.log(`read: ${fp}`);
49
- const buffer = await fsp.readFile(fp);
50
- if (encoding === 'binary')
51
- return buffer;
52
- if (encoding === 'utf-8')
53
- return buffer.toString('utf8');
54
- if (encoding === 'auto') {
55
- const { detect } = await import('chardet');
56
- encoding = detect(buffer);
57
- if (print)
58
- console.log(`${fp} probably has encoding: ${encoding.toLowerCase()}`);
44
+ switch (encoding) {
45
+ case 'utf-8':
46
+ return fsp.readFile(fp, { encoding: 'utf-8' });
47
+ case 'binary':
48
+ return fsp.readFile(fp);
49
+ default: {
50
+ const buffer = await fsp.readFile(fp);
51
+ if (encoding === 'auto') {
52
+ const { detect } = await import('chardet');
53
+ encoding = detect(buffer);
54
+ if (print)
55
+ console.log(`The encoding of '${fp}' might be ${encoding.toLowerCase()}`);
56
+ }
57
+ return iconv.decode(buffer, encoding);
58
+ }
59
59
  }
60
- return iconv.decode(buffer, encoding);
61
60
  }
62
61
  export async function fread_lines(fp, options = {}) {
63
62
  return (await fread(fp, options))
@@ -76,7 +75,7 @@ export async function fwrite(fp, data, { dir, encoding = 'utf-8', print = true,
76
75
  if (dir)
77
76
  fp = path.join(dir, fp);
78
77
  else if (!path.isAbsolute(fp))
79
- throw new Error('fp must be absolute path, or pass in "dir" parameter');
78
+ throw new Error(`fp must be an absolute path, or pass in the 'dir' parameter: ${fp}`);
80
79
  if (print)
81
80
  console.log('write:', fp);
82
81
  }
@@ -98,7 +97,7 @@ export async function fappend(fp, data, { dir, print = true } = {}) {
98
97
  if (dir)
99
98
  fp = path.join(dir, fp);
100
99
  else if (!path.isAbsolute(fp))
101
- throw new Error('fp must be absolute path, or pass in "dir" parameter');
100
+ throw new Error(`fp must be an absolute path, or pass in the 'dir' parameter: ${fp}`);
102
101
  if (print)
103
102
  console.log('append:', fp);
104
103
  if (!Buffer.isBuffer(data) && typeof data !== 'string')
@@ -112,8 +111,7 @@ export async function fappend(fp, data, { dir, print = true } = {}) {
112
111
  - absolute?: `false` Return, print full path instead of relative path
113
112
  - print?: `true`
114
113
  - filter?: `true` RegExp | (fp: string) => any, Note that when deep = true,
115
- directories and files in directories that are filtered out by the filter will not be included in the results
116
- */
114
+ directories and files in directories that are filtered out by the filter will not be included in the results */
117
115
  export async function flist(fpd, options = {}) {
118
116
  const { filter, deep = false, absolute = false, print = true, } = options;
119
117
  if (!path.isAbsolute(fpd))
@@ -140,7 +138,7 @@ export async function flist(fpd, options = {}) {
140
138
  if (filter_fn && !filter(fp))
141
139
  continue;
142
140
  if (print)
143
- console.log(deep || absolute ? fpd + fp : fp);
141
+ console.log(fp);
144
142
  fps.push(fp);
145
143
  }
146
144
  return deep ? (await Promise.all(fps.map(async (fp) => fp.endsWith('/') ?
@@ -155,7 +153,7 @@ export async function flist(fpd, options = {}) {
155
153
  }
156
154
  export async function fstat(fp) {
157
155
  if (!path.isAbsolute(fp))
158
- throw new Error(`fp (${fp}) must be absolute path`);
156
+ throw new Error(`fp must be absolute path: ${fp}`);
159
157
  return fsp.stat(fp, { bigint: true });
160
158
  }
161
159
  /** delete files or folders
@@ -163,8 +161,7 @@ export async function fstat(fp) {
163
161
  - options?:
164
162
  - print?: `true`
165
163
 
166
- Returns whether the delete operation actually took place
167
- */
164
+ Returns whether the delete operation actually took place */
168
165
  export async function fdelete(fp, { print = true } = {}) {
169
166
  if (fp.length < 6)
170
167
  throw new Error(`${fp} 太短`);
@@ -195,8 +192,7 @@ export async function fdelete(fp, { print = true } = {}) {
195
192
  - src: src file/directory absolute path
196
193
  - dst: dst file/directory absolute path
197
194
  @example
198
- fcopy('d:/temp/camera/', 'd:/camera/')
199
- */
195
+ fcopy('d:/temp/camera/', 'd:/camera/') */
200
196
  export async function fcopy(fp_src, fp_dst, { print = true, overwrite = true, } = {}) {
201
197
  if (fp_src.endsWith('/') !== fp_dst.endsWith('/'))
202
198
  throw new Error('fp_src and fp_dst must be both file path or directory path');
@@ -210,8 +206,7 @@ export async function fcopy(fp_src, fp_dst, { print = true, overwrite = true, }
210
206
  - src: src file/directory absolute path
211
207
  - dst: dst file/directory absolute path
212
208
  @example
213
- fmove('d:/temp/camera/', 'd:/camera/')
214
- */
209
+ fmove('d:/temp/camera/', 'd:/camera/') */
215
210
  export async function fmove(src, dst, { overwrite = false, print = true } = {}) {
216
211
  if (src.endsWith('/') !== dst.endsWith('/'))
217
212
  throw new Error('src and dst must be both file path or directory path');
@@ -227,8 +222,7 @@ export async function fmove(src, dst, { overwrite = false, print = true } = {})
227
222
  - options?:
228
223
  - fpd?: fp and fp_ is in same directory
229
224
  - print?: `true`
230
- - overwrite?: `true` better performance without check
231
- */
225
+ - overwrite?: `true` better performance without check */
232
226
  export async function frename(fp, fp_, { fpd, print = true, overwrite = true } = {}) {
233
227
  if (fpd) {
234
228
  fp = path.join(fpd, fp);
@@ -249,8 +243,7 @@ export async function frename(fp, fp_, { fpd, print = true, overwrite = true } =
249
243
  - fpd: Folder full path
250
244
  - options?:
251
245
  - print?: `true`
252
- - mode?: `'0o777'`
253
- */
246
+ - mode?: `'0o777'` */
254
247
  export async function fmkdir(fpd, { print = true, mode, } = {}) {
255
248
  if (!path.isAbsolute(fpd))
256
249
  throw new Error(`fpd must be an absolute path: ${fpd}`);
@@ -266,10 +259,9 @@ export async function fmkdir(fpd, { print = true, mode, } = {}) {
266
259
  console.log('folder already exists:', fpd);
267
260
  return fpd_;
268
261
  }
269
- /**
262
+ /** Create soft links
270
263
  - fp_real: current real file/directory path
271
- - fp_link: target file/directory path
272
- */
264
+ - fp_link: target file/directory path */
273
265
  export async function flink(fp_real, fp_link, { junction = false, print = true } = {}) {
274
266
  if (!path.isAbsolute(fp_real) || !path.isAbsolute(fp_link))
275
267
  throw new Error('fp must be absolute path');
@@ -299,9 +291,7 @@ export let fwatchers = {};
299
291
  event is either 'rename' or 'change', and filename is the name of the file which triggered the event.
300
292
  On most platforms, 'rename' is emitted whenever a filename appears or disappears in the directory.
301
293
 
302
- The listener callback is attached to the 'change' event fired by fs.FSWatcher, but it is not the same thing as the 'change' value of event.
303
-
304
- */
294
+ The listener callback is attached to the 'change' event fired by fs.FSWatcher, but it is not the same thing as the 'change' value of event. */
305
295
  export async function fwatch(fp, onchange, { exec = true } = {}) {
306
296
  if (!path.isAbsolute(fp))
307
297
  throw new Error('fp must be absolute path');
@@ -332,8 +322,7 @@ export async function freplace(fp, pattern, replacement) {
332
322
  - fp: file absolute path
333
323
  - options?:
334
324
  - dryrun?: `true`
335
- - encoding?: `'auto'`
336
- */
325
+ - encoding?: `'auto'` */
337
326
  export async function f2utf8(fp, { dryrun = true, encoding = 'auto', } = {}) {
338
327
  const text = await fread(fp, { encoding });
339
328
  if (dryrun) {
package/file.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,QAAQ,IAAI,GAAG,EACf,OAAO,IAAI,EAAE,GAChB,MAAM,IAAI,CAAA;AAGX,OAAO,IAAI,MAAM,OAAO,CAAA;AACxB,OAAO,KAAK,MAAM,YAAY,CAAA;AAC9B,OAAO,GAAG,MAAM,UAAU,CAAA;AAE1B,OAAO,QAAQ,MAAM,oBAAoB,CAAA;AAGzC,OAAO,GAAG,MAAM,OAAO,CAAA;AAQvB,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AACxC,cAAc,UAAU,CAAA;AAGxB,OAAO,EAAE,GAAG,EAAE,CAAA;AAKd,mDAAmD;AACnD,MAAM,UAAU,OAAO,CAAE,EAAU,EAAE,EAAE,KAAK,GAAG,IAAI,KAA0B,EAAG;IAC5E,MAAM,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC,CAAA;IAEhC,IAAI,KAAK;QACL,OAAO,CAAC,GAAG,CACP,MAAM,CAAC,C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E,CAAC,CAAC;SACZ,UAAU,CAAC,OAAO,EAAE,WAAW,CAAC,CACxC,CAAA;AACL,CAAC;AAED;;;;;EAKE;AACF,MAAM,CAAC,KAAK,UAAU,MAAM,CAAE,EAAU,EAAE,EACtC,MAAM,GAAG,IAAI,EACb,QAAQ,GAAG,MAAM,MAIjB,EAAG;IACH,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAA;IAC1C,IAAI,MAAM,EAAE;QACR,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAA;QACjC,OAAM;KACT;IAED,MAAM,MAAM,GAAG,GAAG,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,kBAAkB,EAAE,UAAU,CAAC,EAAE,CAAA;IAC9E,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC;QAChB,MAAM,KAAK,CAAC,EAAE,EAAE,MAAM,CAAC,CAAA;IAE3B,MAAM,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,CAAA;AAC1B,CAAC","sourcesContent":["import {\n promises as fsp,\n default as fs,\n} from 'fs'\ntype FileHandle = fsp.FileHandle & { fp: string }\n\nimport path from 'upath'\nimport iconv from 'iconv-lite'\nimport fse from 'fs-extra'\n\nimport debounce from 'lodash/debounce.js'\n\n\nimport MFS from 'memfs'\ndeclare module 'memfs' {\n interface IFs {\n join: typeof path.join\n is_mfs: true\n }\n}\n\nimport { to_json } from './prototype.js'\nexport * from './ufs.js'\n\n\nexport { MFS }\n\nexport type Encoding = 'utf-8' | 'gb18030' | 'shift-jis' | 'binary'\n\n\n/** Does the file/folder pointed to by fp exist? */\nexport function fexists (fp: string, { print = true }: { print?: boolean } = { }) {\n const exists = fs.existsSync(fp)\n \n if (print)\n console.log(\n exists ? 'exists:' : 'not exists:',\n fp\n )\n \n return exists\n}\n\n\n/**\n open file, return FileHandle \n Some characters (`< > : \" / \\ | ? *`) are reserved under Windows as documented\n by [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). Under NTFS, if the filename contains\n a colon, Node.js will open a file system stream, as described by [this MSDN page](https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams).\n \n - flags: `'r'`\n - options?:\n - mode?: `'0o666'` Sets the file mode (permission and sticky bits) if the file is created.\n*/\nexport async function fopen (\n fp: string,\n flags: string | number,\n {\n mode,\n print\n }: {\n mode?: fs.Mode\n print?: boolean\n } = { }\n) {\n if (print)\n console.log('fopen:', fp)\n return Object.assign(\n await fsp.open(fp, flags, mode),\n {\n fp,\n flags,\n mode\n }\n )\n}\n\n\nexport function create_mfs () {\n let mfs = MFS.createFsFromVolume(\n new MFS.Volume()\n )\n \n mfs.join = path.join.bind(path)\n mfs.is_mfs = true\n \n return mfs\n}\n\n\nexport async function fread (fp: string): Promise<string>\nexport async function fread (fp: string, { dir, encoding, print }?: { dir?: string, encoding: 'binary', print?: boolean }): Promise<Buffer>\nexport async function fread (fp: string, { dir, encoding, print }?: { dir?: string, encoding?: Encoding | 'auto', print?: boolean }): Promise<string>\nexport async function fread (fp: string, {\n dir, \n encoding = 'utf-8', \n print = true\n}: {\n dir?: string\n encoding?: Encoding | 'auto'\n print?: boolean } = { }\n) {\n if (dir)\n fp = path.join(dir, fp)\n else if (!path.isAbsolute(fp))\n throw new Error('fp must be absolute path, or pass in \\'dir\\' parameter')\n \n if (print)\n console.log(`read: ${fp}`)\n \n const buffer = await fsp.readFile(fp)\n \n if (encoding === 'binary')\n return buffer\n \n if (encoding === 'utf-8')\n return buffer.toString('utf8')\n \n if (encoding === 'auto') {\n const { detect } = await import('chardet')\n encoding = detect(buffer) as any\n if (print)\n console.log(`${fp} probably has encoding: ${encoding.toLowerCase()}`)\n }\n \n return iconv.decode(buffer, encoding)\n}\n\nexport async function fread_lines (fp: string, options: { dir?: string, encoding?: Exclude<Encoding, 'binary'> | 'auto', print?: boolean } = { }) {\n return (await fread(fp, options))\n .split_lines()\n}\n\nexport async function fread_json <T = any> (fp: string, options: { dir?: string, encoding?: Encoding, print?: boolean } = { }): Promise<T> {\n return JSON.parse(\n await fread(fp, options)\n )\n}\n\n\nexport async function fwrite (fp: string | FileHandle, data: Buffer, options?: { dir?: string, print?: boolean, mkdir?: boolean }): Promise<void>\nexport async function fwrite (fp: string | FileHandle, data: any, options?: { dir?: string, encoding?: Encoding, print?: boolean, mkdir?: boolean }): Promise<void>\nexport async function fwrite (\n fp: string | FileHandle,\n data: any,\n {\n dir,\n encoding = 'utf-8',\n print = true,\n mkdir = false,\n }: {\n dir?: string\n encoding?: Encoding\n print?: boolean\n mkdir?: boolean\n } = { }\n) {\n const is_handle = typeof fp === 'object' && fp && 'fd' in fp\n if (is_handle) {\n if (print)\n console.log('write:', (fp as FileHandle).fp)\n } else {\n if (dir)\n fp = path.join(dir, fp)\n else if (!path.isAbsolute(fp as string))\n throw new Error('fp must be absolute path, or pass in \"dir\" parameter')\n \n if (print)\n console.log('write:', fp)\n }\n \n if (encoding === 'gb18030')\n data = iconv.encode(data, encoding)\n \n if (!Buffer.isBuffer(data) && typeof data !== 'string')\n data = to_json(data)\n \n try {\n await fsp.writeFile(fp, data)\n } catch (error) {\n if (!mkdir || error.code !== 'ENOENT' || is_handle)\n throw error\n \n await fmkdir((fp as string).fdir)\n await fsp.writeFile(fp, data)\n }\n}\n\nexport async function fappend (fp: string, data: any, { dir, print = true }: { dir?: string, print?: boolean } = { }) {\n if (dir)\n fp = path.join(dir, fp)\n else if (!path.isAbsolute(fp))\n throw new Error('fp must be absolute path, or pass in \"dir\" parameter')\n \n if (print)\n console.log('append:', fp)\n \n if (!Buffer.isBuffer(data) && typeof data !== 'string')\n throw new Error('data is not Buffer or string')\n \n await fsp.appendFile(fp, data)\n}\n\n\n/**\n - fpd: absolute path of directory\n - optoins?:\n - deep?: `false` recursively\n - absolute?: `false` Return, print full path instead of relative path\n - print?: `true`\n - filter?: `true` RegExp | (fp: string) => any, Note that when deep = true, \n directories and files in directories that are filtered out by the filter will not be included in the results\n*/\nexport async function flist (\n fpd: string,\n options: {\n filter?: RegExp | ((fp: string) => any)\n deep?: boolean\n absolute?: boolean\n print?: boolean\n } = { }\n): Promise<string[]> {\n const {\n filter,\n deep = false,\n absolute = false,\n print = true,\n } = options\n \n if (!path.isAbsolute(fpd))\n throw new Error(`fpd (${fpd}) must be absolute path`)\n \n if (!fpd.endsWith('/'))\n throw new Error(`Argument fpd (${fpd}) must end with /`)\n \n // readdir withFileTypes 参数在底层有什么区别,速度上有什么差异\n // 都调用了 uv_fs_scandir, 且调用参数相同,仅仅是 Node.js 侧的回调不同 AfterScanDir / AfterScanDirWithTypes\n // 回调中通过 uv_fs_scandir_next 获取到每个条目的信息,而 uv_fs_scandir_next 中都会读取 type\n // 速度上:都在 0.2 ms 左右就可以完成\n \n const files = await fsp.readdir(fpd, {\n withFileTypes: true,\n encoding: 'utf-8',\n })\n \n const filter_regexp = filter instanceof RegExp\n const filter_fn = Boolean(filter && !filter_regexp)\n \n let fps: string[] = [ ]\n \n for (const file of files) {\n const fp = \n (absolute ? fpd : '') +\n file.name +\n (file.isDirectory() ? '/' : '')\n \n if (filter_regexp && !filter.test(fp))\n continue\n \n if (filter_fn && !(filter as Function)(fp))\n continue\n \n if (print)\n console.log(\n deep || absolute ? fpd + fp : fp\n )\n \n fps.push(fp)\n }\n \n return deep ? (\n await Promise.all(\n fps.map(async fp => \n fp.endsWith('/') ?\n [\n fp,\n ... (await flist(\n absolute ? fp : fpd + fp,\n options\n )).map(fp_ => \n absolute ? fp_ : fp + fp_)\n ]\n :\n fp)\n )\n ).flat()\n :\n fps\n}\n\n\nexport async function fstat (fp: string) {\n if (!path.isAbsolute(fp))\n throw new Error(`fp (${fp}) must be absolute path`)\n \n return fsp.stat(fp, { bigint: true })\n}\n\n\n/** delete files or folders \n - fp: file path\n - options?:\n - print?: `true`\n \n Returns whether the delete operation actually took place\n*/\nexport async function fdelete (fp: string, { print = true }: { print?: boolean } = { }) {\n if (fp.length < 6)\n throw new Error(`${fp} 太短`)\n \n if (!path.isAbsolute(fp))\n throw new Error('fp must be an absolute path')\n \n try {\n await fsp.rm(fp, { recursive: true })\n if (print)\n if (fp.endsWith('/'))\n console.log(`deleted folder: ${fp}`.red)\n else\n console.log(`deleted file: ${fp}`.red)\n return true\n } catch (error) {\n if (error.code === 'ENOENT') {\n if (print)\n if (fp.endsWith('/'))\n console.log(`folder no longer exists: ${fp}`)\n else\n console.log(`file no longer exists: ${fp}`)\n return false\n }\n \n throw error\n }\n}\n\n\n/** copy file or direcotry\n - src: src file/directory absolute path\n - dst: dst file/directory absolute path\n @example\n fcopy('d:/temp/camera/', 'd:/camera/')\n*/\nexport async function fcopy (fp_src: string, fp_dst: string, {\n print = true,\n overwrite = true,\n}: {\n print?: boolean\n overwrite?: boolean\n} = { }) {\n if (fp_src.endsWith('/') !== fp_dst.endsWith('/'))\n throw new Error('fp_src and fp_dst must be both file path or directory path')\n \n if (!path.isAbsolute(fp_src) || !path.isAbsolute(fp_dst))\n throw new Error('fp_src and fp_dst must be absolute path')\n \n if (print)\n console.log(`copy: ${fp_src} → ${fp_dst}`)\n \n await fse.copy(fp_src, fp_dst, { overwrite, errorOnExist: true })\n}\n\n\n/** move file or direcotry\n - src: src file/directory absolute path\n - dst: dst file/directory absolute path\n @example\n fmove('d:/temp/camera/', 'd:/camera/')\n*/\nexport async function fmove (src: string, dst: string, {\n overwrite = false,\n print = true\n}: {\n overwrite?: boolean\n print?: boolean\n} = { }) {\n if (src.endsWith('/') !== dst.endsWith('/'))\n throw new Error('src and dst must be both file path or directory path')\n \n if (!path.isAbsolute(src) || !path.isAbsolute(dst))\n throw new Error('src and dst must be absolute path')\n \n if (print)\n console.log(`move: ${src} → ${dst}`)\n \n await fse.move(src, dst, { overwrite })\n}\n\n\n/** rename file \n - fp: current filename/path\n - fp_: new filename/path\n - options?:\n - fpd?: fp and fp_ is in same directory\n - print?: `true`\n - overwrite?: `true` better performance without check\n */\nexport async function frename (\n fp: string, \n fp_: string,\n {\n fpd,\n print = true,\n overwrite = true\n }: {\n fpd?: string\n print?: boolean\n overwrite?: boolean\n } = { }\n) {\n if (fpd) {\n fp = path.join(fpd, fp)\n fp_ = path.join(fpd, fp_)\n } else if (!path.isAbsolute(fp) || !path.isAbsolute(fp_))\n throw new Error('fp and fp_ must be absolute path')\n \n if (print)\n console.log('rename:', fp, '→', fp_)\n \n if (!overwrite && fexists(fp_))\n throw new Error(`file already exists:${fp_}`)\n \n await fsp.rename(fp, fp_)\n}\n\n\n/**\n Create folders recursively, make sure the folder pointed to by fpd exists \n Returns the first created folder or undefined\n \n - fpd: Folder full path\n - options?:\n - print?: `true`\n - mode?: `'0o777'`\n*/\nexport async function fmkdir (\n fpd: string,\n {\n print = true,\n mode,\n }: {\n print?: boolean\n \n /** `0o777` A file mode. If a string is passed, it is parsed as an octal integer. */\n mode?: string | number\n } = { }\n) {\n if (!path.isAbsolute(fpd))\n throw new Error(`fpd must be an absolute path: ${fpd}`)\n \n if (!fpd.endsWith('/'))\n throw new Error(`fpd must end with /: ${fpd}`)\n \n // CallingfsPromises.mkdir() when path is a directory that exists results in a rejection only when recursive is false.\n const fpd_ = (\n await fsp.mkdir(fpd, { recursive: true, mode })\n )?.replaceAll('\\\\', '/')\n \n if (fpd_) {\n if (print)\n console.log('folder created:', fpd)\n } else\n if (print)\n console.log('folder already exists:', fpd)\n \n return fpd_\n}\n\n\n/** \n - fp_real: current real file/directory path\n - fp_link: target file/directory path\n*/\nexport async function flink (\n fp_real: string, \n fp_link: string, \n {\n junction = false,\n print = true \n }: { \n junction?: boolean\n print?: boolean\n} = { }) {\n if (!path.isAbsolute(fp_real) || !path.isAbsolute(fp_link))\n throw new Error('fp must be absolute path')\n \n const is_fpd_real = fp_real.endsWith('/')\n const is_fpd_link = fp_link.endsWith('/')\n \n if (is_fpd_real !== is_fpd_link)\n throw new Error('fp_real and fp_link must be both file path or folder path')\n \n if (fexists(fp_link))\n throw new Error(`${ is_fpd_link ? 'folder' : 'file' } exists: ${fp_link}, could not create link`)\n \n if (print)\n console.log(`source file ${fp_real} linked to ${fp_link}`)\n \n if (junction)\n fsp.symlink(fp_real, fp_link, 'junction')\n else\n fsp.symlink(fp_real, fp_link, is_fpd_real ? 'dir' : 'file')\n}\n\n\nexport let fwatchers: Record<string, fs.FSWatcher> = { }\n\n/**\n - fp: path of file or directory\n - callback: called when modified\n - exec: call callback when watch is executed\n \n save fs.FSWatcher in watchers, subsequent call will auto close existing watcher for the same fp\n \n https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_watch_filename_options_listener \n The listener callback gets two arguments (event, fname). \n event is either 'rename' or 'change', and filename is the name of the file which triggered the event.\n On most platforms, 'rename' is emitted whenever a filename appears or disappears in the directory.\n \n The listener callback is attached to the 'change' event fired by fs.FSWatcher, but it is not the same thing as the 'change' value of event. \n \n*/\nexport async function fwatch (\n fp: string,\n onchange: (event: string, fname: string) => any, \n { exec = true }: { exec?: boolean } = { }\n) {\n if (!path.isAbsolute(fp))\n throw new Error('fp must be absolute path')\n \n const _watcher = fwatchers[fp]\n if (_watcher)\n _watcher.close()\n \n if (exec)\n await onchange('change', fp.fname)\n \n const start = new Date().getTime()\n \n const debounced_onchange = debounce(\n (event, fname) => {\n if (new Date().getTime() - start < 800)\n return\n console.log(`file ${event}: ${fname}`)\n onchange(event, path.normalize(fname))\n },\n 500,\n { leading: false, trailing: true }\n )\n \n let watcher = fs.watch(fp, debounced_onchange)\n watcher.on('error', error => {\n console.error(error)\n })\n return fwatchers[fp] = watcher\n}\n\n\n/** open a file and replace certain pattern */\nexport async function freplace (fp: string, pattern: string | RegExp, replacement: string) {\n await fwrite(\n fp,\n (await fread(fp))\n .replaceAll(pattern, replacement)\n )\n}\n\n/** convert file encoding to UTF-8\n - fp: file absolute path\n - options?:\n - dryrun?: `true`\n - encoding?: `'auto'`\n*/\nexport async function f2utf8 (fp: string, {\n dryrun = true,\n encoding = 'auto',\n}: {\n dryrun?: boolean\n encoding?: Encoding | 'auto'\n} = { }) {\n const text = await fread(fp, { encoding })\n if (dryrun) {\n console.log(text.slice(0, 10000))\n return\n }\n \n const fp_bak = `${fp.fdir}${fp.fname.replace(/(.*?)(\\.[^.]+)?$/, '$1.bak$2')}`\n if (!fexists(fp_bak))\n await fcopy(fp, fp_bak)\n \n await fwrite(fp, text)\n}\n\n"]}
1
+ {"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,QAAQ,IAAI,GAAG,EACf,OAAO,IAAI,EAAE,GAChB,MAAM,IAAI,CAAA;AAGX,OAAO,IAAI,MAAM,OAAO,CAAA;AACxB,OAAO,KAAK,MAAM,YAAY,CAAA;AAC9B,OAAO,GAAG,MAAM,UAAU,CAAA;AAE1B,OAAO,QAAQ,MAAM,oBAAoB,CAAA;AAGzC,OAAO,GAAG,MAAM,OAAO,CAAA;AAQvB,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AACxC,cAAc,UAAU,CAAA;AAGxB,OAAO,EAAE,GAAG,EAAE,CAAA;AAKd,mDAAmD;AACnD,MAAM,UAAU,OAAO,CAAE,EAAU,EAAE,EAAE,KAAK,GAAG,IAAI,KAA0B,EAAG;IAC5E,MAAM,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC,CAAA;IAEhC,IAAI,KAAK;QACL,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,aAAa,EAAE,EAAE,CAAC,CAAA;IAEvD,OAAO,MAAM,CAAA;AACjB,CAAC;AAGD;;;;;;;;qGAQqG;AACrG,MAAM,CAAC,KAAK,UAAU,KAAK,CACvB,EAAU,EACV,KAAsB,EACtB,EAAE,IAAI,EAAE,KAAK,KAA0C,EAAG;IAE1D,IAAI,KAAK;QACL,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA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M,MAIjB,EAAG;IACH,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAA;IAC1C,IAAI,MAAM,EAAE;QACR,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAA;QACjC,OAAM;KACT;IAED,MAAM,MAAM,GAAG,GAAG,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,kBAAkB,EAAE,UAAU,CAAC,EAAE,CAAA;IAC9E,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC;QAChB,MAAM,KAAK,CAAC,EAAE,EAAE,MAAM,CAAC,CAAA;IAE3B,MAAM,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,CAAA;AAC1B,CAAC","sourcesContent":["import {\n promises as fsp,\n default as fs,\n} from 'fs'\ntype FileHandle = fsp.FileHandle & { fp: string }\n\nimport path from 'upath'\nimport iconv from 'iconv-lite'\nimport fse from 'fs-extra'\n\nimport debounce from 'lodash/debounce.js'\n\n\nimport MFS from 'memfs'\ndeclare module 'memfs' {\n interface IFs {\n join: typeof path.join\n is_mfs: true\n }\n}\n\nimport { to_json } from './prototype.js'\nexport * from './ufs.js'\n\n\nexport { MFS }\n\nexport type Encoding = 'utf-8' | 'gb18030' | 'shift-jis' | 'binary'\n\n\n/** Does the file/folder pointed to by fp exist? */\nexport function fexists (fp: string, { print = true }: { print?: boolean } = { }) {\n const exists = fs.existsSync(fp)\n \n if (print)\n console.log(exists ? 'exists:' : 'not exists:', fp)\n \n return exists\n}\n\n\n/**\n open file, return FileHandle \n Some characters (`< > : \" / \\ | ? *`) are reserved under Windows as documented\n by [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). Under NTFS, if the filename contains\n a colon, Node.js will open a file system stream, as described by [this MSDN page](https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams).\n \n - flags: `'r'`\n - options?:\n - mode?: `'0o666'` Sets the file mode (permission and sticky bits) if the file is created. */\nexport async function fopen (\n fp: string,\n flags: string | number,\n { mode, print }: { mode?: fs.Mode, print?: boolean } = { }\n) {\n if (print)\n console.log('open file:', fp)\n return Object.assign(\n await fsp.open(fp, flags, mode),\n { fp, flags, mode }\n )\n}\n\n\nexport function create_mfs () {\n let mfs = MFS.createFsFromVolume(\n new MFS.Volume()\n )\n \n mfs.join = path.join.bind(path)\n mfs.is_mfs = true\n \n return mfs\n}\n\n\nexport async function fread (fp: string): Promise<string>\nexport async function fread (fp: string, { dir, encoding, print }?: { dir?: string, encoding: 'binary', print?: boolean }): Promise<Buffer>\nexport async function fread (fp: string, { dir, encoding, print }?: { dir?: string, encoding?: Encoding | 'auto', print?: boolean }): Promise<string>\nexport async function fread (fp: string, {\n dir, \n encoding = 'utf-8', \n print = true\n}: {\n dir?: string\n encoding?: Encoding | 'auto'\n print?: boolean } = { }\n) {\n if (dir)\n fp = path.join(dir, fp)\n else if (!path.isAbsolute(fp))\n throw new Error(`fp must be absolute path, or pass in 'dir' parameter: ${fp}`)\n \n if (print)\n console.log(`read: ${fp}`)\n \n switch (encoding) {\n case 'utf-8':\n return fsp.readFile(fp, { encoding: 'utf-8' })\n \n case 'binary':\n return fsp.readFile(fp)\n \n default: {\n const buffer = await fsp.readFile(fp)\n \n if (encoding === 'auto') {\n const { detect } = await import('chardet')\n encoding = detect(buffer) as any\n if (print)\n console.log(`The encoding of '${fp}' might be ${encoding.toLowerCase()}`)\n }\n \n return iconv.decode(buffer, encoding)\n }\n }\n}\n\nexport async function fread_lines (fp: string, options: { dir?: string, encoding?: Exclude<Encoding, 'binary'> | 'auto', print?: boolean } = { }) {\n return (await fread(fp, options))\n .split_lines()\n}\n\nexport async function fread_json <T = any> (fp: string, options: { dir?: string, encoding?: Encoding, print?: boolean } = { }): Promise<T> {\n return JSON.parse(await fread(fp, options))\n}\n\n\nexport async function fwrite (fp: string | FileHandle, data: Buffer, options?: { dir?: string, print?: boolean, mkdir?: boolean }): Promise<void>\nexport async function fwrite (fp: string | FileHandle, data: any, options?: { dir?: string, encoding?: Encoding, print?: boolean, mkdir?: boolean }): Promise<void>\nexport async function fwrite (\n fp: string | FileHandle,\n data: any,\n {\n dir,\n encoding = 'utf-8',\n print = true,\n mkdir = false,\n }: {\n dir?: string\n encoding?: Encoding\n print?: boolean\n mkdir?: boolean\n } = { }\n) {\n const is_handle = typeof fp === 'object' && fp && 'fd' in fp\n if (is_handle) {\n if (print)\n console.log('write:', (fp as FileHandle).fp)\n } else {\n if (dir)\n fp = path.join(dir, fp)\n else if (!path.isAbsolute(fp as string))\n throw new Error(`fp must be an absolute path, or pass in the 'dir' parameter: ${fp}`)\n \n if (print)\n console.log('write:', fp)\n }\n \n if (encoding === 'gb18030')\n data = iconv.encode(data, encoding)\n \n if (!Buffer.isBuffer(data) && typeof data !== 'string')\n data = to_json(data)\n \n try {\n await fsp.writeFile(fp, data)\n } catch (error) {\n if (!mkdir || error.code !== 'ENOENT' || is_handle)\n throw error\n \n await fmkdir((fp as string).fdir)\n await fsp.writeFile(fp, data)\n }\n}\n\nexport async function fappend (fp: string, data: any, { dir, print = true }: { dir?: string, print?: boolean } = { }) {\n if (dir)\n fp = path.join(dir, fp)\n else if (!path.isAbsolute(fp))\n throw new Error(`fp must be an absolute path, or pass in the 'dir' parameter: ${fp}`)\n \n if (print)\n console.log('append:', fp)\n \n if (!Buffer.isBuffer(data) && typeof data !== 'string')\n throw new Error('data is not Buffer or string')\n \n await fsp.appendFile(fp, data)\n}\n\n\n/**\n - fpd: absolute path of directory\n - optoins?:\n - deep?: `false` recursively\n - absolute?: `false` Return, print full path instead of relative path\n - print?: `true`\n - filter?: `true` RegExp | (fp: string) => any, Note that when deep = true, \n directories and files in directories that are filtered out by the filter will not be included in the results */\nexport async function flist (\n fpd: string,\n options: {\n filter?: RegExp | ((fp: string) => any)\n deep?: boolean\n absolute?: boolean\n print?: boolean\n } = { }\n): Promise<string[]> {\n const {\n filter,\n deep = false,\n absolute = false,\n print = true,\n } = options\n \n if (!path.isAbsolute(fpd))\n throw new Error(`fpd (${fpd}) must be absolute path`)\n \n if (!fpd.endsWith('/'))\n throw new Error(`Argument fpd (${fpd}) must end with /`)\n \n // readdir withFileTypes 参数在底层有什么区别,速度上有什么差异\n // 都调用了 uv_fs_scandir, 且调用参数相同,仅仅是 Node.js 侧的回调不同 AfterScanDir / AfterScanDirWithTypes\n // 回调中通过 uv_fs_scandir_next 获取到每个条目的信息,而 uv_fs_scandir_next 中都会读取 type\n // 速度上:都在 0.2 ms 左右就可以完成\n \n const files = await fsp.readdir(fpd, {\n withFileTypes: true,\n encoding: 'utf-8',\n })\n \n const filter_regexp = filter instanceof RegExp\n const filter_fn = Boolean(filter && !filter_regexp)\n \n let fps: string[] = [ ]\n \n for (const file of files) {\n const fp = \n (absolute ? fpd : '') +\n file.name +\n (file.isDirectory() ? '/' : '')\n \n if (filter_regexp && !filter.test(fp))\n continue\n \n if (filter_fn && !(filter as Function)(fp))\n continue\n \n if (print)\n console.log(fp)\n \n fps.push(fp)\n }\n \n return deep ? (\n await Promise.all(\n fps.map(async fp => \n fp.endsWith('/') ?\n [\n fp,\n ... (await flist(\n absolute ? fp : fpd + fp,\n options\n )).map(fp_ => \n absolute ? fp_ : fp + fp_)\n ]\n :\n fp)\n )\n ).flat()\n :\n fps\n}\n\n\nexport async function fstat (fp: string) {\n if (!path.isAbsolute(fp))\n throw new Error(`fp must be absolute path: ${fp}`)\n \n return fsp.stat(fp, { bigint: true })\n}\n\n\n/** delete files or folders \n - fp: file path\n - options?:\n - print?: `true`\n \n Returns whether the delete operation actually took place */\nexport async function fdelete (fp: string, { print = true }: { print?: boolean } = { }) {\n if (fp.length < 6)\n throw new Error(`${fp} 太短`)\n \n if (!path.isAbsolute(fp))\n throw new Error('fp must be an absolute path')\n \n try {\n await fsp.rm(fp, { recursive: true })\n if (print)\n if (fp.endsWith('/'))\n console.log(`deleted folder: ${fp}`.red)\n else\n console.log(`deleted file: ${fp}`.red)\n return true\n } catch (error) {\n if (error.code === 'ENOENT') {\n if (print)\n if (fp.endsWith('/'))\n console.log(`folder no longer exists: ${fp}`)\n else\n console.log(`file no longer exists: ${fp}`)\n return false\n }\n \n throw error\n }\n}\n\n\n/** copy file or direcotry\n - src: src file/directory absolute path\n - dst: dst file/directory absolute path\n @example\n fcopy('d:/temp/camera/', 'd:/camera/') */\nexport async function fcopy (fp_src: string, fp_dst: string, {\n print = true,\n overwrite = true,\n}: {\n print?: boolean\n overwrite?: boolean\n} = { }) {\n if (fp_src.endsWith('/') !== fp_dst.endsWith('/'))\n throw new Error('fp_src and fp_dst must be both file path or directory path')\n \n if (!path.isAbsolute(fp_src) || !path.isAbsolute(fp_dst))\n throw new Error('fp_src and fp_dst must be absolute path')\n \n if (print)\n console.log(`copy: ${fp_src} → ${fp_dst}`)\n \n await fse.copy(fp_src, fp_dst, { overwrite, errorOnExist: true })\n}\n\n\n/** move file or direcotry\n - src: src file/directory absolute path\n - dst: dst file/directory absolute path\n @example\n fmove('d:/temp/camera/', 'd:/camera/') */\nexport async function fmove (src: string, dst: string, {\n overwrite = false,\n print = true\n}: {\n overwrite?: boolean\n print?: boolean\n} = { }) {\n if (src.endsWith('/') !== dst.endsWith('/'))\n throw new Error('src and dst must be both file path or directory path')\n \n if (!path.isAbsolute(src) || !path.isAbsolute(dst))\n throw new Error('src and dst must be absolute path')\n \n if (print)\n console.log(`move: ${src} → ${dst}`)\n \n await fse.move(src, dst, { overwrite })\n}\n\n\n/** rename file \n - fp: current filename/path\n - fp_: new filename/path\n - options?:\n - fpd?: fp and fp_ is in same directory\n - print?: `true`\n - overwrite?: `true` better performance without check */\nexport async function frename (\n fp: string, \n fp_: string,\n {\n fpd,\n print = true,\n overwrite = true\n }: {\n fpd?: string\n print?: boolean\n overwrite?: boolean\n } = { }\n) {\n if (fpd) {\n fp = path.join(fpd, fp)\n fp_ = path.join(fpd, fp_)\n } else if (!path.isAbsolute(fp) || !path.isAbsolute(fp_))\n throw new Error('fp and fp_ must be absolute path')\n \n if (print)\n console.log('rename:', fp, '→', fp_)\n \n if (!overwrite && fexists(fp_))\n throw new Error(`file already exists:${fp_}`)\n \n await fsp.rename(fp, fp_)\n}\n\n\n/**\n Create folders recursively, make sure the folder pointed to by fpd exists \n Returns the first created folder or undefined\n \n - fpd: Folder full path\n - options?:\n - print?: `true`\n - mode?: `'0o777'` */\nexport async function fmkdir (\n fpd: string,\n {\n print = true,\n mode,\n }: {\n print?: boolean\n \n /** `0o777` A file mode. If a string is passed, it is parsed as an octal integer. */\n mode?: string | number\n } = { }\n) {\n if (!path.isAbsolute(fpd))\n throw new Error(`fpd must be an absolute path: ${fpd}`)\n \n if (!fpd.endsWith('/'))\n throw new Error(`fpd must end with /: ${fpd}`)\n \n // CallingfsPromises.mkdir() when path is a directory that exists results in a rejection only when recursive is false.\n const fpd_ = (\n await fsp.mkdir(fpd, { recursive: true, mode })\n )?.replaceAll('\\\\', '/')\n \n if (fpd_) {\n if (print)\n console.log('folder created:', fpd)\n } else\n if (print)\n console.log('folder already exists:', fpd)\n \n return fpd_\n}\n\n\n/** Create soft links \n - fp_real: current real file/directory path\n - fp_link: target file/directory path */\nexport async function flink (\n fp_real: string, \n fp_link: string, \n {\n junction = false,\n print = true \n }: { \n junction?: boolean\n print?: boolean\n} = { }) {\n if (!path.isAbsolute(fp_real) || !path.isAbsolute(fp_link))\n throw new Error('fp must be absolute path')\n \n const is_fpd_real = fp_real.endsWith('/')\n const is_fpd_link = fp_link.endsWith('/')\n \n if (is_fpd_real !== is_fpd_link)\n throw new Error('fp_real and fp_link must be both file path or folder path')\n \n if (fexists(fp_link))\n throw new Error(`${ is_fpd_link ? 'folder' : 'file' } exists: ${fp_link}, could not create link`)\n \n if (print)\n console.log(`source file ${fp_real} linked to ${fp_link}`)\n \n if (junction)\n fsp.symlink(fp_real, fp_link, 'junction')\n else\n fsp.symlink(fp_real, fp_link, is_fpd_real ? 'dir' : 'file')\n}\n\n\nexport let fwatchers: Record<string, fs.FSWatcher> = { }\n\n/**\n - fp: path of file or directory\n - callback: called when modified\n - exec: call callback when watch is executed\n \n save fs.FSWatcher in watchers, subsequent call will auto close existing watcher for the same fp\n \n https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_watch_filename_options_listener \n The listener callback gets two arguments (event, fname). \n event is either 'rename' or 'change', and filename is the name of the file which triggered the event.\n On most platforms, 'rename' is emitted whenever a filename appears or disappears in the directory.\n \n The listener callback is attached to the 'change' event fired by fs.FSWatcher, but it is not the same thing as the 'change' value of event. */\nexport async function fwatch (\n fp: string,\n onchange: (event: string, fname: string) => any, \n { exec = true }: { exec?: boolean } = { }\n) {\n if (!path.isAbsolute(fp))\n throw new Error('fp must be absolute path')\n \n const _watcher = fwatchers[fp]\n if (_watcher)\n _watcher.close()\n \n if (exec)\n await onchange('change', fp.fname)\n \n const start = new Date().getTime()\n \n const debounced_onchange = debounce(\n (event, fname) => {\n if (new Date().getTime() - start < 800)\n return\n console.log(`file ${event}: ${fname}`)\n onchange(event, path.normalize(fname))\n },\n 500,\n { leading: false, trailing: true }\n )\n \n let watcher = fs.watch(fp, debounced_onchange)\n watcher.on('error', error => {\n console.error(error)\n })\n return fwatchers[fp] = watcher\n}\n\n\n/** open a file and replace certain pattern */\nexport async function freplace (fp: string, pattern: string | RegExp, replacement: string) {\n await fwrite(\n fp,\n (await fread(fp))\n .replaceAll(pattern, replacement)\n )\n}\n\n/** convert file encoding to UTF-8\n - fp: file absolute path\n - options?:\n - dryrun?: `true`\n - encoding?: `'auto'` */\nexport async function f2utf8 (fp: string, {\n dryrun = true,\n encoding = 'auto',\n}: {\n dryrun?: boolean\n encoding?: Encoding | 'auto'\n} = { }) {\n const text = await fread(fp, { encoding })\n if (dryrun) {\n console.log(text.slice(0, 10000))\n return\n }\n \n const fp_bak = `${fp.fdir}${fp.fname.replace(/(.*?)(\\.[^.]+)?$/, '$1.bak$2')}`\n if (!fexists(fp_bak))\n await fcopy(fp, fp_bak)\n \n await fwrite(fp, text)\n}\n\n"]}
package/i18n/dict.d.ts CHANGED
@@ -20,7 +20,7 @@ export declare class Dict {
20
20
  };
21
21
  }
22
22
  /** 配置字段 */
23
- export declare type Item = {
23
+ export type Item = {
24
24
  [language in Language]?: string;
25
25
  };
26
26
  /** JSON.parse(词典文件.json) 得到的对象 */
package/i18n/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { type i18n as I18Next } from 'i18next';
2
2
  import type { Trans } from 'react-i18next';
3
3
  import { type _Dict, type Item } from './dict.js';
4
- export declare type Language = 'zh' | 'en' | 'ja' | 'ko';
4
+ export type Language = 'zh' | 'en' | 'ja' | 'ko';
5
5
  export declare const LANGUAGES: readonly ["zh", "en", "ja", "ko"];
6
6
  declare global {
7
7
  interface Window {
@@ -35,7 +35,7 @@ declare const DEFAULT_CONFIG: {
35
35
  suffix: string;
36
36
  };
37
37
  };
38
- export declare type Config = Partial<(typeof DEFAULT_CONFIG) & {
38
+ export type Config = Partial<(typeof DEFAULT_CONFIG) & {
39
39
  defaultValue?: string;
40
40
  resource?: {
41
41
  loadPath?: string;
package/net.browser.d.ts CHANGED
@@ -43,7 +43,7 @@ export declare function connect_websocket(url: string | URL, { protocols, on_clo
43
43
  - 数组: 会自动被封装为 { id: 相同, data: 返回值, done: true } 这样的消息并调用 websocket.send 将其发送
44
44
  - void: 什么都不做
45
45
  - 以上的 promise */
46
- export declare type MessageHandler = (message: Message, websocket?: WebSocket) => void | any[] | Promise<void | any[]>;
46
+ export type MessageHandler = (message: Message, websocket?: WebSocket) => void | any[] | Promise<void | any[]>;
47
47
  /** 二进制消息格式
48
48
  - json.length (小端序): 4 字节
49
49
  - json 数据
package/net.browser.js CHANGED
@@ -92,7 +92,7 @@ export async function connect_websocket(url, { protocols, on_close, on_error, on
92
92
  on_close?.(event, websocket);
93
93
  });
94
94
  websocket.addEventListener('error', event => {
95
- const message = `${new Date().toLocaleTimeString()} websocket errored: ${websocket.url}`;
95
+ const message = `${new Date().toLocaleTimeString()}: websocket errored: ${websocket.url}`;
96
96
  try {
97
97
  on_error?.(event, websocket);
98
98
  reject(Object.assign(new Error(message), { event }));
@@ -273,7 +273,12 @@ export class Remote {
273
273
  resolve(data);
274
274
  this.handlers.delete(id);
275
275
  });
276
- await this.send({ id, func, data: args }); // 不需要 done: true, 因为对面的 remote.handlers 中不会有这个 id 的 handler
276
+ try {
277
+ await this.send({ id, func, data: args }); // 不需要 done: true, 因为对面的 remote.handlers 中不会有这个 id 的 handler
278
+ }
279
+ catch (error) {
280
+ reject(error);
281
+ }
277
282
  });
278
283
  }
279
284
  }