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 +68 -64
- package/package.json +1 -1
- package/platforms/fnva +0 -0
- package/platforms/fnva-0.0.10.tgz +0 -0
- package/platforms/fnva.exe +0 -0
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:
|
|
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
|
-
//
|
|
223
|
+
// Windows:默认不启动新的会话;可通过 --session 开启旧行为
|
|
192
224
|
if (process.platform === 'win32') {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
'
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
'
|
|
222
|
-
'
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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
package/platforms/fnva
CHANGED
|
Binary file
|
|
Binary file
|
package/platforms/fnva.exe
CHANGED
|
Binary file
|