Skip to content

Commit c78393c

Browse files
feat: support cargo as a package manager, closes #187 (#189)
* feat: support cargo as a package manager, closes #187 * fmt * changefile * Update src/extension.ts Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de> Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>
1 parent bb4268d commit c78393c

2 files changed

Lines changed: 44 additions & 11 deletions

File tree

.changes/cargo.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-vscode": "patch"
3+
---
4+
5+
Add support for `cargo` as a package manager.

src/extension.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
import * as vscode from 'vscode';
6-
import { exec, ChildProcess } from 'child_process';
6+
import { exec, execSync, ChildProcess } from 'child_process';
77
import { runInTerminal } from 'run-in-terminal';
88
import { join } from 'path';
99
import { existsSync, readFileSync } from 'fs';
@@ -381,16 +381,45 @@ function __useYarn(projectPath: string) {
381381
return fs.existsSync(path.join(projectPath, 'yarn.lock'));
382382
}
383383

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+
384397
function __getNpmBin() {
385398
return vscode.workspace.getConfiguration('npm')['bin'] || 'npm';
386399
}
387400

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)
390407
? 'pnpm'
391408
: __useYarn(projectPath)
392409
? '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;
394423
}
395424

396425
interface RunOptions {
@@ -418,19 +447,18 @@ function __runScript(command: string, args: string[], options: RunOptions) {
418447
}
419448

420449
function __runTauriScript(args: string[], options: RunOptions): void {
450+
const command = __getPackageManagerCommand(options.cwd);
451+
if (!command) return;
452+
421453
if (__isVueCliApp(options.cwd)) {
422454
const [cmd, ...cmgArgs] = args;
423455
__runScript(
424-
__getPackageManagerBin(options.cwd),
425-
['run', `tauri:${cmd === 'dev' ? 'serve' : cmd}`, ...cmgArgs],
456+
command,
457+
[`tauri:${cmd === 'dev' ? 'serve' : cmd}`, ...cmgArgs],
426458
options
427459
);
428460
} else {
429-
__runScript(
430-
__getPackageManagerBin(options.cwd),
431-
['run', 'tauri', ...args],
432-
options
433-
);
461+
__runScript(command, ['tauri', ...args], options);
434462
}
435463
}
436464

0 commit comments

Comments
 (0)