diff --git a/bin/sub.js b/bin/sub.js index 02abeff..bf97af9 100644 --- a/bin/sub.js +++ b/bin/sub.js @@ -14,16 +14,17 @@ exports.main = async() => { 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; @@ -33,6 +34,35 @@ exports.main = async() => { return 0; } + + 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') { diff --git a/tmp b/tmp new file mode 100644 index 0000000..e69de29