Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 55 additions & 58 deletions 3_nodejs_project/commands/organize.js
Original file line number Diff line number Diff line change
@@ -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
}
organizeKey: organizeFn,
};
58 changes: 28 additions & 30 deletions 3_nodejs_project/commands/tree.js
Original file line number Diff line number Diff line change
@@ -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
}
treeKey: treeFn,
};