From ccec415a5035fe7fa5349e4ad14590e19760a5bd Mon Sep 17 00:00:00 2001 From: David Favretto Date: Fri, 3 May 2019 13:11:28 -0300 Subject: [PATCH] Remove --cs and --ts args and refactor validations --- .../botskills/src/botskills-list.ts | 45 ++++++++++--------- .../botskills/src/functionality/listSkill.ts | 41 +++++++++++------ 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/lib/typescript/botskills/src/botskills-list.ts b/lib/typescript/botskills/src/botskills-list.ts index 0ca0c52f6f..1e1c32fdb0 100644 --- a/lib/typescript/botskills/src/botskills-list.ts +++ b/lib/typescript/botskills/src/botskills-list.ts @@ -20,6 +20,16 @@ function showErrorHelp(): void { process.exit(1); } +function checkSkillsFile(skillsFile: string): boolean { + const skillsFilePath: string = isAbsolute(skillsFile) ? skillsFile : join(resolve('./'), skillsFile); + if (!existsSync(skillsFilePath)) { + + return false; + } + + return true; +} + const logger: ILogger = new ConsoleLogger(); program.Command.prototype.unknownOption = (flag: string): void => { @@ -31,8 +41,6 @@ program .name('botskills list') .description('List all the Skills connected to your assistant') .option('-f, --skillsFile ', 'Path to assistant Skills configuration file') - .option('--cs', 'Determine your assistant project structure to be a CSharp-like structure') - .option('--ts', 'Determine your assistant project structure to be a TypeScript-like structure') .option('--verbose', '[OPTIONAL] Output detailed information about the processing of the tool') .action((cmd: program.Command, actions: program.Command) => undefined); @@ -40,33 +48,30 @@ const args: program.Command = program.parse(process.argv); logger.isVerbose = args.verbose; -// cs and ts validation -const csAndTsValidationResult: string = validatePairOfArgs(args.cs, args.ts); -if (csAndTsValidationResult) { - logger.error( - csAndTsValidationResult.replace('{0}', 'cs') - .replace('{1}', 'ts') - ); - process.exit(1); -} - // skillsFile validation if (!args.skillsFile) { - args.skillsFile = args.ts ? join('src', 'skills.json') : 'skills.json'; + args.skillsFile = join('src', 'skills.json'); + if (!checkSkillsFile(args.skillsFile)) { + args.skillsFile = 'skills.json'; + if (!checkSkillsFile(args.skillsFile)) { + logger.error(`The 'skillsFile' argument is absent or leads to a non-existing file. +Please make sure to provide a valid path to your Assistant Skills configuration file.`); + process.exit(1); + } + } } else if (extname(args.skillsFile) !== '.json') { logger.error(`The 'skillsFile' argument should be a JSON file.`); process.exit(1); -} - -const skillsFilePath: string = isAbsolute(args.skillsFile) ? args.skillsFile : join(resolve('./'), args.skillsFile); -if (!existsSync(skillsFilePath)) { - logger.error(`The 'skillsFile' argument is absent or leads to a non-existing file. +} else { + if (!checkSkillsFile(args.skillsFile)) { + logger.error(`The 'skillsFile' argument is absent or leads to a non-existing file. Please make sure to provide a valid path to your Assistant Skills configuration file.`); - process.exit(1); + process.exit(1); + } } const configuration: IListConfiguration = { - skillsFile: skillsFilePath, + skillsFile: isAbsolute(args.skillsFile) ? args.skillsFile : join(resolve('./'), args.skillsFile), logger: logger }; diff --git a/lib/typescript/botskills/src/functionality/listSkill.ts b/lib/typescript/botskills/src/functionality/listSkill.ts index 1237d68007..601fb1be3f 100644 --- a/lib/typescript/botskills/src/functionality/listSkill.ts +++ b/lib/typescript/botskills/src/functionality/listSkill.ts @@ -6,25 +6,40 @@ import { ConsoleLogger, ILogger} from '../logger'; import { IListConfiguration, ISkillFIle, ISkillManifest } from '../models'; -export async function listSkill(configuration: IListConfiguration): Promise { +export async function listSkill(configuration: IListConfiguration): Promise { if (configuration.logger) { logger = configuration.logger; } - // Take VA Skills configurations - //tslint:disable-next-line:non-literal-require - const assistantSkillsFile: ISkillFIle = require(configuration.skillsFile); - const assistantSkills: ISkillManifest[] = assistantSkillsFile.skills; + try { + // Take VA Skills configurations + //tslint:disable-next-line:non-literal-require + const assistantSkillsFile: ISkillFIle = require(configuration.skillsFile); + if (!assistantSkillsFile) { + return false; + } else if (!assistantSkillsFile.skills) { + return false; + } + const assistantSkills: ISkillManifest[] = assistantSkillsFile.skills; - if (assistantSkills.length < 1) { - logger.message('There are no Skills connected to the assistant.'); - } else { - let message: string = `The skills already connected to the assistant are the following:`; - assistantSkills.forEach((skillManifest: ISkillManifest) => { - message += `\n\t- ${skillManifest.name}`; - }); + if (assistantSkills.length < 1) { + logger.message('There are no Skills connected to the assistant.'); - logger.message(message); + return false; + } else { + let message: string = `The skills already connected to the assistant are the following:`; + assistantSkills.forEach((skillManifest: ISkillManifest) => { + message += `\n\t- ${skillManifest.name}`; + }); + + logger.message(message); + } + + return true; + } catch (err) { + logger.error(`There was an error while listing the Skills connected to your assistant:\n ${err}`); + + return false; } }