diff --git a/packages/nx/src/command-line/init/implementation/add-nx-to-monorepo.ts b/packages/nx/src/command-line/init/implementation/add-nx-to-monorepo.ts index 0f3a0f555fa11..4e11f0ee06d1a 100644 --- a/packages/nx/src/command-line/init/implementation/add-nx-to-monorepo.ts +++ b/packages/nx/src/command-line/init/implementation/add-nx-to-monorepo.ts @@ -10,13 +10,16 @@ import { addDepsToPackageJson, createNxJsonFile, initCloud, + markPackageJsonAsNxProject, printFinalMessage, runInstall, updateGitIgnore, } from './utils'; import { connectExistingRepoToNxCloudPrompt } from '../../connect/connect-to-nx-cloud'; -type Options = Pick; +type Options = Pick & { + legacy?: boolean; +}; export async function addNxToMonorepo(options: Options) { const repoRoot = process.cwd(); @@ -91,6 +94,14 @@ export async function addNxToMonorepo(options: Options) { cacheableOperations, scriptOutputs ); + if (!options.legacy) { + packageJsonFiles.forEach((packageJsonPath) => { + markPackageJsonAsNxProject( + join(repoRoot, packageJsonPath), + cacheableOperations + ); + }); + } updateGitIgnore(repoRoot); addDepsToPackageJson(repoRoot); diff --git a/packages/nx/src/command-line/init/implementation/add-nx-to-nest.ts b/packages/nx/src/command-line/init/implementation/add-nx-to-nest.ts index 1c5d4287c7eb9..d8d8f21ff126b 100644 --- a/packages/nx/src/command-line/init/implementation/add-nx-to-nest.ts +++ b/packages/nx/src/command-line/init/implementation/add-nx-to-nest.ts @@ -16,7 +16,7 @@ import { addDepsToPackageJson, createNxJsonFile, initCloud, - markRootPackageJsonAsNxProject, + markRootPackageJsonAsNxProjectLegacy, printFinalMessage, runInstall, updateGitIgnore, @@ -121,7 +121,7 @@ export async function addNxToNest(options: Options, packageJson: PackageJson) { updateGitIgnore(repoRoot); addDepsToPackageJson(repoRoot); addNestPluginToPackageJson(repoRoot); - markRootPackageJsonAsNxProject(repoRoot, cacheableOperations, pmc); + markRootPackageJsonAsNxProjectLegacy(repoRoot, cacheableOperations, pmc); createProjectJson(repoRoot, packageJson, nestCLIConfiguration); removeFile(repoRoot, 'nest-cli.json'); diff --git a/packages/nx/src/command-line/init/implementation/add-nx-to-npm-repo.ts b/packages/nx/src/command-line/init/implementation/add-nx-to-npm-repo.ts index cdbfbe8fdd392..b8dbffde2f957 100644 --- a/packages/nx/src/command-line/init/implementation/add-nx-to-npm-repo.ts +++ b/packages/nx/src/command-line/init/implementation/add-nx-to-npm-repo.ts @@ -1,4 +1,5 @@ import * as enquirer from 'enquirer'; +import { join } from 'path'; import { InitArgs } from '../init-v1'; import { readJsonFile } from '../../../utils/fileutils'; import { output } from '../../../utils/output'; @@ -7,14 +8,17 @@ import { addDepsToPackageJson, createNxJsonFile, initCloud, - markRootPackageJsonAsNxProject, + markPackageJsonAsNxProject, + markRootPackageJsonAsNxProjectLegacy, printFinalMessage, runInstall, updateGitIgnore, } from './utils'; import { connectExistingRepoToNxCloudPrompt } from '../../connect/connect-to-nx-cloud'; -type Options = Pick; +type Options = Pick & { + legacy?: boolean; +}; export async function addNxToNpmRepo(options: Options) { const repoRoot = process.cwd(); @@ -78,7 +82,14 @@ export async function addNxToNpmRepo(options: Options) { updateGitIgnore(repoRoot); addDepsToPackageJson(repoRoot); - markRootPackageJsonAsNxProject(repoRoot, cacheableOperations, pmc); + if (options.legacy) { + markRootPackageJsonAsNxProjectLegacy(repoRoot, cacheableOperations, pmc); + } else { + markPackageJsonAsNxProject( + join(repoRoot, 'package.json'), + cacheableOperations + ); + } output.log({ title: '📦 Installing dependencies' }); diff --git a/packages/nx/src/command-line/init/implementation/utils.ts b/packages/nx/src/command-line/init/implementation/utils.ts index 478827fc77d2e..c8bfeb835b37c 100644 --- a/packages/nx/src/command-line/init/implementation/utils.ts +++ b/packages/nx/src/command-line/init/implementation/utils.ts @@ -168,7 +168,7 @@ export function addVsCodeRecommendedExtensions( } } -export function markRootPackageJsonAsNxProject( +export function markRootPackageJsonAsNxProjectLegacy( repoRoot: string, cacheableScripts: string[], pmc: PackageManagerCommands @@ -194,6 +194,24 @@ export function markRootPackageJsonAsNxProject( writeJsonFile(`package.json`, json); } +export function markPackageJsonAsNxProject( + packageJsonPath: string, + cacheableScripts: string[] +) { + const json = readJsonFile(packageJsonPath); + if (!json.scripts) { + return; + } + + json.nx = { includedScripts: [] }; + for (let script of cacheableScripts) { + if (json.scripts[script]) { + json.nx.includedScripts.push(script); + } + } + writeJsonFile(packageJsonPath, json); +} + export function printFinalMessage({ learnMoreLink, bodyLines, diff --git a/packages/nx/src/command-line/init/init-v1.ts b/packages/nx/src/command-line/init/init-v1.ts index 468f2207dc3a0..5acf5f503e43c 100644 --- a/packages/nx/src/command-line/init/init-v1.ts +++ b/packages/nx/src/command-line/init/init-v1.ts @@ -45,9 +45,9 @@ export async function initHandler(options: InitArgs) { } else if (isNestCLI(packageJson)) { await addNxToNest(options, packageJson); } else if (isMonorepo(packageJson)) { - await addNxToMonorepo(options); + await addNxToMonorepo({ ...options, legacy: true }); } else { - await addNxToNpmRepo(options); + await addNxToNpmRepo({ ...options, legacy: true }); } } else { const useDotNxFolder = await prompt<{ useDotNxFolder: string }>([