Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bameyrick committed May 28, 2021
1 parent 4005874 commit 29f0e22
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
10 changes: 6 additions & 4 deletions src/concurrently.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function concurrently(commands: ConcurrentCommand[], index: number = 0):
runCommand(command.command, index, name);

if (nextCommand) {
console.log(chalk.blue(`Waiting for ${delay}ms before running ${getName(nextCommand, nextIndex)}`));
console.log(chalk.blueBright(`Waiting for ${delay}ms before running ${getName(nextCommand, nextIndex)}`));

setTimeout(() => concurrently(commands, nextIndex), delay);
}
Expand All @@ -54,7 +54,7 @@ export function concurrently(commands: ConcurrentCommand[], index: number = 0):

if (nextCommand) {
console.log(
chalk.blue(
chalk.blueBright(
`Waiting for ${name} to log a message including "${command.value}" before running ${getName(nextCommand, nextIndex)}`
)
);
Expand All @@ -71,7 +71,9 @@ export function concurrently(commands: ConcurrentCommand[], index: number = 0):
});

if (nextCommand) {
console.log(chalk.blue(`Waiting for [${name}] to log "${command.value}" before running ${getName(nextCommand, nextIndex)}`));
console.log(
chalk.blueBright(`Waiting for [${name}] to log "${command.value}" before running ${getName(nextCommand, nextIndex)}`)
);
}
break;
}
Expand Down Expand Up @@ -99,7 +101,7 @@ export function concurrently(commands: ConcurrentCommand[], index: number = 0):

if (nextCommand) {
console.log(
chalk.blue(`Waiting for ${name} to stop logging for ${delay}ms before running ${getName(nextCommand, nextIndex)}`)
chalk.blueBright(`Waiting for ${name} to stop logging for ${delay}ms before running ${getName(nextCommand, nextIndex)}`)
);
}
}
Expand Down
24 changes: 21 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import * as chalk from 'chalk';

let lastLogIndex: number;

/**
* Handles logging output from a command
*/
export function logger(index: number, message: string, name: string): void {
name = `${name}: `;
name = `${name}:`;

const spaces = new Array(name.length).fill(' ').join('');
const prefix = lastLogIndex === index ? spaces : name;
const prefix = lastLogIndex === index ? spaces : chalk.bold(name);

message = message.replace(/^\n/, '');

if (/^(npm warn |warn |warning )/.test(message.toLowerCase())) {
message = chalk.yellow(message);
}

message = message.replace(//g, chalk.green('✔'));

const urls = message.match(
/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w-]*))?)(:([0-9]*)(\/)?)?/g
);

urls?.forEach(url => {
message = message.replace(url, chalk.cyanBright(url));
});

console.log(`${prefix}${message.replace(/^\n/, '').replace(/\n/g, `\n${spaces}`)}`);
console.log(`${prefix} ${message.replace(/\n/g, `\n${spaces} `)}`);

lastLogIndex = index;
}
28 changes: 17 additions & 11 deletions src/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@ import { logger } from './logger';
export function runCommand(command: string, index: number, name: string, callback?: (data: string) => void): void {
console.log(chalk.green(`Running ${name}: "${command}"`));

const process = exec(command);
const childProcess = exec(command);

addChildProcess(process);
addChildProcess(childProcess);

process.stdout?.on('data', data => {
data = data.trim();
childProcess.stdout?.on('data', data => handleLog(data, index, name, callback));
childProcess.stderr?.on('data', data => handleLog(data, index, name, callback));
}

/**
* Handles a log event
*/
function handleLog(data: string, index: number, name: string, callback?: (data: string) => void): void {
data = data.trim();

if (data) {
logger(index, data, name);
}
if (data) {
logger(index, data, name);
}

if (callback) {
callback(data);
}
});
if (callback) {
callback(data);
}
}

0 comments on commit 29f0e22

Please sign in to comment.