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/process.d.ts
CHANGED
|
@@ -3,13 +3,14 @@ import type { ChildProcess } from 'child_process';
|
|
|
3
3
|
import './prototype';
|
|
4
4
|
import { Encoding } from './file';
|
|
5
5
|
import { inspect } from './utils';
|
|
6
|
-
export declare const
|
|
6
|
+
export declare const fp_root: string;
|
|
7
|
+
export declare const exe_node: string;
|
|
7
8
|
interface StartOptions {
|
|
8
|
-
/** `'
|
|
9
|
+
/** `'d:/'` */
|
|
9
10
|
cwd?: string;
|
|
10
11
|
/** `process.env` overwrite/add to process.env */
|
|
11
12
|
env?: Record<string, string>;
|
|
12
|
-
/** `'
|
|
13
|
+
/** `'utf-8'` child output encoding */
|
|
13
14
|
encoding?: Encoding;
|
|
14
15
|
/** `true` print option (with details) */
|
|
15
16
|
print?: boolean | {
|
|
@@ -25,12 +26,12 @@ interface StartOptions {
|
|
|
25
26
|
detached?: boolean;
|
|
26
27
|
}
|
|
27
28
|
/** start process
|
|
28
|
-
- exe: .exe path or filename (full path is recommanded to skip
|
|
29
|
+
- exe: .exe path or filename (full path is recommanded to skip path searching for better perf)
|
|
29
30
|
- args: `[]` arguments list
|
|
30
31
|
- options
|
|
31
|
-
- cwd?: `
|
|
32
|
+
- cwd?: `fp_root`
|
|
32
33
|
- env?: `process.env` overwrite/add to process.env
|
|
33
|
-
- encoding?: `'
|
|
34
|
+
- encoding?: `'utf-8'` child output encoding
|
|
34
35
|
- print?: `true` print option (with details)
|
|
35
36
|
- stdio?: `'pipe'` when 'ignore' then ignore stdio processing
|
|
36
37
|
- detached?: `false` whether to break the connection with child (ignore stdio, unref)
|
|
@@ -50,12 +51,12 @@ export interface CallResult<T = string> {
|
|
|
50
51
|
[inspect.custom](): string;
|
|
51
52
|
}
|
|
52
53
|
/** call process for result
|
|
53
|
-
- exe: .exe path or filename (full path is recommanded to skip
|
|
54
|
+
- exe: .exe path or filename (full path is recommanded to skip path searching for better perf)
|
|
54
55
|
- args: `[]` arguments list
|
|
55
56
|
- options
|
|
56
|
-
- cwd?: `'
|
|
57
|
+
- cwd?: `'d:/'`
|
|
57
58
|
- env?: `process.env` overwrite/add to process.env
|
|
58
|
-
- encoding?: `'
|
|
59
|
+
- encoding?: `'utf-8'` child output encoding
|
|
59
60
|
- print?: `true` print option (with details)
|
|
60
61
|
- stdio?: `'pipe'` when 'ignore' then ignore stdio processing
|
|
61
62
|
- detached?: `false` whether to break the connection with child (ignore stdio, unref)
|
|
@@ -63,25 +64,24 @@ export interface CallResult<T = string> {
|
|
|
63
64
|
*/
|
|
64
65
|
export declare function call(exe: string, args?: string[]): Promise<CallResult<string>>;
|
|
65
66
|
export declare function call(exe: string, args?: string[], options?: CallOptions & {
|
|
66
|
-
encoding
|
|
67
|
-
init_buffer_size?: number;
|
|
68
|
-
}): Promise<CallResult<Buffer>>;
|
|
69
|
-
export declare function call(exe: string, args?: string[], options?: CallOptions & {
|
|
70
|
-
encoding?: 'UTF-8' | 'GB18030';
|
|
67
|
+
encoding?: 'utf-8' | 'gb18030';
|
|
71
68
|
}): Promise<CallResult<string>>;
|
|
69
|
+
export declare function call(exe: string, args?: string[], options?: CallOptions & {
|
|
70
|
+
encoding: 'binary';
|
|
71
|
+
}): Promise<CallResult<Buffer>>;
|
|
72
72
|
/** call node <js> for result
|
|
73
73
|
- js: .js path (relative path will resolve based on cwd)
|
|
74
74
|
- args: `[]` arguments list
|
|
75
75
|
- options
|
|
76
|
-
- cwd?: `'
|
|
76
|
+
- cwd?: `'d:/'`
|
|
77
77
|
- env?: `process.env` overwrite/add to process.env
|
|
78
|
-
- encoding?: `'
|
|
78
|
+
- encoding?: `'utf-8'` child process output encoding
|
|
79
79
|
- print?: `true` print option (with details)
|
|
80
80
|
- stdio?: `'pipe'` when 'ignore' then ignore stdio processing
|
|
81
81
|
- detached?: `false` whether to break the connection with child (ignore stdio, unref)
|
|
82
82
|
- throw_code?: `true` whether to throw Error when code is not 0
|
|
83
83
|
*/
|
|
84
84
|
export declare function call_node(js: string, args?: string[], options?: CallOptions & {
|
|
85
|
-
encoding?: '
|
|
85
|
+
encoding?: 'utf-8' | 'gb18030';
|
|
86
86
|
}): Promise<CallResult<string>>;
|
|
87
87
|
export {};
|
package/process.js
CHANGED
|
@@ -1,36 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.call_node = exports.call = exports.start = exports.
|
|
3
|
+
exports.call_node = exports.call = exports.start = exports.exe_node = exports.fp_root = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const child_process_1 = require("child_process");
|
|
6
|
-
const
|
|
7
|
-
const iconv_lite_1 = tslib_1.__importDefault(require("iconv-lite"));
|
|
6
|
+
const iconv_lite_1 = (0, tslib_1.__importDefault)(require("iconv-lite"));
|
|
8
7
|
require("./prototype");
|
|
9
8
|
const utils_1 = require("./utils");
|
|
10
|
-
exports.
|
|
9
|
+
exports.fp_root = `${__dirname}/`.to_slash();
|
|
10
|
+
exports.exe_node = process.execPath.to_slash();
|
|
11
11
|
/** start process
|
|
12
|
-
- exe: .exe path or filename (full path is recommanded to skip
|
|
12
|
+
- exe: .exe path or filename (full path is recommanded to skip path searching for better perf)
|
|
13
13
|
- args: `[]` arguments list
|
|
14
14
|
- options
|
|
15
|
-
- cwd?: `
|
|
15
|
+
- cwd?: `fp_root`
|
|
16
16
|
- env?: `process.env` overwrite/add to process.env
|
|
17
|
-
- encoding?: `'
|
|
17
|
+
- encoding?: `'utf-8'` child output encoding
|
|
18
18
|
- print?: `true` print option (with details)
|
|
19
19
|
- stdio?: `'pipe'` when 'ignore' then ignore stdio processing
|
|
20
20
|
- detached?: `false` whether to break the connection with child (ignore stdio, unref)
|
|
21
21
|
*/
|
|
22
|
-
function start(exe, args = [], { cwd =
|
|
22
|
+
function start(exe, args = [], { cwd = exports.fp_root, encoding = 'utf-8', print = true, stdio = 'pipe', detached = false, env, } = {}) {
|
|
23
23
|
const options = {
|
|
24
24
|
cwd,
|
|
25
25
|
shell: false,
|
|
26
|
-
windowsHide:
|
|
26
|
+
windowsHide: !detached,
|
|
27
27
|
stdio,
|
|
28
|
-
...env ? {
|
|
28
|
+
...env ? {
|
|
29
|
+
env: { ...process.env, ...env }
|
|
30
|
+
} : {},
|
|
29
31
|
};
|
|
30
32
|
if (print)
|
|
31
33
|
console.log(`${exe} ${args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ')}`.blue);
|
|
32
34
|
if (detached) {
|
|
33
|
-
let child = child_process_1.spawn(exe, args, {
|
|
35
|
+
let child = (0, child_process_1.spawn)(exe, args, {
|
|
34
36
|
...options,
|
|
35
37
|
stdio: 'ignore',
|
|
36
38
|
detached: true,
|
|
@@ -38,31 +40,34 @@ function start(exe, args = [], { cwd = 'D:/', encoding = 'UTF-8', print = true,
|
|
|
38
40
|
child.unref();
|
|
39
41
|
return child;
|
|
40
42
|
}
|
|
41
|
-
let child = child_process_1.spawn(exe, args, options);
|
|
43
|
+
let child = (0, child_process_1.spawn)(exe, args, options);
|
|
42
44
|
if (stdio === 'pipe')
|
|
43
45
|
child.stdin.setDefaultEncoding('utf8');
|
|
44
|
-
// prevent child spawn error crashing
|
|
46
|
+
// prevent child spawn error crashing nodejs process
|
|
45
47
|
child.on('error', error => {
|
|
46
48
|
console.error(error);
|
|
47
49
|
});
|
|
48
50
|
if (stdio === 'ignore')
|
|
49
51
|
return child;
|
|
50
|
-
if (encoding !== '
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
if (encoding !== 'binary') {
|
|
53
|
+
if (encoding === 'utf-8') {
|
|
54
|
+
child.stdout.setEncoding('utf-8');
|
|
55
|
+
child.stderr.setEncoding('utf-8');
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
child.stdout = child.stdout.pipe(iconv_lite_1.default.decodeStream(encoding));
|
|
59
|
+
child.stderr = child.stderr.pipe(iconv_lite_1.default.decodeStream(encoding));
|
|
60
|
+
}
|
|
53
61
|
if (print) {
|
|
54
|
-
child.stdout.
|
|
55
|
-
|
|
56
|
-
});
|
|
57
|
-
child.stderr.on('data', (chunk) => {
|
|
58
|
-
process.stderr.write(chunk);
|
|
59
|
-
});
|
|
62
|
+
child.stdout.pipe(process.stdout, { end: false });
|
|
63
|
+
child.stderr.pipe(process.stderr, { end: false });
|
|
60
64
|
}
|
|
61
65
|
}
|
|
62
66
|
return child;
|
|
63
67
|
}
|
|
64
68
|
exports.start = start;
|
|
65
|
-
async function call(exe, args = [],
|
|
69
|
+
async function call(exe, args = [], options = {}) {
|
|
70
|
+
const { encoding = 'utf-8', print = true, throw_code = true, } = options;
|
|
66
71
|
const print_options = typeof print === 'boolean' ?
|
|
67
72
|
{
|
|
68
73
|
command: print,
|
|
@@ -76,64 +81,53 @@ async function call(exe, args = [], { encoding = 'UTF-8', print = true, init_buf
|
|
|
76
81
|
const cmd = `${exe} ${args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ')}`;
|
|
77
82
|
if (print_options.command)
|
|
78
83
|
console.log(cmd.blue);
|
|
79
|
-
|
|
84
|
+
options.print = false;
|
|
85
|
+
let child = start(exe, args, options);
|
|
80
86
|
// --- collect output
|
|
81
|
-
let
|
|
82
|
-
let
|
|
83
|
-
if (encoding !== 'BINARY') {
|
|
84
|
-
stdout = '';
|
|
85
|
-
stderr = '';
|
|
86
|
-
child.stdout.on('data', chunk => {
|
|
87
|
-
if (print_options.stdout)
|
|
88
|
-
process.stdout.write(chunk);
|
|
89
|
-
stdout += chunk;
|
|
90
|
-
});
|
|
91
|
-
child.stderr.on('data', chunk => {
|
|
92
|
-
if (print_options.stderr)
|
|
93
|
-
process.stderr.write(chunk);
|
|
94
|
-
stderr += chunk;
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
const stream_buffer_options = init_buffer_size ? {
|
|
99
|
-
initialSize: init_buffer_size,
|
|
100
|
-
incrementAmount: init_buffer_size,
|
|
101
|
-
} : {};
|
|
102
|
-
stdout = new stream_buffers_1.WritableStreamBuffer(stream_buffer_options);
|
|
103
|
-
stderr = new stream_buffers_1.WritableStreamBuffer(stream_buffer_options);
|
|
104
|
-
if (print_options.stdout)
|
|
105
|
-
child.stdout.pipe(stdout);
|
|
106
|
-
if (print_options.stderr)
|
|
107
|
-
child.stderr.pipe(stderr);
|
|
108
|
-
}
|
|
87
|
+
let stdouts = [];
|
|
88
|
+
let stderrs = [];
|
|
109
89
|
let code, signal;
|
|
110
90
|
await Promise.all([
|
|
111
|
-
new Promise(resolve => {
|
|
112
|
-
child.stdout.on('end', () => { resolve(); });
|
|
113
|
-
}),
|
|
114
|
-
new Promise(resolve => {
|
|
115
|
-
child.stderr.on('end', () => { resolve(); });
|
|
116
|
-
}),
|
|
117
91
|
new Promise(resolve => {
|
|
118
92
|
child.on('exit', (_code, _signal) => {
|
|
119
93
|
code = _code;
|
|
120
94
|
signal = _signal;
|
|
121
95
|
resolve();
|
|
122
96
|
});
|
|
123
|
-
})
|
|
97
|
+
}),
|
|
98
|
+
(async () => {
|
|
99
|
+
for await (const chunk of child.stdout) {
|
|
100
|
+
if (encoding !== 'binary' && print_options.stdout)
|
|
101
|
+
process.stdout.write(chunk);
|
|
102
|
+
stdouts.push(chunk);
|
|
103
|
+
}
|
|
104
|
+
})(),
|
|
105
|
+
(async () => {
|
|
106
|
+
for await (const chunk of child.stderr) {
|
|
107
|
+
if (encoding !== 'binary' && print_options.stderr)
|
|
108
|
+
process.stderr.write(chunk);
|
|
109
|
+
stderrs.push(chunk);
|
|
110
|
+
}
|
|
111
|
+
})()
|
|
124
112
|
]);
|
|
125
|
-
const message = `
|
|
113
|
+
const message = `process(${child.pid}) '${cmd}' exited ${code}${signal ? `, by signal ${signal}` : ''}.`;
|
|
126
114
|
if (print_options.code || code || signal)
|
|
127
|
-
console.log(message[
|
|
115
|
+
console.log(message[code || signal ? 'red' : 'blue']);
|
|
128
116
|
const result = {
|
|
129
117
|
pid: child.pid,
|
|
130
|
-
stdout: encoding
|
|
131
|
-
|
|
118
|
+
stdout: encoding === 'binary' ?
|
|
119
|
+
Buffer.concat(stdouts)
|
|
120
|
+
:
|
|
121
|
+
stdouts.join(''),
|
|
122
|
+
stderr: encoding === 'binary' ?
|
|
123
|
+
Buffer.concat(stderrs)
|
|
124
|
+
:
|
|
125
|
+
stderrs.join(''),
|
|
132
126
|
code,
|
|
133
127
|
signal,
|
|
134
128
|
child,
|
|
135
129
|
[utils_1.inspect.custom]() {
|
|
136
|
-
return utils_1.inspect(this, { omit: ['child'] });
|
|
130
|
+
return (0, utils_1.inspect)(this, { omit: ['child'] });
|
|
137
131
|
}
|
|
138
132
|
};
|
|
139
133
|
if (code && throw_code)
|
|
@@ -145,16 +139,16 @@ exports.call = call;
|
|
|
145
139
|
- js: .js path (relative path will resolve based on cwd)
|
|
146
140
|
- args: `[]` arguments list
|
|
147
141
|
- options
|
|
148
|
-
- cwd?: `'
|
|
142
|
+
- cwd?: `'d:/'`
|
|
149
143
|
- env?: `process.env` overwrite/add to process.env
|
|
150
|
-
- encoding?: `'
|
|
144
|
+
- encoding?: `'utf-8'` child process output encoding
|
|
151
145
|
- print?: `true` print option (with details)
|
|
152
146
|
- stdio?: `'pipe'` when 'ignore' then ignore stdio processing
|
|
153
147
|
- detached?: `false` whether to break the connection with child (ignore stdio, unref)
|
|
154
148
|
- throw_code?: `true` whether to throw Error when code is not 0
|
|
155
149
|
*/
|
|
156
150
|
async function call_node(js, args = [], options) {
|
|
157
|
-
return call(exports.
|
|
151
|
+
return call(exports.exe_node, [js, ...args], options);
|
|
158
152
|
}
|
|
159
153
|
exports.call_node = call_node;
|
|
160
154
|
//# sourceMappingURL=process.js.map
|
package/process.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process.js","sourceRoot":"","sources":["process.ts"],"names":[],"mappings":";;;;AAAA,iDAAqC;AAKrC,mDAAqD;AAErD,oEAA8B;AAG9B,uBAAoB;AAEpB,mCAAiC;AAGpB,QAAA,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAA;AA6BnD;;;;;;;;;;EAUE;AACF,SAAgB,KAAK,CAAE,GAAW,EAAE,OAAiB,EAAE,EAAE,EACrD,GAAG,GAAG,KAAK,EAEX,QAAQ,GAAG,OAAO,EAElB,KAAK,GAAG,IAAI,EAEZ,KAAK,GAAG,MAAM,EAEd,QAAQ,GAAG,KAAK,EAEhB,GAAG,MAEW,EAAG;IACjB,MAAM,OAAO,GAAiB;QAC1B,GAAG;QACH,KAAK,EAAE,KAAK;QACZ,WAAW,EAAE,IAAI;QACjB,KAAK;QACL,GAAI,GAAG,CAAE,CAAC,CAAE,EAAE,GAAG,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,EAAE,CAAE,CAAC,CAAE,EAAG;KACjD,CAAA;IAED,IAAI,KAAK;QACL,OAAO,CAAC,GAAG,CAAC,GAAG,GAAG,IAAK,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG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AC,CAAC;QACxH,IAAI;QACJ,MAAM;QACN,KAAK;QACL,CAAC,eAAO,CAAC,MAAM,CAAC;YACZ,OAAO,eAAO,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;QAC7C,CAAC;KACJ,CAAA;IAED,IAAI,IAAI,IAAI,UAAU;QAClB,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,CAAA;IAEnD,OAAO,MAAM,CAAA;AACjB,CAAC;AAzGD,oBAyGC;AAGD;;;;;;;;;;;EAWE;AACK,KAAK,UAAU,SAAS,CAAE,EAAU,EAAE,OAAiB,EAAE,EAAE,OAA0D;IACxH,OAAO,IAAI,CAAC,gBAAQ,EAAE,CAAC,EAAE,EAAE,GAAG,IAAI,CAAC,EAAE,OAAO,CAAC,CAAA;AACjD,CAAC;AAFD,8BAEC","sourcesContent":["import { spawn } from 'child_process'\nimport type { SpawnOptions, ChildProcess } from 'child_process'\nimport { Readable, Writable } from 'stream'\n\n\nimport { WritableStreamBuffer } from 'stream-buffers'\nimport type { WritableStreamBufferOptions } from 'stream-buffers'\nimport iconv from 'iconv-lite'\n\n\nimport './prototype'\nimport { Encoding } from './file'\nimport { inspect } from './utils'\n\n\nexport const EXE_NODE = process.execPath.to_slash()\n\n// ------------------------------------ Start & Call\ninterface StartOptions {\n /** `'D:/'` */\n cwd?: string\n \n /** `process.env` overwrite/add to process.env */\n env?: Record<string, string>\n \n /** `'UTF-8'` child output encoding */\n encoding?: Encoding\n \n /** `true` print option (with details) */\n print?: boolean | {\n command?: boolean\n stdout?: boolean\n stderr?: boolean\n code?: boolean\n error?: boolean\n }\n \n /** `'pipe'` when 'ignore' then ignore stdio processing */\n stdio?: 'pipe' | 'ignore'\n \n /** `false` whether to break the connection with child (ignore stdio, unref) */\n detached?: boolean\n}\n\n/** start process \n - exe: .exe path or filename (full path is recommanded to skip PATH searching for better perf)\n - args: `[]` arguments list\n - options\n - cwd?: `'D:/'`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'UTF-8'` child output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n*/\nexport function start (exe: string, args: string[] = [], {\n cwd = 'D:/',\n \n encoding = 'UTF-8',\n \n print = true,\n \n stdio = 'pipe',\n \n detached = false,\n \n env,\n \n}: StartOptions = { }): ChildProcess {\n const options: SpawnOptions = {\n cwd,\n shell: false,\n windowsHide: true,\n stdio,\n ... env ? { ...process.env, ...env } : { },\n }\n \n if (print)\n console.log(`${exe} ${ args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ') }`.blue)\n \n if (detached) {\n let child = spawn(exe, args, {\n ...options,\n stdio: 'ignore',\n detached: true,\n })\n \n child.unref()\n return child\n }\n \n let child = spawn(exe, args, options)\n \n if (stdio === 'pipe')\n child.stdin.setDefaultEncoding('utf8')\n \n // prevent child spawn error crashing NodeJS process\n child.on('error', error => {\n console.error(error)\n })\n \n if (stdio === 'ignore') return child\n \n if (encoding !== 'BINARY') {\n child.stdout = child.stdout.pipe(iconv.decodeStream(encoding)) as any as Readable\n child.stderr = child.stderr.pipe(iconv.decodeStream(encoding)) as any as Readable\n \n if (print) {\n child.stdout.on('data', (chunk) => {\n process.stdout.write(chunk)\n })\n child.stderr.on('data', (chunk) => {\n process.stderr.write(chunk)\n })\n }\n }\n \n return child\n}\n\n\nexport interface CallOptions extends StartOptions {\n /** `true` whether to throw Error when code is not 0 */\n throw_code?: boolean\n}\n\nexport interface CallResult<T = string> {\n pid: number\n stdout: T\n stderr: T\n code: number | null\n signal: NodeJS.Signals | null\n child: ChildProcess\n [inspect.custom] (): string\n}\n\n\n/** call process for result\n - exe: .exe path or filename (full path is recommanded to skip PATH searching for better perf)\n - args: `[]` arguments list\n - options\n - cwd?: `'D:/'`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'UTF-8'` child output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n - throw_code?: `true` whether to throw Error when code is not 0\n*/\nexport async function call (exe: string, args?: string[]): Promise<CallResult<string>>\nexport async function call (exe: string, args?: string[], options?: CallOptions & { encoding: 'BINARY', init_buffer_size?: number }): Promise<CallResult<Buffer>>\nexport async function call (exe: string, args?: string[], options?: CallOptions & { encoding?: 'UTF-8' | 'GB18030' }): Promise<CallResult<string>>\nexport async function call (exe: string, args: string[] = [], {\n encoding = 'UTF-8', \n \n print = true,\n \n init_buffer_size,\n \n throw_code = true,\n}: CallOptions & { init_buffer_size?: number } = { }): Promise<CallResult<string | Buffer>> {\n const print_options = typeof print === 'boolean' ?\n {\n command: print,\n stdout: print,\n stderr: print,\n code: print,\n error: print\n }\n :\n print\n \n const cmd = `${exe} ${ args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ') }`\n \n if (print_options.command)\n console.log(cmd.blue)\n \n let child = start(exe, args, Object.assign(arguments[2] || { }, { print: false }))\n \n // --- collect output\n let stdout: string | WritableStreamBuffer\n let stderr: string | WritableStreamBuffer\n \n \n if (encoding !== 'BINARY') {\n stdout = ''\n stderr = ''\n \n child.stdout.on('data', chunk => {\n if (print_options.stdout)\n process.stdout.write(chunk)\n \n stdout += chunk\n })\n \n child.stderr.on('data', chunk => {\n if (print_options.stderr)\n process.stderr.write(chunk)\n \n stderr += chunk\n })\n } else {\n const stream_buffer_options: WritableStreamBufferOptions = init_buffer_size ? {\n initialSize: init_buffer_size,\n incrementAmount: init_buffer_size,\n } : { }\n \n stdout = new WritableStreamBuffer(stream_buffer_options)\n stderr = new WritableStreamBuffer(stream_buffer_options)\n \n if (print_options.stdout)\n child.stdout.pipe(stdout as WritableStreamBuffer)\n if (print_options.stderr)\n child.stderr.pipe(stderr as WritableStreamBuffer)\n }\n \n \n let code: number | null, \n signal: NodeJS.Signals\n \n await Promise.all([\n new Promise<void>( resolve => {\n child.stdout.on('end', () => { resolve() })\n }),\n new Promise<void>( resolve => {\n child.stderr.on('end', () => { resolve() })\n }),\n new Promise<void>( resolve => {\n child.on('exit', (_code, _signal: NodeJS.Signals) => {\n code = _code\n signal = _signal\n resolve()\n })\n })\n ])\n \n const message = `Process(${ child.pid }) '${ cmd }' exited ${ code }${ signal ? `, by signal ${ signal }` : '' }. `\n \n if (print_options.code || code || signal)\n console.log(message[!code && !signal ? 'green' : 'red'].pad(global.WIDTH || 240, { character: '─' }))\n \n const result = {\n pid: child.pid,\n stdout: encoding !== 'BINARY' ? (stdout as string) : ((stdout as WritableStreamBuffer).getContents() || Buffer.alloc(0)),\n stderr: encoding !== 'BINARY' ? (stderr as string) : ((stderr as WritableStreamBuffer).getContents() || Buffer.alloc(0)),\n code,\n signal,\n child,\n [inspect.custom] () {\n return inspect(this, { omit: ['child'] })\n }\n }\n \n if (code && throw_code)\n throw Object.assign(new Error(message), result)\n \n return result\n}\n\n\n/** call node <js> for result\n - js: .js path (relative path will resolve based on cwd)\n - args: `[]` arguments list\n - options\n - cwd?: `'D:/'`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'UTF-8'` child process output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n - throw_code?: `true` whether to throw Error when code is not 0\n*/\nexport async function call_node (js: string, args: string[] = [], options?: CallOptions & { encoding?: 'UTF-8' | 'GB18030' }) {\n return call(EXE_NODE, [js, ...args], options)\n}\n\n"]}
|
|
1
|
+
{"version":3,"file":"process.js","sourceRoot":"","sources":["process.ts"],"names":[],"mappings":";;;;AAAA,iDAAqC;AAIrC,yEAA8B;AAE9B,uBAAoB;AAEpB,mCAAiC;AAEpB,QAAA,OAAO,GAAG,GAAG,SAAS,GAAG,CAAC,QAAQ,EAAE,CAAA;AAEpC,QAAA,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,QA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,IAAI,IAAI,UAAU;QAClB,MAAM,MAAM,CAAC,MAAM,CACf,IAAI,KAAK,CAAC,OAAO,CAAC,EAClB,MAAM,CACT,CAAA;IAEL,OAAO,MAAM,CAAA;AACjB,CAAC;AA/FD,oBA+FC;AAGD;;;;;;;;;;;EAWE;AACK,KAAK,UAAU,SAAS,CAAE,EAAU,EAAE,OAAiB,EAAE,EAAE,OAA0D;IACxH,OAAO,IAAI,CAAC,gBAAQ,EAAE,CAAC,EAAE,EAAE,GAAG,IAAI,CAAC,EAAE,OAAO,CAAC,CAAA;AACjD,CAAC;AAFD,8BAEC","sourcesContent":["import { spawn } from 'child_process'\nimport type { SpawnOptions, ChildProcess } from 'child_process'\nimport { Readable, Writable } from 'stream'\n\nimport iconv from 'iconv-lite'\n\nimport './prototype'\nimport { Encoding } from './file'\nimport { inspect } from './utils'\n\nexport const fp_root = `${__dirname}/`.to_slash()\n\nexport const exe_node = process.execPath.to_slash()\n\n\n// ------------------------------------ start & call\ninterface StartOptions {\n /** `'d:/'` */\n cwd?: string\n \n /** `process.env` overwrite/add to process.env */\n env?: Record<string, string>\n \n /** `'utf-8'` child output encoding */\n encoding?: Encoding\n \n /** `true` print option (with details) */\n print?: boolean | {\n command?: boolean\n stdout?: boolean\n stderr?: boolean\n code?: boolean\n error?: boolean\n }\n \n /** `'pipe'` when 'ignore' then ignore stdio processing */\n stdio?: 'pipe' | 'ignore'\n \n /** `false` whether to break the connection with child (ignore stdio, unref) */\n detached?: boolean\n}\n\n/** start process \n - exe: .exe path or filename (full path is recommanded to skip path searching for better perf)\n - args: `[]` arguments list\n - options\n - cwd?: `fp_root`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'utf-8'` child output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n*/\nexport function start (exe: string, args: string[] = [], {\n cwd = fp_root,\n \n encoding = 'utf-8',\n \n print = true,\n \n stdio = 'pipe',\n \n detached = false,\n \n env,\n \n}: StartOptions = { }): ChildProcess {\n const options: SpawnOptions = {\n cwd,\n shell: false,\n windowsHide: !detached,\n stdio,\n ... env ? {\n env: { ...process.env, ...env }\n } : { },\n }\n \n if (print)\n console.log(`${exe} ${ args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ') }`.blue)\n \n if (detached) {\n let child = spawn(exe, args, {\n ...options,\n stdio: 'ignore',\n detached: true,\n })\n \n child.unref()\n return child\n }\n \n let child = spawn(exe, args, options)\n \n if (stdio === 'pipe')\n child.stdin.setDefaultEncoding('utf8')\n \n // prevent child spawn error crashing nodejs process\n child.on('error', error => {\n console.error(error)\n })\n \n if (stdio === 'ignore') return child\n \n if (encoding !== 'binary') {\n if (encoding === 'utf-8') {\n child.stdout.setEncoding('utf-8')\n child.stderr.setEncoding('utf-8')\n } else {\n child.stdout = child.stdout.pipe(\n iconv.decodeStream(encoding)\n ) as any as Readable\n child.stderr = child.stderr.pipe(\n iconv.decodeStream(encoding)\n ) as any as Readable\n }\n \n if (print) {\n child.stdout.pipe(process.stdout, { end: false })\n child.stderr.pipe(process.stderr, { end: false })\n }\n }\n \n return child\n}\n\n\nexport interface CallOptions extends StartOptions {\n /** `true` whether to throw Error when code is not 0 */\n throw_code?: boolean\n}\n\nexport interface CallResult<T = string> {\n pid: number\n stdout: T\n stderr: T\n code: number | null\n signal: NodeJS.Signals | null\n child: ChildProcess\n [inspect.custom] (): string\n}\n\n\n/** call process for result\n - exe: .exe path or filename (full path is recommanded to skip path searching for better perf)\n - args: `[]` arguments list\n - options\n - cwd?: `'d:/'`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'utf-8'` child output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n - throw_code?: `true` whether to throw Error when code is not 0\n*/\nexport async function call (exe: string, args?: string[]): Promise<CallResult<string>>\nexport async function call (exe: string, args?: string[], options?: CallOptions & { encoding?: 'utf-8' | 'gb18030' }): Promise<CallResult<string>>\nexport async function call (exe: string, args?: string[], options?: CallOptions & { encoding: 'binary' }): Promise<CallResult<Buffer>>\nexport async function call (exe: string, args: string[] = [], options: CallOptions = { }): Promise<CallResult<string | Buffer>> {\n const {\n encoding = 'utf-8', \n print = true,\n throw_code = true,\n } = options\n \n const print_options = typeof print === 'boolean' ?\n {\n command: print,\n stdout: print,\n stderr: print,\n code: print,\n error: print\n }\n :\n print\n \n const cmd = `${exe} ${ args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ') }`\n \n if (print_options.command)\n console.log(cmd.blue)\n \n options.print = false\n \n let child = start(exe, args, options)\n \n \n // --- collect output\n let stdouts: (string | Buffer)[] = [ ]\n let stderrs: (string | Buffer)[] = [ ]\n \n let code: number | null, \n signal: NodeJS.Signals\n \n await Promise.all([\n new Promise<void>(resolve => {\n child.on('exit', (_code, _signal) => {\n code = _code\n signal = _signal\n resolve()\n })\n }),\n (async () => {\n for await (const chunk of child.stdout as AsyncIterable<string | Buffer>) {\n if (encoding !== 'binary' && print_options.stdout)\n process.stdout.write(chunk)\n stdouts.push(chunk)\n }\n })(),\n (async () => {\n for await (const chunk of child.stderr as AsyncIterable<string | Buffer>) {\n if (encoding !== 'binary' && print_options.stderr)\n process.stderr.write(chunk)\n stderrs.push(chunk)\n }\n })()\n ])\n \n const message = `process(${ child.pid }) '${ cmd }' exited ${ code }${ signal ? `, by signal ${ signal }` : '' }.`\n \n if (print_options.code || code || signal)\n console.log(message[code || signal ? 'red' : 'blue'])\n \n const result = {\n pid: child.pid,\n \n stdout: encoding === 'binary' ?\n Buffer.concat(stdouts as Buffer[])\n :\n (stdouts as string[]).join(''),\n \n stderr: encoding === 'binary' ?\n Buffer.concat(stderrs as Buffer[])\n :\n (stderrs as string[]).join(''),\n \n code,\n \n signal,\n \n child,\n \n [inspect.custom] () {\n return inspect(this, { omit: ['child'] })\n }\n }\n \n if (code && throw_code)\n throw Object.assign(\n new Error(message), \n result\n )\n \n return result\n}\n\n\n/** call node <js> for result\n - js: .js path (relative path will resolve based on cwd)\n - args: `[]` arguments list\n - options\n - cwd?: `'d:/'`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'utf-8'` child process output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n - throw_code?: `true` whether to throw Error when code is not 0\n*/\nexport async function call_node (js: string, args: string[] = [], options?: CallOptions & { encoding?: 'utf-8' | 'gb18030' }) {\n return call(exe_node, [js, ...args], options)\n}\n\n"]}
|
package/prototype.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
declare global {
|
|
2
3
|
interface String {
|
|
3
4
|
readonly width: number;
|
|
@@ -26,9 +27,9 @@ declare global {
|
|
|
26
27
|
- pattern_placeholder?: `/\{.*?\}/g`
|
|
27
28
|
|
|
28
29
|
```ts
|
|
29
|
-
'
|
|
30
|
+
'g:/acgn/海贼王/[Skytree][海贼王][One_Piece][893][GB_BIG5_JP][X264_AAC][1080P][CRRIP][天空树双语字幕组].mkv'.reformat(
|
|
30
31
|
'{dirp}/[Skytree][海贼王][{ en_name: \\w+ }][{ episode: \\d+ }][GB_BIG5_JP][{encoding}_AAC][1080P][CRRIP][天空树双语字幕组].{format}',
|
|
31
|
-
'
|
|
32
|
+
'g:/acgn/海贼王/{episode} {encoding}.{format}',
|
|
32
33
|
'\\+',
|
|
33
34
|
'i',
|
|
34
35
|
(name, value) => name === 'episode' ? String(+value + 1) : value.toLowerCase()
|
|
@@ -57,10 +58,10 @@ declare global {
|
|
|
57
58
|
find(this: string, pattern: string, preservations?: string, flags?: string, pattern_placeholder?: RegExp): {
|
|
58
59
|
[name: string]: string;
|
|
59
60
|
};
|
|
60
|
-
/** - type?: `'
|
|
61
|
-
quote(this: string, type?: keyof typeof
|
|
62
|
-
/** - shape?: `'
|
|
63
|
-
bracket(this: string, shape?: keyof typeof
|
|
61
|
+
/** - type?: `'single'` */
|
|
62
|
+
quote(this: string, type?: keyof typeof quotes | 'psh'): string;
|
|
63
|
+
/** - shape?: `'parenthesis'` */
|
|
64
|
+
bracket(this: string, shape?: keyof typeof brackets): string;
|
|
64
65
|
surround(this: string, left: string, right?: string): string;
|
|
65
66
|
surround_tag(this: string, tag_name: string): string;
|
|
66
67
|
to_lf(this: string): string;
|
|
@@ -70,14 +71,21 @@ declare global {
|
|
|
70
71
|
*/
|
|
71
72
|
rm(this: string, pattern: string | RegExp, flags?: string): string;
|
|
72
73
|
readonly red: string;
|
|
74
|
+
readonly red_: string;
|
|
73
75
|
readonly green: string;
|
|
76
|
+
readonly green_: string;
|
|
74
77
|
readonly yellow: string;
|
|
78
|
+
readonly yellow_: string;
|
|
75
79
|
readonly blue: string;
|
|
80
|
+
readonly blue_: string;
|
|
76
81
|
readonly magenta: string;
|
|
82
|
+
readonly magenta_: string;
|
|
77
83
|
readonly cyan: string;
|
|
84
|
+
readonly cyan_: string;
|
|
78
85
|
readonly grey: string;
|
|
79
86
|
readonly underline: string;
|
|
80
87
|
strip_ansi(this: string): string;
|
|
88
|
+
/** split string to lines and strip last '' after last \n */
|
|
81
89
|
split_lines(this: string): string[];
|
|
82
90
|
trim_doc_comment(this: string): string;
|
|
83
91
|
split_indent(this: string): {
|
|
@@ -85,7 +93,10 @@ declare global {
|
|
|
85
93
|
text: string;
|
|
86
94
|
};
|
|
87
95
|
to_base64(this: string): string;
|
|
96
|
+
/** - buffer: `false` return raw Buffer */
|
|
88
97
|
decode_base64(this: string): string;
|
|
98
|
+
decode_base64(this: string, buffer: true): Buffer;
|
|
99
|
+
decode_base64(this: string, buffer?: boolean): string | Buffer;
|
|
89
100
|
space(this: string): string;
|
|
90
101
|
fdir: string;
|
|
91
102
|
/** path.basename, e.g.
|
|
@@ -137,29 +148,29 @@ declare global {
|
|
|
137
148
|
join_lines(): string;
|
|
138
149
|
}
|
|
139
150
|
}
|
|
140
|
-
import
|
|
141
|
-
export declare const
|
|
142
|
-
export {
|
|
151
|
+
import chalk from 'chalk';
|
|
152
|
+
export declare const emoji_regex: RegExp;
|
|
153
|
+
export { chalk };
|
|
143
154
|
export declare function to_method_property_descriptors(methods: {
|
|
144
155
|
[name: string]: Function;
|
|
145
156
|
}): PropertyDescriptorMap;
|
|
146
157
|
export declare function to_getter_property_descriptors(getters: {
|
|
147
158
|
[name: string]: Function;
|
|
148
159
|
}): PropertyDescriptorMap;
|
|
149
|
-
export declare const
|
|
150
|
-
export declare const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
160
|
+
export declare const cjk = "([\u2E80-\u9FFF\uF900-\uFAFF])";
|
|
161
|
+
export declare const quotes: {
|
|
162
|
+
single: string;
|
|
163
|
+
double: string;
|
|
164
|
+
backtick: string;
|
|
154
165
|
};
|
|
155
|
-
export declare const
|
|
156
|
-
readonly
|
|
157
|
-
readonly
|
|
158
|
-
readonly
|
|
159
|
-
readonly
|
|
160
|
-
readonly
|
|
161
|
-
readonly
|
|
162
|
-
readonly
|
|
166
|
+
export declare const brackets: {
|
|
167
|
+
readonly round: readonly ["(", ")"];
|
|
168
|
+
readonly square: readonly ["[", "]"];
|
|
169
|
+
readonly curly: readonly ["{", "}"];
|
|
170
|
+
readonly pointy: readonly ["<", ">"];
|
|
171
|
+
readonly corner: readonly ["「", "」"];
|
|
172
|
+
readonly fat: readonly ["【", "】"];
|
|
173
|
+
readonly tortoise_shell: readonly ["〔", "〕"];
|
|
163
174
|
};
|
|
164
175
|
export declare function to_json(obj: any, replacer?: any): string;
|
|
165
176
|
export declare function to_json_safely(obj: any, replacer?: any): string;
|