From 59a3db868560f27d64336013cd8744c216b81874 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Thu, 5 Dec 2024 10:55:50 -0800 Subject: [PATCH] fix(core): skip nx cloud prompt when interactive is false (#28949) ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes # --- e2e/utils/create-project-utils.ts | 2 +- .../src/internal-utils/prompts.ts | 29 ++++++++++--------- .../create-package/create-package.ts | 2 +- .../files/e2e/__cliName__.spec.ts__tmpl__ | 14 +++++---- .../plugin/src/generators/e2e-project/e2e.ts | 2 +- .../files/src/__pluginName__.spec.ts__tmpl__ | 16 +++++----- 6 files changed, 35 insertions(+), 30 deletions(-) diff --git a/e2e/utils/create-project-utils.ts b/e2e/utils/create-project-utils.ts index 05406751870df..7199e95e8623c 100644 --- a/e2e/utils/create-project-utils.ts +++ b/e2e/utils/create-project-utils.ts @@ -372,7 +372,7 @@ export function runCreatePlugin( let command = `${ pm.runUninstalledPackage - } create-nx-plugin@${getPublishedVersion()} ${name} --nxCloud=skip`; + } create-nx-plugin@${getPublishedVersion()} ${name} --nxCloud=skip --no-interactive`; if (packageManager && !useDetectedPm) { command += ` --package-manager=${packageManager}`; diff --git a/packages/create-nx-workspace/src/internal-utils/prompts.ts b/packages/create-nx-workspace/src/internal-utils/prompts.ts index 93414a9604de7..2b622428a80d3 100644 --- a/packages/create-nx-workspace/src/internal-utils/prompts.ts +++ b/packages/create-nx-workspace/src/internal-utils/prompts.ts @@ -1,4 +1,7 @@ import * as yargs from 'yargs'; +import * as enquirer from 'enquirer'; +import * as chalk from 'chalk'; + import { MessageKey, messages } from '../utils/nx/ab-testing'; import { output } from '../utils/output'; import { deduceDefaultBase } from '../utils/git/default-base'; @@ -8,17 +11,18 @@ import { packageManagerList, } from '../utils/package-manager'; import { stringifyCollection } from '../utils/string-utils'; -import enquirer = require('enquirer'); import { NxCloud } from '../utils/nx/nx-cloud'; -import chalk = require('chalk'); +import { isCI } from '../utils/ci/is-ci'; export async function determineNxCloud( parsedArgs: yargs.Arguments<{ nxCloud: NxCloud }> ): Promise { - if (parsedArgs.nxCloud === undefined) { - return nxCloudPrompt('setupCI'); - } else { + if (parsedArgs.nxCloud) { return parsedArgs.nxCloud; + } else if (!parsedArgs.interactive || isCI()) { + return 'skip'; + } else { + return nxCloudPrompt('setupCI'); } } @@ -70,9 +74,8 @@ export async function determineDefaultBase( parsedArgs: yargs.Arguments<{ defaultBase?: string }> ): Promise { if (parsedArgs.defaultBase) { - return Promise.resolve(parsedArgs.defaultBase); - } - if (parsedArgs.allPrompts) { + return parsedArgs.defaultBase; + } else if (parsedArgs.allPrompts) { return enquirer .prompt<{ DefaultBase: string }>([ { @@ -93,7 +96,7 @@ export async function determineDefaultBase( return a.DefaultBase; }); } - return Promise.resolve(deduceDefaultBase()); + return deduceDefaultBase(); } export async function determinePackageManager( @@ -103,7 +106,7 @@ export async function determinePackageManager( if (packageManager) { if (packageManagerList.includes(packageManager as PackageManager)) { - return Promise.resolve(packageManager as PackageManager); + return packageManager as PackageManager; } output.error({ title: 'Invalid package manager', @@ -114,9 +117,7 @@ export async function determinePackageManager( ], }); process.exit(1); - } - - if (parsedArgs.allPrompts) { + } else if (parsedArgs.allPrompts) { return enquirer .prompt<{ packageManager: PackageManager }>([ { @@ -135,5 +136,5 @@ export async function determinePackageManager( .then((a) => a.packageManager); } - return Promise.resolve(detectInvokedPackageManager()); + return detectInvokedPackageManager(); } diff --git a/packages/plugin/src/generators/create-package/create-package.ts b/packages/plugin/src/generators/create-package/create-package.ts index f6966037b542b..930bd5fc4fbea 100644 --- a/packages/plugin/src/generators/create-package/create-package.ts +++ b/packages/plugin/src/generators/create-package/create-package.ts @@ -229,7 +229,7 @@ function addE2eProject(host: Tree, options: NormalizedSchema) { { pluginName: options.project, cliName: options.name, - packageManagerCommands: getPackageManagerCommand('npm'), + packageManagerCommands: getPackageManagerCommand(), pluginPackageName, tmpl: '', } diff --git a/packages/plugin/src/generators/create-package/files/e2e/__cliName__.spec.ts__tmpl__ b/packages/plugin/src/generators/create-package/files/e2e/__cliName__.spec.ts__tmpl__ index 873b84f1a94b9..11c89d9f74f89 100644 --- a/packages/plugin/src/generators/create-package/files/e2e/__cliName__.spec.ts__tmpl__ +++ b/packages/plugin/src/generators/create-package/files/e2e/__cliName__.spec.ts__tmpl__ @@ -6,11 +6,13 @@ describe('<%= cliName %>', () => { let projectDirectory: string; afterAll(() => { - // Cleanup the test project - rmSync(projectDirectory, { - recursive: true, - force: true, - }); + if (projectDirectory) { + // Cleanup the test project + rmSync(projectDirectory, { + recursive: true, + force: true, + }); + } }); @@ -43,7 +45,7 @@ function createTestProject(extraArgs = '') { }); execSync( - `<%= packageManagerCommands.exec %> --yes <%= cliName %>@e2e ${projectName} ${extraArgs}`, + `<%= packageManagerCommands.dlx %> <%= cliName %>@e2e ${projectName} ${extraArgs}`, { cwd: dirname(projectDirectory), stdio: 'inherit', diff --git a/packages/plugin/src/generators/e2e-project/e2e.ts b/packages/plugin/src/generators/e2e-project/e2e.ts index fa948ff94ae4f..0439dc678742a 100644 --- a/packages/plugin/src/generators/e2e-project/e2e.ts +++ b/packages/plugin/src/generators/e2e-project/e2e.ts @@ -111,7 +111,7 @@ function addFiles(host: Tree, options: NormalizedSchema) { ...options, tmpl: '', rootTsConfigPath: getRelativePathToRootTsConfig(host, options.projectRoot), - packageManagerCommands: getPackageManagerCommand('npm'), + packageManagerCommands: getPackageManagerCommand(), pluginPackageName, }); } diff --git a/packages/plugin/src/generators/e2e-project/files/src/__pluginName__.spec.ts__tmpl__ b/packages/plugin/src/generators/e2e-project/files/src/__pluginName__.spec.ts__tmpl__ index bf71aadb0f6d2..1519076b188c4 100644 --- a/packages/plugin/src/generators/e2e-project/files/src/__pluginName__.spec.ts__tmpl__ +++ b/packages/plugin/src/generators/e2e-project/files/src/__pluginName__.spec.ts__tmpl__ @@ -10,7 +10,7 @@ describe('<%= pluginName %>', () => { // The plugin has been built and published to a local registry in the jest globalSetup // Install the plugin built with the latest source code into the test repo - execSync(`<%= packageManagerCommands.install %> <%= pluginPackageName %>@e2e`, { + execSync(`<%= packageManagerCommands.addDev %> <%= pluginPackageName %>@e2e`, { cwd: projectDirectory, stdio: 'inherit', env: process.env, @@ -18,11 +18,13 @@ describe('<%= pluginName %>', () => { }); afterAll(() => { - // Cleanup the test project - rmSync(projectDirectory, { - recursive: true, - force: true, - }); + if (projectDirectory) { + // Cleanup the test project + rmSync(projectDirectory, { + recursive: true, + force: true, + }); + } }); @@ -53,7 +55,7 @@ function createTestProject() { }); execSync( - `<%= packageManagerCommands.exec %> --yes create-nx-workspace@latest ${projectName} --preset apps --nxCloud=skip --no-interactive`, + `<%= packageManagerCommands.dlx %> create-nx-workspace@latest ${projectName} --preset apps --nxCloud=skip --no-interactive`, { cwd: dirname(projectDirectory), stdio: 'inherit',