From 94faafcec3c88d80771db6c6091fc40f795efdd0 Mon Sep 17 00:00:00 2001 From: Rahul Date: Mon, 30 Aug 2021 13:15:42 +0530 Subject: [PATCH] modified tree.js and organize.js to make them work properly --- 3_nodejs_project/commands/organize.js | 113 +++++++++++++------------- 3_nodejs_project/commands/tree.js | 58 +++++++------ 2 files changed, 83 insertions(+), 88 deletions(-) diff --git a/3_nodejs_project/commands/organize.js b/3_nodejs_project/commands/organize.js index 30a517f..b1ac875 100644 --- a/3_nodejs_project/commands/organize.js +++ b/3_nodejs_project/commands/organize.js @@ -1,71 +1,68 @@ +const path = require("path"); +const fs = require("fs"); function organizeFn(dirPath) { - // console.log("organize command implemnted for ", dirPath); - // 1. input -> directory path given - let destPath; - if (dirPath == undefined) { - destPath = process.cwd(); - return; + // console.log("organize command implemnted for ", dirPath); + // 1. input -> directory path given + let destPath; + if (dirPath == undefined) { + destPath = process.cwd(); + } else { + let doesExist = fs.existsSync(dirPath); + if (doesExist) { + // 2. create -> organized_files -> directory + destPath = path.join(dirPath, "organized_files"); + if (fs.existsSync(destPath) == false) { + fs.mkdirSync(destPath); + } } else { - let doesExist = fs.existsSync(dirPath); - if (doesExist) { - - // 2. create -> organized_files -> directory - destPath = path.join(dirPath, "organized_files"); - if (fs.existsSync(destPath) == false) { - fs.mkdirSync(destPath); - } - - } else { - - console.log("Kindly enter the correct path"); - return; - } + console.log("Kindly enter the correct path"); + return; } - organizeHelper(dirPath, destPath); - // 3. identify categories of all the files present in that input directory -> + } + organizeHelper(dirPath, destPath); + // 3. identify categories of all the files present in that input directory -> } function organizeHelper(src, dest) { - // 3. identify categories of all the files present in that input directory -> - let childNames = fs.readdirSync(src); - // console.log(childNames); - for (let i = 0; i < childNames.length; i++) { - let childAddress = path.join(src, childNames[i]); - let isFile = fs.lstatSync(childAddress).isFile(); - if (isFile) { - // console.log(childNames[i]); - let category = getCategory(childNames[i]); - console.log(childNames[i], "belongs to --> ", category); - // 4. copy / cut files to that organized directory inside of any of category folder - sendFiles(childAddress, dest, category); - } + // 3. identify categories of all the files present in that input directory -> + let childNames = fs.readdirSync(src); + // console.log(childNames); + for (let i = 0; i < childNames.length; i++) { + let childAddress = path.join(src, childNames[i]); + let isFile = fs.lstatSync(childAddress).isFile(); + if (isFile) { + // console.log(childNames[i]); + let category = getCategory(childNames[i]); + console.log(childNames[i], "belongs to --> ", category); + // 4. copy / cut files to that organized directory inside of any of category folder + sendFiles(childAddress, dest, category); } + } } function sendFiles(srcFilePath, dest, category) { - // - let categoryPath = path.join(dest, category); - if (fs.existsSync(categoryPath) == false) { - fs.mkdirSync(categoryPath); - } - let fileName = path.basename(srcFilePath); - let destFilePath = path.join(categoryPath, fileName); - fs.copyFileSync(srcFilePath, destFilePath); - fs.unlinkSync(srcFilePath); - console.log(fileName, "copied to ", category); - + // + let categoryPath = path.join(dest, category); + if (fs.existsSync(categoryPath) == false) { + fs.mkdirSync(categoryPath); + } + let fileName = path.basename(srcFilePath); + let destFilePath = path.join(categoryPath, fileName); + fs.copyFileSync(srcFilePath, destFilePath); + fs.unlinkSync(srcFilePath); + console.log(fileName, "copied to ", category); } function getCategory(name) { - let ext = path.extname(name); - ext = ext.slice(1); - for (let type in types) { - let cTypeArray = types[type]; - for (let i = 0; i < cTypeArray.length; i++) { - if (ext == cTypeArray[i]) { - return type; - } - } + let ext = path.extname(name); + ext = ext.slice(1); + for (let type in types) { + let cTypeArray = types[type]; + for (let i = 0; i < cTypeArray.length; i++) { + if (ext == cTypeArray[i]) { + return type; + } } - return "others"; + } + return "others"; } module.exports = { - organizeKey: organizeFn -} \ No newline at end of file + organizeKey: organizeFn, +}; diff --git a/3_nodejs_project/commands/tree.js b/3_nodejs_project/commands/tree.js index af31abf..e526be4 100644 --- a/3_nodejs_project/commands/tree.js +++ b/3_nodejs_project/commands/tree.js @@ -1,39 +1,37 @@ +const path = require("path"); +const fs = require("fs"); function treeFn(dirPath) { - // let destPath; - if (dirPath == undefined) { - - treeHelper(process.cwd(), ""); - return; + // let destPath; + if (dirPath == undefined) { + treeHelper(process.cwd(), ""); + return; + } else { + let doesExist = fs.existsSync(dirPath); + if (doesExist) { + treeHelper(dirPath, ""); } else { - let doesExist = fs.existsSync(dirPath); - if (doesExist) { - treeHelper(dirPath, ""); - } else { - - console.log("Kindly enter the correct path"); - return; - } + console.log("Kindly enter the correct path"); + return; } + } } function treeHelper(dirPath, indent) { - // is file or folder - let isFile = fs.lstatSync(dirPath).isFile(); - if (isFile == true) { - let fileName = path.basename(dirPath); - console.log(indent + "├──" + fileName); - } else { - let dirName = path.basename(dirPath) - console.log(indent + "└──" + dirName); - let childrens = fs.readdirSync(dirPath); - for (let i = 0; i < childrens.length; i++) { - let childPath = path.join(dirPath, childrens[i]); - treeHelper(childPath, indent + "\t"); - } + // is file or folder + let isFile = fs.lstatSync(dirPath).isFile(); + if (isFile == true) { + let fileName = path.basename(dirPath); + console.log(indent + "├──" + fileName); + } else { + let dirName = path.basename(dirPath); + console.log(indent + "└──" + dirName); + let childrens = fs.readdirSync(dirPath); + for (let i = 0; i < childrens.length; i++) { + let childPath = path.join(dirPath, childrens[i]); + treeHelper(childPath, indent + "\t"); } - - + } } module.exports = { - treeKey: treeFn -} \ No newline at end of file + treeKey: treeFn, +};