Skip to content

Commit 09d22a9

Browse files
authored
Merge pull request #53 from selemondev/feat/support-multiple-workspaces
feat: support multiple workspaces
2 parents 32cb538 + ba1723e commit 09d22a9

File tree

5 files changed

+66
-18
lines changed

5 files changed

+66
-18
lines changed

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,17 @@
139139
"command": "shadcn-svelte.gotoDoc",
140140
"title": "shadcn/svelte: Open Documentation"
141141
}
142-
]
142+
],
143+
"configuration": {
144+
"title": "shadcn/svelte",
145+
"properties": {
146+
"shadcn-svelte.cwd": {
147+
"type": "string",
148+
"default": "",
149+
"markdownDescription": "Absolute path to the working directory when running shadcn-svelte CLI. Default is the current workspace folder."
150+
}
151+
}
152+
}
143153
},
144154
"scripts": {
145155
"vscode:prepublish": "pnpm build",
@@ -149,7 +159,7 @@
149159
"dev": "pnpm build -- --watch",
150160
"deploy:extension": "vsce publish --no-dependencies",
151161
"deploy:openvsx": "ovsx publish -p $OVSX_PAT",
152-
"lint": "eslint src --ext ts"
162+
"lint": "eslint src --ext ts --fix"
153163
},
154164
"devDependencies": {
155165
"@types/node": "20.x",

src/extension.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
getComponentDocLink,
66
getRegistry,
77
} from "./utils/registry";
8-
import { executeCommand } from "./utils/vscode";
8+
import { executeCommand, getOrChooseCwd } from "./utils/vscode";
99
import { getSvelteVersion } from "./utils/getSvelteVersion";
1010
import type { Component, Components } from "./utils/registry";
1111

