Skip to content

Commit

Permalink
feat(core): add prefixColor to run-commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nc1z committed Dec 13, 2024
1 parent 5bdda1d commit 6404389
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
14 changes: 14 additions & 0 deletions docs/generated/packages/nx/executors/run-commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
"type": "string",
"description": "Prefix in front of every line out of the output"
},
"prefixColor": {
"type": "string",
"description": "Color of the prefix",
"enum": [
"black",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white"
]
},
"color": {
"type": "string",
"description": "Color of the output",
Expand Down
19 changes: 14 additions & 5 deletions packages/nx/src/executors/run-commands/run-commands.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface RunCommandsOptions extends Json {
*/
description?: string;
prefix?: string;
prefixColor?: string;
color?: string;
bgColor?: string;
}
Expand Down Expand Up @@ -117,11 +118,11 @@ export default async function (
}

if (
options.commands.find((c: any) => c.prefix || c.color || c.bgColor) &&
options.commands.find((c: any) => c.prefix || c.prefixColor || c.color || c.bgColor) &&
!options.parallel
) {
throw new Error(
'ERROR: Bad executor config for run-commands - "prefix", "color" and "bgColor" can only be set when "parallel=true".'
'ERROR: Bad executor config for run-commands - "prefix", "prefixColor", "color" and "bgColor" can only be set when "parallel=true".'
);
}

Expand Down Expand Up @@ -317,6 +318,7 @@ async function createProcess(
color?: string;
bgColor?: string;
prefix?: string;
prefixColor?: string;
},
readyWhenStatus: { stringToMatch: string; found: boolean }[] = [],
color: boolean,
Expand Down Expand Up @@ -377,6 +379,7 @@ function nodeProcess(
color?: string;
bgColor?: string;
prefix?: string;
prefixColor?: string;
},
cwd: string,
env: Record<string, string>,
Expand Down Expand Up @@ -438,16 +441,22 @@ function addColorAndPrefix(
out: string,
config: {
prefix?: string;
prefixColor?: string;
color?: string;
bgColor?: string;
}
) {
if (config.prefix) {
out = out
.split('\n')
.map((l) =>
l.trim().length > 0 ? `${chalk.bold(config.prefix)} ${l}` : l
)
.map((l) => {
let prefixText = config.prefix;
if (config.prefixColor && chalk[config.prefixColor]) {
prefixText = chalk[config.prefixColor](prefixText);
}
prefixText = chalk.bold(prefixText);
return l.trim().length > 0 ? `${prefixText} ${l}` : l;
})
.join('\n');
}
if (config.color && chalk[config.color]) {
Expand Down
14 changes: 14 additions & 0 deletions packages/nx/src/executors/run-commands/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
"type": "string",
"description": "Prefix in front of every line out of the output"
},
"prefixColor": {
"type": "string",
"description": "Color of the prefix",
"enum": [
"black",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white"
]
},
"color": {
"type": "string",
"description": "Color of the output",
Expand Down

0 comments on commit 6404389

Please sign in to comment.