From baa7c125141a3a18b906f90ecadea95ccfc6df37 Mon Sep 17 00:00:00 2001 From: Animesh Kumar Date: Sun, 24 Mar 2024 20:18:12 +0530 Subject: [PATCH] fix: made the script asynchronous Changes: - made the script asynchronous in nature as suggested by Sergio to make it's performance more better - applied suggestion from here: https://github.com/asyncapi/spec/pull/1046/files#r1535471235 --- scripts/validation/validate-examples.js | 44 +++++++++++++++---------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/scripts/validation/validate-examples.js b/scripts/validation/validate-examples.js index f65b8e09..c45c048b 100644 --- a/scripts/validation/validate-examples.js +++ b/scripts/validation/validate-examples.js @@ -1,35 +1,43 @@ -const { execSync } = require('child_process'); +const { promisify } = require('util'); +const exec = promisify(require('child_process').exec); const glob = require('glob'); // Use glob to find all YAML files in the examples directory const files = glob.sync('./examples/**/*.{yml,yaml}'); -let filesCount = 0; +let filesCount = files.length; let errorFilesCount = 0; let filesWithErrors = []; // Array to store files that failed validation -// Validate each file using AsyncAPI CLI -files.forEach((file) => { - filesCount++; + +// Function to validate a single file asynchronously +async function validateFile(file) { try { console.log(`\nValidating: ${file}`); - execSync(`npx asyncapi validate ${file}`, { stdio: 'inherit' }); + await exec(`npx asyncapi validate ${file}`); + console.log(`Validation successful for: ${file}\n`); } catch (error) { console.error(`Validation failed for: ${file}\n`); errorFilesCount++; filesWithErrors.push(file); } +} -}); +// Run validation for all files asynchronously +(async () => { + await Promise.all(files.map(validateFile)); -console.log(`\n\nValidation Completed!\nTotal files validated = ${filesCount}\nTotal files having error = ${errorFilesCount}`) + // Output validation result + console.log(`\n\nValidation Completed!\nTotal files validated = ${filesCount}\nTotal files having error = ${errorFilesCount}`); -// Display files with errors -if (filesWithErrors.length > 0) { - console.log('\nFiles with validation errors:'); - filesWithErrors.forEach((file) => { - console.log(file); - }); - process.exit(1); -} else { - console.log('\nAll files validated successfully.'); -} \ No newline at end of file + // Display files with errors + if (filesWithErrors.length > 0) { + console.log('\nFiles with validation errors:'); + filesWithErrors.forEach((file) => { + console.log(file); + }); + process.exit(1); + } else { + console.log('\nAll files validated successfully.'); + process.exit(0); + } +})(); \ No newline at end of file