xshell 0.0.8 → 0.0.12
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/README.md +24 -2
- package/extension.js +12 -12
- package/extension.js.map +1 -1
- package/file.d.ts +24 -17
- package/file.js +49 -52
- package/file.js.map +1 -1
- package/index.js +5 -5
- package/index.js.map +1 -1
- package/net.d.ts +28 -19
- package/net.js +80 -70
- package/net.js.map +1 -1
- package/package.json +17 -25
- package/process.d.ts +17 -17
- package/process.js +61 -67
- package/process.js.map +1 -1
- package/prototype.d.ts +33 -22
- package/prototype.js +130 -108
- package/prototype.js.map +1 -1
- package/repl.d.ts +2 -5
- package/repl.js +87 -77
- package/repl.js.map +1 -1
- package/{REPL.png → repl.png} +0 -0
- package/server.d.ts +11 -7
- package/server.js +117 -113
- package/server.js.map +1 -1
- package/tsconfig.json +18 -6
- package/utils.d.ts +17 -10
- package/utils.js +87 -44
- package/utils.js.map +1 -1
- package/xshell.js +1 -1
- package/xshell.js.map +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,29 @@
|
|
|
1
1
|
# xshell
|
|
2
|
-
[](https://www.npmjs.com/package/xshell) [](https://www.npmjs.com/package/xshell)
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
<p align='center'>
|
|
4
|
+
<img src='./xshell.png' alt='xshell' width='64'>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<h2 align='center'>
|
|
8
|
+
xshell
|
|
9
|
+
</h2>
|
|
10
|
+
|
|
11
|
+
<p align='center'>
|
|
12
|
+
<a href='https://www.npmjs.com/package/xshell' target='_blank'>
|
|
13
|
+
<img alt='npm version' src='https://img.shields.io/npm/v/xshell.svg?style=flat-square&color=brightgreen' />
|
|
14
|
+
</a>
|
|
15
|
+
<a href='https://www.npmjs.com/package/xshell' target='_blank'>
|
|
16
|
+
<img alt='npm downloads' src='https://img.shields.io/npm/dt/xshell?style=flat-square&color=brightgreen' />
|
|
17
|
+
</a>
|
|
18
|
+
<a href='https://marketplace.visualstudio.com/items?itemName=ShenHongFei.xshell' target='_blank'>
|
|
19
|
+
<img alt='vscode extension version' src='https://vsmarketplacebadge.apphb.com/version/ShenHongFei.xshell.svg?style=flat-square&color=4c98cf' />
|
|
20
|
+
</a>
|
|
21
|
+
<a href='https://marketplace.visualstudio.com/items?itemName=ShenHongFei.xshell' target='_blank'>
|
|
22
|
+
<img alt='vscode extension installs' src='https://vsmarketplacebadge.apphb.com/installs/ShenHongFei.xshell.svg?style=flat-square&color=4c98cf' />
|
|
23
|
+
</a>
|
|
24
|
+
</p>
|
|
25
|
+
|
|
26
|
+
### xshell is a shell designed to provide a brand new human-computer interaction experience.
|
|
5
27
|
|
|
6
28
|

|
|
7
29
|
|
package/extension.js
CHANGED
|
@@ -17,9 +17,9 @@ const my_commands = [
|
|
|
17
17
|
};
|
|
18
18
|
const language_id = doc.languageId;
|
|
19
19
|
if (!(language_id in languages_map))
|
|
20
|
-
throw new Error(`${language_id} does not support
|
|
21
|
-
const code = get_text('
|
|
22
|
-
await net_1.rpc('repl_code', [...languages_map[language_id], code], { async: true });
|
|
20
|
+
throw new Error(`${language_id} does not support repl`);
|
|
21
|
+
const code = get_text('selection or line');
|
|
22
|
+
await (0, net_1.rpc)('repl_code', [...languages_map[language_id], code], { async: true });
|
|
23
23
|
},
|
|
24
24
|
key: 'ctrl+enter',
|
|
25
25
|
when: 'editorTextFocus'
|
|
@@ -31,31 +31,31 @@ function get_text(selector) {
|
|
|
31
31
|
const document = editor.document;
|
|
32
32
|
const selection = editor.selection;
|
|
33
33
|
const text_selection = document.getText(selection);
|
|
34
|
-
if (selector === '
|
|
34
|
+
if (selector === 'selection')
|
|
35
35
|
return text_selection;
|
|
36
36
|
const text_all = document.getText();
|
|
37
|
-
if (selector === '
|
|
37
|
+
if (selector === 'all')
|
|
38
38
|
return text_all;
|
|
39
39
|
const text_line = document.lineAt(selection.active.line).text;
|
|
40
|
-
if (selector === '
|
|
40
|
+
if (selector === 'line')
|
|
41
41
|
return text_line;
|
|
42
|
-
if (selector === '
|
|
42
|
+
if (selector === 'word')
|
|
43
43
|
return document.getText(document.getWordRangeAtPosition(selection.active));
|
|
44
|
-
if (selector === '
|
|
44
|
+
if (selector === 'selection or all')
|
|
45
45
|
return text_selection || text_all;
|
|
46
|
-
if (selector === '
|
|
46
|
+
if (selector === 'selection or line')
|
|
47
47
|
return text_selection || text_line;
|
|
48
48
|
const start = selection.start;
|
|
49
49
|
const end = selection.end;
|
|
50
50
|
const line = document.lineAt(start.line);
|
|
51
51
|
const line_start = new vscode_1.Position(start.line, 0);
|
|
52
|
-
if (selector === '
|
|
52
|
+
if (selector === 'selection before')
|
|
53
53
|
return document.getText(new vscode_1.Range(line_start, start));
|
|
54
54
|
const line_end = new vscode_1.Position(start.line, line.text.length);
|
|
55
|
-
if (selector === '
|
|
55
|
+
if (selector === 'selection after')
|
|
56
56
|
return document.getText(new vscode_1.Range(end, line_end));
|
|
57
57
|
const line_text_start = new vscode_1.Position(start.line, line.firstNonWhitespaceCharacterIndex);
|
|
58
|
-
if (selector === '
|
|
58
|
+
if (selector === 'selection to text start')
|
|
59
59
|
return document.getText(new vscode_1.Range(line_text_start, start));
|
|
60
60
|
}
|
|
61
61
|
function activate(ctx) {
|
package/extension.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extension.js","sourceRoot":"","sources":["extension.ts"],"names":[],"mappings":";;;AAAA,mCAA0D;AAG1D,uBAAoB;AACpB,+BAA2B;AAG3B,MAAM,WAAW,GAAG;IAChB;QACI,IAAI,EAAE,KAAK,UAAU,WAAW;YAC5B,MAAM,MAAM,GAAG,eAAM,CAAC,gBAAgB,CAAA;YACtC,MAAM,GAAG,GAAG,MAAM,CAAC,QAAQ,CAAA;YAE3B,MAAM,aAAa,GAAG;gBAClB,UAAU,EAAE,CAAC,IAAI,CAAC;gBAClB,eAAe,EAAE,CAAC,IAAI,CAAC;gBACvB,UAAU,EAAE,CAAC,IA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C,CAAE,CAAA;IAG3D,MAAM,QAAQ,GAAK,IAAI,iBAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IAE7D,IAAI,QAAQ,KAAK,iBAAiB;QAC9B,OAAO,QAAQ,CAAC,OAAO,CAAE,IAAI,cAAK,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAE,CAAA;IAGvD,MAAM,eAAe,GAAG,IAAI,iBAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,gCAAgC,CAAC,CAAA;IACvF,IAAI,QAAQ,KAAK,yBAAyB;QACtC,OAAO,QAAQ,CAAC,OAAO,CAAC,IAAI,cAAK,CAAC,eAAe,EAAE,KAAK,CAAC,CAAG,CAAA;AACpE,CAAC;AAGD,SAAgB,QAAQ,CAAE,GAAqB;IAC3C,WAAW,CAAC,OAAO,CAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE;QAC9B,GAAG,CAAC,aAAa,CAAC,IAAI,CAAC,iBAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;IACrE,CAAC,CAAC,CAAA;IACF,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAA;AAChC,CAAC;AALD,4BAKC","sourcesContent":["import { window, commands, Range, Position } from 'vscode'\nimport type { ExtensionContext } from 'vscode'\n\nimport './prototype'\nimport { rpc } from './net'\n\n\nconst my_commands = [\n {\n func: async function xshell_repl () {\n const editor = window.activeTextEditor\n const doc = editor.document\n \n const languages_map = {\n javascript: ['ts'],\n javascriptreact: ['ts'],\n typescript: ['ts'],\n typescriptreact: ['ts'],\n }\n \n const language_id = doc.languageId\n \n if (!(language_id in languages_map))\n throw new Error(`${language_id} does not support repl`)\n \n const code = get_text('selection or line')\n \n await rpc('repl_code', [...languages_map[language_id], code], { async: true })\n },\n key: 'ctrl+enter',\n when: 'editorTextFocus'\n },\n]\n\n\n/** get text by selector */\nfunction get_text (selector: \n 'all' | \n 'line' | \n 'word' |\n 'selection' | \n 'selection or line' |\n 'selection or all' |\n 'selection before' | \n 'selection to text start' | \n 'selection after'\n) {\n const editor = window.activeTextEditor\n const document = editor.document\n const selection = editor.selection\n \n const text_selection = document.getText(selection)\n \n if (selector === 'selection')\n return text_selection\n \n const text_all = document.getText()\n \n if (selector === 'all')\n return text_all\n \n const text_line = document.lineAt(selection.active.line).text\n \n if (selector === 'line')\n return text_line\n \n if (selector === 'word')\n return document.getText(document.getWordRangeAtPosition(selection.active))\n \n if (selector === 'selection or all')\n return text_selection || text_all\n \n if (selector === 'selection or line')\n return text_selection || text_line\n \n \n \n const start = selection.start\n const end = selection.end\n \n const line = document.lineAt(start.line)\n \n const line_start = new Position(start.line, 0)\n \n if (selector === 'selection before')\n return document.getText( new Range(line_start, start) )\n \n \n const line_end = new Position(start.line, line.text.length)\n \n if (selector === 'selection after')\n return document.getText( new Range(end, line_end) )\n \n \n const line_text_start = new Position(start.line, line.firstNonWhitespaceCharacterIndex)\n if (selector === 'selection to text start')\n return document.getText(new Range(line_text_start, start) )\n}\n\n\nexport function activate (ctx: ExtensionContext) {\n my_commands.forEach( ({ func }) => {\n ctx.subscriptions.push(commands.registerCommand(func.name, func))\n })\n console.log('xshell loaded')\n}\n"]}
|
package/file.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type fs from 'fs';
|
|
3
|
-
export declare type Encoding = '
|
|
3
|
+
export declare type Encoding = 'utf-8' | 'gb18030' | 'shift-jis' | 'binary';
|
|
4
4
|
export declare function fread(fp: string): Promise<string>;
|
|
5
5
|
export declare function fread(fp: string, { dir, encoding, print }?: {
|
|
6
6
|
dir?: string;
|
|
7
|
-
encoding: '
|
|
7
|
+
encoding: 'binary';
|
|
8
8
|
print?: boolean;
|
|
9
9
|
}): Promise<Buffer>;
|
|
10
10
|
export declare function fread(fp: string, { dir, encoding, print }?: {
|
|
11
11
|
dir?: string;
|
|
12
|
-
encoding?: Encoding | '
|
|
12
|
+
encoding?: Encoding | 'auto';
|
|
13
13
|
print?: boolean;
|
|
14
14
|
}): Promise<string>;
|
|
15
15
|
export declare function fread_lines(fp: string, options?: {
|
|
16
16
|
dir?: string;
|
|
17
|
-
encoding?: Exclude<Encoding, '
|
|
17
|
+
encoding?: Exclude<Encoding, 'binary'> | 'auto';
|
|
18
18
|
print?: boolean;
|
|
19
19
|
}): Promise<string[]>;
|
|
20
20
|
export declare function fread_json<T = any>(fp: string, options?: {
|
|
@@ -22,7 +22,11 @@ export declare function fread_json<T = any>(fp: string, options?: {
|
|
|
22
22
|
encoding?: Encoding;
|
|
23
23
|
print?: boolean;
|
|
24
24
|
}): Promise<T>;
|
|
25
|
-
export declare function fwrite(fp: string, data:
|
|
25
|
+
export declare function fwrite(fp: string, data: Buffer, options?: {
|
|
26
|
+
dir?: string;
|
|
27
|
+
print?: boolean;
|
|
28
|
+
}): Promise<void>;
|
|
29
|
+
export declare function fwrite(fp: string, data: any, options?: {
|
|
26
30
|
dir?: string;
|
|
27
31
|
encoding?: Encoding;
|
|
28
32
|
print?: boolean;
|
|
@@ -45,11 +49,10 @@ export declare function flist(fpd: string, { filter, deep, absolute, print }?: {
|
|
|
45
49
|
absolute?: boolean;
|
|
46
50
|
print?: boolean;
|
|
47
51
|
}): Promise<string[]>;
|
|
48
|
-
/** delete file or directory
|
|
52
|
+
/** delete file or directory (use rimraf to delete directory fast)
|
|
49
53
|
- fp: path
|
|
50
54
|
- options?:
|
|
51
55
|
- print?: `true` (effective only delete single file)
|
|
52
|
-
- fast?: `false` use rimraf to delete quickly
|
|
53
56
|
*/
|
|
54
57
|
export declare function fdelete(fp: string, { print, fast }?: {
|
|
55
58
|
print?: boolean;
|
|
@@ -59,28 +62,32 @@ export declare function fdelete(fp: string, { print, fast }?: {
|
|
|
59
62
|
- src: src file/directory absolute path
|
|
60
63
|
- dst: dst file/directory absolute path
|
|
61
64
|
@example
|
|
62
|
-
fcopy('
|
|
65
|
+
fcopy('d:/temp/camera/', 'd:/camera/')
|
|
63
66
|
*/
|
|
64
|
-
export declare function fcopy(src: string, dst: string, { print }?: {
|
|
67
|
+
export declare function fcopy(src: string, dst: string, { print, overwrite, }?: {
|
|
65
68
|
print?: boolean;
|
|
69
|
+
overwrite?: boolean;
|
|
66
70
|
}): Promise<void>;
|
|
67
71
|
/** move file or direcotry
|
|
68
72
|
- src: src file/directory absolute path
|
|
69
73
|
- dst: dst file/directory absolute path
|
|
70
74
|
@example
|
|
71
|
-
fmove('
|
|
75
|
+
fmove('d:/temp/camera/', 'd:/camera/')
|
|
72
76
|
*/
|
|
73
77
|
export declare function fmove(src: string, dst: string, { overwrite, print }?: {
|
|
74
78
|
overwrite?: boolean;
|
|
75
79
|
print?: boolean;
|
|
76
80
|
}): Promise<void>;
|
|
77
81
|
/** rename file
|
|
78
|
-
-
|
|
79
|
-
-
|
|
80
|
-
-
|
|
82
|
+
- fp: current filename/path
|
|
83
|
+
- fp_: new filename/path
|
|
84
|
+
- options?:
|
|
85
|
+
- fpd?: fp and fp_ is in same directory
|
|
86
|
+
- print?: `true`
|
|
87
|
+
- overwrite?: `true` better performance without check
|
|
81
88
|
*/
|
|
82
|
-
export declare function frename(fp: string, fp_: string, {
|
|
83
|
-
|
|
89
|
+
export declare function frename(fp: string, fp_: string, { fpd, print, overwrite }?: {
|
|
90
|
+
fpd?: string;
|
|
84
91
|
print?: boolean;
|
|
85
92
|
overwrite?: boolean;
|
|
86
93
|
}): Promise<void>;
|
|
@@ -120,9 +127,9 @@ export declare function freplace(fp: string, pattern: string | RegExp, replaceme
|
|
|
120
127
|
- fp: file absolute path
|
|
121
128
|
- options?:
|
|
122
129
|
- dryrun?: `true`
|
|
123
|
-
- encoding?: `'
|
|
130
|
+
- encoding?: `'auto'`
|
|
124
131
|
*/
|
|
125
132
|
export declare function f2utf8(fp: string, { dryrun, encoding, }?: {
|
|
126
133
|
dryrun?: boolean;
|
|
127
|
-
encoding?: Encoding | '
|
|
134
|
+
encoding?: Encoding | 'auto';
|
|
128
135
|
}): Promise<void>;
|
package/file.js
CHANGED
|
@@ -3,34 +3,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.f2utf8 = exports.freplace = exports.fwatch = exports.fwatchers = exports.link_shortcut = exports.flink = exports.fmkdir = exports.frename = exports.fmove = exports.fcopy = exports.fdelete = exports.flist = exports.fappend = exports.fwrite = exports.fread_json = exports.fread_lines = exports.fread = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
|
-
const upath_1 = tslib_1.__importDefault(require("upath"));
|
|
7
|
-
const iconv_lite_1 = tslib_1.__importDefault(require("iconv-lite"));
|
|
6
|
+
const upath_1 = (0, tslib_1.__importDefault)(require("upath"));
|
|
7
|
+
const iconv_lite_1 = (0, tslib_1.__importDefault)(require("iconv-lite"));
|
|
8
8
|
const readdir_enhanced_1 = require("readdir-enhanced");
|
|
9
|
-
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const isRegExp_1 = tslib_1.__importDefault(require("lodash/isRegExp"));
|
|
13
|
-
const isString_1 = tslib_1.__importDefault(require("lodash/isString"));
|
|
14
|
-
const debounce_1 = tslib_1.__importDefault(require("lodash/debounce"));
|
|
9
|
+
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
10
|
+
const rimraf_1 = (0, tslib_1.__importDefault)(require("rimraf"));
|
|
11
|
+
const debounce_1 = (0, tslib_1.__importDefault)(require("lodash/debounce"));
|
|
15
12
|
const prototype_1 = require("./prototype");
|
|
16
13
|
const utils_1 = require("./utils");
|
|
17
|
-
async function fread(fp, { dir, encoding = '
|
|
14
|
+
async function fread(fp, { dir, encoding = 'utf-8', print = true } = {}) {
|
|
18
15
|
if (dir)
|
|
19
16
|
fp = upath_1.default.join(dir, fp);
|
|
20
17
|
else if (!upath_1.default.isAbsolute(fp))
|
|
21
18
|
throw new Error('fp must be absolute path, or pass in "dir" parameter');
|
|
22
19
|
if (print)
|
|
23
|
-
console.log(
|
|
20
|
+
console.log(`read: ${fp}`);
|
|
24
21
|
const buffer = await fs_1.promises.readFile(fp);
|
|
25
|
-
if (encoding === '
|
|
22
|
+
if (encoding === 'binary')
|
|
26
23
|
return buffer;
|
|
27
|
-
if (encoding === '
|
|
24
|
+
if (encoding === 'utf-8')
|
|
28
25
|
return buffer.toString('utf8');
|
|
29
|
-
if (encoding === '
|
|
30
|
-
const { detect } = await Promise.resolve().then(() => tslib_1.__importStar(require('chardet')));
|
|
26
|
+
if (encoding === 'auto') {
|
|
27
|
+
const { detect } = await Promise.resolve().then(() => (0, tslib_1.__importStar)(require('chardet')));
|
|
31
28
|
encoding = detect(buffer);
|
|
32
29
|
if (print)
|
|
33
|
-
console.log(`${fp} probably has encoding: ${encoding}`);
|
|
30
|
+
console.log(`${fp} probably has encoding: ${encoding.toLowerCase()}`);
|
|
34
31
|
}
|
|
35
32
|
return iconv_lite_1.default.decode(buffer, encoding);
|
|
36
33
|
}
|
|
@@ -44,17 +41,17 @@ async function fread_json(fp, options = {}) {
|
|
|
44
41
|
return JSON.parse(await fread(fp, options));
|
|
45
42
|
}
|
|
46
43
|
exports.fread_json = fread_json;
|
|
47
|
-
async function fwrite(fp, data, { dir, encoding = '
|
|
44
|
+
async function fwrite(fp, data, { dir, encoding = 'utf-8', print = true } = {}) {
|
|
48
45
|
if (dir)
|
|
49
46
|
fp = upath_1.default.join(dir, fp);
|
|
50
47
|
else if (!upath_1.default.isAbsolute(fp))
|
|
51
48
|
throw new Error('fp must be absolute path, or pass in "dir" parameter');
|
|
52
49
|
if (print)
|
|
53
50
|
console.log('write:', fp);
|
|
54
|
-
if (encoding === '
|
|
51
|
+
if (encoding === 'gb18030')
|
|
55
52
|
data = iconv_lite_1.default.encode(data, encoding);
|
|
56
|
-
if (!Buffer.isBuffer(data) &&
|
|
57
|
-
data = prototype_1.to_json(data);
|
|
53
|
+
if (!Buffer.isBuffer(data) && typeof data !== 'string')
|
|
54
|
+
data = (0, prototype_1.to_json)(data);
|
|
58
55
|
await fs_1.promises.writeFile(fp, data);
|
|
59
56
|
}
|
|
60
57
|
exports.fwrite = fwrite;
|
|
@@ -65,7 +62,7 @@ async function fappend(fp, data, { dir, print = true } = {}) {
|
|
|
65
62
|
throw new Error('fp must be absolute path, or pass in "dir" parameter');
|
|
66
63
|
if (print)
|
|
67
64
|
console.log('append:', fp);
|
|
68
|
-
if (!Buffer.isBuffer(data) &&
|
|
65
|
+
if (!Buffer.isBuffer(data) && typeof data !== 'string')
|
|
69
66
|
throw new Error('data is not Buffer or string');
|
|
70
67
|
await fs_1.promises.appendFile(fp, data);
|
|
71
68
|
}
|
|
@@ -81,7 +78,7 @@ exports.fappend = fappend;
|
|
|
81
78
|
async function flist(fpd, { filter, deep = false, absolute = false, print = true } = {}) {
|
|
82
79
|
if (!upath_1.default.isAbsolute(fpd))
|
|
83
80
|
throw new Error('fpd must be absolute path');
|
|
84
|
-
let fps = await readdir_enhanced_1.readdirAsync(fpd, {
|
|
81
|
+
let fps = await (0, readdir_enhanced_1.readdirAsync)(fpd, {
|
|
85
82
|
...(absolute ? { basePath: fpd } : {}),
|
|
86
83
|
deep,
|
|
87
84
|
stats: false,
|
|
@@ -92,7 +89,7 @@ async function flist(fpd, { filter, deep = false, absolute = false, print = true
|
|
|
92
89
|
console.log(fp);
|
|
93
90
|
return fp;
|
|
94
91
|
});
|
|
95
|
-
if (
|
|
92
|
+
if (filter instanceof RegExp)
|
|
96
93
|
return fps.filter(fp => filter.test(fp));
|
|
97
94
|
else if (filter)
|
|
98
95
|
return fps.filter(filter);
|
|
@@ -100,11 +97,10 @@ async function flist(fpd, { filter, deep = false, absolute = false, print = true
|
|
|
100
97
|
return fps;
|
|
101
98
|
}
|
|
102
99
|
exports.flist = flist;
|
|
103
|
-
/** delete file or directory
|
|
100
|
+
/** delete file or directory (use rimraf to delete directory fast)
|
|
104
101
|
- fp: path
|
|
105
102
|
- options?:
|
|
106
103
|
- print?: `true` (effective only delete single file)
|
|
107
|
-
- fast?: `false` use rimraf to delete quickly
|
|
108
104
|
*/
|
|
109
105
|
async function fdelete(fp, { print = true, fast = false } = {}) {
|
|
110
106
|
if (fp.length < 6)
|
|
@@ -112,18 +108,16 @@ async function fdelete(fp, { print = true, fast = false } = {}) {
|
|
|
112
108
|
if (!upath_1.default.isAbsolute(fp))
|
|
113
109
|
throw new Error('fpd must be absolute path');
|
|
114
110
|
if (fp.is_dir) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
});
|
|
111
|
+
if (print)
|
|
112
|
+
console.log(`delete directory: ${fp}`.red);
|
|
113
|
+
await new Promise((resolve, reject) => {
|
|
114
|
+
(0, rimraf_1.default)(fp, { glob: false, disableGlob: true }, error => {
|
|
115
|
+
if (error)
|
|
116
|
+
reject(error);
|
|
117
|
+
else
|
|
118
|
+
resolve();
|
|
124
119
|
});
|
|
125
|
-
|
|
126
|
-
await trash_1.default(fp, { glob: false });
|
|
120
|
+
});
|
|
127
121
|
}
|
|
128
122
|
else {
|
|
129
123
|
if (print)
|
|
@@ -136,23 +130,23 @@ exports.fdelete = fdelete;
|
|
|
136
130
|
- src: src file/directory absolute path
|
|
137
131
|
- dst: dst file/directory absolute path
|
|
138
132
|
@example
|
|
139
|
-
fcopy('
|
|
133
|
+
fcopy('d:/temp/camera/', 'd:/camera/')
|
|
140
134
|
*/
|
|
141
|
-
async function fcopy(src, dst, { print = true } = {}) {
|
|
135
|
+
async function fcopy(src, dst, { print = true, overwrite = true, } = {}) {
|
|
142
136
|
if (src.endsWith('/') !== dst.endsWith('/'))
|
|
143
137
|
throw new Error('src and dst must be both file path or directory path');
|
|
144
138
|
if (!upath_1.default.isAbsolute(src) || !upath_1.default.isAbsolute(dst))
|
|
145
139
|
throw new Error('src and dst must be absolute path');
|
|
146
140
|
if (print)
|
|
147
141
|
console.log(`copy: ${src} → ${dst}`);
|
|
148
|
-
await fs_extra_1.default.copy(src, dst);
|
|
142
|
+
await fs_extra_1.default.copy(src, dst, { overwrite, errorOnExist: true });
|
|
149
143
|
}
|
|
150
144
|
exports.fcopy = fcopy;
|
|
151
145
|
/** move file or direcotry
|
|
152
146
|
- src: src file/directory absolute path
|
|
153
147
|
- dst: dst file/directory absolute path
|
|
154
148
|
@example
|
|
155
|
-
fmove('
|
|
149
|
+
fmove('d:/temp/camera/', 'd:/camera/')
|
|
156
150
|
*/
|
|
157
151
|
async function fmove(src, dst, { overwrite = false, print = true } = {}) {
|
|
158
152
|
if (src.endsWith('/') !== dst.endsWith('/'))
|
|
@@ -165,14 +159,17 @@ async function fmove(src, dst, { overwrite = false, print = true } = {}) {
|
|
|
165
159
|
}
|
|
166
160
|
exports.fmove = fmove;
|
|
167
161
|
/** rename file
|
|
168
|
-
-
|
|
169
|
-
-
|
|
170
|
-
-
|
|
162
|
+
- fp: current filename/path
|
|
163
|
+
- fp_: new filename/path
|
|
164
|
+
- options?:
|
|
165
|
+
- fpd?: fp and fp_ is in same directory
|
|
166
|
+
- print?: `true`
|
|
167
|
+
- overwrite?: `true` better performance without check
|
|
171
168
|
*/
|
|
172
|
-
async function frename(fp, fp_, {
|
|
173
|
-
if (
|
|
174
|
-
fp = upath_1.default.join(
|
|
175
|
-
fp_ = upath_1.default.join(
|
|
169
|
+
async function frename(fp, fp_, { fpd, print = true, overwrite = true } = {}) {
|
|
170
|
+
if (fpd) {
|
|
171
|
+
fp = upath_1.default.join(fpd, fp);
|
|
172
|
+
fp_ = upath_1.default.join(fpd, fp_);
|
|
176
173
|
}
|
|
177
174
|
else if (!upath_1.default.isAbsolute(fp) || !upath_1.default.isAbsolute(fp_))
|
|
178
175
|
throw new Error('fp and fp_ must be absolute path');
|
|
@@ -223,9 +220,9 @@ async function flink(fp_real, fp_link, { junction = false, print = true } = {})
|
|
|
223
220
|
}
|
|
224
221
|
exports.flink = flink;
|
|
225
222
|
function link_shortcut(target, name, { args } = {}) {
|
|
226
|
-
const cmd = utils_1.dedent `
|
|
223
|
+
const cmd = (0, utils_1.dedent) `
|
|
227
224
|
$wsh_shell = New-Object -comObject WScript.Shell
|
|
228
|
-
$shortcut = $wsh_shell.CreateShortcut("
|
|
225
|
+
$shortcut = $wsh_shell.CreateShortcut("d:/links/#{name}.lnk")
|
|
229
226
|
$shortcut.TargetPath = '${target}'
|
|
230
227
|
$shortcut.Arguments = "${args || ''}"
|
|
231
228
|
$shortcut.WorkingDirectory = '${target.fdir}'
|
|
@@ -259,11 +256,11 @@ async function fwatch(fp, onchange, { exec = true } = {}) {
|
|
|
259
256
|
_watcher.close();
|
|
260
257
|
if (exec)
|
|
261
258
|
await onchange('change', fp);
|
|
262
|
-
const debounced_onchange = debounce_1.default((event, filename) => {
|
|
259
|
+
const debounced_onchange = (0, debounce_1.default)((event, filename) => {
|
|
263
260
|
console.log(`file changed (${event}): ${filename}`);
|
|
264
261
|
onchange(event, upath_1.default.normalize(filename));
|
|
265
262
|
}, 500, { leading: false, trailing: true });
|
|
266
|
-
const watcher = fs_1.watch(fp, debounced_onchange);
|
|
263
|
+
const watcher = (0, fs_1.watch)(fp, debounced_onchange);
|
|
267
264
|
watcher.on('error', error => { console.error(error); });
|
|
268
265
|
return exports.fwatchers[fp] = watcher;
|
|
269
266
|
}
|
|
@@ -278,9 +275,9 @@ exports.freplace = freplace;
|
|
|
278
275
|
- fp: file absolute path
|
|
279
276
|
- options?:
|
|
280
277
|
- dryrun?: `true`
|
|
281
|
-
- encoding?: `'
|
|
278
|
+
- encoding?: `'auto'`
|
|
282
279
|
*/
|
|
283
|
-
async function f2utf8(fp, { dryrun = true, encoding = '
|
|
280
|
+
async function f2utf8(fp, { dryrun = true, encoding = 'auto', } = {}) {
|
|
284
281
|
const text = await fread(fp, { encoding });
|
|
285
282
|
if (dryrun) {
|
|
286
283
|
console.log(text.slice(0,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AA;IAC7C,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA,CAAC,CAAC,CAAC,CAAA;IACtD,OAAO,iBAAS,CAAC,EAAE,CAAC,GAAG,OAAO,CAAA;AAClC,CAAC;AAjBD,wBAiBC;AAGD,8CAA8C;AACvC,KAAK,UAAU,QAAQ,CAAE,EAAU,EAAE,OAAwB,EAAE,WAAmB;IACrF,MAAM,MAAM,CACR,EAAE,EACF,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC,CAAC;SACZ,UAAU,CAAC,OAAO,EAAE,WAAW,CAAC,CACxC,CAAA;AACL,CAAC;AAND,4BAMC;AAED;;;;;EAKE;AACK,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,MAAM,CAAC,OAAO;QACf,MAAM,KAAK,CAAC,EAAE,EAAE,MAAM,CAAC,CAAA;IAE3B,MAAM,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,CAAA;AAC1B,CAAC;AAlBD,wBAkBC","sourcesContent":["import { promises as fsp, watch } from 'fs'\nimport type fs from 'fs'\n\nimport path from 'upath'\nimport iconv from 'iconv-lite'\nimport { readdirAsync } from 'readdir-enhanced'\nimport fse from 'fs-extra'\nimport trash from 'trash'\nimport rimraf from 'rimraf'\n\nimport is_regx from 'lodash/isRegExp'\nimport is_str from 'lodash/isString'\nimport debounce from 'lodash/debounce'\n\n\nimport { to_json } from './prototype'\nimport { dedent } from './utils'\n\n\nexport type Encoding = 'UTF-8' | 'GB18030' | 'Shift_JIS' | 'BINARY'\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}`)\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, data: any, { dir, encoding = 'UTF-8', print = true }: { dir?: string, encoding?: Encoding, 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('write:', fp)\n \n if (encoding === 'GB18030')\n data = iconv.encode(data, encoding)\n \n if (!Buffer.isBuffer(data) && !is_str(data))\n data = to_json(data)\n \n await fsp.writeFile(fp, data)\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) && !is_str(data))\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 absolute path\n - print?: `true`\n - filter?: `true` RegExp | (fp: string) => any\n*/\nexport async function flist (fpd: string, {\n filter, \n deep = false, \n absolute = false,\n print = true\n}: {\n filter?: RegExp | ((fp: string) => any)\n deep?: boolean\n absolute?: boolean\n print?: boolean\n} = { }) {\n if (!path.isAbsolute(fpd))\n throw new Error('fpd must be absolute path')\n \n let fps = await readdirAsync(fpd, {\n ...(absolute ? { basePath: fpd } : { }),\n deep,\n stats: false,\n })\n \n fps = fps.map( fp => {\n fp = path.normalize(fp)\n if (print)\n console.log(fp)\n return fp\n })\n \n if (is_regx(filter))\n return fps.filter( fp => filter.test(fp))\n else if (filter)\n return fps.filter(filter)\n else\n return fps\n}\n\n\n/** delete file or directory \n - fp: path\n - options?:\n - print?: `true` (effective only delete single file)\n - fast?: `false` use rimraf to delete quickly\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 if (fp.is_dir) {\n console.log(`delete directory: ${fp}`.red)\n if (fast)\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 await trash(fp, { glob: false })\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 (src: string, dst: string, { print = true }: { print?: boolean } = { }) {\n if (src.endsWith('/') !== dst.endsWith('/')) throw new Error('src and dst must be both file path or directory path')\n if (!path.isAbsolute(src) || !path.isAbsolute(dst)) throw new Error('src and dst must be absolute path')\n if (print)\n console.log(`copy: ${src} → ${dst}`)\n await fse.copy(src, dst)\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, { overwrite = false, print = true }: { overwrite?: boolean, print?: boolean } = { }) {\n if (src.endsWith('/') !== dst.endsWith('/')) throw new Error('src and dst must be both file path or directory path')\n if (!path.isAbsolute(src) || !path.isAbsolute(dst)) throw new Error('src and dst must be absolute path')\n if (print)\n console.log(`move: ${src} → ${dst}`)\n await fse.move(src, dst, { overwrite })\n}\n\n\n/** rename file \n - dir?\n - print?: `true`\n - overwrite?: `true` better performance without check\n */\nexport async function frename (fp: string, fp_: string, { dir, print = true, overwrite = true }: { dir?: string, print?: boolean, overwrite?: boolean } = { }) {\n if (dir) {\n fp = path.join(dir, fp)\n fp_ = path.join(dir, 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 && fp_.fexists) throw new Error(`file already exists:${fp_}`)\n \n await fsp.rename(fp, fp_)\n}\n\n\nexport async function fmkdir (fpd: string, options: fs.MakeDirectoryOptions & { print?: boolean, suppress_existence?: boolean } = { }) {\n if (!path.isAbsolute(fpd))\n throw new Error('fpd must be absolute path')\n \n options.print ??= true\n \n if (fpd.fexists)\n if (fpd.is_dir) {\n if (options.print && !options.suppress_existence)\n console.log('directory already exists:', fpd)\n return\n } else throw new Error(`file with same name already exists, cannot create directory: ${fpd}`)\n else if (options.print)\n console.log('create new directory:', fpd)\n \n await fsp.mkdir(fpd, { recursive: true })\n}\n\n\n/** - link: can be file path or 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('fpd must be absolute path')\n \n if (fp_link.fexists)\n if (fp_link.is_dir)\n fp_link = path.join(fp_link, fp_real.fname)\n else if ( (await fsp.lstat(fp_link)).isSymbolicLink() ) {\n console.log('link already exists:', fp_link)\n return\n } else\n throw new Error('file with same name already exists, cannot create new link')\n \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, fp_real.is_dir ? 'dir' : 'file')\n}\n\n\nexport function link_shortcut (target: string, name: string, { args }: { args?: string[] } = { }) {\n const cmd = dedent`\n $wsh_shell = New-Object -comObject WScript.Shell\n $shortcut = $wsh_shell.CreateShortcut(\"D:/Shortcuts/#{name}.lnk\")\n $shortcut.TargetPath = '${target}'\n $shortcut.Arguments = \"${args || ''}\"\n $shortcut.WorkingDirectory = '${target.fdir}'\n ${ target.is_dir ? '$shortcut.IconLocation = \"%SystemRoot%\\\\System32\\\\SHELL32.dll,3\"' : '' }\n $shortcut.Save()\n `\n console.log(cmd)\n // await psh(cmd)\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 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, filename). \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 (fp: string, onchange: (event: string, filename: string) => any, { exec = true }: { exec?: boolean } = { }) {\n if (!path.isAbsolute(fp)) 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)\n \n const debounced_onchange = debounce((event, filename) => {\n console.log(`file changed (${event}): ${filename}`)\n onchange(event, path.normalize(filename))\n }, 500, { leading: false, trailing: true })\n const watcher = watch(fp, debounced_onchange)\n watcher.on('error', error => { console.error(error) })\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 (!fp_bak.fexists)\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,2BAA2C;AAG3C,+DAAwB;AACxB,yEAA8B;AAC9B,uDAA+C;AAC/C,qEAA0B;AAC1B,iEAA2B;AAE3B,4EAAsC;AAGtC,2CAAqC;AACrC,mCAAgC;AASzB,KAAK,UAAU,KAAK,CAAE,EAAU,EAAE,EACrC,GAAG,EACH,QAAQ,GAAG,OAAO,EAClB,K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AE,CAAC,CAAC;SACZ,UAAU,CAAC,OAAO,EAAE,WAAW,CAAC,CACxC,CAAA;AACL,CAAC;AAND,4BAMC;AAED;;;;;EAKE;AACK,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,MAAM,CAAC,OAAO;QACf,MAAM,KAAK,CAAC,EAAE,EAAE,MAAM,CAAC,CAAA;IAE3B,MAAM,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,CAAA;AAC1B,CAAC;AAlBD,wBAkBC","sourcesContent":["import { promises as fsp, watch } from 'fs'\nimport type fs from 'fs'\n\nimport path from 'upath'\nimport iconv from 'iconv-lite'\nimport { readdirAsync } from 'readdir-enhanced'\nimport fse from 'fs-extra'\nimport rimraf from 'rimraf'\n\nimport debounce from 'lodash/debounce'\n\n\nimport { to_json } from './prototype'\nimport { dedent } from './utils'\n\n\nexport type Encoding = 'utf-8' | 'gb18030' | 'shift-jis' | 'binary'\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, data: Buffer, options?: { dir?: string, print?: boolean }): Promise<void>\nexport async function fwrite (fp: string, data: any, options?: { dir?: string, encoding?: Encoding, print?: boolean }): Promise<void>\nexport async function fwrite (fp: string, data: any, { dir, encoding = 'utf-8', print = true }: { dir?: string, encoding?: Encoding, 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('write:', fp)\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 await fsp.writeFile(fp, data)\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 absolute path\n - print?: `true`\n - filter?: `true` RegExp | (fp: string) => any\n*/\nexport async function flist (fpd: string, {\n filter, \n deep = false, \n absolute = false,\n print = true\n}: {\n filter?: RegExp | ((fp: string) => any)\n deep?: boolean\n absolute?: boolean\n print?: boolean\n} = { }) {\n if (!path.isAbsolute(fpd))\n throw new Error('fpd must be absolute path')\n \n let fps = await readdirAsync(fpd, {\n ...(absolute ? { basePath: fpd } : { }),\n deep,\n stats: false,\n })\n \n fps = fps.map( fp => {\n fp = path.normalize(fp)\n if (print)\n console.log(fp)\n return fp\n })\n \n if (filter instanceof RegExp)\n return fps.filter( fp => filter.test(fp))\n else if (filter)\n return fps.filter(filter)\n else\n return fps\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.is_dir) {\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 (src: string, dst: string, {\n print = true,\n overwrite = true,\n}: {\n print?: boolean\n overwrite?: boolean\n} = { }) {\n if (src.endsWith('/') !== dst.endsWith('/')) throw new Error('src and dst must be both file path or directory path')\n if (!path.isAbsolute(src) || !path.isAbsolute(dst)) throw new Error('src and dst must be absolute path')\n if (print)\n console.log(`copy: ${src} → ${dst}`)\n await fse.copy(src, 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('/')) throw new Error('src and dst must be both file path or directory path')\n if (!path.isAbsolute(src) || !path.isAbsolute(dst)) throw new Error('src and dst must be absolute path')\n if (print)\n console.log(`move: ${src} → ${dst}`)\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 && fp_.fexists)\n throw new Error(`file already exists:${fp_}`)\n \n await fsp.rename(fp, fp_)\n}\n\n\nexport async function fmkdir (fpd: string, options: fs.MakeDirectoryOptions & { print?: boolean, suppress_existence?: boolean } = { }) {\n if (!path.isAbsolute(fpd))\n throw new Error('fpd must be absolute path')\n \n options.print ??= true\n \n if (fpd.fexists)\n if (fpd.is_dir) {\n if (options.print && !options.suppress_existence)\n console.log('directory already exists:', fpd)\n return\n } else throw new Error(`file with same name already exists, cannot create directory: ${fpd}`)\n else if (options.print)\n console.log('create new directory:', fpd)\n \n await fsp.mkdir(fpd, { recursive: true })\n}\n\n\n/** - link: can be file path or 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('fpd must be absolute path')\n \n if (fp_link.fexists)\n if (fp_link.is_dir)\n fp_link = path.join(fp_link, fp_real.fname)\n else if ( (await fsp.lstat(fp_link)).isSymbolicLink() ) {\n console.log('link already exists:', fp_link)\n return\n } else\n throw new Error('file with same name already exists, cannot create new link')\n \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, fp_real.is_dir ? 'dir' : 'file')\n}\n\n\nexport function link_shortcut (target: string, name: string, { args }: { args?: string[] } = { }) {\n const cmd = dedent`\n $wsh_shell = New-Object -comObject WScript.Shell\n $shortcut = $wsh_shell.CreateShortcut(\"d:/links/#{name}.lnk\")\n $shortcut.TargetPath = '${target}'\n $shortcut.Arguments = \"${args || ''}\"\n $shortcut.WorkingDirectory = '${target.fdir}'\n ${ target.is_dir ? '$shortcut.IconLocation = \"%SystemRoot%\\\\System32\\\\SHELL32.dll,3\"' : '' }\n $shortcut.Save()\n `\n console.log(cmd)\n // await psh(cmd)\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 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, filename). \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, filename: string) => any,\n { exec = true }: { exec?: boolean } = { }\n) {\n if (!path.isAbsolute(fp)) 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)\n \n const debounced_onchange = debounce((event, filename) => {\n console.log(`file changed (${event}): ${filename}`)\n onchange(event, path.normalize(filename))\n }, 500, { leading: false, trailing: true })\n const watcher = watch(fp, debounced_onchange)\n watcher.on('error', error => { console.error(error) })\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 (!fp_bak.fexists)\n await fcopy(fp, fp_bak)\n \n await fwrite(fp, text)\n}\n\n"]}
|
package/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./prototype"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./utils"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./file"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./process"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./net"), exports);
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./prototype"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./utils"), exports);
|
|
6
|
+
(0, tslib_1.__exportStar)(require("./file"), exports);
|
|
7
|
+
(0, tslib_1.__exportStar)(require("./process"), exports);
|
|
8
|
+
(0, tslib_1.__exportStar)(require("./net"), exports);
|
|
9
9
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,2DAA2B;AAC3B,uDAAuB;AACvB,sDAAsB;AACtB,yDAAyB;AACzB,qDAAqB","sourcesContent":["export * from './prototype'\nexport * from './utils'\nexport * from './file'\nexport * from './process'\nexport * from './net'\n"]}
|