From 24a9754848a4753354c7529fc94f0b3b2becf367 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Fri, 22 Mar 2024 12:52:36 -0400 Subject: [PATCH] fix(core): fix no plugins found for nx init without packge.json (#22434) (cherry picked from commit b7b70da6f8bb147a1f0ad9f06bc5b6907c6ac76c) --- packages/nx/src/command-line/add/add.ts | 2 +- packages/nx/src/command-line/init/init-v2.ts | 34 ++++++++++++-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/packages/nx/src/command-line/add/add.ts b/packages/nx/src/command-line/add/add.ts index ebd810b52a23b..d1f30ae659a7d 100644 --- a/packages/nx/src/command-line/add/add.ts +++ b/packages/nx/src/command-line/add/add.ts @@ -63,7 +63,7 @@ async function installPackage(pkgName: string, version: string): Promise { writeJsonFile('nx.json', nxJson); try { - await runNxAsync('--help'); + await runNxAsync('--help', { silent: true }); } catch (e) { // revert adding the plugin to nx.json nxJson.installation.plugins[pkgName] = undefined; diff --git a/packages/nx/src/command-line/init/init-v2.ts b/packages/nx/src/command-line/init/init-v2.ts index 01fca0532c046..84878c119260a 100644 --- a/packages/nx/src/command-line/init/init-v2.ts +++ b/packages/nx/src/command-line/init/init-v2.ts @@ -72,9 +72,9 @@ export async function initHandler(options: InitArgs): Promise { output.log({ title: '🧐 Checking dependencies' }); - const detectPluginsResponse = await detectPlugins(); + const { plugins, updatePackageScripts } = await detectPlugins(); - if (!detectPluginsResponse?.plugins.length) { + if (!plugins.length) { // If no plugins are detected/chosen, guide users to setup // their targetDefaults correctly so their package scripts will work. const packageJson: PackageJson = readJsonFile('package.json'); @@ -96,19 +96,17 @@ export async function initHandler(options: InitArgs): Promise { createNxJsonFile(repoRoot, [], [], {}); updateGitIgnore(repoRoot); - addDepsToPackageJson(repoRoot, detectPluginsResponse.plugins); + addDepsToPackageJson(repoRoot, plugins); output.log({ title: '📦 Installing Nx' }); runInstall(repoRoot, pmc); output.log({ title: '🔨 Configuring plugins' }); - for (const plugin of detectPluginsResponse.plugins) { + for (const plugin of plugins) { execSync( `${pmc.exec} nx g ${plugin}:init --keepExistingVersions ${ - detectPluginsResponse.updatePackageScripts - ? '--updatePackageScripts' - : '' + updatePackageScripts ? '--updatePackageScripts' : '' } --no-interactive`, { stdio: [0, 1, 2], @@ -117,7 +115,7 @@ export async function initHandler(options: InitArgs): Promise { ); } - if (!detectPluginsResponse.updatePackageScripts) { + if (!updatePackageScripts) { const rootPackageJsonPath = join(repoRoot, 'package.json'); const json = readJsonFile(rootPackageJsonPath); json.nx = { includedScripts: [] }; @@ -167,9 +165,10 @@ const npmPackageToPluginMap: Record = { '@remix-run/dev': '@nx/remix', }; -async function detectPlugins(): Promise< - undefined | { plugins: string[]; updatePackageScripts: boolean } -> { +async function detectPlugins(): Promise<{ + plugins: string[]; + updatePackageScripts: boolean; +}> { let files = ['package.json'].concat( globWithWorkspaceContext(process.cwd(), ['**/*/package.json']) ); @@ -203,7 +202,12 @@ async function detectPlugins(): Promise< const plugins = Array.from(detectedPlugins); - if (plugins.length === 0) return undefined; + if (plugins.length === 0) { + return { + plugins: [], + updatePackageScripts: false, + }; + } output.log({ title: `Recommended Plugins:`, @@ -222,7 +226,11 @@ async function detectPlugins(): Promise< }, ]).then((r) => r.plugins); - if (pluginsToInstall?.length === 0) return undefined; + if (pluginsToInstall?.length === 0) + return { + plugins: [], + updatePackageScripts: false, + }; const updatePackageScripts = existsSync('package.json') &&