From 801a22b243b271c468017338a9100efe2383da52 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Tue, 16 Apr 2024 16:48:48 -0400 Subject: [PATCH] fix(core): fix init logging and package.json updates (#22843) --- e2e/nx-init/src/nx-init-nest.test.ts | 2 +- e2e/nx-init/src/nx-init-npm-repo.test.ts | 3 +-- .../init/implementation/add-nx-to-monorepo.ts | 18 ------------- .../init/implementation/add-nx-to-nest.ts | 7 ------ .../init/implementation/add-nx-to-npm-repo.ts | 6 ----- .../init/implementation/angular/index.ts | 8 ------ .../angular/legacy-angular-versions.ts | 9 +------ .../init/implementation/react/index.ts | 22 ---------------- .../command-line/init/implementation/utils.ts | 10 +++----- packages/nx/src/command-line/init/init-v1.ts | 25 ++++++++++++++++++- packages/nx/src/command-line/init/init-v2.ts | 16 +++++++----- 11 files changed, 40 insertions(+), 86 deletions(-) diff --git a/e2e/nx-init/src/nx-init-nest.test.ts b/e2e/nx-init/src/nx-init-nest.test.ts index d42ade6e68324..e48cb9bf0acfb 100644 --- a/e2e/nx-init/src/nx-init-nest.test.ts +++ b/e2e/nx-init/src/nx-init-nest.test.ts @@ -48,7 +48,7 @@ describe('nx init (for NestCLI - legacy)', () => { } ); - expect(output).toContain('Enabled computation caching'); + expect(output).toContain('Run it again to replay the cached computation.'); // nest-cli.json is removed expect(exists(`${projectRoot}/nest-cli.json`)).toBeFalsy(); diff --git a/e2e/nx-init/src/nx-init-npm-repo.test.ts b/e2e/nx-init/src/nx-init-npm-repo.test.ts index 53c4ae292a869..36a51f979d039 100644 --- a/e2e/nx-init/src/nx-init-npm-repo.test.ts +++ b/e2e/nx-init/src/nx-init-npm-repo.test.ts @@ -42,8 +42,7 @@ describe('nx init (NPM repo - legacy)', () => { pmc.runUninstalledPackage } nx@${getPublishedVersion()} init --cacheable=echo --no-interactive` ); - console.log(output); - expect(output).toContain('Enabled computation caching'); + expect(output).toContain('Run it again to replay the cached computation.'); expect(runCLI('echo')).toContain('123'); renameFile('nx.json', 'nx.json.old'); 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 a767f5d1feb2d..40d22d184fa3e 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 @@ -5,13 +5,10 @@ import { join, relative } from 'path'; import { InitArgs } from '../init-v1'; import { readJsonFile } from '../../../utils/fileutils'; import { output } from '../../../utils/output'; -import { getPackageManagerCommand } from '../../../utils/package-manager'; import { addDepsToPackageJson, createNxJsonFile, initCloud, - markPackageJsonAsNxProject, - printFinalMessage, runInstall, updateGitIgnore, } from './utils'; @@ -94,11 +91,6 @@ export async function addNxToMonorepo(options: Options) { cacheableOperations, scriptOutputs ); - if (!options.legacy) { - packageJsonFiles.forEach((packageJsonPath) => { - markPackageJsonAsNxProject(join(repoRoot, packageJsonPath)); - }); - } updateGitIgnore(repoRoot); addDepsToPackageJson(repoRoot); @@ -110,16 +102,6 @@ export async function addNxToMonorepo(options: Options) { output.log({ title: '🛠️ Setting up Nx Cloud' }); initCloud(repoRoot, 'nx-init-monorepo'); } - - const pmc = getPackageManagerCommand(); - printFinalMessage({ - learnMoreLink: 'https://nx.dev/recipes/adopting-nx/adding-to-monorepo', - bodyLines: [ - `- Run "${pmc.exec} nx run-many --target=build" to run the build script for every project in the monorepo.`, - '- Run it again to replay the cached computation.', - `- Run "${pmc.exec} nx graph" to see the structure of the monorepo.`, - ], - }); } // scanning package.json files 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 d8d8f21ff126b..5ebfcf374e7bf 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 @@ -17,7 +17,6 @@ import { createNxJsonFile, initCloud, markRootPackageJsonAsNxProjectLegacy, - printFinalMessage, runInstall, updateGitIgnore, } from './utils'; @@ -139,10 +138,6 @@ export async function addNxToNest(options: Options, packageJson: PackageJson) { output.log({ title: '🛠️ Setting up Nx Cloud' }); initCloud(repoRoot, 'nx-init-nest'); } - - printFinalMessage({ - learnMoreLink: 'https://nx.dev/recipes/adopting-nx/adding-to-monorepo', - }); } function addNestPluginToPackageJson(repoRoot: string) { @@ -178,8 +173,6 @@ function createProjectJson( }, }; - console.log(nestCLIOptions); - if (nestCLIOptions.webpackOptions) { json.targets['build'] = { executor: '@nx/webpack:webpack', 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 c8a3263ebe0a3..6ca9fa8af16b4 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 @@ -10,7 +10,6 @@ import { initCloud, markPackageJsonAsNxProject, markRootPackageJsonAsNxProjectLegacy, - printFinalMessage, runInstall, updateGitIgnore, } from './utils'; @@ -96,9 +95,4 @@ export async function addNxToNpmRepo(options: Options) { output.log({ title: '🛠️ Setting up Nx Cloud' }); initCloud(repoRoot, 'nx-init-npm-repo'); } - - printFinalMessage({ - learnMoreLink: - 'https://nx.dev/recipes/adopting-nx/adding-to-existing-project', - }); } diff --git a/packages/nx/src/command-line/init/implementation/angular/index.ts b/packages/nx/src/command-line/init/implementation/angular/index.ts index 59d73712f04f9..b65a469b92bd9 100644 --- a/packages/nx/src/command-line/init/implementation/angular/index.ts +++ b/packages/nx/src/command-line/init/implementation/angular/index.ts @@ -8,7 +8,6 @@ import type { PackageJson } from '../../../../utils/package-json'; import { addDepsToPackageJson, initCloud, - printFinalMessage, runInstall, updateGitIgnore, } from '../utils'; @@ -65,13 +64,6 @@ export async function addNxToAngularCliRepo(options: Options) { output.log({ title: '🛠️ Setting up Nx Cloud' }); initCloud(repoRoot, 'nx-init-angular'); } - - printFinalMessage({ - learnMoreLink: 'https://nx.dev/recipes/angular/migration/angular', - bodyLines: [ - '- Execute "npx nx build" twice to see the computation caching in action.', - ], - }); } async function collectCacheableOperations(options: Options): Promise { diff --git a/packages/nx/src/command-line/init/implementation/angular/legacy-angular-versions.ts b/packages/nx/src/command-line/init/implementation/angular/legacy-angular-versions.ts index d8e5678ee75c8..86afc96f1715f 100644 --- a/packages/nx/src/command-line/init/implementation/angular/legacy-angular-versions.ts +++ b/packages/nx/src/command-line/init/implementation/angular/legacy-angular-versions.ts @@ -11,7 +11,7 @@ import { resolvePackageVersionUsingInstallation, resolvePackageVersionUsingRegistry, } from '../../../../utils/package-manager'; -import { initCloud, printFinalMessage } from '../utils'; +import { initCloud } from '../utils'; import type { Options } from './types'; import { connectExistingRepoToNxCloudPrompt } from '../../../connect/connect-to-nx-cloud'; @@ -111,13 +111,6 @@ export async function getLegacyMigrationFunctionIfApplicable( output.log({ title: '🛠️ Setting up Nx Cloud' }); initCloud(repoRoot, 'nx-init-angular'); } - - printFinalMessage({ - learnMoreLink: 'https://nx.dev/recipes/angular/migration/angular', - bodyLines: [ - '- Execute "npx nx build" twice to see the computation caching in action.', - ], - }); }; } diff --git a/packages/nx/src/command-line/init/implementation/react/index.ts b/packages/nx/src/command-line/init/implementation/react/index.ts index 235a507036c0f..ae3b6c2bb77d9 100644 --- a/packages/nx/src/command-line/init/implementation/react/index.ts +++ b/packages/nx/src/command-line/init/implementation/react/index.ts @@ -14,7 +14,6 @@ import { PackageManagerCommands, } from '../../../../utils/package-manager'; import { PackageJson } from '../../../../utils/package-json'; -import { printFinalMessage } from '../utils'; import { checkForCustomWebpackSetup } from './check-for-custom-webpack-setup'; import { checkForUncommittedChanges } from './check-for-uncommitted-changes'; import { cleanUpFiles } from './clean-up-files'; @@ -141,27 +140,6 @@ async function reorgnizeWorkspaceStructure(options: NormalizedOptions) { output.log({ title: '📦 Installing dependencies' }); installDependencies(options); - const buildCommand = options.integrated - ? `npx nx build ${options.reactAppName}` - : 'npm run build'; - printFinalMessage({ - learnMoreLink: - 'https://nx.dev/recipes/adopting-nx/adding-to-existing-project', - bodyLines: [ - `- Execute "${buildCommand}" twice to see the computation caching in action.`, - ], - }); - - output.note({ - title: 'First time using Nx? Check out this interactive Nx tutorial.', - bodyLines: [ - `https://nx.dev/react-tutorial/1-code-generation`, - ` `, - `Prefer watching videos? Check out this free Nx course on Egghead.io.`, - `https://egghead.io/playlists/scale-react-development-with-nx-4038`, - ], - }); - if (options.isVite) { const indexPath = options.isStandalone ? 'index.html' diff --git a/packages/nx/src/command-line/init/implementation/utils.ts b/packages/nx/src/command-line/init/implementation/utils.ts index ce133924bcb1d..dd5b3af5ae42c 100644 --- a/packages/nx/src/command-line/init/implementation/utils.ts +++ b/packages/nx/src/command-line/init/implementation/utils.ts @@ -206,20 +206,16 @@ export function markPackageJsonAsNxProject(packageJsonPath: string) { export function printFinalMessage({ learnMoreLink, - bodyLines, }: { learnMoreLink?: string; - bodyLines?: string[]; }): void { - const normalizedBodyLines = (bodyLines ?? []).map((l) => - l.startsWith('- ') ? l : `- ${l}` - ); + const pmc = getPackageManagerCommand(); output.success({ title: '🎉 Done!', bodyLines: [ - '- Enabled computation caching!', - ...normalizedBodyLines, + `- Run "${pmc.exec} nx run-many -t build" to run the build target for every project in the workspace. Run it again to replay the cached computation. https://nx.dev/features/cache-task-results`, + `- Run "${pmc.exec} nx graph" to see the graph of projects and tasks in your workspace. https://nx.dev/core-features/explore-graph`, learnMoreLink ? `- Learn more at ${learnMoreLink}.` : undefined, ].filter(Boolean), }); diff --git a/packages/nx/src/command-line/init/init-v1.ts b/packages/nx/src/command-line/init/init-v1.ts index 5acf5f503e43c..8eb114e278ff4 100644 --- a/packages/nx/src/command-line/init/init-v1.ts +++ b/packages/nx/src/command-line/init/init-v1.ts @@ -12,7 +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'; +import { isMonorepo, printFinalMessage } from './implementation/utils'; export interface InitArgs { addE2e: boolean; @@ -40,14 +40,37 @@ export async function initHandler(options: InitArgs) { const packageJson: PackageJson = readJsonFile('package.json'); if (existsSync('angular.json')) { await addNxToAngularCliRepo(options); + + printFinalMessage({ + learnMoreLink: 'https://nx.dev/recipes/angular/migration/angular', + }); + return; } else if (isCRA(packageJson)) { await addNxToCraRepo(options); + + printFinalMessage({ + learnMoreLink: options.integrated + ? 'https://nx.dev/getting-started/tutorials/react-monorepo-tutorial' + : 'https://nx.dev/getting-started/tutorials/react-standalone-tutorial', + }); + return; } else if (isNestCLI(packageJson)) { await addNxToNest(options, packageJson); + printFinalMessage({ + learnMoreLink: 'https://nx.dev/recipes/adopting-nx/adding-to-monorepo', + }); + return; } else if (isMonorepo(packageJson)) { await addNxToMonorepo({ ...options, legacy: true }); + printFinalMessage({ + learnMoreLink: 'https://nx.dev/recipes/adopting-nx/adding-to-monorepo', + }); } else { await addNxToNpmRepo({ ...options, legacy: true }); + printFinalMessage({ + learnMoreLink: + 'https://nx.dev/recipes/adopting-nx/adding-to-existing-project', + }); } } else { const useDotNxFolder = await prompt<{ useDotNxFolder: string }>([ diff --git a/packages/nx/src/command-line/init/init-v2.ts b/packages/nx/src/command-line/init/init-v2.ts index 62e11f83cbc34..e2d0e31900cf5 100644 --- a/packages/nx/src/command-line/init/init-v2.ts +++ b/packages/nx/src/command-line/init/init-v2.ts @@ -11,6 +11,7 @@ import { addDepsToPackageJson, createNxJsonFile, isMonorepo, + printFinalMessage, runInstall, updateGitIgnore, } from './implementation/utils'; @@ -67,6 +68,10 @@ export async function initHandler(options: InitArgs): Promise { ...options, integrated: !!options.integrated, }); + + printFinalMessage({ + learnMoreLink: 'https://nx.dev/recipes/angular/migration/angular', + }); return; } @@ -86,6 +91,9 @@ export async function initHandler(options: InitArgs): Promise { nxCloud: false, }); } + const learnMoreLink = isMonorepo(packageJson) + ? 'https://nx.dev/getting-started/tutorials/npm-workspaces-tutorial' + : 'https://nx.dev/recipes/adopting-nx/adding-to-existing-project'; const useNxCloud = options.nxCloud ?? (options.interactive ? await connectExistingRepoToNxCloudPrompt() : false); @@ -128,12 +136,8 @@ export async function initHandler(options: InitArgs): Promise { ); } - output.log({ - title: '👀 Explore Your Workspace', - bodyLines: [ - `Run "nx graph" to show the graph of the workspace. It will show tasks that you can run with Nx.`, - `Read this guide on exploring your workspace: https://nx.dev/core-features/explore-graph`, - ], + printFinalMessage({ + learnMoreLink, }); }