@flun/windows 2.0.1 → 2.0.3
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/CHANGELOG.md +3 -3
- package/README.md +360 -360
- package/index.js +8 -8
- package/lib/cmd.js +53 -53
- package/lib/daemon.js +421 -421
- package/lib/eventlog.js +225 -225
- package/package.json +58 -58
package/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
import { platform } from './lib/binaries.js';
|
|
3
|
-
// 平台检查
|
|
4
|
-
if (!platform().startsWith('win')) throw new Error('@flun/windows 仅支持在Windows系统上运行');
|
|
5
|
-
|
|
6
|
-
export { elevate, sudo, isAdminUser } from './lib/binaries.js';
|
|
7
|
-
export { kill, list } from './lib/cmd.js';
|
|
8
|
-
export { Service } from './lib/daemon.js';
|
|
1
|
+
|
|
2
|
+
import { platform } from './lib/binaries.js';
|
|
3
|
+
// 平台检查
|
|
4
|
+
if (!platform().startsWith('win')) throw new Error('@flun/windows 仅支持在Windows系统上运行');
|
|
5
|
+
|
|
6
|
+
export { elevate, sudo, isAdminUser } from './lib/binaries.js';
|
|
7
|
+
export { kill, list } from './lib/cmd.js';
|
|
8
|
+
export { Service } from './lib/daemon.js';
|
|
9
9
|
export { EventLogger } from './lib/eventlog.js';
|
package/lib/cmd.js
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
import { exec } from './shared.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 结束指定进程
|
|
5
|
-
* >查看定义:@see {@link kill}
|
|
6
|
-
* @param {number} pid - 进程PID
|
|
7
|
-
* @param {(error: Error|null, stdout: string, stderr: string) => void} [callback] - 执行完成后的回调函数
|
|
8
|
-
* @param {boolean} [force=false] - 是否强制结束进程(默认: false)
|
|
9
|
-
* @returns {void}
|
|
10
|
-
* @example
|
|
11
|
-
* import { kill } from '@flun/windows';
|
|
12
|
-
* kill(进程PID, () => {
|
|
13
|
-
* console.log('进程已终止');
|
|
14
|
-
* });
|
|
15
|
-
*/
|
|
16
|
-
const kill = (pid, callback, force = false) => {
|
|
17
|
-
if (!pid) throw new Error('PID是kill操作必需的参数。');
|
|
18
|
-
if (isNaN(pid)) throw new Error('PID必须为数字。');
|
|
19
|
-
if (typeof callback !== 'function') callback = function () { };
|
|
20
|
-
exec(`taskkill /PID ${pid}${force == true ? ' /f' : ''}`, callback);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 列出服务器上正在运行的进程
|
|
25
|
-
* >查看定义:@see {@link list}
|
|
26
|
-
* @param {(processes: Array<Record<string, string>>) => void} callback - 回调函数,接收进程对象数组
|
|
27
|
-
* @param {boolean} [verbose=false] - 是否显示详细信息(默认: false)
|
|
28
|
-
* @returns {void}
|
|
29
|
-
* @example
|
|
30
|
-
* import { list } from '@flun/windows';
|
|
31
|
-
* list(processes => {
|
|
32
|
-
* console.log(processes);
|
|
33
|
-
* }, true); // true 显示详细信息
|
|
34
|
-
*/
|
|
35
|
-
const list = (callback, verbose = false) => {
|
|
36
|
-
exec(`tasklist /FO CSV${verbose ? ' /V' : ''}`, (err, stdout, stderr) => {
|
|
37
|
-
const lines = stdout.split('\r\n'), processes = [],
|
|
38
|
-
commaQuoteRegex = /",/g, quoteRegex = /['"]/g, whitespaceRegex = /\s/g; // 预编译正则表达式(匹配 CSV 格式)
|
|
39
|
-
let headers = null;
|
|
40
|
-
for (const line of lines.slice(1, -1)) {
|
|
41
|
-
if (!line.trim()) continue; // 跳过空行
|
|
42
|
-
// 替换 CSV 中的字段分隔符,移除所有引号
|
|
43
|
-
let record = line.replace(commaQuoteRegex, '";').replace(quoteRegex, '').split(';');
|
|
44
|
-
if (!headers) headers = record.map(header => header.replace(whitespaceRegex, ''));
|
|
45
|
-
else {
|
|
46
|
-
const processObj = {};
|
|
47
|
-
record.forEach((value, index) => processObj[headers[index]] = value.replace(quoteRegex, ''));
|
|
48
|
-
processes.push(processObj);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
callback(processes);
|
|
52
|
-
});
|
|
53
|
-
}
|
|
1
|
+
import { exec } from './shared.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 结束指定进程
|
|
5
|
+
* >查看定义:@see {@link kill}
|
|
6
|
+
* @param {number} pid - 进程PID
|
|
7
|
+
* @param {(error: Error|null, stdout: string, stderr: string) => void} [callback] - 执行完成后的回调函数
|
|
8
|
+
* @param {boolean} [force=false] - 是否强制结束进程(默认: false)
|
|
9
|
+
* @returns {void}
|
|
10
|
+
* @example
|
|
11
|
+
* import { kill } from '@flun/windows';
|
|
12
|
+
* kill(进程PID, () => {
|
|
13
|
+
* console.log('进程已终止');
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
const kill = (pid, callback, force = false) => {
|
|
17
|
+
if (!pid) throw new Error('PID是kill操作必需的参数。');
|
|
18
|
+
if (isNaN(pid)) throw new Error('PID必须为数字。');
|
|
19
|
+
if (typeof callback !== 'function') callback = function () { };
|
|
20
|
+
exec(`taskkill /PID ${pid}${force == true ? ' /f' : ''}`, callback);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 列出服务器上正在运行的进程
|
|
25
|
+
* >查看定义:@see {@link list}
|
|
26
|
+
* @param {(processes: Array<Record<string, string>>) => void} callback - 回调函数,接收进程对象数组
|
|
27
|
+
* @param {boolean} [verbose=false] - 是否显示详细信息(默认: false)
|
|
28
|
+
* @returns {void}
|
|
29
|
+
* @example
|
|
30
|
+
* import { list } from '@flun/windows';
|
|
31
|
+
* list(processes => {
|
|
32
|
+
* console.log(processes);
|
|
33
|
+
* }, true); // true 显示详细信息
|
|
34
|
+
*/
|
|
35
|
+
const list = (callback, verbose = false) => {
|
|
36
|
+
exec(`tasklist /FO CSV${verbose ? ' /V' : ''}`, (err, stdout, stderr) => {
|
|
37
|
+
const lines = stdout.split('\r\n'), processes = [],
|
|
38
|
+
commaQuoteRegex = /",/g, quoteRegex = /['"]/g, whitespaceRegex = /\s/g; // 预编译正则表达式(匹配 CSV 格式)
|
|
39
|
+
let headers = null;
|
|
40
|
+
for (const line of lines.slice(1, -1)) {
|
|
41
|
+
if (!line.trim()) continue; // 跳过空行
|
|
42
|
+
// 替换 CSV 中的字段分隔符,移除所有引号
|
|
43
|
+
let record = line.replace(commaQuoteRegex, '";').replace(quoteRegex, '').split(';');
|
|
44
|
+
if (!headers) headers = record.map(header => header.replace(whitespaceRegex, ''));
|
|
45
|
+
else {
|
|
46
|
+
const processObj = {};
|
|
47
|
+
record.forEach((value, index) => processObj[headers[index]] = value.replace(quoteRegex, ''));
|
|
48
|
+
processes.push(processObj);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
callback(processes);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
54
|
export { kill, list };
|