Skip to content

Commit baa7c12

Browse files
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
1 parent d2e81b1 commit baa7c12

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed
+26-18
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
1-
const { execSync } = require('child_process');
1+
const { promisify } = require('util');
2+
const exec = promisify(require('child_process').exec);
23
const glob = require('glob');
34

45
// Use glob to find all YAML files in the examples directory
56
const files = glob.sync('./examples/**/*.{yml,yaml}');
67

7-
let filesCount = 0;
8+
let filesCount = files.length;
89
let errorFilesCount = 0;
910
let filesWithErrors = []; // Array to store files that failed validation
10-
// Validate each file using AsyncAPI CLI
11-
files.forEach((file) => {
12-
filesCount++;
11+
12+
// Function to validate a single file asynchronously
13+
async function validateFile(file) {
1314
try {
1415
console.log(`\nValidating: ${file}`);
15-
execSync(`npx asyncapi validate ${file}`, { stdio: 'inherit' });
16+
await exec(`npx asyncapi validate ${file}`);
17+
console.log(`Validation successful for: ${file}\n`);
1618
} catch (error) {
1719
console.error(`Validation failed for: ${file}\n`);
1820
errorFilesCount++;
1921
filesWithErrors.push(file);
2022
}
23+
}
2124

22-
});
25+
// Run validation for all files asynchronously
26+
(async () => {
27+
await Promise.all(files.map(validateFile));
2328

24-
console.log(`\n\nValidation Completed!\nTotal files validated = ${filesCount}\nTotal files having error = ${errorFilesCount}`)
29+
// Output validation result
30+
console.log(`\n\nValidation Completed!\nTotal files validated = ${filesCount}\nTotal files having error = ${errorFilesCount}`);
2531

26-
// Display files with errors
27-
if (filesWithErrors.length > 0) {
28-
console.log('\nFiles with validation errors:');
29-
filesWithErrors.forEach((file) => {
30-
console.log(file);
31-
});
32-
process.exit(1);
33-
} else {
34-
console.log('\nAll files validated successfully.');
35-
}
32+
// Display files with errors
33+
if (filesWithErrors.length > 0) {
34+
console.log('\nFiles with validation errors:');
35+
filesWithErrors.forEach((file) => {
36+
console.log(file);
37+
});
38+
process.exit(1);
39+
} else {
40+
console.log('\nAll files validated successfully.');
41+
process.exit(0);
42+
}
43+
})();

0 commit comments

Comments
 (0)