xshell 0.0.9 → 0.0.13
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 +12 -12
- package/extension.js.map +1 -1
- package/file.d.ts +39 -17
- package/file.js +57 -53
- 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 +18 -24
- 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 +129 -107
- package/prototype.js.map +1 -1
- package/repl.d.ts +2 -5
- package/repl.js +86 -73
- package/repl.js.map +1 -1
- package/server.d.ts +11 -7
- package/server.js +116 -112
- package/server.js.map +1 -1
- package/tsconfig.json +18 -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 +87 -44
- package/utils.js.map +1 -1
- package/xshell.js +1 -1
- package/xshell.js.map +1 -1
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,gB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,UAAU,GAAG,IAAI,iBAAQ,CAAC,KAAK,CAAC,IAAI,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;
|
|
@@ -45,11 +64,10 @@ export declare function flist(fpd: string, { filter, deep, absolute, print }?: {
|
|
|
45
64
|
absolute?: boolean;
|
|
46
65
|
print?: boolean;
|
|
47
66
|
}): Promise<string[]>;
|
|
48
|
-
/** delete file or directory
|
|
67
|
+
/** delete file or directory (use rimraf to delete directory fast)
|
|
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;
|
|
@@ -59,28 +77,32 @@ export declare function fdelete(fp: string, { print, fast }?: {
|
|
|
59
77
|
- src: src file/directory absolute path
|
|
60
78
|
- dst: dst file/directory absolute path
|
|
61
79
|
@example
|
|
62
|
-
fcopy('
|
|
80
|
+
fcopy('d:/temp/camera/', 'd:/camera/')
|
|
63
81
|
*/
|
|
64
|
-
export declare function fcopy(src: string, dst: string, { print }?: {
|
|
82
|
+
export declare function fcopy(src: string, dst: string, { print, overwrite, }?: {
|
|
65
83
|
print?: boolean;
|
|
84
|
+
overwrite?: boolean;
|
|
66
85
|
}): Promise<void>;
|
|
67
86
|
/** move file or direcotry
|
|
68
87
|
- src: src file/directory absolute path
|
|
69
88
|
- dst: dst file/directory absolute path
|
|
70
89
|
@example
|
|
71
|
-
fmove('
|
|
90
|
+
fmove('d:/temp/camera/', 'd:/camera/')
|
|
72
91
|
*/
|
|
73
92
|
export declare function fmove(src: string, dst: string, { overwrite, print }?: {
|
|
74
93
|
overwrite?: boolean;
|
|
75
94
|
print?: boolean;
|
|
76
95
|
}): Promise<void>;
|
|
77
96
|
/** rename file
|
|
78
|
-
-
|
|
79
|
-
-
|
|
80
|
-
-
|
|
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
|
|
81
103
|
*/
|
|
82
|
-
export declare function frename(fp: string, fp_: string, {
|
|
83
|
-
|
|
104
|
+
export declare function frename(fp: string, fp_: string, { fpd, print, overwrite }?: {
|
|
105
|
+
fpd?: string;
|
|
84
106
|
print?: boolean;
|
|
85
107
|
overwrite?: boolean;
|
|
86
108
|
}): Promise<void>;
|
|
@@ -120,9 +142,9 @@ export declare function freplace(fp: string, pattern: string | RegExp, replaceme
|
|
|
120
142
|
- fp: file absolute path
|
|
121
143
|
- options?:
|
|
122
144
|
- dryrun?: `true`
|
|
123
|
-
- encoding?: `'
|
|
145
|
+
- encoding?: `'auto'`
|
|
124
146
|
*/
|
|
125
147
|
export declare function f2utf8(fp: string, { dryrun, encoding, }?: {
|
|
126
148
|
dryrun?: boolean;
|
|
127
|
-
encoding?: Encoding | '
|
|
149
|
+
encoding?: Encoding | 'auto';
|
|
128
150
|
}): Promise<void>;
|
package/file.js
CHANGED
|
@@ -1,36 +1,40 @@
|
|
|
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
|
-
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
|
|
13
|
-
|
|
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"));
|
|
12
|
+
const memfs_1 = (0, tslib_1.__importDefault)(require("memfs"));
|
|
13
|
+
exports.MFS = memfs_1.default;
|
|
15
14
|
const prototype_1 = require("./prototype");
|
|
16
15
|
const utils_1 = require("./utils");
|
|
17
|
-
|
|
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 } = {}) {
|
|
18
22
|
if (dir)
|
|
19
23
|
fp = upath_1.default.join(dir, fp);
|
|
20
24
|
else if (!upath_1.default.isAbsolute(fp))
|
|
21
25
|
throw new Error('fp must be absolute path, or pass in "dir" parameter');
|
|
22
26
|
if (print)
|
|
23
|
-
console.log(
|
|
27
|
+
console.log(`read: ${fp}`);
|
|
24
28
|
const buffer = await fs_1.promises.readFile(fp);
|
|
25
|
-
if (encoding === '
|
|
29
|
+
if (encoding === 'binary')
|
|
26
30
|
return buffer;
|
|
27
|
-
if (encoding === '
|
|
31
|
+
if (encoding === 'utf-8')
|
|
28
32
|
return buffer.toString('utf8');
|
|
29
|
-
if (encoding === '
|
|
30
|
-
const { detect } = await Promise.resolve().then(() => tslib_1.__importStar(require('chardet')));
|
|
33
|
+
if (encoding === 'auto') {
|
|
34
|
+
const { detect } = await Promise.resolve().then(() => (0, tslib_1.__importStar)(require('chardet')));
|
|
31
35
|
encoding = detect(buffer);
|
|
32
36
|
if (print)
|
|
33
|
-
console.log(`${fp} probably has encoding: ${encoding}`);
|
|
37
|
+
console.log(`${fp} probably has encoding: ${encoding.toLowerCase()}`);
|
|
34
38
|
}
|
|
35
39
|
return iconv_lite_1.default.decode(buffer, encoding);
|
|
36
40
|
}
|
|
@@ -44,17 +48,17 @@ async function fread_json(fp, options = {}) {
|
|
|
44
48
|
return JSON.parse(await fread(fp, options));
|
|
45
49
|
}
|
|
46
50
|
exports.fread_json = fread_json;
|
|
47
|
-
async function fwrite(fp, data, { dir, encoding = '
|
|
51
|
+
async function fwrite(fp, data, { dir, encoding = 'utf-8', print = true } = {}) {
|
|
48
52
|
if (dir)
|
|
49
53
|
fp = upath_1.default.join(dir, fp);
|
|
50
54
|
else if (!upath_1.default.isAbsolute(fp))
|
|
51
55
|
throw new Error('fp must be absolute path, or pass in "dir" parameter');
|
|
52
56
|
if (print)
|
|
53
57
|
console.log('write:', fp);
|
|
54
|
-
if (encoding === '
|
|
58
|
+
if (encoding === 'gb18030')
|
|
55
59
|
data = iconv_lite_1.default.encode(data, encoding);
|
|
56
|
-
if (!Buffer.isBuffer(data) &&
|
|
57
|
-
data = prototype_1.to_json(data);
|
|
60
|
+
if (!Buffer.isBuffer(data) && typeof data !== 'string')
|
|
61
|
+
data = (0, prototype_1.to_json)(data);
|
|
58
62
|
await fs_1.promises.writeFile(fp, data);
|
|
59
63
|
}
|
|
60
64
|
exports.fwrite = fwrite;
|
|
@@ -65,7 +69,7 @@ async function fappend(fp, data, { dir, print = true } = {}) {
|
|
|
65
69
|
throw new Error('fp must be absolute path, or pass in "dir" parameter');
|
|
66
70
|
if (print)
|
|
67
71
|
console.log('append:', fp);
|
|
68
|
-
if (!Buffer.isBuffer(data) &&
|
|
72
|
+
if (!Buffer.isBuffer(data) && typeof data !== 'string')
|
|
69
73
|
throw new Error('data is not Buffer or string');
|
|
70
74
|
await fs_1.promises.appendFile(fp, data);
|
|
71
75
|
}
|
|
@@ -81,7 +85,7 @@ exports.fappend = fappend;
|
|
|
81
85
|
async function flist(fpd, { filter, deep = false, absolute = false, print = true } = {}) {
|
|
82
86
|
if (!upath_1.default.isAbsolute(fpd))
|
|
83
87
|
throw new Error('fpd must be absolute path');
|
|
84
|
-
let fps = await readdir_enhanced_1.readdirAsync(fpd, {
|
|
88
|
+
let fps = await (0, readdir_enhanced_1.readdirAsync)(fpd, {
|
|
85
89
|
...(absolute ? { basePath: fpd } : {}),
|
|
86
90
|
deep,
|
|
87
91
|
stats: false,
|
|
@@ -92,7 +96,7 @@ async function flist(fpd, { filter, deep = false, absolute = false, print = true
|
|
|
92
96
|
console.log(fp);
|
|
93
97
|
return fp;
|
|
94
98
|
});
|
|
95
|
-
if (
|
|
99
|
+
if (filter instanceof RegExp)
|
|
96
100
|
return fps.filter(fp => filter.test(fp));
|
|
97
101
|
else if (filter)
|
|
98
102
|
return fps.filter(filter);
|
|
@@ -100,11 +104,10 @@ async function flist(fpd, { filter, deep = false, absolute = false, print = true
|
|
|
100
104
|
return fps;
|
|
101
105
|
}
|
|
102
106
|
exports.flist = flist;
|
|
103
|
-
/** delete file or directory
|
|
107
|
+
/** delete file or directory (use rimraf to delete directory fast)
|
|
104
108
|
- fp: path
|
|
105
109
|
- options?:
|
|
106
110
|
- print?: `true` (effective only delete single file)
|
|
107
|
-
- fast?: `false` use rimraf to delete quickly
|
|
108
111
|
*/
|
|
109
112
|
async function fdelete(fp, { print = true, fast = false } = {}) {
|
|
110
113
|
if (fp.length < 6)
|
|
@@ -112,18 +115,16 @@ async function fdelete(fp, { print = true, fast = false } = {}) {
|
|
|
112
115
|
if (!upath_1.default.isAbsolute(fp))
|
|
113
116
|
throw new Error('fpd must be absolute path');
|
|
114
117
|
if (fp.is_dir) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
});
|
|
118
|
+
if (print)
|
|
119
|
+
console.log(`delete directory: ${fp}`.red);
|
|
120
|
+
await new Promise((resolve, reject) => {
|
|
121
|
+
(0, rimraf_1.default)(fp, { glob: false, disableGlob: true }, error => {
|
|
122
|
+
if (error)
|
|
123
|
+
reject(error);
|
|
124
|
+
else
|
|
125
|
+
resolve();
|
|
124
126
|
});
|
|
125
|
-
|
|
126
|
-
await trash_1.default(fp, { glob: false });
|
|
127
|
+
});
|
|
127
128
|
}
|
|
128
129
|
else {
|
|
129
130
|
if (print)
|
|
@@ -136,23 +137,23 @@ exports.fdelete = fdelete;
|
|
|
136
137
|
- src: src file/directory absolute path
|
|
137
138
|
- dst: dst file/directory absolute path
|
|
138
139
|
@example
|
|
139
|
-
fcopy('
|
|
140
|
+
fcopy('d:/temp/camera/', 'd:/camera/')
|
|
140
141
|
*/
|
|
141
|
-
async function fcopy(src, dst, { print = true } = {}) {
|
|
142
|
+
async function fcopy(src, dst, { print = true, overwrite = true, } = {}) {
|
|
142
143
|
if (src.endsWith('/') !== dst.endsWith('/'))
|
|
143
144
|
throw new Error('src and dst must be both file path or directory path');
|
|
144
145
|
if (!upath_1.default.isAbsolute(src) || !upath_1.default.isAbsolute(dst))
|
|
145
146
|
throw new Error('src and dst must be absolute path');
|
|
146
147
|
if (print)
|
|
147
148
|
console.log(`copy: ${src} → ${dst}`);
|
|
148
|
-
await fs_extra_1.default.copy(src, dst);
|
|
149
|
+
await fs_extra_1.default.copy(src, dst, { overwrite, errorOnExist: true });
|
|
149
150
|
}
|
|
150
151
|
exports.fcopy = fcopy;
|
|
151
152
|
/** move file or direcotry
|
|
152
153
|
- src: src file/directory absolute path
|
|
153
154
|
- dst: dst file/directory absolute path
|
|
154
155
|
@example
|
|
155
|
-
fmove('
|
|
156
|
+
fmove('d:/temp/camera/', 'd:/camera/')
|
|
156
157
|
*/
|
|
157
158
|
async function fmove(src, dst, { overwrite = false, print = true } = {}) {
|
|
158
159
|
if (src.endsWith('/') !== dst.endsWith('/'))
|
|
@@ -165,14 +166,17 @@ async function fmove(src, dst, { overwrite = false, print = true } = {}) {
|
|
|
165
166
|
}
|
|
166
167
|
exports.fmove = fmove;
|
|
167
168
|
/** rename file
|
|
168
|
-
-
|
|
169
|
-
-
|
|
170
|
-
-
|
|
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
|
|
171
175
|
*/
|
|
172
|
-
async function frename(fp, fp_, {
|
|
173
|
-
if (
|
|
174
|
-
fp = upath_1.default.join(
|
|
175
|
-
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_);
|
|
176
180
|
}
|
|
177
181
|
else if (!upath_1.default.isAbsolute(fp) || !upath_1.default.isAbsolute(fp_))
|
|
178
182
|
throw new Error('fp and fp_ must be absolute path');
|
|
@@ -223,9 +227,9 @@ async function flink(fp_real, fp_link, { junction = false, print = true } = {})
|
|
|
223
227
|
}
|
|
224
228
|
exports.flink = flink;
|
|
225
229
|
function link_shortcut(target, name, { args } = {}) {
|
|
226
|
-
const cmd = utils_1.dedent `
|
|
230
|
+
const cmd = (0, utils_1.dedent) `
|
|
227
231
|
$wsh_shell = New-Object -comObject WScript.Shell
|
|
228
|
-
$shortcut = $wsh_shell.CreateShortcut("
|
|
232
|
+
$shortcut = $wsh_shell.CreateShortcut("d:/links/#{name}.lnk")
|
|
229
233
|
$shortcut.TargetPath = '${target}'
|
|
230
234
|
$shortcut.Arguments = "${args || ''}"
|
|
231
235
|
$shortcut.WorkingDirectory = '${target.fdir}'
|
|
@@ -259,11 +263,11 @@ async function fwatch(fp, onchange, { exec = true } = {}) {
|
|
|
259
263
|
_watcher.close();
|
|
260
264
|
if (exec)
|
|
261
265
|
await onchange('change', fp);
|
|
262
|
-
const debounced_onchange = debounce_1.default((event, filename) => {
|
|
266
|
+
const debounced_onchange = (0, debounce_1.default)((event, filename) => {
|
|
263
267
|
console.log(`file changed (${event}): ${filename}`);
|
|
264
268
|
onchange(event, upath_1.default.normalize(filename));
|
|
265
269
|
}, 500, { leading: false, trailing: true });
|
|
266
|
-
const watcher = fs_1.watch(fp, debounced_onchange);
|
|
270
|
+
const watcher = (0, fs_1.watch)(fp, debounced_onchange);
|
|
267
271
|
watcher.on('error', error => { console.error(error); });
|
|
268
272
|
return exports.fwatchers[fp] = watcher;
|
|
269
273
|
}
|
|
@@ -278,9 +282,9 @@ exports.freplace = freplace;
|
|
|
278
282
|
- fp: file absolute path
|
|
279
283
|
- options?:
|
|
280
284
|
- dryrun?: `true`
|
|
281
|
-
- encoding?: `'
|
|
285
|
+
- encoding?: `'auto'`
|
|
282
286
|
*/
|
|
283
|
-
async function f2utf8(fp, { dryrun = true, encoding = '
|
|
287
|
+
async function f2utf8(fp, { dryrun = true, encoding = 'auto', } = {}) {
|
|
284
288
|
const text = await fread(fp, { encoding });
|
|
285
289
|
if (dryrun) {
|
|
286
290
|
console.log(text.slice(0, 10000));
|
package/file.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[],"mappings":";;;;AAAA,2BAA2C;AAG3C,0DAAwB;AACxB,oEAA8B;AAC9B,uDAA+C;AAC/C,gEAA0B;AAC1B,0DAAyB;AACzB,4DAA2B;AAE3B,uEAAqC;AACrC,uEAAqC;AACrC,uEAAsC;AAGtC,2CAAqC;AACrC,mCAAgC;A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mB;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,+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,EAAE,IAAI,EAAE,eAAI,CAAC,IAAI,CAAC,IAAI,CAAC,eAAI,CAAC,EAAE,CACjC,CAAA;AACL,CAAC;AAPD,gCAOC;AAQM,KAAK,UAAU,KAAK,CAAE,EAAU,EAAE,EACrC,GAAG,EACH,QAAQ,GAAG,OAAO,EACl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,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/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"]}
|