xshell 0.0.10 → 0.0.14
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/extension.js +11 -11
- package/extension.js.map +1 -1
- package/file.d.ts +34 -13
- package/file.js +28 -19
- package/file.js.map +1 -1
- package/net.d.ts +28 -19
- package/net.js +66 -56
- package/net.js.map +1 -1
- package/package.json +15 -19
- package/process.d.ts +14 -14
- package/process.js +56 -62
- package/process.js.map +1 -1
- package/prototype.d.ts +31 -20
- package/prototype.js +115 -96
- package/prototype.js.map +1 -1
- package/repl.d.ts +2 -5
- package/repl.js +72 -58
- package/repl.js.map +1 -1
- package/server.d.ts +26 -9
- package/server.js +224 -100
- package/server.js.map +1 -1
- package/tsconfig.json +6 -6
- package/ufs.d.ts +21 -0
- package/ufs.js +153 -0
- package/ufs.js.map +1 -0
- package/utils.d.ts +17 -10
- package/utils.js +84 -41
- package/utils.js.map +1 -1
package/extension.js
CHANGED
|
@@ -17,8 +17,8 @@ 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('
|
|
20
|
+
throw new Error(`${language_id} does not support repl`);
|
|
21
|
+
const code = get_text('selection or line');
|
|
22
22
|
await (0, net_1.rpc)('repl_code', [...languages_map[language_id], code], { async: true });
|
|
23
23
|
},
|
|
24
24
|
key: 'ctrl+enter',
|
|
@@ -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,CAA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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
|
|
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,IAAI,CAAC;gBAClB,eAAe,EAAE,CAAC,IAAI,CAAC;aAC1B,CAAA;YAED,MAAM,WAAW,GAAG,GAAG,CAAC,UAAU,CAAA;YAElC,IAAI,CAAC,CAAC,WAAW,IAAI,aAAa,CAAC;gBAC/B,MAAM,IAAI,KAAK,CAAC,GAAG,WAAW,wBAAwB,CAAC,CAAA;YAE3D,MAAM,IAAI,GAAG,QAAQ,CAAC,mBAAmB,CAAC,CAAA;YAE1C,MAAM,IAAA,SAAG,EAAC,WAAW,EAAE,CAAC,GAAG,aAAa,CAAC,WAAW,CAAC,EAAE,IAAI,CAAC,EAAE,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AI,EAAE,CAAC,CAAC,CAAA;IAE9C,IAAI,QAAQ,KAAK,kBAAkB;QAC/B,OAAO,QAAQ,CAAC,OAAO,CAAE,IAAI,cAAK,CAAC,UAAU,EAAE,KAAK,CAAC,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,35 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type fs from 'fs';
|
|
3
|
-
|
|
3
|
+
import path from 'upath';
|
|
4
|
+
import MFS from 'memfs';
|
|
5
|
+
declare module 'memfs' {
|
|
6
|
+
interface IFs {
|
|
7
|
+
join: typeof path.join;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export * from './ufs';
|
|
11
|
+
export { MFS };
|
|
12
|
+
export declare type Encoding = 'utf-8' | 'gb18030' | 'shift-jis' | 'binary';
|
|
13
|
+
export declare function create_mfs(): MFS.IFs & {
|
|
14
|
+
join: any;
|
|
15
|
+
};
|
|
16
|
+
export declare let mfs: MFS.IFs & {
|
|
17
|
+
join: typeof path.join;
|
|
18
|
+
};
|
|
4
19
|
export declare function fread(fp: string): Promise<string>;
|
|
5
20
|
export declare function fread(fp: string, { dir, encoding, print }?: {
|
|
6
21
|
dir?: string;
|
|
7
|
-
encoding: '
|
|
22
|
+
encoding: 'binary';
|
|
8
23
|
print?: boolean;
|
|
9
24
|
}): Promise<Buffer>;
|
|
10
25
|
export declare function fread(fp: string, { dir, encoding, print }?: {
|
|
11
26
|
dir?: string;
|
|
12
|
-
encoding?: Encoding | '
|
|
27
|
+
encoding?: Encoding | 'auto';
|
|
13
28
|
print?: boolean;
|
|
14
29
|
}): Promise<string>;
|
|
15
30
|
export declare function fread_lines(fp: string, options?: {
|
|
16
31
|
dir?: string;
|
|
17
|
-
encoding?: Exclude<Encoding, '
|
|
32
|
+
encoding?: Exclude<Encoding, 'binary'> | 'auto';
|
|
18
33
|
print?: boolean;
|
|
19
34
|
}): Promise<string[]>;
|
|
20
35
|
export declare function fread_json<T = any>(fp: string, options?: {
|
|
@@ -22,7 +37,11 @@ export declare function fread_json<T = any>(fp: string, options?: {
|
|
|
22
37
|
encoding?: Encoding;
|
|
23
38
|
print?: boolean;
|
|
24
39
|
}): Promise<T>;
|
|
25
|
-
export declare function fwrite(fp: string, data:
|
|
40
|
+
export declare function fwrite(fp: string, data: Buffer, options?: {
|
|
41
|
+
dir?: string;
|
|
42
|
+
print?: boolean;
|
|
43
|
+
}): Promise<void>;
|
|
44
|
+
export declare function fwrite(fp: string, data: any, options?: {
|
|
26
45
|
dir?: string;
|
|
27
46
|
encoding?: Encoding;
|
|
28
47
|
print?: boolean;
|
|
@@ -49,7 +68,6 @@ export declare function flist(fpd: string, { filter, deep, absolute, print }?: {
|
|
|
49
68
|
- fp: path
|
|
50
69
|
- options?:
|
|
51
70
|
- print?: `true` (effective only delete single file)
|
|
52
|
-
- fast?: `false` use rimraf to delete quickly
|
|
53
71
|
*/
|
|
54
72
|
export declare function fdelete(fp: string, { print, fast }?: {
|
|
55
73
|
print?: boolean;
|
|
@@ -76,12 +94,15 @@ export declare function fmove(src: string, dst: string, { overwrite, print }?: {
|
|
|
76
94
|
print?: boolean;
|
|
77
95
|
}): Promise<void>;
|
|
78
96
|
/** rename file
|
|
79
|
-
-
|
|
80
|
-
-
|
|
81
|
-
-
|
|
97
|
+
- fp: current filename/path
|
|
98
|
+
- fp_: new filename/path
|
|
99
|
+
- options?:
|
|
100
|
+
- fpd?: fp and fp_ is in same directory
|
|
101
|
+
- print?: `true`
|
|
102
|
+
- overwrite?: `true` better performance without check
|
|
82
103
|
*/
|
|
83
|
-
export declare function frename(fp: string, fp_: string, {
|
|
84
|
-
|
|
104
|
+
export declare function frename(fp: string, fp_: string, { fpd, print, overwrite }?: {
|
|
105
|
+
fpd?: string;
|
|
85
106
|
print?: boolean;
|
|
86
107
|
overwrite?: boolean;
|
|
87
108
|
}): Promise<void>;
|
|
@@ -121,9 +142,9 @@ export declare function freplace(fp: string, pattern: string | RegExp, replaceme
|
|
|
121
142
|
- fp: file absolute path
|
|
122
143
|
- options?:
|
|
123
144
|
- dryrun?: `true`
|
|
124
|
-
- encoding?: `'
|
|
145
|
+
- encoding?: `'auto'`
|
|
125
146
|
*/
|
|
126
147
|
export declare function f2utf8(fp: string, { dryrun, encoding, }?: {
|
|
127
148
|
dryrun?: boolean;
|
|
128
|
-
encoding?: Encoding | '
|
|
149
|
+
encoding?: Encoding | 'auto';
|
|
129
150
|
}): Promise<void>;
|
package/file.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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;
|
|
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 = exports.mfs = exports.create_mfs = exports.MFS = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const upath_1 = (0, tslib_1.__importDefault)(require("upath"));
|
|
@@ -9,25 +9,32 @@ const readdir_enhanced_1 = require("readdir-enhanced");
|
|
|
9
9
|
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
10
10
|
const rimraf_1 = (0, tslib_1.__importDefault)(require("rimraf"));
|
|
11
11
|
const debounce_1 = (0, tslib_1.__importDefault)(require("lodash/debounce"));
|
|
12
|
+
const memfs_1 = (0, tslib_1.__importDefault)(require("memfs"));
|
|
13
|
+
exports.MFS = memfs_1.default;
|
|
12
14
|
const prototype_1 = require("./prototype");
|
|
13
15
|
const utils_1 = require("./utils");
|
|
14
|
-
|
|
16
|
+
(0, tslib_1.__exportStar)(require("./ufs"), exports);
|
|
17
|
+
function create_mfs() {
|
|
18
|
+
return exports.mfs = Object.assign(memfs_1.default.createFsFromVolume(new memfs_1.default.Volume()), { join: upath_1.default.join.bind(upath_1.default) });
|
|
19
|
+
}
|
|
20
|
+
exports.create_mfs = create_mfs;
|
|
21
|
+
async function fread(fp, { dir, encoding = 'utf-8', print = true } = {}) {
|
|
15
22
|
if (dir)
|
|
16
23
|
fp = upath_1.default.join(dir, fp);
|
|
17
24
|
else if (!upath_1.default.isAbsolute(fp))
|
|
18
25
|
throw new Error('fp must be absolute path, or pass in "dir" parameter');
|
|
19
26
|
if (print)
|
|
20
|
-
console.log(
|
|
27
|
+
console.log(`read: ${fp}`);
|
|
21
28
|
const buffer = await fs_1.promises.readFile(fp);
|
|
22
|
-
if (encoding === '
|
|
29
|
+
if (encoding === 'binary')
|
|
23
30
|
return buffer;
|
|
24
|
-
if (encoding === '
|
|
31
|
+
if (encoding === 'utf-8')
|
|
25
32
|
return buffer.toString('utf8');
|
|
26
|
-
if (encoding === '
|
|
33
|
+
if (encoding === 'auto') {
|
|
27
34
|
const { detect } = await Promise.resolve().then(() => (0, tslib_1.__importStar)(require('chardet')));
|
|
28
35
|
encoding = detect(buffer);
|
|
29
36
|
if (print)
|
|
30
|
-
console.log(`${fp} probably has encoding: ${encoding}`);
|
|
37
|
+
console.log(`${fp} probably has encoding: ${encoding.toLowerCase()}`);
|
|
31
38
|
}
|
|
32
39
|
return iconv_lite_1.default.decode(buffer, encoding);
|
|
33
40
|
}
|
|
@@ -41,14 +48,14 @@ async function fread_json(fp, options = {}) {
|
|
|
41
48
|
return JSON.parse(await fread(fp, options));
|
|
42
49
|
}
|
|
43
50
|
exports.fread_json = fread_json;
|
|
44
|
-
async function fwrite(fp, data, { dir, encoding = '
|
|
51
|
+
async function fwrite(fp, data, { dir, encoding = 'utf-8', print = true } = {}) {
|
|
45
52
|
if (dir)
|
|
46
53
|
fp = upath_1.default.join(dir, fp);
|
|
47
54
|
else if (!upath_1.default.isAbsolute(fp))
|
|
48
55
|
throw new Error('fp must be absolute path, or pass in "dir" parameter');
|
|
49
56
|
if (print)
|
|
50
57
|
console.log('write:', fp);
|
|
51
|
-
if (encoding === '
|
|
58
|
+
if (encoding === 'gb18030')
|
|
52
59
|
data = iconv_lite_1.default.encode(data, encoding);
|
|
53
60
|
if (!Buffer.isBuffer(data) && typeof data !== 'string')
|
|
54
61
|
data = (0, prototype_1.to_json)(data);
|
|
@@ -101,7 +108,6 @@ exports.flist = flist;
|
|
|
101
108
|
- fp: path
|
|
102
109
|
- options?:
|
|
103
110
|
- print?: `true` (effective only delete single file)
|
|
104
|
-
- fast?: `false` use rimraf to delete quickly
|
|
105
111
|
*/
|
|
106
112
|
async function fdelete(fp, { print = true, fast = false } = {}) {
|
|
107
113
|
if (fp.length < 6)
|
|
@@ -160,14 +166,17 @@ async function fmove(src, dst, { overwrite = false, print = true } = {}) {
|
|
|
160
166
|
}
|
|
161
167
|
exports.fmove = fmove;
|
|
162
168
|
/** rename file
|
|
163
|
-
-
|
|
164
|
-
-
|
|
165
|
-
-
|
|
169
|
+
- fp: current filename/path
|
|
170
|
+
- fp_: new filename/path
|
|
171
|
+
- options?:
|
|
172
|
+
- fpd?: fp and fp_ is in same directory
|
|
173
|
+
- print?: `true`
|
|
174
|
+
- overwrite?: `true` better performance without check
|
|
166
175
|
*/
|
|
167
|
-
async function frename(fp, fp_, {
|
|
168
|
-
if (
|
|
169
|
-
fp = upath_1.default.join(
|
|
170
|
-
fp_ = upath_1.default.join(
|
|
176
|
+
async function frename(fp, fp_, { fpd, print = true, overwrite = true } = {}) {
|
|
177
|
+
if (fpd) {
|
|
178
|
+
fp = upath_1.default.join(fpd, fp);
|
|
179
|
+
fp_ = upath_1.default.join(fpd, fp_);
|
|
171
180
|
}
|
|
172
181
|
else if (!upath_1.default.isAbsolute(fp) || !upath_1.default.isAbsolute(fp_))
|
|
173
182
|
throw new Error('fp and fp_ must be absolute path');
|
|
@@ -273,9 +282,9 @@ exports.freplace = freplace;
|
|
|
273
282
|
- fp: file absolute path
|
|
274
283
|
- options?:
|
|
275
284
|
- dryrun?: `true`
|
|
276
|
-
- encoding?: `'
|
|
285
|
+
- encoding?: `'auto'`
|
|
277
286
|
*/
|
|
278
|
-
async function f2utf8(fp, { dryrun = true, encoding = '
|
|
287
|
+
async function f2utf8(fp, { dryrun = true, encoding = 'auto', } = {}) {
|
|
279
288
|
const text = await fread(fp, { encoding });
|
|
280
289
|
if (dryrun) {
|
|
281
290
|
console.log(text.slice(0, 10000));
|
package/file.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file.js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A,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 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}`)\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) && 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 - 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 \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 - 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:/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 (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,+DAAuB;AAWd,cAXF,eAAG,CAWE;AAJZ,2CAAqC;AACrC,mCAAgC;AAChC,qDAAqB;AAOrB,SAAgB,UAAU;IACtB,OAAO,WAAG,GAAG,MAAM,CAAC,MAAM,CACtB,eAAG,CAAC,kBAAkB,CAClB,IAAI,eAAG,CAAC,MAAM,EAAE,CACnB,EACD,EAA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,CAAC,GAAG,OAAO,CAAA;AAClC,CAAC;AArBD,wBAqBC;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 rimraf from 'rimraf'\n\nimport debounce from 'lodash/debounce'\n\n\nimport MFS from 'memfs'\ndeclare module 'memfs' {\n interface IFs {\n join: typeof path.join\n }\n}\n\nimport { to_json } from './prototype'\nimport { dedent } from './utils'\nexport * from './ufs'\n\nexport { MFS }\n\nexport type Encoding = 'utf-8' | 'gb18030' | 'shift-jis' | 'binary'\n\n\nexport function create_mfs () {\n return mfs = Object.assign(\n MFS.createFsFromVolume(\n new MFS.Volume()\n ), \n { join: path.join.bind(path) }\n )\n}\n\nexport let mfs: MFS.IFs & { join: typeof path.join }\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/net.d.ts
CHANGED
|
@@ -11,10 +11,10 @@ declare module 'tough-cookie' {
|
|
|
11
11
|
import './prototype';
|
|
12
12
|
import type { Encoding } from './file';
|
|
13
13
|
export declare enum MyProxy {
|
|
14
|
-
|
|
15
|
-
whistle = "http://
|
|
14
|
+
socks5 = "http://localhost:10080",
|
|
15
|
+
whistle = "http://localhost:8899"
|
|
16
16
|
}
|
|
17
|
-
export declare const
|
|
17
|
+
export declare const cookies: {
|
|
18
18
|
store: MemoryCookieStore;
|
|
19
19
|
jar: request_lib.CookieJar;
|
|
20
20
|
get(domain_or_url: string, str?: boolean): string | Cookie[];
|
|
@@ -23,49 +23,58 @@ export { Cookie };
|
|
|
23
23
|
export declare const _request: request_lib.RequestAPI<request_promise.RequestPromise<any>, request_promise.RequestPromiseOptions, request_lib.RequiredUriUrl>;
|
|
24
24
|
export declare function request_retry(retries: number, request_options: request_promise.OptionsWithUrl): Promise<request_lib.Response>;
|
|
25
25
|
export interface RequestOptions {
|
|
26
|
+
method?: 'get' | 'post' | 'put' | 'head' | 'delete' | 'patch';
|
|
26
27
|
queries?: Record<string, any>;
|
|
27
|
-
|
|
28
|
+
headers?: Record<string, string>;
|
|
28
29
|
body?: string | Record<string, any>;
|
|
29
|
-
|
|
30
|
-
json?: boolean;
|
|
31
|
-
/** form (x-www-form-urlencoded or multipart) format */
|
|
32
|
-
form?: boolean | 'application/x-www-form-urlencoded' | 'multipart/form-data';
|
|
33
|
-
/** `false` when proxy is true then use MyProxy.whistle */
|
|
30
|
+
type?: 'application/json' | 'application/x-www-form-urlencoded' | 'multipart/form-data';
|
|
34
31
|
proxy?: boolean | MyProxy | string;
|
|
35
|
-
method?: 'GET' | 'POST' | 'PUT' | 'HEAD' | 'DELETE' | 'PATCH';
|
|
36
|
-
headers?: Record<string, string>;
|
|
37
|
-
/** `(webpage content-type: charset=gb18030) || 'UTF-8'` */
|
|
38
32
|
encoding?: Encoding;
|
|
39
|
-
|
|
40
|
-
retries?: boolean | number;
|
|
41
|
-
/** `20 * 1000` */
|
|
33
|
+
retries?: true | number;
|
|
42
34
|
timeout?: number;
|
|
43
35
|
auth?: {
|
|
44
36
|
username: string;
|
|
45
37
|
password: string;
|
|
46
38
|
};
|
|
47
|
-
/** `raw -> false; else -> true` */
|
|
48
39
|
gzip?: boolean;
|
|
49
40
|
cookies?: Record<string, string>;
|
|
50
41
|
}
|
|
51
42
|
export interface RequestRawOptions extends RequestOptions {
|
|
52
43
|
raw: true;
|
|
53
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
- url: must be full url
|
|
47
|
+
- options:
|
|
48
|
+
- raw: `false` 传入后返回整个 response
|
|
49
|
+
- encoding: `(response content-type: charset=gb18030) || 'utf-8'` when 'binary' then return buffer
|
|
50
|
+
- type: `'application/json'` request content-type header (if has body)
|
|
51
|
+
- proxy: `false` proxy === true then use MyProxy.whistle
|
|
52
|
+
- retries: `false` could be true (default 3 times) or retry times
|
|
53
|
+
- timeout: `20 * 1000`
|
|
54
|
+
- gzip: `raw -> false; else -> true`
|
|
55
|
+
*/
|
|
54
56
|
export declare function request(url: string | URL): Promise<string>;
|
|
55
57
|
export declare function request(url: string | URL, options: RequestRawOptions): Promise<request_lib.Response>;
|
|
56
58
|
export declare function request(url: string | URL, options: RequestOptions & {
|
|
57
|
-
encoding: '
|
|
59
|
+
encoding: 'binary';
|
|
58
60
|
}): Promise<Buffer>;
|
|
59
61
|
export declare function request(url: string | URL, options: RequestOptions): Promise<string>;
|
|
62
|
+
/** make http request and parse body as json */
|
|
60
63
|
export declare function request_json<T = any>(url: string | URL, options?: RequestOptions): Promise<T>;
|
|
61
64
|
/** use $.html(cheerio_element) to get outer html */
|
|
62
65
|
export declare function parse_html(html: string): cheerio.Root;
|
|
63
66
|
/** use $.html(cheerio_element) to get outer html */
|
|
64
67
|
export declare function request_page(url: string | URL, options?: RequestOptions): Promise<cheerio.Root>;
|
|
65
|
-
export declare function to_curl(url: string | URL, { queries, headers, method, body,
|
|
68
|
+
export declare function to_curl(url: string | URL, { queries, headers, method, body, proxy, exe }?: RequestOptions & {
|
|
66
69
|
exe?: boolean;
|
|
67
70
|
}): string;
|
|
68
|
-
/**
|
|
71
|
+
/** post json to http://localhost:8421/api/rpc
|
|
72
|
+
- func: function name
|
|
73
|
+
- args?: argument array
|
|
74
|
+
- options?:
|
|
75
|
+
- ignore?: `false` wait for execution but do not serialize result to response
|
|
76
|
+
- async?: `false` do not wait for exec
|
|
77
|
+
*/
|
|
69
78
|
export declare function rpc(func: string, args?: any[], { url, async: _async, ignore }?: {
|
|
70
79
|
url?: string;
|
|
71
80
|
async?: boolean;
|