|
3 | 3 | // SPDX-License-Identifier: MIT |
4 | 4 |
|
5 | 5 | import * as vscode from 'vscode'; |
6 | | -import { exec, ChildProcess } from 'child_process'; |
| 6 | +import { exec, execSync, ChildProcess } from 'child_process'; |
7 | 7 | import { runInTerminal } from 'run-in-terminal'; |
8 | 8 | import { join } from 'path'; |
9 | 9 | import { existsSync, readFileSync } from 'fs'; |
@@ -381,16 +381,45 @@ function __useYarn(projectPath: string) { |
381 | 381 | return fs.existsSync(path.join(projectPath, 'yarn.lock')); |
382 | 382 | } |
383 | 383 |
|
| 384 | +function __useNpm(projectPath: string) { |
| 385 | + return fs.existsSync(path.join(projectPath, 'package-lock.json')); |
| 386 | +} |
| 387 | + |
| 388 | +function __useCargo() { |
| 389 | + try { |
| 390 | + execSync('cargo tauri --version', { windowsHide: true }); |
| 391 | + return true; |
| 392 | + } catch { |
| 393 | + return false; |
| 394 | + } |
| 395 | +} |
| 396 | + |
384 | 397 | function __getNpmBin() { |
385 | 398 | return vscode.workspace.getConfiguration('npm')['bin'] || 'npm'; |
386 | 399 | } |
387 | 400 |
|
388 | | -function __getPackageManagerBin(projectPath: string) { |
389 | | - return __usePnpm(projectPath) |
| 401 | +function __getNpmCommand() { |
| 402 | + return __getNpmBin() + ' run'; |
| 403 | +} |
| 404 | + |
| 405 | +function __getPackageManagerCommand(projectPath: string): string | null { |
| 406 | + const m = __usePnpm(projectPath) |
390 | 407 | ? 'pnpm' |
391 | 408 | : __useYarn(projectPath) |
392 | 409 | ? 'yarn' |
393 | | - : __getNpmBin(); |
| 410 | + : __useNpm(projectPath) |
| 411 | + ? __getNpmCommand() |
| 412 | + : __useCargo() |
| 413 | + ? 'cargo' |
| 414 | + : null; |
| 415 | + |
| 416 | + if (!m) { |
| 417 | + vscode.window.showErrorMessage( |
| 418 | + "Couldn't detect package manager for current project." |
| 419 | + ); |
| 420 | + } |
| 421 | + |
| 422 | + return m; |
394 | 423 | } |
395 | 424 |
|
396 | 425 | interface RunOptions { |
@@ -418,19 +447,18 @@ function __runScript(command: string, args: string[], options: RunOptions) { |
418 | 447 | } |
419 | 448 |
|
420 | 449 | function __runTauriScript(args: string[], options: RunOptions): void { |
| 450 | + const command = __getPackageManagerCommand(options.cwd); |
| 451 | + if (!command) return; |
| 452 | + |
421 | 453 | if (__isVueCliApp(options.cwd)) { |
422 | 454 | const [cmd, ...cmgArgs] = args; |
423 | 455 | __runScript( |
424 | | - __getPackageManagerBin(options.cwd), |
425 | | - ['run', `tauri:${cmd === 'dev' ? 'serve' : cmd}`, ...cmgArgs], |
| 456 | + command, |
| 457 | + [`tauri:${cmd === 'dev' ? 'serve' : cmd}`, ...cmgArgs], |
426 | 458 | options |
427 | 459 | ); |
428 | 460 | } else { |
429 | | - __runScript( |
430 | | - __getPackageManagerBin(options.cwd), |
431 | | - ['run', 'tauri', ...args], |
432 | | - options |
433 | | - ); |
| 461 | + __runScript(command, ['tauri', ...args], options); |
434 | 462 | } |
435 | 463 | } |
436 | 464 |
|
|
0 commit comments