Node JS Project #2: Create a CLI File Creation App Using Node.js

preview_player
Показать описание

📌 What You’ll Learn:

Reading user input for file names and content
Closing the program cleanly

------------------------------------------------------------------------
🚀 Boost Your Skills with these Pre-Requisite Videos:

------------------------------------------------------------------------

------------------------------------------------------------------------
✌️ Join Us!

------------------------------------------------------------------------

⌛TIMELINE⏳
0:40 - What We Are Going to Make
1:40 - Pause the Video and Try It Yourself
2:28 - What We Wanted in Our App
3:30 - Importing Modules
4:10 - Creating CLI Interface to Read and Write
5:15 - Adding Prompt to Ask File Name and Content
7:02 - How to Write File and Add Content in It
10:01 - Close the Interface Connection
10:20 - Checking the App / Running the App
10:50 - Outro
Рекомендации по теме
Комментарии
Автор

Day - 29 still Watching ❤ Thank u vinod bhaiya 🎉

shaikdilkhushsyedbabasaifu
Автор

still watching sir respect from Noor from pakistan kashmir..🙂

NOOR_Kashmiri-n
Автор

can you make video of git hub .... understand the setting and improve the performance of the github and new feature and many more

RiteshMaurya-kx
Автор

Ye command sir kha likh rhe h?
Maira terminal par chala kr unexpected token aa rha h

Koi bta do

ankit-ks
Автор

import readline from 'readline';
import fs from 'fs';

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const showMenu = () => {
rl.question("Enter Your File Name: ", (fileName) => {
rl.question("Enter Your Content: ", (content) => {
fs.writeFile(fileName, content, (err) => {
if (err) {
console.log("Error:", err);
} else {
console.log("File created successfully!");
}

});
});
});



};

showMenu();

Huzaifakhan-yw
Автор

Are Sir Ye "Kajal meri budy" kon h

Facts_
Автор

i create this by my own


import fs from "fs";
import readline from "readline";

// Create interface for reading from terminal
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

// Function to prompt user for input
function askQuestion(query) {
return new Promise((resolve) => {
rl.question(query, (answer) => {
resolve(answer);
});
});
}

// Main function to run the program
async function createTextFile() {
try {
// Ask for the file name
const fileName = await askQuestion("Enter file name (without extension): ");

// Ensure we have a valid filename
if (!fileName || fileName.trim() === "") {
console.log("File name cannot be empty. Please try again.");
rl.close();
return;
}

// Add .txt extension
const fullFileName = `${fileName.trim()}.txt`;

// Ask for the file content
const fileContent = await askQuestion("Write data: ");

// Write the file
fs.writeFileSync(fullFileName, fileContent);

console.log(`The file "${fullFileName}" has been created successfully!`);
} catch (error) {
console.error("An error occurred:", error.message);
} finally {
// Close the readline interface
rl.close();
}
}

// Run the program
createTextFile();

mobileg
Автор

MY FILE CREATOR APP:
import fs from 'fs';
import readline from 'readline';

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

let DIR_PATH = `Your File DIR Path`;


const fileContent = (fileName, data) => {
fs.appendFile(fileName, data, "utf-8", (err) => {
if (err) {
console.error("Error adding content to file:", err);
rl.close();
return;
}
console.log("\nFile content added successfully");
createFile();
});
}

const createFileHandler = (fileName) => {
fs.writeFile(fileName, "", "utf-8", (err) => {
if (err) {
console.error("Error creating file:", err);
rl.close();
return;
}
console.log("\nFile created successfully");

});
}

const createNewFile = () => {
rl.question("Enter the file name: ", (fileName) => {

createFile();
});
}

const viewFile = () => {
if (!fs.existsSync(DIR_PATH)) {
console.error("Error: Directory does not exist.");
rl.close();
return;
}
fs.readdir(DIR_PATH, (err, files) => {
if (err) {
console.error("Error reading directory:", err);
rl.close();
return;
}
console.log("\nExisting files are: ");
files.forEach((file, ind) => {
console.log(`${ind + 1}. ${file}`);
});
createFile();
});
}

const deleteFile = () => {
rl.question("Enter the file name: ", (fileName) => {
if {
console.error(`Error: ${fileName} file does not exist.`);
rl.close();
return;
}
fs.unlink(`${DIR_PATH}${fileName}`, (err) => {
if (err) {
console.error("Error deleting file:", err);
rl.close();
return;
}
else {
console.log("File deleted successfully");
createFile();
}
});
});
}

const renameFile = () => {
rl.question("Enter the file name: ", (fileName) => {
rl.question("Enter the new file name: ", (newFileName) => {
const sourcePath = `${DIR_PATH}${fileName}`;
const destPath = `${DIR_PATH}${newFileName}`;

if (!fs.existsSync(sourcePath)) {
console.error(`Error: ${fileName} file does not exist.`);
rl.close();
return;
}

if (fs.existsSync(destPath)) {
console.error(`Error: A file with the '${newFileName}' name already exists.`);
rl.close();
return;
}

fs.rename(sourcePath, destPath, (err) => {
if (err) {
console.error("Error renaming file:", err.message);
rl.close();
return;
}
console.log("File renamed successfully");
createFile();
});
});
});
};

const addContent = () => {
rl.question("Enter the file name: ", (fileName) => {
if {
console.error(`Error: ${fileName} file does not exist.`);
rl.close();
return;
}

rl.question("Enter the content: ", (data) => {
fileContent(`${DIR_PATH}${fileName}`, `${data}\n`);

});
});
}

const optionHandler = (option) => {
switch (option) {
case "1":
createNewFile();
break;
case "2":
viewFile();
break;

case "3":
deleteFile();
break;
case "4":
renameFile();
break;
case "5":
addContent();
break;
case "6":
rl.close();
break;
}
}

const createFile = () => {
console.log("\nWelcome to the File Creater App");
setTimeout(() => {
console.log("1. Create a new file");
console.log("2. View existing file");
console.log("3. Delete a file");
console.log("4. Rename a file");
console.log("5. Add content to a file");
console.log("6. Exit");
rl.question("select the option: ", optionHandler);
}, 1000);
}

createFile();

AkkalDhami-lihe
welcome to shbcf.ru