fnva 0.0.9 → 0.0.10

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/bin/fnva.js CHANGED
@@ -84,6 +84,25 @@ function hasDirectExecuteFlag(args) {
84
84
  return args.includes('--exec') || args.includes('-e');
85
85
  }
86
86
 
87
+ function getShellArg(args) {
88
+ const idx = args.indexOf('--shell');
89
+ if (idx !== -1 && idx + 1 < args.length) {
90
+ return args[idx + 1];
91
+ }
92
+ return null;
93
+ }
94
+
95
+ function detectShell() {
96
+ if (process.platform === 'win32') {
97
+ return 'powershell';
98
+ }
99
+ return 'bash';
100
+ }
101
+
102
+ function hasSessionFlag(args) {
103
+ return args.includes('--session');
104
+ }
105
+
87
106
  function parseEnvironmentScript(scriptContent) {
88
107
  if (!scriptContent || scriptContent.trim() === '') {
89
108
  return {};
@@ -165,10 +184,23 @@ function run() {
165
184
  const isSwitchCommand = isEnvironmentSwitchCommand(args);
166
185
 
167
186
  if (isSwitchCommand) {
187
+ const shellArg = getShellArg(args);
188
+ if (!shellArg || shellArg === 'auto') {
189
+ const detected = detectShell();
190
+ if (shellArg === 'auto') {
191
+ const idx = args.indexOf('--shell');
192
+ if (idx !== -1 && idx + 1 < args.length) {
193
+ args[idx + 1] = detected;
194
+ }
195
+ } else {
196
+ args.push('--shell', detected);
197
+ }
198
+ }
199
+
168
200
  const { spawnSync } = require('child_process');
169
201
  const result = spawnSync(binaryPath, args, {
170
202
  encoding: 'utf8',
171
- shell: true
203
+ shell: false
172
204
  });
173
205
 
174
206
  if (result.error) {
@@ -188,70 +220,42 @@ function run() {
188
220
  const envType = args[0];
189
221
  const envName = args[2];
190
222
 
191
- // Windows 上:启动新的 PowerShell 会话并自动执行环境切换
223
+ // Windows:默认不启动新的会话;可通过 --session 开启旧行为
192
224
  if (process.platform === 'win32') {
193
- console.log(`✅ Switched to ${envType} environment: ${envName}`);
194
- console.log(`🚀 Starting new PowerShell session with ${envName} environment...`);
195
- console.log(`Type "exit" to return to previous session\n`);
196
-
197
- try {
198
- // 创建临时脚本文件
199
- const os = require('os');
200
- const fs = require('fs');
201
- const tempScript = os.tmpdir() + '\\fnva_env_' + Date.now() + '.ps1';
202
-
203
- // 写入环境设置脚本 + 启动交互式会话
204
- const fullScript = script + '\n\n' +
205
- 'Write-Host "✨ Environment ready! You are now in fnva: ' + envName + '" -ForegroundColor Green\n' +
206
- 'Write-Host "Current Java version:" -ForegroundColor Yellow\n' +
207
- 'java --version\n' +
208
- 'Write-Host ""\n' +
209
- 'Write-Host "Type "exit" to return to previous session." -ForegroundColor Cyan\n' +
210
- 'Write-Host ""\n' +
211
- '$Host.UI.RawUI.WindowTitle = "fnva: ' + envName + '"\n' +
212
- '# Start interactive prompt\n' +
213
- 'while ($true) {\n' +
214
- ' try {\n' +
215
- ' $input = Read-Host "PS fnva:' + envName + '"\n' +
216
- ' if ($input -eq "exit") { break }\n' +
217
- ' if ($input.Trim() -ne "") {\n' +
218
- ' try {\n' +
219
- ' Invoke-Expression $input\n' +
220
- ' } catch {\n' +
221
- ' Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red\n' +
222
- ' }\n' +
223
- ' }\n' +
224
- ' } catch {\n' +
225
- ' break\n' +
226
- ' }\n' +
227
- '}\n' +
228
- 'Write-Host "👋 Returning to original session..." -ForegroundColor Cyan\n';
229
-
230
- fs.writeFileSync(tempScript, fullScript, 'utf8');
231
-
232
- // 启动新的交互式 PowerShell 会话
233
- const { spawn } = require('child_process');
234
- const ps = spawn('powershell', ['-NoExit', '-ExecutionPolicy', 'Bypass', '-File', tempScript], {
235
- stdio: 'inherit',
236
- shell: false // 避免 shell 注入问题
237
- });
238
-
239
- ps.on('exit', (code) => {
240
- // 清理临时文件
241
- try {
242
- fs.unlinkSync(tempScript);
243
- } catch (e) {
244
- // 忽略清理错误
245
- }
246
- console.log('👋 Returned to original session');
247
- });
248
-
249
- // 保持当前进程运行直到子进程结束
250
- return;
251
-
252
- } catch (error) {
253
- console.error(`Failed to start PowerShell session: ${error.message}`);
254
- console.log(`📝 Script was: ${script}`);
225
+ if (hasSessionFlag(args)) {
226
+ console.log(`✅ Switched to ${envType} environment: ${envName}`);
227
+ console.log(`🚀 Starting new PowerShell session with ${envName} environment...`);
228
+ console.log(`Type "exit" to return to previous session\n`);
229
+
230
+ try {
231
+ const os = require('os');
232
+ const fs = require('fs');
233
+ const tempScript = os.tmpdir() + '\\fnva_env_' + Date.now() + '.ps1';
234
+ const fullScript = script + '\n';
235
+ fs.writeFileSync(tempScript, fullScript, 'utf8');
236
+ const { spawn } = require('child_process');
237
+ const ps = spawn('powershell', ['-NoExit', '-ExecutionPolicy', 'Bypass', '-File', tempScript], {
238
+ stdio: 'inherit',
239
+ shell: false
240
+ });
241
+ ps.on('exit', () => {
242
+ try { fs.unlinkSync(tempScript); } catch (_) {}
243
+ console.log('👋 Returned to original session');
244
+ });
245
+ return;
246
+ } catch (error) {
247
+ console.error(`Failed to start PowerShell session: ${error.message}`);
248
+ console.log(`📝 Script was: ${script}`);
249
+ }
250
+ } else {
251
+ console.log(`✅ Switched to ${envType} environment: ${envName}`);
252
+ if (process.stdout.isTTY) {
253
+ console.log('');
254
+ console.log('💡 在当前会话应用环境:');
255
+ console.log(` fnva ${envType} use ${envName} --shell powershell | Invoke-Expression`);
256
+ } else {
257
+ process.stdout.write(script);
258
+ }
255
259
  }
256
260
  } else {
257
261
  // Unix-like systems: 显示使用说明
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fnva",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "跨平台环境切换工具,支持 Java 和 LLM 环境配置",
5
5
  "author": "protagonistss",
6
6
  "license": "MIT",
package/platforms/fnva CHANGED
Binary file
Binary file
Binary file