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 1fca4e6
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 64 deletions.
21 changes: 20 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,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;
Expand Down Expand Up @@ -129,7 +131,24 @@ 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);

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
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
}
24 changes: 9 additions & 15 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,14 @@ 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,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
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
21 changes: 17 additions & 4 deletions packages/create-nx-workspace/src/utils/error-utils.ts
Original file line number Diff line number Diff line change
@@ -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';
}
}
Expand Down
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

0 comments on commit 1fca4e6

Please sign in to comment.