From 9f6527300b8d1062ebf3ed6b5bf784de1e1d9a37 Mon Sep 17 00:00:00 2001 From: Manthan Surkar <42006277+thesmallstar@users.noreply.github.com> Date: Sun, 10 Feb 2019 09:58:09 +0530 Subject: [PATCH] add regex to check input being file or directory --- bin/sub.js | 63 ++++++++++++++++++++++++++++++++++++++++++---------- lib/ifdir.js | 4 +--- tmp | 0 3 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 tmp diff --git a/bin/sub.js b/bin/sub.js index 87913d3..bf97af9 100644 --- a/bin/sub.js +++ b/bin/sub.js @@ -7,22 +7,24 @@ const ifdir = require('../lib/ifdir'); const func = require('../lib/functions'); -exports.main = (exports.main = async() => { +exports.main = async() => { header.init(); const result = await ifdir.ask(); const { isdir } = result; - if (isdir === 'd') { - const dirRes = await ifdir.getdir(); - const { dirname } = dirRes; - create.createdir(dirname); - console.log( - chalk.blue('Directory created successfully') + chalk.red('!!!'), - ); - return 0; - } if (isdir === 'f') { + // regex to check whether input string is file or directory + var isValid = (function(){ + var rg1 = /^[^\\/:\*\?"<>\|]+$/; // forbidden characters \ / : * ? " < > | + var rg2 = /^\./; // cannot start with dot (.) + var rg3 = /^(nul|prn|con|lpt[0-9]|com[0-9])(\.|$)/i; // forbidden file names + return function isValid(fname){ + return rg1.test(fname) && !rg2.test(fname) && !rg3.test(fname); + }; + })(); + + if (isValid(isdir) === true){ const input = await question.questions(); // console.log(input); const { filename, extension } = input; @@ -31,7 +33,37 @@ exports.main = (exports.main = async() => { console.log(chalk.blue('File created successfully') + chalk.red('!!!')); return 0; - } if (isdir === 'dld') { + } + + if (isValid(isdir) === false){ + const dirRes = await ifdir.getdir(); + const { dirname } = dirRes; + create.createdir(dirname); + console.log( + chalk.blue('Directory created successfully') + chalk.red('!!!'), + ); + return 0; + } + // if (isdir === 'd') { + // const dirRes = await ifdir.getdir(); + // const { dirname } = dirRes; + // create.createdir(dirname); + // console.log( + // chalk.blue('Directory created successfully') + chalk.red('!!!'), + // ); + // return 0; + // } if (isdir === 'f') { + // const input = await question.questions(); + // // console.log(input); + // const { filename, extension } = input; + // // console.log(filename); + // create.create(filename, extension); + // console.log(chalk.blue('File created successfully') + chalk.red('!!!')); + + // return 0; + // } + + if (isdir === 'dld') { func.deletedirec(); } else if (isdir === 'df') { func.deletefile(); @@ -39,8 +71,15 @@ exports.main = (exports.main = async() => { func.filesize(); } else if (isdir === 'rf') { func.renamefile(); + } else if (isdir === 'h' || isdir === 'help') { + console.log( + chalk.green( + 'Commands for-\n 1. Directory\n Create: d\n Delete: dld\n' + + '2. File\n create: f\n Delete: df\n Rename: rf\n Size: fs' + ) + ); } else { // Invalid response in isdir return 2; } -}); +}; diff --git a/lib/ifdir.js b/lib/ifdir.js index e830dcb..470445c 100644 --- a/lib/ifdir.js +++ b/lib/ifdir.js @@ -6,9 +6,7 @@ exports.ask = () => { { name: 'isdir', type: 'input', - message: - 'Type (d/f) to create a Directory/file or (dld/df) to' + - ' delete a directory/file (rf/fs) to rename file/get file size ', + message: 'Enter command (Type help to see all commands):', }, ]; diff --git a/tmp b/tmp new file mode 100644 index 0000000..e69de29