Skip to content

Commit

Permalink
fix: made the script asynchronous
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AnimeshKumar923 committed Mar 24, 2024
1 parent d2e81b1 commit baa7c12
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions scripts/validation/validate-examples.js
Original file line number Diff line number Diff line change
@@ -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.');
}
// 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);
}
})();

0 comments on commit baa7c12

Please sign in to comment.