From 9db3e04f0bbb62aade7d173257c632df1bd890ff Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 12 Sep 2023 14:49:21 +0530 Subject: [PATCH 1/6] Added Checks to verify Extension of files --- src/commands/new/file.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/commands/new/file.ts b/src/commands/new/file.ts index ac94083201c..5d298beb56a 100644 --- a/src/commands/new/file.ts +++ b/src/commands/new/file.ts @@ -136,10 +136,34 @@ export default class NewFile extends Command { } async createAsyncapiFile(fileName:string, selectedTemplate:string) { + const asyncApiFile = await readFile(resolve(__dirname, '../../../assets/examples/', selectedTemplate), { encoding: 'utf8' }); const fileNameHasFileExtension = fileName.includes('.'); - const fileNameToWriteToDisk = fileNameHasFileExtension ? fileName : `${fileName}.yaml`; + + let fileNameToWriteToDisk=`${fileName}.yaml`; + + if(fileName.includes(".")==false){ + + fileNameToWriteToDisk=`${fileName}.yaml`; + } + else{ + + const extension=fileName.split(".")[1]; + + if(extension==="yml"||extension==="yaml"){ + + fileNameToWriteToDisk=fileName + } + else{ + + console.log("Please use either yml and yaml extension file") + + return ; + + } + } + try { const content = await readFile(fileNameToWriteToDisk, { encoding: 'utf8' }); From 98f7e918f6daf1b0f7b94cf3c0ef11647e96e34e Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 12 Sep 2023 14:53:31 +0530 Subject: [PATCH 2/6] Remove invalid line --- src/commands/new/file.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/new/file.ts b/src/commands/new/file.ts index 5d298beb56a..d3e086da3ad 100644 --- a/src/commands/new/file.ts +++ b/src/commands/new/file.ts @@ -141,7 +141,7 @@ export default class NewFile extends Command { const fileNameHasFileExtension = fileName.includes('.'); - let fileNameToWriteToDisk=`${fileName}.yaml`; + let fileNameToWriteToDisk; if(fileName.includes(".")==false){ From 1cf9a684b0bb0b9198dead0d2cf2c09f01d9bfc5 Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 12 Sep 2023 15:19:35 +0530 Subject: [PATCH 3/6] Add case for json --- src/commands/new/file.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/new/file.ts b/src/commands/new/file.ts index d3e086da3ad..9e1759c5aea 100644 --- a/src/commands/new/file.ts +++ b/src/commands/new/file.ts @@ -151,7 +151,7 @@ export default class NewFile extends Command { const extension=fileName.split(".")[1]; - if(extension==="yml"||extension==="yaml"){ + if(extension==="yml"||extension==="yaml"||extension=="json"){ fileNameToWriteToDisk=fileName } From ca1b818988c7876c5adb5c44f6656b8fc6d85643 Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 12 Sep 2023 15:20:49 +0530 Subject: [PATCH 4/6] Update the message --- src/commands/new/file.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/new/file.ts b/src/commands/new/file.ts index 9e1759c5aea..6ed01deeb26 100644 --- a/src/commands/new/file.ts +++ b/src/commands/new/file.ts @@ -157,7 +157,7 @@ export default class NewFile extends Command { } else{ - console.log("Please use either yml and yaml extension file") + console.log("CLI Support only yml, yaml and json extension for file") return ; From 93bea8edc797c4edf7619f93f71705781a1560f3 Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 12 Sep 2023 15:24:28 +0530 Subject: [PATCH 5/6] Remove Code Smell --- src/commands/new/file.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/commands/new/file.ts b/src/commands/new/file.ts index 6ed01deeb26..9c1fd14f4a0 100644 --- a/src/commands/new/file.ts +++ b/src/commands/new/file.ts @@ -139,11 +139,10 @@ export default class NewFile extends Command { const asyncApiFile = await readFile(resolve(__dirname, '../../../assets/examples/', selectedTemplate), { encoding: 'utf8' }); - const fileNameHasFileExtension = fileName.includes('.'); let fileNameToWriteToDisk; - if(fileName.includes(".")==false){ + if(!fileName.includes(".")){ fileNameToWriteToDisk=`${fileName}.yaml`; } From 5c0daafea7e2f2ac9111170b651637aa82faeaef Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 12 Sep 2023 15:36:14 +0530 Subject: [PATCH 6/6] fix lint issue --- src/commands/new/file.ts | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/commands/new/file.ts b/src/commands/new/file.ts index 9c1fd14f4a0..98159a3f6d4 100644 --- a/src/commands/new/file.ts +++ b/src/commands/new/file.ts @@ -136,33 +136,23 @@ export default class NewFile extends Command { } async createAsyncapiFile(fileName:string, selectedTemplate:string) { - const asyncApiFile = await readFile(resolve(__dirname, '../../../assets/examples/', selectedTemplate), { encoding: 'utf8' }); - let fileNameToWriteToDisk; - if(!fileName.includes(".")){ - - fileNameToWriteToDisk=`${fileName}.yaml`; - } - else{ + if (!fileName.includes('.')) { + fileNameToWriteToDisk=`${fileName}.yaml`; + } else { + const extension=fileName.split('.')[1]; - const extension=fileName.split(".")[1]; - - if(extension==="yml"||extension==="yaml"||extension=="json"){ - - fileNameToWriteToDisk=fileName - } - else{ - - console.log("CLI Support only yml, yaml and json extension for file") - - return ; + if (extension==='yml'||extension==='yaml'||extension==='json') { + fileNameToWriteToDisk=fileName; + } else { + console.log('CLI Support only yml, yaml and json extension for file'); + return; } } - try { const content = await readFile(fileNameToWriteToDisk, { encoding: 'utf8' });