Skip to content

Commit 73d5b84

Browse files
committed
fix: user's cwd preference
1 parent 47545ca commit 73d5b84

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
"dist": true // set this to false to include "dist" folder in search results
1010
},
1111
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
12-
"typescript.tsc.autoDetect": "off"
12+
"typescript.tsc.autoDetect": "off",
13+
"terminal.integrated.cwd": "${workspaceFolder}/backend",
14+
"terminal.integrated.defaultProfile.linux": ""
1315
}

src/extension.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
getRegistry,
88
shadCnDocUrl,
99
} from "./utils/registry";
10-
import { executeCommand } from "./utils/vscode";
10+
import { executeCommand, getOrChooseCwd } from "./utils/vscode";
1111
import type { Components, Component } from "./utils/registry";
1212

1313
const commands = {
@@ -24,7 +24,8 @@ export function activate(context: vscode.ExtensionContext) {
2424

2525
const disposables: vscode.Disposable[] = [
2626
vscode.commands.registerCommand(commands.initCli, async () => {
27-
const intCmd = await getInitCmd();
27+
const cwd = await getOrChooseCwd();
28+
const intCmd = await getInitCmd(cwd);
2829
executeCommand(intCmd);
2930
}),
3031
vscode.commands.registerCommand(commands.addNewComponent, async () => {
@@ -46,8 +47,8 @@ export function activate(context: vscode.ExtensionContext) {
4647
if (!selectedComponent) {
4748
return;
4849
}
49-
50-
const installCmd = await getInstallCmd([selectedComponent.label]);
50+
const cwd = await getOrChooseCwd();
51+
const installCmd = await getInstallCmd([selectedComponent.label], cwd);
5152
executeCommand(installCmd);
5253
}),
5354

@@ -73,8 +74,8 @@ export function activate(context: vscode.ExtensionContext) {
7374
}
7475

7576
const selectedComponent = selectedComponents.map((component: Component) => component.label);
76-
77-
const installCmd = await getInstallCmd(selectedComponent);
77+
const cwd = await getOrChooseCwd();
78+
const installCmd = await getInstallCmd(selectedComponent, cwd);
7879
executeCommand(installCmd);
7980
}),
8081
vscode.commands.registerCommand(commands.gotoComponentDoc, async () => {
@@ -120,4 +121,4 @@ export function activate(context: vscode.ExtensionContext) {
120121
}
121122

122123
// This method is called when your extension is deactivated
123-
export function deactivate() {}
124+
export function deactivate() { }

src/utils/registry.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,33 @@ export const getRegistry = async (): Promise<Components | null> => {
4545
return components;
4646
};
4747

48-
export const getInstallCmd = async (components: string[]) => {
48+
export const getInstallCmd = async (components: string[], cwd: string) => {
4949
const packageManager = await detectPackageManager();
5050
const componentStr = components.join(" ");
5151

5252
if (packageManager === "bun") {
53-
return `bunx shadcn-vue add ${componentStr}`;
53+
return `bunx shadcn-vue add ${componentStr} -c ${cwd}`;
5454
}
5555

5656
if (packageManager === "pnpm") {
57-
return `pnpm dlx shadcn-vue@latest add ${componentStr}`;
57+
return `pnpm dlx shadcn-vue@latest add ${componentStr} -c ${cwd}`;
5858
}
5959

60-
return `npx shadcn-vue@latest add ${componentStr}`;
60+
return `npx shadcn-vue@latest add ${componentStr} -c ${cwd}`;
6161
};
6262

63-
export const getInitCmd = async () => {
63+
export const getInitCmd = async (cwd: string) => {
6464
const packageManager = await detectPackageManager();
6565

6666
if (packageManager === "bun") {
67-
return "bunx shadcn-vue init";
67+
return `bunx shadcn-vue@latest init -c ${cwd}`;
6868
}
6969

7070
if (packageManager === "pnpm") {
71-
return "pnpm dlx shadcn-vue@latest init";
71+
return `pnpm dlx shadcn-vue@latest init -c ${cwd}`;
7272
}
7373

74-
return "npx shadcn-vue@latest init";
74+
return `npx shadcn-vue@latest init -c ${cwd}`;
7575
};
7676

7777
export const getComponentDocLink = (component: string) => {

src/utils/vscode.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,42 @@ export const detectPackageManager = async (): Promise<PackageManager> => {
5656

5757
return "npm";
5858
};
59+
60+
61+
export const getOrChooseCwd = async (): Promise<string> => {
62+
let cwd = "";
63+
const prefix = "${workspaceFolder}/";
64+
65+
const workspaceFolders = (vscode.workspace.workspaceFolders ?? []).filter(
66+
(f) => f.uri.scheme === "file"
67+
);
68+
69+
if (!workspaceFolders.length) { return "./"; }
70+
71+
const workspacePath = workspaceFolders[0]?.uri.fsPath ?? "";
72+
const cwdFromConfig = vscode.workspace
73+
.getConfiguration()
74+
.get<string>("terminal.integrated.cwd")
75+
?.trim();
76+
77+
if (cwdFromConfig) {
78+
if (cwdFromConfig.startsWith(prefix)) {
79+
cwd = cwdFromConfig.slice(prefix.length);
80+
}
81+
else if (cwdFromConfig.startsWith(workspacePath)) {
82+
cwd = cwdFromConfig.replace(new RegExp(`^${workspacePath}/?`), "");
83+
} else {
84+
cwd = cwdFromConfig;
85+
}
86+
87+
return `${workspacePath}/${cwd}`;
88+
}
89+
90+
const choice = await vscode.window.showQuickPick(
91+
workspaceFolders.map((f) => f.name)
92+
);
93+
94+
if (!choice) { return "./"; }
95+
96+
return workspaceFolders.find((f) => f.name === choice)?.uri.fsPath ?? "./";
97+
};

0 commit comments

Comments
 (0)