Skip to content

Commit

Permalink
feat(misc): make createWorkspace quiter by default
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 14, 2023
1 parent c02ec9f commit c2aff8b
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 77 deletions.
33 changes: 32 additions & 1 deletion packages/create-nx-plugin/bin/create-nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import { createWorkspace, CreateWorkspaceOptions } from 'create-nx-workspace';
import { output } from 'create-nx-workspace/src/utils/output';
import { CI } from 'create-nx-workspace/src/utils/ci/ci-list';
import type { PackageManager } from 'create-nx-workspace/src/utils/package-manager';
import { showNxWarning } from 'create-nx-workspace/src/utils/nx/show-nx-warning';
import { printNxCloudSuccessMessage } from 'create-nx-workspace/src/utils/nx/nx-cloud';
import {
messages,
recordStat,
} from 'create-nx-workspace/src/utils/nx/ab-testing';

export const yargsDecorator = {
'Options:': `${chalk.green`Options`}:`,
Expand Down Expand Up @@ -120,7 +126,32 @@ async function main(parsedArgs: yargs.Arguments<CreateNxPluginArguments>) {
? parsedArgs.pluginName.split('/')[1]
: parsedArgs.pluginName,
};
await createWorkspace('@nrwl/nx-plugin', populatedArguments);

output.log({
title: `Creating an Nx v${nxVersion} plugin.`,
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(
'@nrwl/nx-plugin',
populatedArguments
);

showNxWarning(parsedArgs.pluginName);

await recordStat({
nxVersion,
command: 'create-nx-workspace',
useCloud: parsedArgs.nxCloud,
meta: messages.codeOfSelectedPromptMessage('nxCloudCreation'),
});

if (parsedArgs.nxCloud && workspaceInfo.nxCloudInfo) {
printNxCloudSuccessMessage(workspaceInfo.nxCloudInfo);
}
}

/**
Expand Down
29 changes: 28 additions & 1 deletion packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ 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';
import { messages, recordStat } from '../src/utils/nx/ab-testing';

interface Arguments extends CreateWorkspaceOptions {
preset: string;
Expand Down Expand Up @@ -129,7 +132,31 @@ export const commandsObject: yargs.Argv<Arguments> = yargs
) as yargs.Argv<Arguments>;

async function main(parsedArgs: yargs.Arguments<Arguments>) {
await createWorkspace<Arguments>(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<Arguments>(
parsedArgs.preset,
parsedArgs
);

showNxWarning(parsedArgs.name);

await recordStat({
nxVersion,
command: 'create-nx-workspace',
useCloud: parsedArgs.nxCloud,
meta: messages.codeOfSelectedPromptMessage('nxCloudCreation'),
});

if (parsedArgs.nxCloud && workspaceInfo.nxCloudInfo) {
printNxCloudSuccessMessage(workspaceInfo.nxCloudInfo);
}

if (isKnownPreset(parsedArgs.preset)) {
pointToTutorialAndCourse(parsedArgs.preset as Preset);
Expand Down
4 changes: 2 additions & 2 deletions packages/create-nx-workspace/src/create-empty-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ export async function createEmptyWorkspace<T extends CreateWorkspaceOptions>(
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 {
Expand Down
6 changes: 1 addition & 5 deletions packages/create-nx-workspace/src/create-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function createPreset<T extends CreateWorkspaceOptions>(
if (process.env.NX_VERBOSE_LOGGING !== 'true') {
args = '--quiet ' + args;
}
const command = `g ${preset}:preset ${args}`;
const command = `g ${preset}:preset ${args} --quiet`;

try {
const [exec, ...args] = pmc.exec.split(' ');
Expand All @@ -52,10 +52,6 @@ export async function createPreset<T extends CreateWorkspaceOptions>(
...command.split(' ')
);
await spawnAndWait(exec, args, directory);

output.log({
title: `Successfully applied preset: ${preset}.`,
});
} catch (e) {
output.error({
title: `Failed to apply preset: ${preset}`,
Expand Down
2 changes: 1 addition & 1 deletion packages/create-nx-workspace/src/create-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
30 changes: 9 additions & 21 deletions packages/create-nx-workspace/src/create-workspace.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,15 +23,12 @@ export async function createWorkspace<T extends CreateWorkspaceOptions>(
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);

Expand Down Expand Up @@ -79,16 +75,8 @@ export async function createWorkspace<T extends CreateWorkspaceOptions>(
}
}

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,
directory,
};
}
2 changes: 1 addition & 1 deletion packages/create-nx-workspace/src/utils/ci/setup-ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions packages/create-nx-workspace/src/utils/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,4 @@ export async function initializeGitRepo(
const message = options.commit.message || 'initial commit';
await execute(['commit', `-m "${message}"`]);
}
output.log({
title: 'Successfully initialized git.',
});
}
2 changes: 1 addition & 1 deletion packages/create-nx-workspace/src/utils/nx/nx-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
54 changes: 16 additions & 38 deletions packages/create-nx-workspace/src/utils/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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() {
Expand All @@ -125,7 +129,7 @@ class CLIOutput {
);
}

error({ title, slug, bodyLines }: CLIErrorMessageConfig) {
error({ title, bodyLines }: CLIErrorMessageConfig) {
this.addNewline();

this.writeOutputTitle({
Expand All @@ -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({
Expand All @@ -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();
}

Expand Down Expand Up @@ -212,8 +192,6 @@ class CLIOutput {
this.addNewline();
}

logCommand(message: string) {}

log({ title, bodyLines, color }: CLIWarnMessageConfig & { color?: string }) {
this.addNewline();

Expand Down
8 changes: 5 additions & 3 deletions packages/nx/src/command-line/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,11 @@ export async function generate(cwd: string, args: { [k: string]: any }) {
].join('/n')
);
}
logger.info(
`NX Generating ${opts.collectionName}:${normalizedGeneratorName}`
);
if (!opts.quiet) {
logger.info(
`NX Generating ${opts.collectionName}:${normalizedGeneratorName}`
);
}

if (opts.help) {
printGenHelp(opts, schema, normalizedGeneratorName, aliases);
Expand Down

0 comments on commit c2aff8b

Please sign in to comment.