From 0a1dd9bfc474f27662fbdd6871a51aa35e2418e4 Mon Sep 17 00:00:00 2001 From: bulbex Date: Fri, 23 Jun 2023 19:46:39 +0200 Subject: [PATCH 1/5] feat: added develop branch --- file_operations/add.js | 13 +++++++++ file_operations/cat.js | 8 ++++++ file_operations/handleOperations.js | 27 +++++++++++++++++++ package.json | 16 +++++++++++ user_input/handleUserInput.js | 42 +++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 file_operations/add.js create mode 100644 file_operations/cat.js create mode 100644 file_operations/handleOperations.js create mode 100644 package.json create mode 100644 user_input/handleUserInput.js diff --git a/file_operations/add.js b/file_operations/add.js new file mode 100644 index 0000000..5127af4 --- /dev/null +++ b/file_operations/add.js @@ -0,0 +1,13 @@ +import { writeFile} from "node:fs/promises" +import { existsSync } from "node:fs" +import { printCWD } from "../index.js" + +export async function addFile(path) { + if (existsSync(path)) { + console.log("File with this name already exists") + return + } + await writeFile(path, "", {encoding: "utf8"}) + .catch(error => { console.error(`Error: ${error.message}`) }) + .then(() => {console.log(`Successfully added ${path}`); printCWD()}) +} \ No newline at end of file diff --git a/file_operations/cat.js b/file_operations/cat.js new file mode 100644 index 0000000..f251bba --- /dev/null +++ b/file_operations/cat.js @@ -0,0 +1,8 @@ +import { createReadStream } from "node:fs" +import { printCWD } from "../index.js" + +export function readContent(path) { + createReadStream(path) + .on("error", error => {throw new Error(error)}) + .on("data", data => {console.log(data.toString()); printCWD()}) +} \ No newline at end of file diff --git a/file_operations/handleOperations.js b/file_operations/handleOperations.js new file mode 100644 index 0000000..c45a7c4 --- /dev/null +++ b/file_operations/handleOperations.js @@ -0,0 +1,27 @@ +import { readContent } from "./cat.js"; +import { addFile } from "./add.js"; + +export async function handleFileOperation(operation, paths) { + switch (operation) { + case "cat": + readContent(paths[0]) + break; + case "add": + addFile(paths[0]) + break; + case "rn": + + break; + case "cp": + + break; + case "mv": + + break; + case "rm": + + break; + default: + break; + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..6474b6b --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "file-manager", + "version": "1.0.0", + "description": "RS school file manager task", + "main": "index.js", + "type": "module", + "scripts": { + "start": "node index.js" + }, + "repository": { + "type": "git", + "url": "" + }, + "author": "bulbex", + "license": "ISC" +} diff --git a/user_input/handleUserInput.js b/user_input/handleUserInput.js new file mode 100644 index 0000000..a2986e0 --- /dev/null +++ b/user_input/handleUserInput.js @@ -0,0 +1,42 @@ +import { handleFileOperation } from "../file_operations/handleOperations.js" +import { printCWD } from "../index.js" +import { readdir } from "node:fs" + +export async function handleUserInput(line) { + if (line === "up") { + process.chdir("..") + printCWD() + return + } + + if (line.startsWith("cd ")) { + process.chdir(line.split(" ").slice(1).join(" ")) + printCWD() + return + } + + if (line === "ls") { + readdir(process.cwd(), {withFileTypes: true}, + (err, files) => { + if (err) { + console.error(err) + return + } + console.table( + files.sort((a, b) => { + return a.isDirectory() + ? b.isDirectory() + ? a.name - b.name + : -1 + : b.isDirectory() + ? 1 + : a.name - b.name + }).map(file => Object.assign({ name: file.name, type: file.isDirectory() ? 'directory' : 'file' }))) + }) + return + } + + const command = line.split(" ")[0] + const filePaths = line.split(" ").slice(1) + await handleFileOperation(command, filePaths) +} \ No newline at end of file From 05be5a5306de4d9176527eb4ec52997ce384426c Mon Sep 17 00:00:00 2001 From: bulbex Date: Sat, 24 Jun 2023 21:20:37 +0200 Subject: [PATCH 2/5] feat: resolved all cases --- compress_decompress/compress.js | 19 +++++ compress_decompress/decompress.js | 21 +++++ compress_decompress/handleCompDecomp.js | 19 +++++ file_operations/add.js | 14 ++-- file_operations/cat.js | 4 +- file_operations/copy.js | 22 ++++++ file_operations/handleOperations.js | 15 ++-- file_operations/move.js | 9 +++ file_operations/remove.js | 7 ++ file_operations/rename.js | 7 ++ hash/handleHashCommand.js | 12 +++ index.js | 17 +++- os/handleOSCommands.js | 24 ++++++ user_input/handleUserInput.js | 100 ++++++++++++++++-------- utils/getFileFromPath.js | 5 ++ utils/printCWD.js | 1 + 16 files changed, 243 insertions(+), 53 deletions(-) create mode 100644 compress_decompress/compress.js create mode 100644 compress_decompress/decompress.js create mode 100644 compress_decompress/handleCompDecomp.js create mode 100644 file_operations/copy.js create mode 100644 file_operations/move.js create mode 100644 file_operations/remove.js create mode 100644 file_operations/rename.js create mode 100644 hash/handleHashCommand.js create mode 100644 os/handleOSCommands.js create mode 100644 utils/getFileFromPath.js create mode 100644 utils/printCWD.js diff --git a/compress_decompress/compress.js b/compress_decompress/compress.js new file mode 100644 index 0000000..7978005 --- /dev/null +++ b/compress_decompress/compress.js @@ -0,0 +1,19 @@ +import { createReadStream, createWriteStream } from "node:fs" +import path from "node:path" +import { createBrotliCompress } from "node:zlib" +import { getFileFromPath } from "../utils/getFileFromPath.js" + +export function compressFile(paths) { + const fileToCompress = getFileFromPath(paths[0]) + + const input = createReadStream(paths[0]) + .on("error", error => console.error("Operation failed")) + + const output = createWriteStream(path.join(paths[1], `${fileToCompress}.br`)) + .on("error", error => console.error("Operation failed")) + + const brotli = createBrotliCompress() + + input.pipe(brotli).pipe(output) + .on("error", error => console.error("Operation failed")) +} \ No newline at end of file diff --git a/compress_decompress/decompress.js b/compress_decompress/decompress.js new file mode 100644 index 0000000..34958cd --- /dev/null +++ b/compress_decompress/decompress.js @@ -0,0 +1,21 @@ +import { createReadStream, createWriteStream } from "node:fs" +import { createBrotliDecompress } from "node:zlib" +import path from "node:path" +import { getFileFromPath } from "../utils/getFileFromPath.js" + +export function decompressFile(paths) { + + const fileToCompress = getFileFromPath(paths[0]).split(".") + fileToCompress.pop() // Remove .br extension + + const input = createReadStream(paths[0]) + .on("error", error => console.error("Operation failed")) + + const output = createWriteStream(path.join(paths[1], fileToCompress.join("."))) + .on("error", error => console.error("Operation failed")) + + const brotli = createBrotliDecompress() + + input.pipe(brotli).pipe(output) + .on("error", error => console.error("Operation failed")) +} \ No newline at end of file diff --git a/compress_decompress/handleCompDecomp.js b/compress_decompress/handleCompDecomp.js new file mode 100644 index 0000000..a0c8112 --- /dev/null +++ b/compress_decompress/handleCompDecomp.js @@ -0,0 +1,19 @@ +import { printCWD } from "../utils/printCWD.js"; +import { compressFile } from "./compress.js"; +import { decompressFile } from "./decompress.js"; + +export function handleCompDecomp(command, paths) { + switch (command) { + case "compress": + compressFile(paths) + printCWD() + break; + case "decompress": + decompressFile(paths) + printCWD() + break; + default: + console.error("Invalid input") + break; + } +} \ No newline at end of file diff --git a/file_operations/add.js b/file_operations/add.js index 5127af4..338edac 100644 --- a/file_operations/add.js +++ b/file_operations/add.js @@ -1,13 +1,11 @@ import { writeFile} from "node:fs/promises" -import { existsSync } from "node:fs" -import { printCWD } from "../index.js" +import { printCWD } from "../utils/printCWD.js" export async function addFile(path) { - if (existsSync(path)) { - console.log("File with this name already exists") - return + try { + await writeFile(path, "", {encoding: "utf8"}) + } catch (error) { + console.error("Operation failed") } - await writeFile(path, "", {encoding: "utf8"}) - .catch(error => { console.error(`Error: ${error.message}`) }) - .then(() => {console.log(`Successfully added ${path}`); printCWD()}) + printCWD() } \ No newline at end of file diff --git a/file_operations/cat.js b/file_operations/cat.js index f251bba..87d104f 100644 --- a/file_operations/cat.js +++ b/file_operations/cat.js @@ -1,8 +1,8 @@ import { createReadStream } from "node:fs" -import { printCWD } from "../index.js" +import { printCWD } from "../utils/printCWD.js" export function readContent(path) { createReadStream(path) - .on("error", error => {throw new Error(error)}) + .on("error", error => console.error("Operation failed")) .on("data", data => {console.log(data.toString()); printCWD()}) } \ No newline at end of file diff --git a/file_operations/copy.js b/file_operations/copy.js new file mode 100644 index 0000000..317f7a8 --- /dev/null +++ b/file_operations/copy.js @@ -0,0 +1,22 @@ +import { createReadStream, createWriteStream, existsSync } from "node:fs" +import { printCWD } from "../utils/printCWD.js" +import path from "node:path" +import { mkdir } from "node:fs/promises" +import { getFileFromPath } from "../utils/getFileFromPath.js" + +export async function copyFile(paths) { + if (!existsSync(paths[1])) { + await mkdir(paths[1], { recursive: true }) + } + + const fileToCopy = getFileFromPath(paths[0]) + + const dest = path.join(paths[1], fileToCopy) + const writeStream = createWriteStream(dest).on("error", error => console.error("Operation failed")) + + createReadStream(paths[0], { encoding: "utf8" }) + .on("error", error => console.error("Operation failed")) + .on("data", data => writeStream.write(data)) + + printCWD() +} \ No newline at end of file diff --git a/file_operations/handleOperations.js b/file_operations/handleOperations.js index c45a7c4..90fb1fc 100644 --- a/file_operations/handleOperations.js +++ b/file_operations/handleOperations.js @@ -1,5 +1,9 @@ import { readContent } from "./cat.js"; import { addFile } from "./add.js"; +import { renameFile } from "./rename.js"; +import { copyFile } from "./copy.js"; +import { moveFile } from "./move.js"; +import { removeFile } from "./remove.js"; export async function handleFileOperation(operation, paths) { switch (operation) { @@ -7,21 +11,22 @@ export async function handleFileOperation(operation, paths) { readContent(paths[0]) break; case "add": - addFile(paths[0]) + await addFile(paths[0]) break; case "rn": - + await renameFile(paths) break; case "cp": - + await copyFile(paths) break; case "mv": - + await moveFile(paths) break; case "rm": - + await removeFile(paths[0]) break; default: + console.error("Invalid input") break; } } \ No newline at end of file diff --git a/file_operations/move.js b/file_operations/move.js new file mode 100644 index 0000000..9a085a5 --- /dev/null +++ b/file_operations/move.js @@ -0,0 +1,9 @@ +import { printCWD } from "../utils/printCWD.js"; +import { copyFile } from "./copy.js"; +import { rm } from "node:fs/promises" + +export async function moveFile(paths) { + await copyFile(paths) + await rm(paths[0]).catch(error => console.error("Operation failed")) + printCWD() +} \ No newline at end of file diff --git a/file_operations/remove.js b/file_operations/remove.js new file mode 100644 index 0000000..4d3d781 --- /dev/null +++ b/file_operations/remove.js @@ -0,0 +1,7 @@ +import { rm } from "node:fs/promises" +import { printCWD } from "../utils/printCWD.js" + +export async function removeFile(path) { + await rm(path).catch(error => console.error("Operation failed")) + printCWD() +} \ No newline at end of file diff --git a/file_operations/rename.js b/file_operations/rename.js new file mode 100644 index 0000000..959b480 --- /dev/null +++ b/file_operations/rename.js @@ -0,0 +1,7 @@ +import { rename } from "node:fs/promises" +import { printCWD } from "../utils/printCWD.js" + +export async function renameFile(paths) { + await rename(paths[0], paths[1]).catch(error => console.error("Operation failed")) + printCWD() +} \ No newline at end of file diff --git a/hash/handleHashCommand.js b/hash/handleHashCommand.js new file mode 100644 index 0000000..c28215a --- /dev/null +++ b/hash/handleHashCommand.js @@ -0,0 +1,12 @@ +import { createHash } from "node:crypto" +import { readFile } from "node:fs/promises" +import { printCWD } from "../utils/printCWD.js" + +export async function hashFile(path) { + try { + console.log(createHash("sha256").update(await readFile(path)).digest("hex")) + } catch (error) { + console.error("Operation failed") + } + printCWD() +} \ No newline at end of file diff --git a/index.js b/index.js index 2d41852..cf82e24 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,28 @@ import process from "node:process" import readline from "node:readline/promises" import { handleUserInput } from "./user_input/handleUserInput.js" - -export const printCWD = () => console.log(`You are currently in ${process.cwd()}`) +import { homedir } from "node:os" +import { printCWD } from "./utils/printCWD.js" async function fileManager() { - const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) + const rl = readline.createInterface({ input: process.stdin, output: process.stdout}) let username = process.argv.find(arg => arg.startsWith("--username"))?.split("=")[1] || await rl.question("Please, provide your username: ") console.log(`Welcome to the File Manager, ${username}!`) + + process.chdir(homedir()) + printCWD() - rl.on("line", async (line) => await handleUserInput(line)) + rl.on("line", + (line) => line === ".exit" || line === "CLOSE" + ? process.exit() + : handleUserInput(line.trim()) + ) + + process.on("exit", () => console.log(`Thank you for using File Manager, ${username}, goodbye!`)) } await fileManager() \ No newline at end of file diff --git a/os/handleOSCommands.js b/os/handleOSCommands.js new file mode 100644 index 0000000..91db03e --- /dev/null +++ b/os/handleOSCommands.js @@ -0,0 +1,24 @@ +import { EOL, cpus, homedir, userInfo } from "node:os" + +export function handleOSCommands(command) { + switch (command) { + case "--EOL": + console.log(`End of line: ${JSON.stringify(EOL)}`) + break; + case "--cpus": + console.log("Machine CPUs: ", cpus().map(cpu => cpu.model)) + break; + case "--homedir": + console.log(`Home directory: ${homedir}`) + break; + case "--username": + console.log(`System username: ${userInfo().username}`) + break; + case "--architecture": + console.log(`CPU architecture: ${process.arch}`) + break; + default: + console.error("Invalid input") + break; + } +} \ No newline at end of file diff --git a/user_input/handleUserInput.js b/user_input/handleUserInput.js index a2986e0..5ecefa1 100644 --- a/user_input/handleUserInput.js +++ b/user_input/handleUserInput.js @@ -1,42 +1,74 @@ import { handleFileOperation } from "../file_operations/handleOperations.js" -import { printCWD } from "../index.js" +import { handleOSCommands } from "../os/handleOSCommands.js" +import { printCWD } from "../utils/printCWD.js" import { readdir } from "node:fs" +import { hashFile } from "../hash/handleHashCommand.js" +import { handleCompDecomp } from "../compress_decompress/handleCompDecomp.js" export async function handleUserInput(line) { - if (line === "up") { - process.chdir("..") - printCWD() - return - } + try { + if (line === "") { + printCWD() + return + } - if (line.startsWith("cd ")) { - process.chdir(line.split(" ").slice(1).join(" ")) - printCWD() - return - } + if (line === "up") { + process.chdir("..") + printCWD() + return + } + + if (line === "ls") { + readdir(process.cwd(), {withFileTypes: true}, + (err, files) => { + if (err) { + console.error("Operation failed") + return + } + console.table( + files.sort((a, b) => { + return a.isDirectory() + ? b.isDirectory() + ? a.name - b.name + : -1 + : b.isDirectory() + ? 1 + : a.name - b.name + }).map(file => Object.assign({ name: file.name, type: file.isDirectory() ? 'directory' : 'file' }))) + }) + return + } - if (line === "ls") { - readdir(process.cwd(), {withFileTypes: true}, - (err, files) => { - if (err) { - console.error(err) - return - } - console.table( - files.sort((a, b) => { - return a.isDirectory() - ? b.isDirectory() - ? a.name - b.name - : -1 - : b.isDirectory() - ? 1 - : a.name - b.name - }).map(file => Object.assign({ name: file.name, type: file.isDirectory() ? 'directory' : 'file' }))) - }) - return - } + if (line.startsWith("cd ")) { + process.chdir(line.split(" ").slice(1).join(" ")) + printCWD() + return + } + + if (line.startsWith("os ")) { + handleOSCommands(line.split(" ")[1]) + printCWD() + return + } - const command = line.split(" ")[0] - const filePaths = line.split(" ").slice(1) - await handleFileOperation(command, filePaths) + const filePaths = line.split(" ").slice(1) + + if (line.startsWith("hash ")) { + await hashFile(filePaths[0]) + return + } + + const command = line.split(" ")[0] + + if (line.startsWith("compress ") || line.startsWith("decompress ")) { + handleCompDecomp(command, filePaths) + return + } + + await handleFileOperation(command, filePaths) + + } catch (error) { + console.error("Operation failed") + printCWD() + } } \ No newline at end of file diff --git a/utils/getFileFromPath.js b/utils/getFileFromPath.js new file mode 100644 index 0000000..6be3416 --- /dev/null +++ b/utils/getFileFromPath.js @@ -0,0 +1,5 @@ +export function getFileFromPath(path) { + return path.includes("/") + ? `${path.split("/").slice(-1)}` + : `${path.split("\\").slice(-1)}` +} \ No newline at end of file diff --git a/utils/printCWD.js b/utils/printCWD.js new file mode 100644 index 0000000..ff651f2 --- /dev/null +++ b/utils/printCWD.js @@ -0,0 +1 @@ +export const printCWD = () => console.log(`You are currently in ${process.cwd()}`) \ No newline at end of file From 849ca399033494d4edccb8d1e8c121341c7f3168 Mon Sep 17 00:00:00 2001 From: bulbex Date: Sun, 25 Jun 2023 16:52:10 +0200 Subject: [PATCH 3/5] feat: finished tasks --- compress_decompress/compress.js | 14 ++++++++------ compress_decompress/decompress.js | 18 ++++++++++-------- compress_decompress/handleCompDecomp.js | 3 --- file_operations/copy.js | 24 +++++++++++++++++++----- file_operations/handleOperations.js | 3 +-- file_operations/move.js | 9 --------- file_operations/rename.js | 7 ++++++- index.js | 4 ++-- user_input/handleUserInput.js | 9 +++++---- 9 files changed, 51 insertions(+), 40 deletions(-) delete mode 100644 file_operations/move.js diff --git a/compress_decompress/compress.js b/compress_decompress/compress.js index 7978005..241499f 100644 --- a/compress_decompress/compress.js +++ b/compress_decompress/compress.js @@ -2,18 +2,20 @@ import { createReadStream, createWriteStream } from "node:fs" import path from "node:path" import { createBrotliCompress } from "node:zlib" import { getFileFromPath } from "../utils/getFileFromPath.js" +import { printCWD } from "../utils/printCWD.js" export function compressFile(paths) { const fileToCompress = getFileFromPath(paths[0]) const input = createReadStream(paths[0]) - .on("error", error => console.error("Operation failed")) - - const output = createWriteStream(path.join(paths[1], `${fileToCompress}.br`)) - .on("error", error => console.error("Operation failed")) const brotli = createBrotliCompress() - input.pipe(brotli).pipe(output) - .on("error", error => console.error("Operation failed")) + input.on("error", error => {console.error("Operation failed"); printCWD()}) + .pipe(brotli.on("error", error => {console.error("Operation failed"); printCWD()})) + .on("data", data => createWriteStream(path.join(paths[1], `${fileToCompress}.br`)) + .on("error", error => {console.error("Operation failed"); printCWD()}) + .on("open", () => printCWD()) + .write(data) + ) } \ No newline at end of file diff --git a/compress_decompress/decompress.js b/compress_decompress/decompress.js index 34958cd..f0efb89 100644 --- a/compress_decompress/decompress.js +++ b/compress_decompress/decompress.js @@ -2,20 +2,22 @@ import { createReadStream, createWriteStream } from "node:fs" import { createBrotliDecompress } from "node:zlib" import path from "node:path" import { getFileFromPath } from "../utils/getFileFromPath.js" +import { printCWD } from "../utils/printCWD.js" export function decompressFile(paths) { - const fileToCompress = getFileFromPath(paths[0]).split(".") - fileToCompress.pop() // Remove .br extension + const fileToDecompress = getFileFromPath(paths[0]).split(".") + fileToDecompress.pop() // Remove .br extension const input = createReadStream(paths[0]) - .on("error", error => console.error("Operation failed")) - - const output = createWriteStream(path.join(paths[1], fileToCompress.join("."))) - .on("error", error => console.error("Operation failed")) const brotli = createBrotliDecompress() - input.pipe(brotli).pipe(output) - .on("error", error => console.error("Operation failed")) + input.on("error", error => {console.error("Operation failed"); printCWD()}) + .pipe(brotli.on("error", error => {console.error("Operation failed"); printCWD()})) + .on("data", data => createWriteStream(path.join(paths[1], fileToDecompress.join("."))) + .on("error", error => console.error("Operation failed")) + .on("open", () => printCWD()) + .write(data) + ) } \ No newline at end of file diff --git a/compress_decompress/handleCompDecomp.js b/compress_decompress/handleCompDecomp.js index a0c8112..fed41bb 100644 --- a/compress_decompress/handleCompDecomp.js +++ b/compress_decompress/handleCompDecomp.js @@ -1,4 +1,3 @@ -import { printCWD } from "../utils/printCWD.js"; import { compressFile } from "./compress.js"; import { decompressFile } from "./decompress.js"; @@ -6,11 +5,9 @@ export function handleCompDecomp(command, paths) { switch (command) { case "compress": compressFile(paths) - printCWD() break; case "decompress": decompressFile(paths) - printCWD() break; default: console.error("Invalid input") diff --git a/file_operations/copy.js b/file_operations/copy.js index 317f7a8..f2905de 100644 --- a/file_operations/copy.js +++ b/file_operations/copy.js @@ -3,8 +3,9 @@ import { printCWD } from "../utils/printCWD.js" import path from "node:path" import { mkdir } from "node:fs/promises" import { getFileFromPath } from "../utils/getFileFromPath.js" +import { rm } from "node:fs/promises" -export async function copyFile(paths) { +export async function copyFile(paths, withRemove = false) { if (!existsSync(paths[1])) { await mkdir(paths[1], { recursive: true }) } @@ -12,11 +13,24 @@ export async function copyFile(paths) { const fileToCopy = getFileFromPath(paths[0]) const dest = path.join(paths[1], fileToCopy) - const writeStream = createWriteStream(dest).on("error", error => console.error("Operation failed")) + // const writeStream = createWriteStream(dest).on("error", error => console.error("Operation failed")) createReadStream(paths[0], { encoding: "utf8" }) - .on("error", error => console.error("Operation failed")) - .on("data", data => writeStream.write(data)) + .on("error", error => {console.error("Operation failed"); printCWD()}) + .on("data", data => { + createWriteStream(dest) + .on("error", error => console.error("Operation failed")) + .on("open", async () => withRemove + ? (await rm(paths[0]).catch(error => console.error("Operation failed")), printCWD()) + : printCWD() + ) + .write(data) + + + }) + + // if (withRemove) { + // await rm(paths[0]).catch(error => console.error("Operation failed")) + // } - printCWD() } \ No newline at end of file diff --git a/file_operations/handleOperations.js b/file_operations/handleOperations.js index 90fb1fc..f07d291 100644 --- a/file_operations/handleOperations.js +++ b/file_operations/handleOperations.js @@ -2,7 +2,6 @@ import { readContent } from "./cat.js"; import { addFile } from "./add.js"; import { renameFile } from "./rename.js"; import { copyFile } from "./copy.js"; -import { moveFile } from "./move.js"; import { removeFile } from "./remove.js"; export async function handleFileOperation(operation, paths) { @@ -20,7 +19,7 @@ export async function handleFileOperation(operation, paths) { await copyFile(paths) break; case "mv": - await moveFile(paths) + await copyFile(paths, true) break; case "rm": await removeFile(paths[0]) diff --git a/file_operations/move.js b/file_operations/move.js deleted file mode 100644 index 9a085a5..0000000 --- a/file_operations/move.js +++ /dev/null @@ -1,9 +0,0 @@ -import { printCWD } from "../utils/printCWD.js"; -import { copyFile } from "./copy.js"; -import { rm } from "node:fs/promises" - -export async function moveFile(paths) { - await copyFile(paths) - await rm(paths[0]).catch(error => console.error("Operation failed")) - printCWD() -} \ No newline at end of file diff --git a/file_operations/rename.js b/file_operations/rename.js index 959b480..46dbfaf 100644 --- a/file_operations/rename.js +++ b/file_operations/rename.js @@ -1,7 +1,12 @@ import { rename } from "node:fs/promises" import { printCWD } from "../utils/printCWD.js" +import { getFileFromPath } from "../utils/getFileFromPath.js" +import path from "node:path" export async function renameFile(paths) { - await rename(paths[0], paths[1]).catch(error => console.error("Operation failed")) + const fileNameLength = getFileFromPath(paths[0]).length + const pathToFile = paths[0].slice(0, paths[0].length - fileNameLength) + await rename(paths[0], path.join(pathToFile, paths[1])) + .catch(error => console.error("Operation failed")) printCWD() } \ No newline at end of file diff --git a/index.js b/index.js index cf82e24..9013fed 100644 --- a/index.js +++ b/index.js @@ -5,14 +5,14 @@ import { homedir } from "node:os" import { printCWD } from "./utils/printCWD.js" async function fileManager() { - const rl = readline.createInterface({ input: process.stdin, output: process.stdout}) + const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) let username = process.argv.find(arg => arg.startsWith("--username"))?.split("=")[1] || await rl.question("Please, provide your username: ") console.log(`Welcome to the File Manager, ${username}!`) - process.chdir(homedir()) + // process.chdir(homedir()) printCWD() diff --git a/user_input/handleUserInput.js b/user_input/handleUserInput.js index 5ecefa1..2e22246 100644 --- a/user_input/handleUserInput.js +++ b/user_input/handleUserInput.js @@ -35,6 +35,7 @@ export async function handleUserInput(line) { ? 1 : a.name - b.name }).map(file => Object.assign({ name: file.name, type: file.isDirectory() ? 'directory' : 'file' }))) + printCWD() }) return } @@ -51,14 +52,14 @@ export async function handleUserInput(line) { return } - const filePaths = line.split(" ").slice(1) + const command = line.split(" ")[0] + const filePaths = line.slice(command.length).match(/[^ (\"|\')][\:*\w+ \/\.]+[^ (\"|\')]/g) if (line.startsWith("hash ")) { await hashFile(filePaths[0]) - return + return } - const command = line.split(" ")[0] if (line.startsWith("compress ") || line.startsWith("decompress ")) { handleCompDecomp(command, filePaths) @@ -68,7 +69,7 @@ export async function handleUserInput(line) { await handleFileOperation(command, filePaths) } catch (error) { - console.error("Operation failed") + console.error(error) printCWD() } } \ No newline at end of file From 8d95ce9e81700ca941089a63ea2e6dfb796246fb Mon Sep 17 00:00:00 2001 From: bulbex Date: Sun, 25 Jun 2023 16:52:31 +0200 Subject: [PATCH 4/5] fix: start from homedir --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 9013fed..a666668 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ async function fileManager() { console.log(`Welcome to the File Manager, ${username}!`) - // process.chdir(homedir()) + process.chdir(homedir()) printCWD() From f7eec560697fc191695bf5aae8cddb20c65b70cb Mon Sep 17 00:00:00 2001 From: bulbex Date: Sun, 25 Jun 2023 18:05:03 +0200 Subject: [PATCH 5/5] fix: bug with paths --- file_operations/copy.js | 47 ++++++++++++++++------------------- user_input/handleUserInput.js | 4 ++- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/file_operations/copy.js b/file_operations/copy.js index f2905de..0131370 100644 --- a/file_operations/copy.js +++ b/file_operations/copy.js @@ -6,31 +6,28 @@ import { getFileFromPath } from "../utils/getFileFromPath.js" import { rm } from "node:fs/promises" export async function copyFile(paths, withRemove = false) { - if (!existsSync(paths[1])) { - await mkdir(paths[1], { recursive: true }) + try { + if (!existsSync(paths[1])) { + await mkdir(paths[1], { recursive: true }) + } + + const fileToCopy = getFileFromPath(paths[0]) + + const dest = path.join(paths[1], fileToCopy) + + createReadStream(paths[0], { encoding: "utf8" }) + .on("error", error => {console.error("Operation failed"); printCWD()}) + .on("data", data => { + createWriteStream(dest) + .on("error", error => console.error("Operation failed")) + .on("open", async () => withRemove + ? (await rm(paths[0]).catch(error => console.error("Operation failed")), printCWD()) + : printCWD() + ) + .write(data) + }) + } catch (error) { + console.error("Operation failed") } - const fileToCopy = getFileFromPath(paths[0]) - - const dest = path.join(paths[1], fileToCopy) - // const writeStream = createWriteStream(dest).on("error", error => console.error("Operation failed")) - - createReadStream(paths[0], { encoding: "utf8" }) - .on("error", error => {console.error("Operation failed"); printCWD()}) - .on("data", data => { - createWriteStream(dest) - .on("error", error => console.error("Operation failed")) - .on("open", async () => withRemove - ? (await rm(paths[0]).catch(error => console.error("Operation failed")), printCWD()) - : printCWD() - ) - .write(data) - - - }) - - // if (withRemove) { - // await rm(paths[0]).catch(error => console.error("Operation failed")) - // } - } \ No newline at end of file diff --git a/user_input/handleUserInput.js b/user_input/handleUserInput.js index 2e22246..a1b508c 100644 --- a/user_input/handleUserInput.js +++ b/user_input/handleUserInput.js @@ -53,7 +53,9 @@ export async function handleUserInput(line) { } const command = line.split(" ")[0] - const filePaths = line.slice(command.length).match(/[^ (\"|\')][\:*\w+ \/\.]+[^ (\"|\')]/g) + const filePaths = command.startsWith("cp") && line.split(" ").slice(1).length === 2 + ? line.split(" ").slice(1) + : line.slice(command.length).match(/[^ (\"|\')][\:*\w+ \/\.]+[^ (\"|\')]/g) if (line.startsWith("hash ")) { await hashFile(filePaths[0])