Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Manush-2005 committed Jun 7, 2024
1 parent 87ac58c commit 2b36999
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions scripts/converter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,26 @@ const toVersion = '3.0.0';
*/
function convertExample(exampleFile) {
console.warn(`Converting: ${exampleFile}`);
const document = fs.readFileSync(exampleFile, 'utf-8');
const loadedDocument = jsYaml.load(document);
let document;

try {
document = fs.readFileSync(exampleFile, 'utf8');

} catch (error) {
console.error(`Error reading file ${exampleFile}:`, error);
return;

}

let loadedDocument;
try {
loadedDocument = jsYaml.load(document);

} catch (error) {
console.error(`Error parsing YAML for file ${exampleFile}:`, error);
return;

}
if(loadedDocument.asyncapi === undefined) {
//Probably encountered a common file (used in other files), ignore
console.error(`___________________________________________________________________________________
Expand All @@ -27,7 +45,13 @@ ________________________________________________________________________________
console.warn(`${exampleFile} is already version ${toVersion}`);
return;
}
const convertedDocument = convert(document, toVersion, { });
let convertedDocument;
try {
convertedDocument = convert(document, toVersion, { });
} catch (error) {
console.error(`Error converting file ${exampleFile}:`, error);
return;
}
fs.writeFileSync(exampleFile, convertedDocument);
}

Expand All @@ -37,7 +61,13 @@ ________________________________________________________________________________
* @param {*} directoryPath full path to a directory to convert examples from.
*/
async function convertExampleDir(directoryPath) {
let examplesFiles = await fs.promises.readdir(directoryPath);
let examplesFiles;
try {
examplesFiles = fs.readdir(directoryPath);
} catch (error) {
console.error(`Error reading directory ${directoryPath}:`, error);
return;
}
examplesFiles = examplesFiles.map((file) => path.resolve(directoryPath, file));
const nestedDirectory = examplesFiles.filter((file) => fs.lstatSync(file).isDirectory());
for (const dir of nestedDirectory) {
Expand All @@ -53,5 +83,9 @@ async function convertExampleDir(directoryPath) {
*
*/
(async () => {
await convertExampleDir(examplesDirectory);
try {
await convertExampleDir(examplesDirectory);
} catch (error) {
console.error('Error during conversion process:', error);
}
})()

0 comments on commit 2b36999

Please sign in to comment.