@@ -28,7 +28,8 @@ export async function activate(context: vscode.ExtensionContext) {
2828

2929
const disposables: vscode.Disposable[] = [
3030
vscode.commands.registerCommand(commands.initCli, async () => {
31-
const intCmd = await getInitCmd();
31+
const cwd = await getOrChooseCwd();
32+
const intCmd = await getInitCmd(cwd);
3233
executeCommand(intCmd);
3334
}),
3435
vscode.commands.registerCommand(commands.addNewComponent, async () => {
@@ -49,8 +50,8 @@ export async function activate(context: vscode.ExtensionContext) {
4950
if (!selectedComponent) {
5051
return;
5152
}
52-
53-
const installCmd = await getInstallCmd([selectedComponent.label]);
53+
const cwd = await getOrChooseCwd();
54+
const installCmd = await getInstallCmd([selectedComponent.label], cwd);
5455
executeCommand(installCmd);
5556
}),
5657

@@ -75,8 +76,8 @@ export async function activate(context: vscode.ExtensionContext) {
7576
}
7677

7778
const selectedComponent = selectedComponents.map((component: Component) => component.label);
78-
79-
const installCmd = await getInstallCmd(selectedComponent);
79+
const cwd = await getOrChooseCwd();
80+
const installCmd = await getInstallCmd(selectedComponent, cwd);
8081
executeCommand(installCmd);
8182
}),
8283
vscode.commands.registerCommand(commands.gotoComponentDoc, async () => {

src/utils/getSvelteVersion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { getProjectDependencies } from "./getProjectDeps";
33
export const getSvelteVersion = async (): Promise<number> => {
44
const dependencies = await getProjectDependencies();
55
const svelteDep = dependencies.find((dep) => dep.name === 'svelte');
6-
return svelteDep!.version;
6+
return svelteDep ? svelteDep?.version : 4;
77
};

src/utils/registry.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,39 @@ 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
const svelteVersion = await getSvelteVersion();
5252

5353
if (packageManager === "bun") {
54-
return svelteVersion >= 5 ? `bunx shadcn-svelte@next add ${componentStr}` : `bunx shadcn-svelte add ${componentStr}`;
54+
return svelteVersion >= 5 ? `bunx shadcn-svelte@next add ${componentStr} -c ${cwd}` : `bunx shadcn-svelte add ${componentStr} -c ${cwd}`;
5555
}
5656

5757
if (packageManager === "pnpm") {
58-
return svelteVersion >= 5 ? `pnpm dlx shadcn-svelte@next add ${componentStr}` : `pnpm dlx shadcn-svelte@latest add ${componentStr}`;
58+
return svelteVersion >= 5 ? `pnpm dlx shadcn-svelte@next add ${componentStr} -c ${cwd}` : `pnpm dlx shadcn-svelte@latest add ${componentStr} -c ${cwd}`;
5959
}
6060

61-
return svelteVersion >= 5 ? `npx shadcn-svelte@next add ${componentStr}` : `npx shadcn-svelte@latest add ${componentStr}`;
61+
return svelteVersion >= 5 ? `npx shadcn-svelte@next add ${componentStr} -c ${cwd}` : `npx shadcn-svelte@latest add ${componentStr} -c ${cwd}`;
6262
};
6363

64-
export const getInitCmd = async () => {
64+
export const getInitCmd = async (cwd: string) => {
6565
const packageManager = await detectPackageManager();
6666
const svelteVersion = await getSvelteVersion();
6767

6868
if (packageManager === "bun") {
69-
return svelteVersion >= 5 ? 'bunx shadcn-svelte@next init' : "bunx shadcn-svelte init";
69+
return svelteVersion >= 5 ? `bunx shadcn-svelte@next init -c ${cwd}` : `bunx shadcn-svelte init -c ${cwd}`;
7070
}
7171

7272
if (packageManager === "pnpm") {
73-
return svelteVersion >= 5 ? 'pnpm dlx shadcn-svelte@next init' : "pnpm dlx shadcn-svelte@latest init";
73+
return svelteVersion >= 5 ? `pnpm dlx shadcn-svelte@next init -c ${cwd}` : `pnpm dlx shadcn-svelte@latest init -c ${cwd}`;
7474
}
7575

76-
return svelteVersion >= 5 ? 'npx shadcn-svelte@next init' : "npx shadcn-svelte@latest init";
76+
return svelteVersion >= 5 ? `npx shadcn-svelte@next init -c ${cwd}` : `npx shadcn-svelte@latest init -c ${cwd}`;
7777
};
7878

7979
export const getComponentDocLink = async (component: string) => {
8080
const svelteVersion = await getSvelteVersion();
8181
const shadCnDocUrl = svelteVersion >= 5 ? "https://next.shadcn-svelte.com/docs" : "https://shadcn-svelte.com/docs";
8282
return `${shadCnDocUrl}/components/${component}`;
83-
};
83+
};

src/utils/vscode.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,40 @@ export const detectPackageManager = async (): Promise<PackageManager> => {
5050

5151
return "npm";
5252
};
53+
54+
export const getOrChooseCwd = async (): Promise<string> => {
55+
const cwdFromConfig = vscode.workspace
56+
.getConfiguration("shadcn-svelte")
57+
.get<string>("cwd")
58+
?.trim();
59+
60+
// Always use the value from settings if it's provided
61+
if (cwdFromConfig) {
62+
return cwdFromConfig;
63+
}
64+
65+
// Get the currently opened workspace folders
66+
const workspaceFolders = (vscode.workspace.workspaceFolders ?? []).filter(
67+
(f) => f.uri.scheme === "file"
68+
);
69+
70+
// If there are no workspace folders open or if there is only one workspace folder, just use the current working dir
71+
if (!workspaceFolders.length || workspaceFolders.length === 1) {
72+
return "./";
73+
}
74+
75+
// If there are multiple workspaces open, allow the user to pick which one
76+
// they want to use.
77+
const choice = await vscode.window.showQuickPick(
78+
workspaceFolders.map((f) => f.name)
79+
);
80+
81+
if (!choice) {
82+
return "./";
83+
}
84+
85+
// Map the chosen workspace name to absolute path
86+
const fsPath = workspaceFolders.find((f) => f.name === choice)?.uri.fsPath;
87+
88+
return fsPath ?? "./";
89+
};

0 commit comments

Comments
 (0)