fnva 0.0.23 → 0.0.24
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/lib/encoding-utils.js +152 -0
- package/package.json +3 -1
- package/platforms/fnva +0 -0
- package/platforms/fnva.exe +0 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 编码处理工具模块
|
|
3
|
+
* 用于统一处理跨平台字符编码问题
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 编码处理工具类
|
|
11
|
+
*/
|
|
12
|
+
class EncodingUtils {
|
|
13
|
+
/**
|
|
14
|
+
* 设置Windows控制台编码为UTF-8
|
|
15
|
+
*/
|
|
16
|
+
static setWindowsConsoleEncoding() {
|
|
17
|
+
if (process.platform === 'win32') {
|
|
18
|
+
try {
|
|
19
|
+
// 尝试设置控制台编码为UTF-8
|
|
20
|
+
const { execSync } = require('child_process');
|
|
21
|
+
execSync('chcp 65001 > nul 2>&1', { stdio: 'ignore' });
|
|
22
|
+
|
|
23
|
+
// 设置Node.js输出编码
|
|
24
|
+
if (process.stdout._handle && process.stdout._handle.setEncoding) {
|
|
25
|
+
process.stdout._handle.setEncoding('utf8');
|
|
26
|
+
}
|
|
27
|
+
if (process.stderr._handle && process.stderr._handle.setEncoding) {
|
|
28
|
+
process.stderr._handle.setEncoding('utf8');
|
|
29
|
+
}
|
|
30
|
+
} catch (error) {
|
|
31
|
+
// 静默忽略错误,避免影响正常功能
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 为文件内容添加适当的编码标记
|
|
38
|
+
* @param {string} content - 文件内容
|
|
39
|
+
* @param {string} filePath - 文件路径(用于确定文件类型)
|
|
40
|
+
* @returns {string} - 处理后的内容
|
|
41
|
+
*/
|
|
42
|
+
static addEncodingSignature(content, filePath) {
|
|
43
|
+
// 为Windows PowerShell脚本添加BOM
|
|
44
|
+
if (process.platform === 'win32' && this.isPowerShellScript(filePath)) {
|
|
45
|
+
return '\ufeff' + content;
|
|
46
|
+
}
|
|
47
|
+
return content;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 检查文件是否为PowerShell脚本
|
|
52
|
+
* @param {string} filePath - 文件路径
|
|
53
|
+
* @returns {boolean} - 是否为PowerShell脚本
|
|
54
|
+
*/
|
|
55
|
+
static isPowerShellScript(filePath) {
|
|
56
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
57
|
+
return ext === '.ps1';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 安全地写入文件,自动处理编码
|
|
62
|
+
* @param {string} filePath - 文件路径
|
|
63
|
+
* @param {string} content - 文件内容
|
|
64
|
+
* @param {string} encoding - 编码格式,默认为utf8
|
|
65
|
+
*/
|
|
66
|
+
static writeFileWithEncoding(filePath, content, encoding = 'utf8') {
|
|
67
|
+
const processedContent = this.addEncodingSignature(content, filePath);
|
|
68
|
+
fs.writeFileSync(filePath, processedContent, encoding);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 生成PowerShell编码设置脚本
|
|
73
|
+
* @returns {string} - PowerShell编码设置代码
|
|
74
|
+
*/
|
|
75
|
+
static generatePowerShellEncodingSetup() {
|
|
76
|
+
return [
|
|
77
|
+
'# 设置UTF-8编码以正确显示中文',
|
|
78
|
+
'[Console]::OutputEncoding = [System.Text.Encoding]::UTF8',
|
|
79
|
+
'$OutputEncoding = [System.Console]::OutputEncoding',
|
|
80
|
+
''
|
|
81
|
+
].join('\n');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 检测系统默认编码
|
|
86
|
+
* @returns {string} - 系统编码名称
|
|
87
|
+
*/
|
|
88
|
+
static detectSystemEncoding() {
|
|
89
|
+
if (process.platform === 'win32') {
|
|
90
|
+
try {
|
|
91
|
+
const { execSync } = require('child_process');
|
|
92
|
+
const result = execSync('chcp', { encoding: 'utf8' });
|
|
93
|
+
const match = result.match(/活动代码页: (\d+)/);
|
|
94
|
+
return match ? `cp${match[1]}` : 'utf8';
|
|
95
|
+
} catch (e) {
|
|
96
|
+
return 'cp936'; // Windows中文默认编码
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
return 'utf8'; // Unix-like系统默认UTF-8
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 创建临时PowerShell脚本文件
|
|
105
|
+
* @param {string} content - 脚本内容
|
|
106
|
+
* @param {string} prefix - 文件名前缀
|
|
107
|
+
* @returns {string} - 临时文件路径
|
|
108
|
+
*/
|
|
109
|
+
static createTempPowerShellScript(content, prefix = 'fnva') {
|
|
110
|
+
const os = require('os');
|
|
111
|
+
const path = require('path');
|
|
112
|
+
|
|
113
|
+
const tempDir = os.tmpdir();
|
|
114
|
+
const timestamp = Date.now();
|
|
115
|
+
const scriptFile = path.join(tempDir, `${prefix}_${timestamp}.ps1`);
|
|
116
|
+
|
|
117
|
+
// 在脚本开头添加编码设置
|
|
118
|
+
const encodingSetup = this.generatePowerShellEncodingSetup();
|
|
119
|
+
const fullContent = encodingSetup + content;
|
|
120
|
+
|
|
121
|
+
this.writeFileWithEncoding(scriptFile, fullContent);
|
|
122
|
+
|
|
123
|
+
return scriptFile;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 安全地执行PowerShell脚本,确保编码正确
|
|
128
|
+
* @param {string} scriptPath - 脚本路径
|
|
129
|
+
* @param {Array} args - 传递给PowerShell的参数
|
|
130
|
+
* @param {Object} options - 执行选项
|
|
131
|
+
* @returns {Object} - 执行结果
|
|
132
|
+
*/
|
|
133
|
+
static executePowerShellScript(scriptPath, args = [], options = {}) {
|
|
134
|
+
const { spawn } = require('child_process');
|
|
135
|
+
|
|
136
|
+
const defaultOptions = {
|
|
137
|
+
stdio: 'inherit',
|
|
138
|
+
shell: false,
|
|
139
|
+
...options
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const psArgs = [
|
|
143
|
+
'-ExecutionPolicy', 'Bypass',
|
|
144
|
+
'-File', scriptPath,
|
|
145
|
+
...args
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
return spawn('powershell', psArgs, defaultOptions);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
module.exports = EncodingUtils;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fnva",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.24",
|
|
4
4
|
"description": "跨平台环境切换工具,支持 Java 和 LLM 环境配置",
|
|
5
5
|
"author": "protagonistss",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
20
|
"bin/",
|
|
21
|
+
"lib/",
|
|
21
22
|
"platforms/",
|
|
22
23
|
"scripts/",
|
|
23
24
|
"README.md",
|
|
@@ -48,3 +49,4 @@
|
|
|
48
49
|
"node": ">=14.0.0"
|
|
49
50
|
}
|
|
50
51
|
}
|
|
52
|
+
|
package/platforms/fnva
CHANGED
|
Binary file
|
package/platforms/fnva.exe
CHANGED
|
Binary file
|