diff --git a/packages/yarn-plugin/package.json b/packages/yarn-plugin/package.json index 5b4d8f3aea..b6b97c0475 100644 --- a/packages/yarn-plugin/package.json +++ b/packages/yarn-plugin/package.json @@ -30,6 +30,7 @@ "watch": "./src" }, "dependencies": { + "@backstage/cli-common": "workspace:^", "@backstage/release-manifests": "workspace:^", "@yarnpkg/core": "^4.0.3", "@yarnpkg/fslib": "^3.0.2", diff --git a/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.test.ts b/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.test.ts index f5f15b861b..ca99b88437 100644 --- a/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.test.ts +++ b/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.test.ts @@ -46,6 +46,10 @@ describe('beforeWorkspacePacking', () => { .spyOn(ppath, 'cwd') .mockReturnValue(npath.toPortablePath(mockDir.path)); + jest + .spyOn(process, 'cwd') + .mockReturnValue(npath.toPortablePath(mockDir.path)); + mockDir.setContent({ 'backstage.json': JSON.stringify({ version: '1.23.45', diff --git a/packages/yarn-plugin/src/resolver/BackstageResolver.test.ts b/packages/yarn-plugin/src/resolver/BackstageResolver.test.ts index 9aad0cbc09..4cc0c1c9f2 100644 --- a/packages/yarn-plugin/src/resolver/BackstageResolver.test.ts +++ b/packages/yarn-plugin/src/resolver/BackstageResolver.test.ts @@ -40,6 +40,10 @@ describe('BackstageResolver', () => { .spyOn(ppath, 'cwd') .mockReturnValue(npath.toPortablePath(mockDir.path)); + jest + .spyOn(process, 'cwd') + .mockReturnValue(npath.toPortablePath(mockDir.path)); + mockDir.setContent({ 'backstage.json': JSON.stringify({ version: '1.23.45', diff --git a/packages/yarn-plugin/src/util.ts b/packages/yarn-plugin/src/util.ts index 3ee544289f..8431e23cfa 100644 --- a/packages/yarn-plugin/src/util.ts +++ b/packages/yarn-plugin/src/util.ts @@ -14,46 +14,18 @@ * limitations under the License. */ -import { PortablePath, ppath, xfs } from '@yarnpkg/fslib'; +import { ppath, xfs } from '@yarnpkg/fslib'; import { valid as semverValid } from 'semver'; import { getManifestByVersion } from '@backstage/release-manifests'; +import { BACKSTAGE_JSON, findPaths } from '@backstage/cli-common'; import { Descriptor, structUtils } from '@yarnpkg/core'; import { PROTOCOL } from './constants'; -const isWorkspaceRoot = (dir: PortablePath) => { - const manifestPath = ppath.join(dir, 'package.json'); - - if (xfs.existsSync(manifestPath)) { - const manifest = xfs.readJsonSync(manifestPath); - - if (manifest.workspaces) { - return true; - } - } - - return false; -}; - -const findWorkspaceRoot = () => { - const cwd = ppath.cwd(); - let currentDir = cwd; - - while (!isWorkspaceRoot(currentDir)) { - const parentDir = ppath.dirname(currentDir); - - if (parentDir === currentDir) { - throw new Error(`Workspace root not found from ${cwd}`); - } - - currentDir = parentDir; - } - - return currentDir; -}; - export const getCurrentBackstageVersion = () => { + const workspaceRoot = ppath.resolve(findPaths(ppath.cwd()).targetRoot); + const backstageJson = xfs.readJsonSync( - ppath.join(findWorkspaceRoot(), 'backstage.json'), + ppath.join(workspaceRoot, BACKSTAGE_JSON), ); const backstageVersion = semverValid(backstageJson.version); diff --git a/yarn.lock b/yarn.lock index 9ff777da8c..9f00d91e00 100644 --- a/yarn.lock +++ b/yarn.lock @@ -45133,6 +45133,7 @@ __metadata: dependencies: "@backstage/backend-test-utils": "workspace:^" "@backstage/cli": "workspace:^" + "@backstage/cli-common": "workspace:^" "@backstage/release-manifests": "workspace:^" "@yarnpkg/builder": ^4.0.0 "@yarnpkg/core": ^4.0.3