From 1fca4e64589a10a810719491028246ec4d08bf10 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Mon, 10 Apr 2023 13:16:38 -0400 Subject: [PATCH] feat(misc): make createWorkspace quiter by default --- .../bin/create-nx-workspace.ts | 21 +++++++- .../src/create-empty-workspace.ts | 4 +- .../create-nx-workspace/src/create-sandbox.ts | 2 +- .../src/create-workspace-options.d.ts | 1 + .../src/create-workspace.ts | 24 ++++----- .../src/utils/child-process-utils.ts | 4 +- .../src/utils/ci/setup-ci.ts | 2 +- .../src/utils/error-utils.ts | 21 ++++++-- .../src/utils/nx/nx-cloud.ts | 2 +- .../create-nx-workspace/src/utils/output.ts | 54 ++++++------------- 10 files changed, 71 insertions(+), 64 deletions(-) diff --git a/packages/create-nx-workspace/bin/create-nx-workspace.ts b/packages/create-nx-workspace/bin/create-nx-workspace.ts index 4d17dd28e88576..d2161db03503c1 100644 --- a/packages/create-nx-workspace/bin/create-nx-workspace.ts +++ b/packages/create-nx-workspace/bin/create-nx-workspace.ts @@ -28,6 +28,8 @@ import { withOptions, withPackageManager, } from '../src/internal-utils/yargs-options'; +import { showNxWarning } from '../src/utils/nx/show-nx-warning'; +import { printNxCloudSuccessMessage } from '../src/utils/nx/nx-cloud'; interface Arguments extends CreateWorkspaceOptions { preset: string; @@ -129,7 +131,24 @@ export const commandsObject: yargs.Argv = yargs ) as yargs.Argv; async function main(parsedArgs: yargs.Arguments) { - await createWorkspace(parsedArgs.preset, parsedArgs); + output.log({ + title: `Creating your v${nxVersion} workspace.`, + bodyLines: [ + 'To make sure the command works reliably in all environments, and that the preset is applied correctly,', + `Nx will run "${parsedArgs.packageManager} install" several times. Please wait.`, + ], + }); + + const workspaceInfo = await createWorkspace( + parsedArgs.preset, + parsedArgs + ); + + showNxWarning(parsedArgs.name); + + if (parsedArgs.nxCloud && workspaceInfo.nxCloudInfo) { + printNxCloudSuccessMessage(workspaceInfo.nxCloudInfo); + } if (isKnownPreset(parsedArgs.preset)) { pointToTutorialAndCourse(parsedArgs.preset as Preset); diff --git a/packages/create-nx-workspace/src/create-empty-workspace.ts b/packages/create-nx-workspace/src/create-empty-workspace.ts index ab673a60f036fa..ff647d5a308503 100644 --- a/packages/create-nx-workspace/src/create-empty-workspace.ts +++ b/packages/create-nx-workspace/src/create-empty-workspace.ts @@ -64,13 +64,13 @@ export async function createEmptyWorkspace( await execAndWait(fullCommand, tmpDir); workspaceSetupSpinner.succeed( - `Nx has successfully created the workspace: ${getFileName(name)}.` + `Successfully created the workspace: ${getFileName(name)}.` ); } catch (e) { workspaceSetupSpinner.fail(); if (e instanceof Error) { output.error({ - title: `Nx failed to create a workspace.`, + title: `Failed to create a workspace.`, bodyLines: mapErrorToBodyLines(e), }); } else { diff --git a/packages/create-nx-workspace/src/create-sandbox.ts b/packages/create-nx-workspace/src/create-sandbox.ts index fd27bad941d54a..fe531b77d57cb3 100644 --- a/packages/create-nx-workspace/src/create-sandbox.ts +++ b/packages/create-nx-workspace/src/create-sandbox.ts @@ -46,7 +46,7 @@ export async function createSandbox(packageManager: PackageManager) { installSpinner.fail(); if (e instanceof Error) { output.error({ - title: `Nx failed to install dependencies`, + title: `Failed to install dependencies`, bodyLines: mapErrorToBodyLines(e), }); } else { diff --git a/packages/create-nx-workspace/src/create-workspace-options.d.ts b/packages/create-nx-workspace/src/create-workspace-options.d.ts index c0ecfe190c974c..1b14c13ac08308 100644 --- a/packages/create-nx-workspace/src/create-workspace-options.d.ts +++ b/packages/create-nx-workspace/src/create-workspace-options.d.ts @@ -29,4 +29,5 @@ export interface CreateWorkspaceOptions { email: string; // Email to use for the initial commit message: string; // Message to use for the initial commit }; + cliName?: string; // Name of the CLI, used when displaying outputs. e.g. nx, Nx } diff --git a/packages/create-nx-workspace/src/create-workspace.ts b/packages/create-nx-workspace/src/create-workspace.ts index ed7d090f24f075..7ef13b4ded4170 100644 --- a/packages/create-nx-workspace/src/create-workspace.ts +++ b/packages/create-nx-workspace/src/create-workspace.ts @@ -1,10 +1,9 @@ import { CreateWorkspaceOptions } from './create-workspace-options'; import { output } from './utils/output'; -import { printNxCloudSuccessMessage, setupNxCloud } from './utils/nx/nx-cloud'; +import { setupNxCloud } from './utils/nx/nx-cloud'; import { createSandbox } from './create-sandbox'; import { createEmptyWorkspace } from './create-empty-workspace'; import { createPreset } from './create-preset'; -import { showNxWarning } from './utils/nx/show-nx-warning'; import { setupCI } from './utils/ci/setup-ci'; import { messages, recordStat } from './utils/nx/ab-testing'; import { initializeGitRepo } from './utils/git/git'; @@ -24,15 +23,12 @@ export async function createWorkspace( skipGit = false, defaultBase = 'main', commit, + cliName, } = options; - output.log({ - title: `Nx is creating your v${nxVersion} workspace.`, - bodyLines: [ - 'To make sure the command works reliably in all environments, and that the preset is applied correctly,', - `Nx will run "${options.packageManager} install" several times. Please wait.`, - ], - }); + if (cliName) { + output.setCliName(cliName ?? 'NX'); + } const tmpDir = await createSandbox(packageManager); @@ -79,16 +75,14 @@ export async function createWorkspace( } } - showNxWarning(name); - - if (nxCloud && nxCloudInstallRes?.code === 0) { - printNxCloudSuccessMessage(nxCloudInstallRes.stdout); - } - await recordStat({ nxVersion, command: 'create-nx-workspace', useCloud: nxCloud, meta: messages.codeOfSelectedPromptMessage('nxCloudCreation'), }); + + return { + nxCloudInfo: nxCloudInstallRes?.stdout, + }; } diff --git a/packages/create-nx-workspace/src/utils/child-process-utils.ts b/packages/create-nx-workspace/src/utils/child-process-utils.ts index 9b57d4cb971225..a141a23abb3110 100644 --- a/packages/create-nx-workspace/src/utils/child-process-utils.ts +++ b/packages/create-nx-workspace/src/utils/child-process-utils.ts @@ -34,7 +34,9 @@ export function execAndWait(command: string, cwd: string) { const logFile = join(cwd, 'error.log'); writeFileSync(logFile, `${stdout}\n${stderr}`); const message = stderr && stderr.trim().length ? stderr : stdout; - rej(new CreateNxWorkspaceError(message, error.code, logFile)); + rej( + new CreateNxWorkspaceError(message, { code: error.code, logFile }) + ); } else { res({ code: 0, stdout }); } diff --git a/packages/create-nx-workspace/src/utils/ci/setup-ci.ts b/packages/create-nx-workspace/src/utils/ci/setup-ci.ts index 485cdef08e8970..dedff15a26b60c 100644 --- a/packages/create-nx-workspace/src/utils/ci/setup-ci.ts +++ b/packages/create-nx-workspace/src/utils/ci/setup-ci.ts @@ -35,7 +35,7 @@ export async function setupCI( ciSpinner.fail(); if (e instanceof Error) { output.error({ - title: `Nx failed to generate CI workflow`, + title: `Failed to generate CI workflow`, bodyLines: mapErrorToBodyLines(e), }); } else { diff --git a/packages/create-nx-workspace/src/utils/error-utils.ts b/packages/create-nx-workspace/src/utils/error-utils.ts index d36f589a158861..9dc1e724360b2d 100644 --- a/packages/create-nx-workspace/src/utils/error-utils.ts +++ b/packages/create-nx-workspace/src/utils/error-utils.ts @@ -1,10 +1,23 @@ export class CreateNxWorkspaceError extends Error { + public code?: number | null; + public logFile?: string; + public title?: string; + public bodyLines?: string[]; + constructor( - public logMessage: string, - public code: number | null | undefined, - public logFile: string + message: string, + opts?: { + title?: string; + code?: number | null; + logFile?: string; + bodyLines?: string[]; + } ) { - super(logMessage); + super(message); + this.code = opts?.code; + this.logFile = opts?.logFile; + this.title = opts?.title; + this.bodyLines = opts?.bodyLines; this.name = 'CreateNxWorkspaceError'; } } diff --git a/packages/create-nx-workspace/src/utils/nx/nx-cloud.ts b/packages/create-nx-workspace/src/utils/nx/nx-cloud.ts index afc1de5a915965..642b5737c65319 100644 --- a/packages/create-nx-workspace/src/utils/nx/nx-cloud.ts +++ b/packages/create-nx-workspace/src/utils/nx/nx-cloud.ts @@ -24,7 +24,7 @@ export async function setupNxCloud( if (e instanceof Error) { output.error({ - title: `Nx failed to setup NxCloud`, + title: `Failed to setup NxCloud`, bodyLines: mapErrorToBodyLines(e), }); } else { diff --git a/packages/create-nx-workspace/src/utils/output.ts b/packages/create-nx-workspace/src/utils/output.ts index 429bc8586aba9b..b74be435f1c908 100644 --- a/packages/create-nx-workspace/src/utils/output.ts +++ b/packages/create-nx-workspace/src/utils/output.ts @@ -10,13 +10,11 @@ import { isCI } from './ci/is-ci'; export interface CLIErrorMessageConfig { title: string; bodyLines?: string[]; - slug?: string; } export interface CLIWarnMessageConfig { title: string; bodyLines?: string[]; - slug?: string; } export interface CLINoteMessageConfig { @@ -82,7 +80,7 @@ class CLIOutput { color: string; title: string; }): void { - this.writeToStdOut(` ${this.applyNxPrefix(color, title)}${EOL}`); + this.writeToStdOut(` ${this.applyCLIPrefix(color, title)}${EOL}`); } private writeOptionalOutputBody(bodyLines?: string[]): void { @@ -93,18 +91,24 @@ class CLIOutput { bodyLines.forEach((bodyLine) => this.writeToStdOut(` ${bodyLine}${EOL}`)); } - applyNxPrefix(color = 'cyan', text: string): string { - let nxPrefix = ''; + private cliName = 'NX'; + + setCliName(name: string) { + this.cliName = name; + } + + applyCLIPrefix(color = 'cyan', text: string): string { + let cliPrefix = ''; if ((chalk as any)[color]) { - nxPrefix = `${(chalk as any)[color]('>')} ${( + cliPrefix = `${(chalk as any)[color]('>')} ${( chalk as any - ).reset.inverse.bold[color](' NX ')}`; + ).reset.inverse.bold[color](` ${this.cliName} `)}`; } else { - nxPrefix = `${chalk.keyword(color)( + cliPrefix = `${chalk.keyword(color)( '>' - )} ${chalk.reset.inverse.bold.keyword(color)(' NX ')}`; + )} ${chalk.reset.inverse.bold.keyword(color)(` ${this.cliName} `)}`; } - return `${nxPrefix} ${text}`; + return `${cliPrefix} ${text}`; } addNewline() { @@ -125,7 +129,7 @@ class CLIOutput { ); } - error({ title, slug, bodyLines }: CLIErrorMessageConfig) { + error({ title, bodyLines }: CLIErrorMessageConfig) { this.addNewline(); this.writeOutputTitle({ @@ -135,22 +139,10 @@ class CLIOutput { this.writeOptionalOutputBody(bodyLines); - /** - * Optional slug to be used in an Nx error message redirect URL - */ - if (slug && typeof slug === 'string') { - this.addNewline(); - this.writeToStdOut( - `${chalk.grey( - ' Learn more about this error: ' - )}https://errors.nx.dev/${slug}${EOL}` - ); - } - this.addNewline(); } - warn({ title, slug, bodyLines }: CLIWarnMessageConfig) { + warn({ title, bodyLines }: CLIWarnMessageConfig) { this.addNewline(); this.writeOutputTitle({ @@ -160,18 +152,6 @@ class CLIOutput { this.writeOptionalOutputBody(bodyLines); - /** - * Optional slug to be used in an Nx warning message redirect URL - */ - if (slug && typeof slug === 'string') { - this.addNewline(); - this.writeToStdOut( - `${chalk.grey( - ' Learn more about this warning: ' - )}https://errors.nx.dev/${slug}\n` - ); - } - this.addNewline(); } @@ -212,8 +192,6 @@ class CLIOutput { this.addNewline(); } - logCommand(message: string) {} - log({ title, bodyLines, color }: CLIWarnMessageConfig & { color?: string }) { this.addNewline();