From 05c4eae7de30f5f09f285f54165d7f28ed829549 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sun, 4 Sep 2022 15:03:17 +0200 Subject: [PATCH] cli: add isMonoRepo helper Signed-off-by: Patrik Oldsberg --- packages/cli/src/lib/monorepo/isMonoRepo.ts | 28 +++++++++++ .../cli/src/lib/monorepo/isMonorepo.test.ts | 50 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 packages/cli/src/lib/monorepo/isMonoRepo.ts create mode 100644 packages/cli/src/lib/monorepo/isMonorepo.test.ts diff --git a/packages/cli/src/lib/monorepo/isMonoRepo.ts b/packages/cli/src/lib/monorepo/isMonoRepo.ts new file mode 100644 index 0000000000..9b1ab93762 --- /dev/null +++ b/packages/cli/src/lib/monorepo/isMonoRepo.ts @@ -0,0 +1,28 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { paths } from '../paths'; +import fs from 'fs-extra'; + +export async function isMonoRepo(): Promise { + const rootPackageJsonPath = paths.resolveTargetRoot('package.json'); + try { + const pkg = await fs.readJson(rootPackageJsonPath); + return Boolean(pkg?.workspaces?.packages); + } catch (error) { + return false; + } +} diff --git a/packages/cli/src/lib/monorepo/isMonorepo.test.ts b/packages/cli/src/lib/monorepo/isMonorepo.test.ts new file mode 100644 index 0000000000..eb466a6ac8 --- /dev/null +++ b/packages/cli/src/lib/monorepo/isMonorepo.test.ts @@ -0,0 +1,50 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { isMonoRepo } from './isMonoRepo'; +import mockFs from 'mock-fs'; + +describe('isMonoRepo', () => { + afterEach(() => { + mockFs.restore(); + }); + + it('should detect a monorepo', async () => { + mockFs({ + 'package.json': JSON.stringify({ + name: 'foo', + workspaces: { + packages: ['packages/*'], + }, + }), + }); + await expect(isMonoRepo()).resolves.toBe(true); + }); + + it('should detect a non- monorepo', async () => { + mockFs({ + 'package.json': JSON.stringify({ + name: 'foo', + }), + }); + await expect(isMonoRepo()).resolves.toBe(false); + }); + + it('should return false if package.json is missing', async () => { + mockFs({}); + await expect(isMonoRepo()).resolves.toBe(false); + }); +});