Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/salty-apes-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

fix(add): include monorepo root in dependency detection
19 changes: 15 additions & 4 deletions packages/cli/commands/add/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ export async function createWorkspace({

let dependencies: Record<string, string> = {};
let directory = resolvedCwd;
const root = findRoot(resolvedCwd);
while (directory && directory !== root) {
const workspaceRoot = findWorkspaceRoot(directory);
const { root } = path.parse(directory);
while (
// we have a directory
directory &&
// we are still in the workspace (including the workspace root)
directory.length >= workspaceRoot.length
) {
if (fs.existsSync(path.join(directory, commonFilePaths.packageJson))) {
const { data: packageJson } = getPackageJson(directory);
dependencies = {
Expand All @@ -43,6 +49,7 @@ export async function createWorkspace({
...dependencies
};
}
if (root === directory) break; // we are at the root root, let's stop
directory = path.dirname(directory);
}
// removes the version ranges (e.g. `^` is removed from: `^9.0.0`)
Expand All @@ -61,22 +68,26 @@ export async function createWorkspace({
};
}

function findRoot(cwd: string): string {
function findWorkspaceRoot(cwd: string): string {
const { root } = path.parse(cwd);
let directory = cwd;
while (directory && directory !== root) {
if (fs.existsSync(path.join(directory, commonFilePaths.packageJson))) {
// in pnpm it can be a file
if (fs.existsSync(path.join(directory, 'pnpm-workspace.yaml'))) {
return directory;
}
// in other package managers it's a workspaces key in the package.json
const { data } = getPackageJson(directory);
if (data.workspaces) {
return directory;
}
}
directory = path.dirname(directory);
}
return root;
// We didn't find a workspace root, so we return the original directory
// it's a standalone project
return cwd;
}

function parseKitOptions(cwd: string) {
Expand Down
Loading