xshell 0.0.54 → 0.0.57
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/chalk.browser.d.ts +1 -1
- package/chalk.browser.js +3 -19
- package/chalk.browser.js.map +1 -1
- package/file.d.ts +10 -9
- package/file.js +95 -114
- package/file.js.map +1 -1
- package/i18n/dict.js +1 -5
- package/i18n/dict.js.map +1 -1
- package/i18n/i18n-scan.js +9 -12
- package/i18n/i18n-scan.js.map +1 -1
- package/i18n/index.js +11 -16
- package/i18n/index.js.map +1 -1
- package/i18n/rwdict.js +3 -7
- package/i18n/rwdict.js.map +1 -1
- package/i18n/scanner/checker.js +6 -11
- package/i18n/scanner/checker.js.map +1 -1
- package/i18n/scanner/index.js +39 -44
- package/i18n/scanner/index.js.map +1 -1
- package/i18n/scanner/parser.js +15 -19
- package/i18n/scanner/parser.js.map +1 -1
- package/i18n/utils.js +3 -7
- package/i18n/utils.js.map +1 -1
- package/index.js +5 -8
- package/index.js.map +1 -1
- package/net.browser.js +6 -13
- package/net.browser.js.map +1 -1
- package/net.d.ts +2 -2
- package/net.js +50 -63
- package/net.js.map +1 -1
- package/package.json +24 -26
- package/process.d.ts +3 -3
- package/process.js +18 -23
- package/process.js.map +1 -1
- package/prototype.browser.js +24 -32
- package/prototype.browser.js.map +1 -1
- package/prototype.d.ts +1 -1
- package/prototype.js +37 -46
- package/prototype.js.map +1 -1
- package/repl.js +66 -80
- package/repl.js.map +1 -1
- package/server.d.ts +3 -3
- package/server.js +32 -37
- package/server.js.map +1 -1
- package/toaster.browser.js +6 -9
- package/toaster.browser.js.map +1 -1
- package/toaster.sass +1 -1
- package/tsconfig.json +3 -3
- package/ufs.d.ts +1 -1
- package/ufs.js +5 -9
- package/ufs.js.map +1 -1
- package/utils.browser.js +4 -11
- package/utils.browser.js.map +1 -1
- package/utils.d.ts +3 -3
- package/utils.js +41 -62
- package/utils.js.map +1 -1
- package/xshell.js +2 -4
- package/xshell.js.map +1 -1
package/chalk.browser.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const red: import("chalk").
|
|
1
|
+
declare const red: import("chalk").ChalkInstance, green: import("chalk").ChalkInstance, yellow: import("chalk").ChalkInstance, blue: import("chalk").ChalkInstance, magenta: import("chalk").ChalkInstance, cyan: import("chalk").ChalkInstance, grey: import("chalk").ChalkInstance, red_: import("chalk").ChalkInstance, green_: import("chalk").ChalkInstance, yellow_: import("chalk").ChalkInstance, blue_: import("chalk").ChalkInstance, magenta_: import("chalk").ChalkInstance, cyan_: import("chalk").ChalkInstance, underline: import("chalk").ChalkInstance;
|
|
2
2
|
export { red, green, yellow, blue, magenta, cyan, grey, red_, green_, yellow_, blue_, magenta_, cyan_, underline };
|
package/chalk.browser.js
CHANGED
|
@@ -1,21 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.underline = exports.cyan_ = exports.magenta_ = exports.blue_ = exports.yellow_ = exports.green_ = exports.red_ = exports.grey = exports.cyan = exports.magenta = exports.blue = exports.yellow = exports.green = exports.red = void 0;
|
|
4
|
-
const chalk_1 = require("chalk");
|
|
5
|
-
const chalk = new chalk_1.Instance({ level: 2 });
|
|
1
|
+
import { Chalk } from 'chalk';
|
|
2
|
+
const chalk = new Chalk({ level: 2 });
|
|
6
3
|
const { red, green, yellow, blue, magenta, cyan, grey, redBright: red_, greenBright: green_, yellowBright: yellow_, blueBright: blue_, magentaBright: magenta_, cyanBright: cyan_, underline, } = chalk;
|
|
7
|
-
|
|
8
|
-
exports.green = green;
|
|
9
|
-
exports.yellow = yellow;
|
|
10
|
-
exports.blue = blue;
|
|
11
|
-
exports.magenta = magenta;
|
|
12
|
-
exports.cyan = cyan;
|
|
13
|
-
exports.grey = grey;
|
|
14
|
-
exports.red_ = red_;
|
|
15
|
-
exports.green_ = green_;
|
|
16
|
-
exports.yellow_ = yellow_;
|
|
17
|
-
exports.blue_ = blue_;
|
|
18
|
-
exports.magenta_ = magenta_;
|
|
19
|
-
exports.cyan_ = cyan_;
|
|
20
|
-
exports.underline = underline;
|
|
4
|
+
export { red, green, yellow, blue, magenta, cyan, grey, red_, green_, yellow_, blue_, magenta_, cyan_, underline };
|
|
21
5
|
//# sourceMappingURL=chalk.browser.js.map
|
package/chalk.browser.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chalk.browser.js","sourceRoot":"","sources":["chalk.browser.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"chalk.browser.js","sourceRoot":"","sources":["chalk.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAA;AAE7B,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAA;AAErC,MAAM,EACF,GAAG,EACH,KAAK,EACL,MAAM,EACN,IAAI,EACJ,OAAO,EACP,IAAI,EACJ,IAAI,EAEJ,SAAS,EAAE,IAAI,EACf,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,OAAO,EACrB,UAAU,EAAE,KAAK,EACjB,aAAa,EAAE,QAAQ,EACvB,UAAU,EAAE,KAAK,EAEjB,SAAS,GACZ,GAAG,KAAK,CAAA;AAET,OAAO,EACH,GAAG,EACH,KAAK,EACL,MAAM,EACN,IAAI,EACJ,OAAO,EACP,IAAI,EACJ,IAAI,EAEJ,IAAI,EACJ,MAAM,EACN,OAAO,EACP,KAAK,EACL,QAAQ,EACR,KAAK,EAEL,SAAS,EACZ,CAAA","sourcesContent":["import { Chalk } from 'chalk'\n\nconst chalk = new Chalk({ level: 2 })\n\nconst {\n red, \n green, \n yellow, \n blue, \n magenta, \n cyan, \n grey,\n \n redBright: red_, \n greenBright: green_, \n yellowBright: yellow_, \n blueBright: blue_, \n magentaBright: magenta_,\n cyanBright: cyan_,\n \n underline,\n} = chalk\n\nexport {\n red,\n green,\n yellow,\n blue,\n magenta,\n cyan,\n grey,\n \n red_,\n green_,\n yellow_,\n blue_,\n magenta_,\n cyan_,\n \n underline\n}\n"]}
|
package/file.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
/// <reference types="node" />
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
4
4
|
import { promises as fsp, default as fs } from 'fs';
|
|
5
5
|
declare type FileHandle = fsp.FileHandle & {
|
|
6
6
|
fp: string;
|
|
@@ -91,15 +91,16 @@ export declare function flist(fpd: string, options?: {
|
|
|
91
91
|
print?: boolean;
|
|
92
92
|
}): Promise<string[]>;
|
|
93
93
|
export declare function fstat(fp: string): Promise<fs.BigIntStats>;
|
|
94
|
-
/** delete
|
|
95
|
-
- fp: path
|
|
94
|
+
/** delete files or folders
|
|
95
|
+
- fp: file path
|
|
96
96
|
- options?:
|
|
97
|
-
- print?: `true`
|
|
97
|
+
- print?: `true`
|
|
98
|
+
|
|
99
|
+
Returns whether the delete operation actually took place
|
|
98
100
|
*/
|
|
99
|
-
export declare function fdelete(fp: string, { print
|
|
101
|
+
export declare function fdelete(fp: string, { print }?: {
|
|
100
102
|
print?: boolean;
|
|
101
|
-
|
|
102
|
-
}): Promise<void>;
|
|
103
|
+
}): Promise<boolean>;
|
|
103
104
|
/** copy file or direcotry
|
|
104
105
|
- src: src file/directory absolute path
|
|
105
106
|
- dst: dst file/directory absolute path
|
package/file.js
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const debounce_js_1 = tslib_1.__importDefault(require("lodash/debounce.js"));
|
|
11
|
-
const memfs_1 = tslib_1.__importDefault(require("memfs"));
|
|
12
|
-
exports.MFS = memfs_1.default;
|
|
13
|
-
const prototype_js_1 = require("./prototype.js");
|
|
14
|
-
tslib_1.__exportStar(require("./ufs.js"), exports);
|
|
1
|
+
import { promises as fsp, default as fs, } from 'fs';
|
|
2
|
+
import path from 'upath';
|
|
3
|
+
import iconv from 'iconv-lite';
|
|
4
|
+
import fse from 'fs-extra';
|
|
5
|
+
import debounce from 'lodash/debounce.js';
|
|
6
|
+
import MFS from 'memfs';
|
|
7
|
+
import { to_json } from './prototype.js';
|
|
8
|
+
export * from './ufs.js';
|
|
9
|
+
export { MFS };
|
|
15
10
|
/** Does the file/folder pointed to by fp exist? */
|
|
16
|
-
function fexists(fp, { print = true } = {}) {
|
|
17
|
-
const exists =
|
|
11
|
+
export function fexists(fp, { print = true } = {}) {
|
|
12
|
+
const exists = fs.existsSync(fp);
|
|
18
13
|
if (print)
|
|
19
14
|
console.log(exists ? 'exists:' : 'not exists:', fp);
|
|
20
15
|
return exists;
|
|
21
16
|
}
|
|
22
|
-
exports.fexists = fexists;
|
|
23
17
|
/**
|
|
24
18
|
open file, return FileHandle
|
|
25
19
|
Some characters (`< > : " / \ | ? *`) are reserved under Windows as documented
|
|
@@ -30,54 +24,49 @@ exports.fexists = fexists;
|
|
|
30
24
|
- options?:
|
|
31
25
|
- mode?: `'0o666'` Sets the file mode (permission and sticky bits) if the file is created.
|
|
32
26
|
*/
|
|
33
|
-
async function fopen(fp, flags, { mode, print } = {}) {
|
|
27
|
+
export async function fopen(fp, flags, { mode, print } = {}) {
|
|
34
28
|
if (print)
|
|
35
29
|
console.log('fopen:', fp);
|
|
36
|
-
return Object.assign(await
|
|
30
|
+
return Object.assign(await fsp.open(fp, flags, mode), {
|
|
37
31
|
fp,
|
|
38
32
|
flags,
|
|
39
33
|
mode
|
|
40
34
|
});
|
|
41
35
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
mfs.join = upath_1.default.join.bind(upath_1.default);
|
|
36
|
+
export function create_mfs() {
|
|
37
|
+
let mfs = MFS.createFsFromVolume(new MFS.Volume());
|
|
38
|
+
mfs.join = path.join.bind(path);
|
|
46
39
|
mfs.is_mfs = true;
|
|
47
40
|
return mfs;
|
|
48
41
|
}
|
|
49
|
-
|
|
50
|
-
async function fread(fp, { dir, encoding = 'utf-8', print = true } = {}) {
|
|
42
|
+
export async function fread(fp, { dir, encoding = 'utf-8', print = true } = {}) {
|
|
51
43
|
if (dir)
|
|
52
|
-
fp =
|
|
53
|
-
else if (!
|
|
44
|
+
fp = path.join(dir, fp);
|
|
45
|
+
else if (!path.isAbsolute(fp))
|
|
54
46
|
throw new Error('fp must be absolute path, or pass in \'dir\' parameter');
|
|
55
47
|
if (print)
|
|
56
48
|
console.log(`read: ${fp}`);
|
|
57
|
-
const buffer = await
|
|
49
|
+
const buffer = await fsp.readFile(fp);
|
|
58
50
|
if (encoding === 'binary')
|
|
59
51
|
return buffer;
|
|
60
52
|
if (encoding === 'utf-8')
|
|
61
53
|
return buffer.toString('utf8');
|
|
62
54
|
if (encoding === 'auto') {
|
|
63
|
-
const { detect } = await
|
|
55
|
+
const { detect } = await import('chardet');
|
|
64
56
|
encoding = detect(buffer);
|
|
65
57
|
if (print)
|
|
66
58
|
console.log(`${fp} probably has encoding: ${encoding.toLowerCase()}`);
|
|
67
59
|
}
|
|
68
|
-
return
|
|
60
|
+
return iconv.decode(buffer, encoding);
|
|
69
61
|
}
|
|
70
|
-
|
|
71
|
-
async function fread_lines(fp, options = {}) {
|
|
62
|
+
export async function fread_lines(fp, options = {}) {
|
|
72
63
|
return (await fread(fp, options))
|
|
73
64
|
.split_lines();
|
|
74
65
|
}
|
|
75
|
-
|
|
76
|
-
async function fread_json(fp, options = {}) {
|
|
66
|
+
export async function fread_json(fp, options = {}) {
|
|
77
67
|
return JSON.parse(await fread(fp, options));
|
|
78
68
|
}
|
|
79
|
-
|
|
80
|
-
async function fwrite(fp, data, { dir, encoding = 'utf-8', print = true, mkdir = false, } = {}) {
|
|
69
|
+
export async function fwrite(fp, data, { dir, encoding = 'utf-8', print = true, mkdir = false, } = {}) {
|
|
81
70
|
const is_handle = typeof fp === 'object' && fp && 'fd' in fp;
|
|
82
71
|
if (is_handle) {
|
|
83
72
|
if (print)
|
|
@@ -85,39 +74,37 @@ async function fwrite(fp, data, { dir, encoding = 'utf-8', print = true, mkdir =
|
|
|
85
74
|
}
|
|
86
75
|
else {
|
|
87
76
|
if (dir)
|
|
88
|
-
fp =
|
|
89
|
-
else if (!
|
|
77
|
+
fp = path.join(dir, fp);
|
|
78
|
+
else if (!path.isAbsolute(fp))
|
|
90
79
|
throw new Error('fp must be absolute path, or pass in "dir" parameter');
|
|
91
80
|
if (print)
|
|
92
81
|
console.log('write:', fp);
|
|
93
82
|
}
|
|
94
83
|
if (encoding === 'gb18030')
|
|
95
|
-
data =
|
|
84
|
+
data = iconv.encode(data, encoding);
|
|
96
85
|
if (!Buffer.isBuffer(data) && typeof data !== 'string')
|
|
97
|
-
data =
|
|
86
|
+
data = to_json(data);
|
|
98
87
|
try {
|
|
99
|
-
await
|
|
88
|
+
await fsp.writeFile(fp, data);
|
|
100
89
|
}
|
|
101
90
|
catch (error) {
|
|
102
91
|
if (!mkdir || error.code !== 'ENOENT' || is_handle)
|
|
103
92
|
throw error;
|
|
104
93
|
await fmkdir(fp.fdir);
|
|
105
|
-
await
|
|
94
|
+
await fsp.writeFile(fp, data);
|
|
106
95
|
}
|
|
107
96
|
}
|
|
108
|
-
|
|
109
|
-
async function fappend(fp, data, { dir, print = true } = {}) {
|
|
97
|
+
export async function fappend(fp, data, { dir, print = true } = {}) {
|
|
110
98
|
if (dir)
|
|
111
|
-
fp =
|
|
112
|
-
else if (!
|
|
99
|
+
fp = path.join(dir, fp);
|
|
100
|
+
else if (!path.isAbsolute(fp))
|
|
113
101
|
throw new Error('fp must be absolute path, or pass in "dir" parameter');
|
|
114
102
|
if (print)
|
|
115
103
|
console.log('append:', fp);
|
|
116
104
|
if (!Buffer.isBuffer(data) && typeof data !== 'string')
|
|
117
105
|
throw new Error('data is not Buffer or string');
|
|
118
|
-
await
|
|
106
|
+
await fsp.appendFile(fp, data);
|
|
119
107
|
}
|
|
120
|
-
exports.fappend = fappend;
|
|
121
108
|
/**
|
|
122
109
|
- fpd: absolute path of directory
|
|
123
110
|
- optoins?:
|
|
@@ -127,9 +114,9 @@ exports.fappend = fappend;
|
|
|
127
114
|
- filter?: `true` RegExp | (fp: string) => any, Note that when deep = true,
|
|
128
115
|
directories and files in directories that are filtered out by the filter will not be included in the results
|
|
129
116
|
*/
|
|
130
|
-
async function flist(fpd, options = {}) {
|
|
117
|
+
export async function flist(fpd, options = {}) {
|
|
131
118
|
const { filter, deep = false, absolute = false, print = true, } = options;
|
|
132
|
-
if (!
|
|
119
|
+
if (!path.isAbsolute(fpd))
|
|
133
120
|
throw new Error(`fpd (${fpd}) must be absolute path`);
|
|
134
121
|
if (!fpd.endsWith('/'))
|
|
135
122
|
throw new Error(`Argument fpd (${fpd}) must end with /`);
|
|
@@ -137,7 +124,7 @@ async function flist(fpd, options = {}) {
|
|
|
137
124
|
// 都调用了 uv_fs_scandir, 且调用参数相同,仅仅是 Node.js 侧的回调不同 AfterScanDir / AfterScanDirWithTypes
|
|
138
125
|
// 回调中通过 uv_fs_scandir_next 获取到每个条目的信息,而 uv_fs_scandir_next 中都会读取 type
|
|
139
126
|
// 速度上:都在 0.2 ms 左右就可以完成
|
|
140
|
-
const files = await
|
|
127
|
+
const files = await fsp.readdir(fpd, {
|
|
141
128
|
withFileTypes: true,
|
|
142
129
|
encoding: 'utf-8',
|
|
143
130
|
});
|
|
@@ -166,74 +153,74 @@ async function flist(fpd, options = {}) {
|
|
|
166
153
|
:
|
|
167
154
|
fps;
|
|
168
155
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (!upath_1.default.isAbsolute(fp))
|
|
156
|
+
export async function fstat(fp) {
|
|
157
|
+
if (!path.isAbsolute(fp))
|
|
172
158
|
throw new Error(`fp (${fp}) must be absolute path`);
|
|
173
|
-
return
|
|
159
|
+
return fsp.stat(fp, { bigint: true });
|
|
174
160
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
- fp: path
|
|
161
|
+
/** delete files or folders
|
|
162
|
+
- fp: file path
|
|
178
163
|
- options?:
|
|
179
|
-
- print?: `true`
|
|
164
|
+
- print?: `true`
|
|
165
|
+
|
|
166
|
+
Returns whether the delete operation actually took place
|
|
180
167
|
*/
|
|
181
|
-
async function fdelete(fp, { print = true
|
|
168
|
+
export async function fdelete(fp, { print = true } = {}) {
|
|
182
169
|
if (fp.length < 6)
|
|
183
|
-
throw new Error(`${fp}
|
|
184
|
-
if (!
|
|
185
|
-
throw new Error('
|
|
186
|
-
|
|
170
|
+
throw new Error(`${fp} 太短`);
|
|
171
|
+
if (!path.isAbsolute(fp))
|
|
172
|
+
throw new Error('fp must be an absolute path');
|
|
173
|
+
try {
|
|
174
|
+
await fsp.rm(fp, { recursive: true });
|
|
187
175
|
if (print)
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
else
|
|
194
|
-
resolve();
|
|
195
|
-
});
|
|
196
|
-
});
|
|
176
|
+
if (fp.endsWith('/'))
|
|
177
|
+
console.log(`deleted folder: ${fp}`.red);
|
|
178
|
+
else
|
|
179
|
+
console.log(`deleted file: ${fp}`.red);
|
|
180
|
+
return true;
|
|
197
181
|
}
|
|
198
|
-
|
|
199
|
-
if (
|
|
200
|
-
|
|
201
|
-
|
|
182
|
+
catch (error) {
|
|
183
|
+
if (error.code === 'ENOENT') {
|
|
184
|
+
if (print)
|
|
185
|
+
if (fp.endsWith('/'))
|
|
186
|
+
console.log(`folder no longer exists: ${fp}`);
|
|
187
|
+
else
|
|
188
|
+
console.log(`file no longer exists: ${fp}`);
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
throw error;
|
|
202
192
|
}
|
|
203
193
|
}
|
|
204
|
-
exports.fdelete = fdelete;
|
|
205
194
|
/** copy file or direcotry
|
|
206
195
|
- src: src file/directory absolute path
|
|
207
196
|
- dst: dst file/directory absolute path
|
|
208
197
|
@example
|
|
209
198
|
fcopy('d:/temp/camera/', 'd:/camera/')
|
|
210
199
|
*/
|
|
211
|
-
async function fcopy(fp_src, fp_dst, { print = true, overwrite = true, } = {}) {
|
|
200
|
+
export async function fcopy(fp_src, fp_dst, { print = true, overwrite = true, } = {}) {
|
|
212
201
|
if (fp_src.endsWith('/') !== fp_dst.endsWith('/'))
|
|
213
202
|
throw new Error('fp_src and fp_dst must be both file path or directory path');
|
|
214
|
-
if (!
|
|
203
|
+
if (!path.isAbsolute(fp_src) || !path.isAbsolute(fp_dst))
|
|
215
204
|
throw new Error('fp_src and fp_dst must be absolute path');
|
|
216
205
|
if (print)
|
|
217
206
|
console.log(`copy: ${fp_src} → ${fp_dst}`);
|
|
218
|
-
await
|
|
207
|
+
await fse.copy(fp_src, fp_dst, { overwrite, errorOnExist: true });
|
|
219
208
|
}
|
|
220
|
-
exports.fcopy = fcopy;
|
|
221
209
|
/** move file or direcotry
|
|
222
210
|
- src: src file/directory absolute path
|
|
223
211
|
- dst: dst file/directory absolute path
|
|
224
212
|
@example
|
|
225
213
|
fmove('d:/temp/camera/', 'd:/camera/')
|
|
226
214
|
*/
|
|
227
|
-
async function fmove(src, dst, { overwrite = false, print = true } = {}) {
|
|
215
|
+
export async function fmove(src, dst, { overwrite = false, print = true } = {}) {
|
|
228
216
|
if (src.endsWith('/') !== dst.endsWith('/'))
|
|
229
217
|
throw new Error('src and dst must be both file path or directory path');
|
|
230
|
-
if (!
|
|
218
|
+
if (!path.isAbsolute(src) || !path.isAbsolute(dst))
|
|
231
219
|
throw new Error('src and dst must be absolute path');
|
|
232
220
|
if (print)
|
|
233
221
|
console.log(`move: ${src} → ${dst}`);
|
|
234
|
-
await
|
|
222
|
+
await fse.move(src, dst, { overwrite });
|
|
235
223
|
}
|
|
236
|
-
exports.fmove = fmove;
|
|
237
224
|
/** rename file
|
|
238
225
|
- fp: current filename/path
|
|
239
226
|
- fp_: new filename/path
|
|
@@ -242,20 +229,19 @@ exports.fmove = fmove;
|
|
|
242
229
|
- print?: `true`
|
|
243
230
|
- overwrite?: `true` better performance without check
|
|
244
231
|
*/
|
|
245
|
-
async function frename(fp, fp_, { fpd, print = true, overwrite = true } = {}) {
|
|
232
|
+
export async function frename(fp, fp_, { fpd, print = true, overwrite = true } = {}) {
|
|
246
233
|
if (fpd) {
|
|
247
|
-
fp =
|
|
248
|
-
fp_ =
|
|
234
|
+
fp = path.join(fpd, fp);
|
|
235
|
+
fp_ = path.join(fpd, fp_);
|
|
249
236
|
}
|
|
250
|
-
else if (!
|
|
237
|
+
else if (!path.isAbsolute(fp) || !path.isAbsolute(fp_))
|
|
251
238
|
throw new Error('fp and fp_ must be absolute path');
|
|
252
239
|
if (print)
|
|
253
240
|
console.log('rename:', fp, '→', fp_);
|
|
254
241
|
if (!overwrite && fexists(fp_))
|
|
255
242
|
throw new Error(`file already exists:${fp_}`);
|
|
256
|
-
await
|
|
243
|
+
await fsp.rename(fp, fp_);
|
|
257
244
|
}
|
|
258
|
-
exports.frename = frename;
|
|
259
245
|
/**
|
|
260
246
|
Create folders recursively, make sure the folder pointed to by fpd exists
|
|
261
247
|
Returns the first created folder or undefined
|
|
@@ -265,13 +251,13 @@ exports.frename = frename;
|
|
|
265
251
|
- print?: `true`
|
|
266
252
|
- mode?: `'0o777'`
|
|
267
253
|
*/
|
|
268
|
-
async function fmkdir(fpd, { print = true, mode, } = {}) {
|
|
269
|
-
if (!
|
|
254
|
+
export async function fmkdir(fpd, { print = true, mode, } = {}) {
|
|
255
|
+
if (!path.isAbsolute(fpd))
|
|
270
256
|
throw new Error(`fpd must be an absolute path: ${fpd}`);
|
|
271
257
|
if (!fpd.endsWith('/'))
|
|
272
258
|
throw new Error(`fpd must end with /: ${fpd}`);
|
|
273
259
|
// CallingfsPromises.mkdir() when path is a directory that exists results in a rejection only when recursive is false.
|
|
274
|
-
const fpd_ = (await
|
|
260
|
+
const fpd_ = (await fsp.mkdir(fpd, { recursive: true, mode }))?.replaceAll('\\', '/');
|
|
275
261
|
if (fpd_) {
|
|
276
262
|
if (print)
|
|
277
263
|
console.log('folder created:', fpd);
|
|
@@ -280,13 +266,12 @@ async function fmkdir(fpd, { print = true, mode, } = {}) {
|
|
|
280
266
|
console.log('folder already exists:', fpd);
|
|
281
267
|
return fpd_;
|
|
282
268
|
}
|
|
283
|
-
exports.fmkdir = fmkdir;
|
|
284
269
|
/**
|
|
285
270
|
- fp_real: current real file/directory path
|
|
286
271
|
- fp_link: target file/directory path
|
|
287
272
|
*/
|
|
288
|
-
async function flink(fp_real, fp_link, { junction = false, print = true } = {}) {
|
|
289
|
-
if (!
|
|
273
|
+
export async function flink(fp_real, fp_link, { junction = false, print = true } = {}) {
|
|
274
|
+
if (!path.isAbsolute(fp_real) || !path.isAbsolute(fp_link))
|
|
290
275
|
throw new Error('fp must be absolute path');
|
|
291
276
|
const is_fpd_real = fp_real.endsWith('/');
|
|
292
277
|
const is_fpd_link = fp_link.endsWith('/');
|
|
@@ -297,12 +282,11 @@ async function flink(fp_real, fp_link, { junction = false, print = true } = {})
|
|
|
297
282
|
if (print)
|
|
298
283
|
console.log(`source file ${fp_real} linked to ${fp_link}`);
|
|
299
284
|
if (junction)
|
|
300
|
-
|
|
285
|
+
fsp.symlink(fp_real, fp_link, 'junction');
|
|
301
286
|
else
|
|
302
|
-
|
|
287
|
+
fsp.symlink(fp_real, fp_link, is_fpd_real ? 'dir' : 'file');
|
|
303
288
|
}
|
|
304
|
-
|
|
305
|
-
exports.fwatchers = {};
|
|
289
|
+
export let fwatchers = {};
|
|
306
290
|
/**
|
|
307
291
|
- fp: path of file or directory
|
|
308
292
|
- callback: called when modified
|
|
@@ -318,41 +302,39 @@ exports.fwatchers = {};
|
|
|
318
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.
|
|
319
303
|
|
|
320
304
|
*/
|
|
321
|
-
async function fwatch(fp, onchange, { exec = true } = {}) {
|
|
322
|
-
if (!
|
|
305
|
+
export async function fwatch(fp, onchange, { exec = true } = {}) {
|
|
306
|
+
if (!path.isAbsolute(fp))
|
|
323
307
|
throw new Error('fp must be absolute path');
|
|
324
|
-
const _watcher =
|
|
308
|
+
const _watcher = fwatchers[fp];
|
|
325
309
|
if (_watcher)
|
|
326
310
|
_watcher.close();
|
|
327
311
|
if (exec)
|
|
328
312
|
await onchange('change', fp.fname);
|
|
329
313
|
const start = new Date().getTime();
|
|
330
|
-
const debounced_onchange = (
|
|
314
|
+
const debounced_onchange = debounce((event, fname) => {
|
|
331
315
|
if (new Date().getTime() - start < 800)
|
|
332
316
|
return;
|
|
333
317
|
console.log(`file ${event}: ${fname}`);
|
|
334
|
-
onchange(event,
|
|
318
|
+
onchange(event, path.normalize(fname));
|
|
335
319
|
}, 500, { leading: false, trailing: true });
|
|
336
|
-
let watcher =
|
|
320
|
+
let watcher = fs.watch(fp, debounced_onchange);
|
|
337
321
|
watcher.on('error', error => {
|
|
338
322
|
console.error(error);
|
|
339
323
|
});
|
|
340
|
-
return
|
|
324
|
+
return fwatchers[fp] = watcher;
|
|
341
325
|
}
|
|
342
|
-
exports.fwatch = fwatch;
|
|
343
326
|
/** open a file and replace certain pattern */
|
|
344
|
-
async function freplace(fp, pattern, replacement) {
|
|
327
|
+
export async function freplace(fp, pattern, replacement) {
|
|
345
328
|
await fwrite(fp, (await fread(fp))
|
|
346
329
|
.replaceAll(pattern, replacement));
|
|
347
330
|
}
|
|
348
|
-
exports.freplace = freplace;
|
|
349
331
|
/** convert file encoding to UTF-8
|
|
350
332
|
- fp: file absolute path
|
|
351
333
|
- options?:
|
|
352
334
|
- dryrun?: `true`
|
|
353
335
|
- encoding?: `'auto'`
|
|
354
336
|
*/
|
|
355
|
-
async function f2utf8(fp, { dryrun = true, encoding = 'auto', } = {}) {
|
|
337
|
+
export async function f2utf8(fp, { dryrun = true, encoding = 'auto', } = {}) {
|
|
356
338
|
const text = await fread(fp, { encoding });
|
|
357
339
|
if (dryrun) {
|
|
358
340
|
console.log(text.slice(0, 10000));
|
|
@@ -363,5 +345,4 @@ async function f2utf8(fp, { dryrun = true, encoding = 'auto', } = {}) {
|
|
|
363
345
|
await fcopy(fp, fp_bak);
|
|
364
346
|
await fwrite(fp, text);
|
|
365
347
|
}
|
|
366
|
-
exports.f2utf8 = f2utf8;
|
|
367
348
|
//# sourceMappingURL=file.js.map
|
package/file.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[],"mappings":";;;;AAAA,iDAGW;AAGX,0DAAwB;AACxB,oEAA8B;AAC9B,gEAA0B;AAC1B,4DAA2B;AAE3B,6EAAyC;AAGzC,0DAAuB;AAYd,cAZF,eAAG,CAYE;AAJZ,iDAAwC;AACxC,mDAAwB;AAQxB,mDAAmD;AACnD,SAAgB,OAAO,CAAE,EAAU,EAAE,EAAE,KAAK,GAAG,IAAI,KAA0B,EAAG;IAC5E,MAAM,MAAM,GAAG,YAAE,CAAC,UAAU,CAAC,EAAE,CAAC,CAAA;IAEhC,IAAI,KAAK;QACL,OAAO,CAAC,GAAG,CACP,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,aAAa,EAClC,EAAE,CACL,CAAA;IAEL,OAAO,MAAM,CAAA;AACjB,CAAC;AAVD,0BAUC;AAGD;;;;;;;;;EASE;AACK,KAAK,UAAU,KAAK,CACvB,EAAU,EACV,KAAsB,EACtB,EACI,IAAI,EACJ,KAAK,KAIL,EAAG;IAEP,IAAI,KAAK;QACL,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA;IAC7B,OAAO,MAAM,CAAC,MAAM,CAChB,MAAM,aAAG,CAAC,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,EAC/B;QACI,EAAE;QACF,KAAK;QACL,IAAI;KACP,CACJ,CAAA;AACL,CAAC;AArBD,sB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,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;AAlBD,wBAkBC","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'\nimport rimraf from 'rimraf'\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 file or directory (use rimraf to delete directory fast) \n - fp: path\n - options?:\n - print?: `true` (effective only delete single file)\n*/\nexport async function fdelete (fp: string, { print = true, fast = false }: { print?: boolean, fast?: boolean } = { }) {\n if (fp.length < 6) throw new Error(`${fp} too short`)\n if (!path.isAbsolute(fp))\n throw new Error('fpd must be absolute path')\n \n if (fp.endsWith('/')) {\n if (print)\n console.log(`delete directory: ${fp}`.red)\n await new Promise<void>((resolve, reject) => {\n rimraf(fp, { glob: false, disableGlob: true }, error => {\n if (error)\n reject(error)\n else\n resolve()\n })\n })\n } else {\n if (print)\n console.log('delete:', fp)\n await fsp.unlink(fp)\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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andle & { 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"]}
|