Skip to content

Commit

Permalink
feat(core): support customizing command header
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed May 20, 2022
1 parent 271c3d4 commit 8fddedf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 40 deletions.
58 changes: 28 additions & 30 deletions packages/create-nx-workspace/bin/output.ts
@@ -1,7 +1,29 @@
/*
* Because we don't want to depend on @nrwl/workspace (to speed up the workspace creation)
* we duplicate the helper functions from @nrwl/workspace in this file.
*/

import * as chalk from 'chalk';
import { EOL } from 'os';
import { isCI } from './is-ci';
import { TaskStatus } from '../tasks-runner/tasks-runner';

export function isCI() {
return (
process.env.CI === 'true' ||
process.env.TF_BUILD === 'true' ||
process.env['bamboo.buildKey'] ||
process.env.BUILDKITE === 'true' ||
process.env.CIRCLECI === 'true' ||
process.env.CIRRUS_CI === 'true' ||
process.env.CODEBUILD_BUILD_ID ||
process.env.GITHUB_ACTIONS === 'true' ||
process.env.GITLAB_CI ||
process.env.HEROKU_TEST_RUN_ID ||
process.env.BUILD_ID ||
process.env.BUILD_BUILDID ||
process.env.TEAMCITY_VERSION ||
process.env.TRAVIS === 'true'
);
}

export interface CLIErrorMessageConfig {
title: string;
Expand All @@ -28,15 +50,12 @@ export interface CLISuccessMessageConfig {
/**
* Automatically disable styling applied by chalk if CI=true
*/
const forceColor =
process.env.FORCE_COLOR === '' || process.env.FORCE_COLOR === 'true';
if (isCI() && !forceColor) {
if (isCI()) {
(chalk as any).level = 0;
}

class CLIOutput {
readonly X_PADDING = ' ';
cliName = 'NX';

/**
* Longer dash character which forms more of a continuous line when place side to side
Expand Down Expand Up @@ -96,12 +115,12 @@ class CLIOutput {
let nxPrefix = '';
if (chalk[color]) {
nxPrefix = `${chalk[color]('>')} ${chalk.reset.inverse.bold[color](
` ${this.cliName} `
' NX '
)}`;
} else {
nxPrefix = `${chalk.keyword(color)(
'>'
)} ${chalk.reset.inverse.bold.keyword(color)(` ${this.cliName} `)}`;
)} ${chalk.reset.inverse.bold.keyword(color)(' NX ')}`;
}
return `${nxPrefix} ${text}`;
}
Expand Down Expand Up @@ -209,28 +228,7 @@ class CLIOutput {
this.addNewline();
}

logCommand(message: string, taskStatus?: TaskStatus) {
// normalize the message
if (message.startsWith('nx run ')) {
message = message.substring('nx run '.length);
} else if (message.startsWith('run ')) {
message = message.substring('run '.length);
}

this.addNewline();
let commandOutput = `${chalk.dim('> nx run')} ${message}`;
if (taskStatus === 'local-cache') {
commandOutput += ` ${chalk.dim('[local cache]')}`;
} else if (taskStatus === 'remote-cache') {
commandOutput += ` ${chalk.dim('[remote cache]')}`;
} else if (taskStatus === 'local-cache-kept-existing') {
commandOutput += ` ${chalk.dim(
'[existing outputs match the cache, left as is]'
)}`;
}
this.writeToStdOut(commandOutput);
this.addNewline();
}
logCommand(message: string) {}

log({ title, bodyLines, color }: CLIWarnMessageConfig & { color?: string }) {
this.addNewline();
Expand Down
42 changes: 32 additions & 10 deletions packages/nx/src/utils/output.ts
Expand Up @@ -37,6 +37,7 @@ if (isCI() && !forceColor) {
class CLIOutput {
readonly X_PADDING = ' ';
cliName = 'NX';
formatCommand = (message: string) => `${chalk.dim('> nx run')} ${message}`;

/**
* Longer dash character which forms more of a continuous line when place side to side
Expand Down Expand Up @@ -210,26 +211,47 @@ class CLIOutput {
}

logCommand(message: string, taskStatus?: TaskStatus) {
// normalize the message
this.addNewline();
const commandOutput = this.formatCommand(this.normalizeMessage(message));
const commandOutputWithStatus = this.addTaskStatus(
taskStatus,
commandOutput
);
this.writeToStdOut(commandOutputWithStatus);
this.addNewline();
}

private normalizeMessage(message: string) {
if (message.startsWith('nx run ')) {
message = message.substring('nx run '.length);
return message.substring('nx run '.length);
} else if (message.startsWith('run ')) {
message = message.substring('run '.length);
return message.substring('run '.length);
} else {
return message;
}
}

this.addNewline();
let commandOutput = `${chalk.dim('> nx run')} ${message}`;
private addTaskStatus(
taskStatus:
| 'success'
| 'failure'
| 'skipped'
| 'local-cache-kept-existing'
| 'local-cache'
| 'remote-cache',
commandOutput: string
) {
if (taskStatus === 'local-cache') {
commandOutput += ` ${chalk.dim('[local cache]')}`;
return `${commandOutput} ${chalk.dim('[local cache]')}`;
} else if (taskStatus === 'remote-cache') {
commandOutput += ` ${chalk.dim('[remote cache]')}`;
return `${commandOutput} ${chalk.dim('[remote cache]')}`;
} else if (taskStatus === 'local-cache-kept-existing') {
commandOutput += ` ${chalk.dim(
return `${commandOutput} ${chalk.dim(
'[existing outputs match the cache, left as is]'
)}`;
} else {
return commandOutput;
}
this.writeToStdOut(commandOutput);
this.addNewline();
}

log({ title, bodyLines, color }: CLIWarnMessageConfig & { color?: string }) {
Expand Down

0 comments on commit 8fddedf

Please sign in to comment.