From 0ae10ddf17a0c350f7e288de8d7db0a336ce8f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Lefran=C3=A7ois?= Date: Fri, 1 Mar 2024 13:52:19 -0500 Subject: [PATCH 1/4] Add output and error handling to hook-runner --- cli/src/services/formatters/hook-runner.ts | 7 +++++++ cli/src/services/hook-runner.ts | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cli/src/services/formatters/hook-runner.ts b/cli/src/services/formatters/hook-runner.ts index ffd7ba626..27f24ba4b 100644 --- a/cli/src/services/formatters/hook-runner.ts +++ b/cli/src/services/formatters/hook-runner.ts @@ -14,4 +14,11 @@ export default class HookRunnerFomatter { }); console.log(''); } + error(command: string, errors: string[]) { + console.log(chalk.red('⚠ '), chalk.bold(chalk.red(`${command}:`))); + errors.forEach((error) => { + console.log(' ', chalk.red(error)); + }); + console.log(''); + } } diff --git a/cli/src/services/hook-runner.ts b/cli/src/services/hook-runner.ts index cbb0bc196..c8db487e9 100644 --- a/cli/src/services/hook-runner.ts +++ b/cli/src/services/hook-runner.ts @@ -23,9 +23,18 @@ export default class HookRunner { const hooks = this.hooks[name]; if (hooks) { - new Formatter().log(name, hooks); - - hooks.forEach(execSync); + const formatter = new Formatter() + formatter.log(name, hooks); + + hooks.forEach((hook) => { + try { + const output: string = execSync(hook, { stdio: 'pipe' }).toString(); + formatter.log(hook, [output]) + } catch (error: any) { + formatter.error(hook, [error.stderr.toString()]); + throw new Error(`Hook execution failed for '${hook}': ${error.stderr.toString()}`); + } + }); } return this.document.refreshPaths(); From 1be57dc3111d9f4530f991f15115c26ee3524a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Lefran=C3=A7ois?= Date: Fri, 1 Mar 2024 14:12:21 -0500 Subject: [PATCH 2/4] cleanup + linting --- cli/src/services/formatters/hook-runner.ts | 2 +- cli/src/services/hook-runner.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/src/services/formatters/hook-runner.ts b/cli/src/services/formatters/hook-runner.ts index 27f24ba4b..fabb2d3d5 100644 --- a/cli/src/services/formatters/hook-runner.ts +++ b/cli/src/services/formatters/hook-runner.ts @@ -7,7 +7,7 @@ const capitalizeFirstLetter = (str: string) => export default class HookRunnerFomatter { log(name: string, commands: string[]) { - const operation = capitalizeFirstLetter(decamelize(name, {separator: ' '})); + const operation = capitalizeFirstLetter(decamelize(name, { separator: ' ' })); console.log(chalk.yellow('➤ '), chalk.bold(chalk.yellow(`${operation}:`))); commands.forEach((command) => { console.log(' ', chalk.yellow(command)); diff --git a/cli/src/services/hook-runner.ts b/cli/src/services/hook-runner.ts index c8db487e9..39835b8d5 100644 --- a/cli/src/services/hook-runner.ts +++ b/cli/src/services/hook-runner.ts @@ -1,11 +1,11 @@ // Vendor -import {execSync} from 'child_process'; +import { execSync } from 'child_process'; // Formatters import Formatter from './formatters/hook-runner'; // Types -import {HookConfig, Hooks} from '../types/document-config'; +import { HookConfig, Hooks } from '../types/document-config'; import Document from './document'; export default class HookRunner { @@ -32,7 +32,7 @@ export default class HookRunner { formatter.log(hook, [output]) } catch (error: any) { formatter.error(hook, [error.stderr.toString()]); - throw new Error(`Hook execution failed for '${hook}': ${error.stderr.toString()}`); + process.exit(error.status); } }); } From a36ddeca5003a5d22dff4570cf25a380859bc14c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Lefran=C3=A7ois?= Date: Fri, 1 Mar 2024 14:13:10 -0500 Subject: [PATCH 3/4] run make format also --- cli/src/services/formatters/hook-runner.ts | 2 +- cli/src/services/hook-runner.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/src/services/formatters/hook-runner.ts b/cli/src/services/formatters/hook-runner.ts index fabb2d3d5..27f24ba4b 100644 --- a/cli/src/services/formatters/hook-runner.ts +++ b/cli/src/services/formatters/hook-runner.ts @@ -7,7 +7,7 @@ const capitalizeFirstLetter = (str: string) => export default class HookRunnerFomatter { log(name: string, commands: string[]) { - const operation = capitalizeFirstLetter(decamelize(name, { separator: ' ' })); + const operation = capitalizeFirstLetter(decamelize(name, {separator: ' '})); console.log(chalk.yellow('➤ '), chalk.bold(chalk.yellow(`${operation}:`))); commands.forEach((command) => { console.log(' ', chalk.yellow(command)); diff --git a/cli/src/services/hook-runner.ts b/cli/src/services/hook-runner.ts index 39835b8d5..9b278f831 100644 --- a/cli/src/services/hook-runner.ts +++ b/cli/src/services/hook-runner.ts @@ -1,11 +1,11 @@ // Vendor -import { execSync } from 'child_process'; +import {execSync} from 'child_process'; // Formatters import Formatter from './formatters/hook-runner'; // Types -import { HookConfig, Hooks } from '../types/document-config'; +import {HookConfig, Hooks} from '../types/document-config'; import Document from './document'; export default class HookRunner { @@ -23,13 +23,13 @@ export default class HookRunner { const hooks = this.hooks[name]; if (hooks) { - const formatter = new Formatter() + const formatter = new Formatter(); formatter.log(name, hooks); hooks.forEach((hook) => { try { - const output: string = execSync(hook, { stdio: 'pipe' }).toString(); - formatter.log(hook, [output]) + const output: string = execSync(hook, {stdio: 'pipe'}).toString(); + formatter.log(hook, [output]); } catch (error: any) { formatter.error(hook, [error.stderr.toString()]); process.exit(error.status); From fde2c0b2facfc7d2a5580d4e0ea4e90c149ae412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Lefran=C3=A7ois?= Date: Fri, 1 Mar 2024 16:57:53 -0500 Subject: [PATCH 4/4] Nicer output when script succeeds --- cli/src/services/formatters/hook-runner.ts | 5 +++++ cli/src/services/hook-runner.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cli/src/services/formatters/hook-runner.ts b/cli/src/services/formatters/hook-runner.ts index 27f24ba4b..0f5d72dcc 100644 --- a/cli/src/services/formatters/hook-runner.ts +++ b/cli/src/services/formatters/hook-runner.ts @@ -21,4 +21,9 @@ export default class HookRunnerFomatter { }); console.log(''); } + success(command: string, message: string) { + console.log(chalk.green('✓ '), chalk.bold(chalk.green(`${command}:`))); + console.log(' ', chalk.green(message)); + console.log(''); +} } diff --git a/cli/src/services/hook-runner.ts b/cli/src/services/hook-runner.ts index 9b278f831..2f3381e8c 100644 --- a/cli/src/services/hook-runner.ts +++ b/cli/src/services/hook-runner.ts @@ -29,7 +29,7 @@ export default class HookRunner { hooks.forEach((hook) => { try { const output: string = execSync(hook, {stdio: 'pipe'}).toString(); - formatter.log(hook, [output]); + formatter.success(hook, output); } catch (error: any) { formatter.error(hook, [error.stderr.toString()]); process.exit(error.status);