diff --git a/packages/nx/src/command-line/init/implementation/utils.ts b/packages/nx/src/command-line/init/implementation/utils.ts index 4f51fb17f0f9f..40f624709fc34 100644 --- a/packages/nx/src/command-line/init/implementation/utils.ts +++ b/packages/nx/src/command-line/init/implementation/utils.ts @@ -16,7 +16,7 @@ import { } from '../../../utils/package-manager'; import { joinPathFragments } from '../../../utils/path'; import { nxVersion } from '../../../utils/versions'; -import { readFileSync, writeFileSync } from 'fs'; +import { existsSync, readFileSync, writeFileSync } from 'fs'; export function createNxJsonFile( repoRoot: string, @@ -223,3 +223,14 @@ export function printFinalMessage({ ].filter(Boolean), }); } + +export function isMonorepo(packageJson: PackageJson) { + if (!!packageJson.workspaces) return true; + + if (existsSync('pnpm-workspace.yaml') || existsSync('pnpm-workspace.yml')) + return true; + + if (existsSync('lerna.json')) return true; + + return false; +} diff --git a/packages/nx/src/command-line/init/init-v1.ts b/packages/nx/src/command-line/init/init-v1.ts index 4ffac5738d9d4..468f2207dc3a0 100644 --- a/packages/nx/src/command-line/init/init-v1.ts +++ b/packages/nx/src/command-line/init/init-v1.ts @@ -12,6 +12,7 @@ import { runNxSync } from '../../utils/child-process'; import { directoryExists, readJsonFile } from '../../utils/fileutils'; import { PackageJson } from '../../utils/package-json'; import { nxVersion } from '../../utils/versions'; +import { isMonorepo } from './implementation/utils'; export interface InitArgs { addE2e: boolean; @@ -106,17 +107,6 @@ function isNestCLI(packageJson: PackageJson) { ); } -function isMonorepo(packageJson: PackageJson) { - if (!!packageJson.workspaces) return true; - - if (existsSync('pnpm-workspace.yaml') || existsSync('pnpm-workspace.yml')) - return true; - - if (existsSync('lerna.json')) return true; - - return false; -} - function setupDotNxInstallation(version: string) { if (process.platform !== 'win32') { console.log( diff --git a/packages/nx/src/command-line/init/init-v2.ts b/packages/nx/src/command-line/init/init-v2.ts index 011fddb2ea296..30f6a955e84e3 100644 --- a/packages/nx/src/command-line/init/init-v2.ts +++ b/packages/nx/src/command-line/init/init-v2.ts @@ -10,6 +10,7 @@ import { nxVersion } from '../../utils/versions'; import { addDepsToPackageJson, createNxJsonFile, + isMonorepo, runInstall, updateGitIgnore, } from './implementation/utils'; @@ -19,6 +20,7 @@ import { addNxToAngularCliRepo } from './implementation/angular'; import { globWithWorkspaceContext } from '../../utils/workspace-context'; import { connectExistingRepoToNxCloudPrompt } from '../connect/connect-to-nx-cloud'; import { addNxToNpmRepo } from './implementation/add-nx-to-npm-repo'; +import { addNxToMonorepo } from './implementation/add-nx-to-monorepo'; export interface InitArgs { interactive: boolean; @@ -59,22 +61,17 @@ export async function initHandler(options: InitArgs): Promise { return; } - const repoRoot = process.cwd(); - const cacheableOperations: string[] = []; - createNxJsonFile(repoRoot, [], cacheableOperations, {}); - - const pmc = getPackageManagerCommand(); - - updateGitIgnore(repoRoot); - const detectPluginsResponse = await detectPlugins(); if (!detectPluginsResponse?.plugins.length) { // If no plugins are detected/chosen, guide users to setup // their targetDefaults correctly so their package scripts will work. - await addNxToNpmRepo({ - interactive: options.interactive, - }); + const packageJson: PackageJson = readJsonFile('package.json'); + if (isMonorepo(packageJson)) { + await addNxToMonorepo({ interactive: options.interactive }); + } else { + await addNxToNpmRepo({ interactive: options.interactive }); + } } else { const useNxCloud = options.nxCloud ?? @@ -82,6 +79,12 @@ export async function initHandler(options: InitArgs): Promise { ? await connectExistingRepoToNxCloudPrompt() : false); + const repoRoot = process.cwd(); + const pmc = getPackageManagerCommand(); + + createNxJsonFile(repoRoot, [], [], {}); + updateGitIgnore(repoRoot); + addDepsToPackageJson(repoRoot, detectPluginsResponse?.plugins ?? []); output.log({ title: '📦 Installing Nx' });