From 98fe9b8c4e74466f91a8f6128d0691030d30b450 Mon Sep 17 00:00:00 2001 From: user Date: Fri, 31 Oct 2025 10:02:32 +0300 Subject: [PATCH 01/14] init: start file-manager task --- package.json | 58 +++++-------------- src/cli/args.js | 5 -- src/cli/env.js | 5 -- src/cp/cp.js | 6 -- src/cp/files/script.js | 19 ------ src/fs/copy.js | 5 -- src/fs/create.js | 5 -- src/fs/delete.js | 5 -- src/fs/files/dontLookAtMe.txt | 1 - src/fs/files/fileToRead.txt | 7 --- src/fs/files/fileToRemove.txt | 1 - src/fs/files/hello.txt | 1 - src/fs/files/wrongFilename.txt | 3 - src/fs/list.js | 5 -- src/fs/read.js | 5 -- src/fs/rename.js | 5 -- src/hash/calcHash.js | 5 -- src/hash/files/fileToCalculateHashFor.txt | 1 - .../files/fileToWrite.txt => index.js} | 0 src/modules/cjsToEsm.cjs | 34 ----------- src/modules/files/a.json | 5 -- src/modules/files/b.json | 5 -- src/modules/files/c.cjs | 1 - src/streams/files/fileToRead.txt | 1 - src/streams/read.js | 5 -- src/streams/transform.js | 5 -- src/streams/write.js | 5 -- src/wt/main.js | 5 -- src/wt/worker.js | 8 --- src/zip/compress.js | 5 -- src/zip/decompress.js | 5 -- src/zip/files/fileToCompress.txt | 1 - 32 files changed, 16 insertions(+), 211 deletions(-) delete mode 100644 src/cli/args.js delete mode 100644 src/cli/env.js delete mode 100644 src/cp/cp.js delete mode 100644 src/cp/files/script.js delete mode 100644 src/fs/copy.js delete mode 100644 src/fs/create.js delete mode 100644 src/fs/delete.js delete mode 100644 src/fs/files/dontLookAtMe.txt delete mode 100644 src/fs/files/fileToRead.txt delete mode 100644 src/fs/files/fileToRemove.txt delete mode 100644 src/fs/files/hello.txt delete mode 100644 src/fs/files/wrongFilename.txt delete mode 100644 src/fs/list.js delete mode 100644 src/fs/read.js delete mode 100644 src/fs/rename.js delete mode 100644 src/hash/calcHash.js delete mode 100644 src/hash/files/fileToCalculateHashFor.txt rename src/{streams/files/fileToWrite.txt => index.js} (100%) delete mode 100644 src/modules/cjsToEsm.cjs delete mode 100644 src/modules/files/a.json delete mode 100644 src/modules/files/b.json delete mode 100644 src/modules/files/c.cjs delete mode 100644 src/streams/files/fileToRead.txt delete mode 100644 src/streams/read.js delete mode 100644 src/streams/transform.js delete mode 100644 src/streams/write.js delete mode 100644 src/wt/main.js delete mode 100644 src/wt/worker.js delete mode 100644 src/zip/compress.js delete mode 100644 src/zip/decompress.js delete mode 100644 src/zip/files/fileToCompress.txt diff --git a/package.json b/package.json index 3108c6c969..57c0837b35 100644 --- a/package.json +++ b/package.json @@ -1,44 +1,18 @@ { - "name": "node-nodejs-basics", - "version": "1.0.0", - "description": "This repository is the part of nodejs-assignments https://github.com/AlreadyBored/nodejs-assignments", - "engines": { - "node": ">=24.10.0", - "npm": ">=10.9.2" - }, - "type": "module", - "scripts": { - "cli:args": "node src/cli/args.js --some-arg value1 --other 1337 --arg2 42", - "cli:env": "npx cross-env SOME=any RSS_foo=bar RSS_bar=baz node src/cli/env.js", - "cp": "node src/cp/cp.js", - "fs:copy": "node src/fs/copy.js", - "fs:create": "node src/fs/create.js", - "fs:delete": "node src/fs/delete.js", - "fs:list": "node src/fs/list.js", - "fs:read": "node src/fs/read.js", - "fs:rename": "node src/fs/rename.js", - "hash": "node src/hash/calcHash.js", - "modules": "node src/modules/esm.mjs", - "streams:read": "node src/streams/read.js", - "streams:transform": "node src/streams/transform.js", - "streams:write": "node src/streams/write.js", - "wt": "node src/wt/main.js", - "zip:compress": "node src/zip/compress.js", - "zip:decompress": "node src/zip/decompress.js" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/AlreadyBored/node-nodejs-basics.git" - }, - "keywords": [ - "nodejs", - "assignments", - "alreadybored" - ], - "author": "alreadybored", - "license": "ISC", - "bugs": { - "url": "https://github.com/AlreadyBored/node-nodejs-basics/issues" - }, - "homepage": "https://github.com/AlreadyBored/node-nodejs-basics#readme" + "name": "file-manager", + "version": "1.0.0", + "engines": { + "node": ">=24.10.0", + "npm": ">=10.9.2" + }, + "type": "module", + "scripts": { + "start": "node src/index.js" + }, + "author": "evgkamok", + "license": "ISC", + "bugs": { + "url": "https://github.com/AlreadyBored/nodejs-assignments/issues" + }, + "homepage": "https://github.com/AlreadyBored/nodejs-assignments/blob/main/assignments/file-manager/assignment.md" } diff --git a/src/cli/args.js b/src/cli/args.js deleted file mode 100644 index 9e3622f791..0000000000 --- a/src/cli/args.js +++ /dev/null @@ -1,5 +0,0 @@ -const parseArgs = () => { - // Write your code here -}; - -parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js deleted file mode 100644 index e3616dc8e7..0000000000 --- a/src/cli/env.js +++ /dev/null @@ -1,5 +0,0 @@ -const parseEnv = () => { - // Write your code here -}; - -parseEnv(); diff --git a/src/cp/cp.js b/src/cp/cp.js deleted file mode 100644 index 72c6addc9c..0000000000 --- a/src/cp/cp.js +++ /dev/null @@ -1,6 +0,0 @@ -const spawnChildProcess = async (args) => { - // Write your code here -}; - -// Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); diff --git a/src/cp/files/script.js b/src/cp/files/script.js deleted file mode 100644 index 0c6654f12f..0000000000 --- a/src/cp/files/script.js +++ /dev/null @@ -1,19 +0,0 @@ -import { EOL } from 'node:os'; -import { argv, stdout, stdin, exit } from 'node:process'; - -const args = argv.slice(2); - -console.log(`Total number of arguments is ${args.length}`); -console.log(`Arguments: ${JSON.stringify(args)}${EOL}`); - -const echoInput = (chunk) => { - const chunkStringified = chunk.toString(); - - if (chunkStringified.includes('CLOSE')) { - exit(0); - } - - stdout.write(`Received from master process: ${chunk.toString()}${EOL}`); -}; - -stdin.on('data', echoInput); diff --git a/src/fs/copy.js b/src/fs/copy.js deleted file mode 100644 index e226075b4c..0000000000 --- a/src/fs/copy.js +++ /dev/null @@ -1,5 +0,0 @@ -const copy = async () => { - // Write your code here -}; - -await copy(); diff --git a/src/fs/create.js b/src/fs/create.js deleted file mode 100644 index 6ede285599..0000000000 --- a/src/fs/create.js +++ /dev/null @@ -1,5 +0,0 @@ -const create = async () => { - // Write your code here -}; - -await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js deleted file mode 100644 index a70b13766c..0000000000 --- a/src/fs/delete.js +++ /dev/null @@ -1,5 +0,0 @@ -const remove = async () => { - // Write your code here -}; - -await remove(); diff --git a/src/fs/files/dontLookAtMe.txt b/src/fs/files/dontLookAtMe.txt deleted file mode 100644 index 8979bab743..0000000000 --- a/src/fs/files/dontLookAtMe.txt +++ /dev/null @@ -1 +0,0 @@ -What are you looking at?! \ No newline at end of file diff --git a/src/fs/files/fileToRead.txt b/src/fs/files/fileToRead.txt deleted file mode 100644 index 5d66c332d6..0000000000 --- a/src/fs/files/fileToRead.txt +++ /dev/null @@ -1,7 +0,0 @@ -My content -should -be -printed -into -console -! \ No newline at end of file diff --git a/src/fs/files/fileToRemove.txt b/src/fs/files/fileToRemove.txt deleted file mode 100644 index 43e64cd45c..0000000000 --- a/src/fs/files/fileToRemove.txt +++ /dev/null @@ -1 +0,0 @@ -How dare you! \ No newline at end of file diff --git a/src/fs/files/hello.txt b/src/fs/files/hello.txt deleted file mode 100644 index 4e65f7775f..0000000000 --- a/src/fs/files/hello.txt +++ /dev/null @@ -1 +0,0 @@ -Hello Node.js \ No newline at end of file diff --git a/src/fs/files/wrongFilename.txt b/src/fs/files/wrongFilename.txt deleted file mode 100644 index 38cca5db19..0000000000 --- a/src/fs/files/wrongFilename.txt +++ /dev/null @@ -1,3 +0,0 @@ -# This is a file with a wrong filename - -Hello from **markdown**! \ No newline at end of file diff --git a/src/fs/list.js b/src/fs/list.js deleted file mode 100644 index 0c0fa21f7e..0000000000 --- a/src/fs/list.js +++ /dev/null @@ -1,5 +0,0 @@ -const list = async () => { - // Write your code here -}; - -await list(); diff --git a/src/fs/read.js b/src/fs/read.js deleted file mode 100644 index e3938be563..0000000000 --- a/src/fs/read.js +++ /dev/null @@ -1,5 +0,0 @@ -const read = async () => { - // Write your code here -}; - -await read(); diff --git a/src/fs/rename.js b/src/fs/rename.js deleted file mode 100644 index b1d65b0c86..0000000000 --- a/src/fs/rename.js +++ /dev/null @@ -1,5 +0,0 @@ -const rename = async () => { - // Write your code here -}; - -await rename(); diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js deleted file mode 100644 index e37c17ed62..0000000000 --- a/src/hash/calcHash.js +++ /dev/null @@ -1,5 +0,0 @@ -const calculateHash = async () => { - // Write your code here -}; - -await calculateHash(); diff --git a/src/hash/files/fileToCalculateHashFor.txt b/src/hash/files/fileToCalculateHashFor.txt deleted file mode 100644 index 08f56564f8..0000000000 --- a/src/hash/files/fileToCalculateHashFor.txt +++ /dev/null @@ -1 +0,0 @@ -Calculate hash for me! \ No newline at end of file diff --git a/src/streams/files/fileToWrite.txt b/src/index.js similarity index 100% rename from src/streams/files/fileToWrite.txt rename to src/index.js diff --git a/src/modules/cjsToEsm.cjs b/src/modules/cjsToEsm.cjs deleted file mode 100644 index 089bd2db13..0000000000 --- a/src/modules/cjsToEsm.cjs +++ /dev/null @@ -1,34 +0,0 @@ -const path = require('node:path'); -const { release, version } = require('node:os'); -const { createServer: createServerHttp } = require('node:http'); - -require('./files/c.cjs'); - -const random = Math.random(); - -const unknownObject = random > 0.5 ? require('./files/a.json') : require('./files/b.json'); - -console.log(`Release ${release()}`); -console.log(`Version ${version()}`); -console.log(`Path segment separator is "${path.sep}"`); - -console.log(`Path to current file is ${__filename}`); -console.log(`Path to current directory is ${__dirname}`); - -const myServer = createServerHttp((_, res) => { - res.end('Request accepted'); -}); - -const PORT = 3000; - -console.log(unknownObject); - -myServer.listen(PORT, () => { - console.log(`Server is listening on port ${PORT}`); - console.log('To terminate it, use Ctrl+C combination'); -}); - -module.exports = { - unknownObject, - myServer, -}; diff --git a/src/modules/files/a.json b/src/modules/files/a.json deleted file mode 100644 index d1f6dac48a..0000000000 --- a/src/modules/files/a.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "a": 1, - "b": 2, - "c": 3 -} diff --git a/src/modules/files/b.json b/src/modules/files/b.json deleted file mode 100644 index e442128649..0000000000 --- a/src/modules/files/b.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "a": 11, - "b": 22, - "c": 33 -} diff --git a/src/modules/files/c.cjs b/src/modules/files/c.cjs deleted file mode 100644 index df728e1627..0000000000 --- a/src/modules/files/c.cjs +++ /dev/null @@ -1 +0,0 @@ -console.log('Hello from c.cjs!'); diff --git a/src/streams/files/fileToRead.txt b/src/streams/files/fileToRead.txt deleted file mode 100644 index c7e8d132a1..0000000000 --- a/src/streams/files/fileToRead.txt +++ /dev/null @@ -1 +0,0 @@ -This file should be read using Streams API \ No newline at end of file diff --git a/src/streams/read.js b/src/streams/read.js deleted file mode 100644 index e3938be563..0000000000 --- a/src/streams/read.js +++ /dev/null @@ -1,5 +0,0 @@ -const read = async () => { - // Write your code here -}; - -await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js deleted file mode 100644 index 9e6c15fe84..0000000000 --- a/src/streams/transform.js +++ /dev/null @@ -1,5 +0,0 @@ -const transform = async () => { - // Write your code here -}; - -await transform(); diff --git a/src/streams/write.js b/src/streams/write.js deleted file mode 100644 index 84aa11e7cb..0000000000 --- a/src/streams/write.js +++ /dev/null @@ -1,5 +0,0 @@ -const write = async () => { - // Write your code here -}; - -await write(); diff --git a/src/wt/main.js b/src/wt/main.js deleted file mode 100644 index e2ef054d41..0000000000 --- a/src/wt/main.js +++ /dev/null @@ -1,5 +0,0 @@ -const performCalculations = async () => { - // Write your code here -}; - -await performCalculations(); diff --git a/src/wt/worker.js b/src/wt/worker.js deleted file mode 100644 index 405595394d..0000000000 --- a/src/wt/worker.js +++ /dev/null @@ -1,8 +0,0 @@ -// n should be received from main thread -const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); - -const sendResult = () => { - // This function sends result of nthFibonacci computations to main thread -}; - -sendResult(); diff --git a/src/zip/compress.js b/src/zip/compress.js deleted file mode 100644 index d55209587e..0000000000 --- a/src/zip/compress.js +++ /dev/null @@ -1,5 +0,0 @@ -const compress = async () => { - // Write your code here -}; - -await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js deleted file mode 100644 index 8aaf26c8a4..0000000000 --- a/src/zip/decompress.js +++ /dev/null @@ -1,5 +0,0 @@ -const decompress = async () => { - // Write your code here -}; - -await decompress(); diff --git a/src/zip/files/fileToCompress.txt b/src/zip/files/fileToCompress.txt deleted file mode 100644 index 4d4efc82fe..0000000000 --- a/src/zip/files/fileToCompress.txt +++ /dev/null @@ -1 +0,0 @@ -Compress me! \ No newline at end of file From b599df0c344914ac67f16f9c869d50bfd93aec32 Mon Sep 17 00:00:00 2001 From: user Date: Sat, 1 Nov 2025 20:46:16 +0300 Subject: [PATCH 02/14] feat: implement function - parceCliArgs --- jsconfig.json | 5 +++++ package-lock.json | 17 +++++++++++++++++ src/cli/parser.js | 26 ++++++++++++++++++++++++++ src/index.js | 6 ++++++ 4 files changed, 54 insertions(+) create mode 100644 jsconfig.json create mode 100644 package-lock.json create mode 100644 src/cli/parser.js diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000000..2e03e8f24f --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,5 @@ +{ + // "compilerOptions": { "checkJs": true }, + "typeAcquisition": { "include": ["node"] }, + "exclude": ["node_modules"] +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000000..8d53355a27 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,17 @@ +{ + "name": "file-manager", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "file-manager", + "version": "1.0.0", + "license": "ISC", + "engines": { + "node": ">=24.10.0", + "npm": ">=10.9.2" + } + } + } +} diff --git a/src/cli/parser.js b/src/cli/parser.js new file mode 100644 index 0000000000..e55df3f52b --- /dev/null +++ b/src/cli/parser.js @@ -0,0 +1,26 @@ +import { parseArgs } from 'node:util' + +export function parseCliArgs() { + try { + const args = process.argv.slice(2) + + const options = { + username: { + type: 'string', + }, + } + + const { values } = parseArgs({ args, options }) + + if (!values.username) { + return 'user' + } else { + return { + username: values.username, + } + } + } catch (error) { + console.error('Error', error.message) + process.exit(1) + } +} diff --git a/src/index.js b/src/index.js index e69de29bb2..d2bf3c0cee 100644 --- a/src/index.js +++ b/src/index.js @@ -0,0 +1,6 @@ +import { parseCliArgs } from './cli/parser.js' + +const { username } = parseCliArgs() + +console.log(`Welcome to the File Manager, ${username}!`) +console.log(`You are currently in ${process.cwd()}`) From 39a5dfcd3e3b928d9e5dd663f9191cb5a162dc23 Mon Sep 17 00:00:00 2001 From: user Date: Sat, 1 Nov 2025 21:20:48 +0300 Subject: [PATCH 03/14] feat: implement readline interface --- src/cli/interface.js | 39 +++++++++++++++++++++++++++++++++++++++ src/index.js | 22 ++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/cli/interface.js diff --git a/src/cli/interface.js b/src/cli/interface.js new file mode 100644 index 0000000000..54cf69f728 --- /dev/null +++ b/src/cli/interface.js @@ -0,0 +1,39 @@ +import { createInterface } from 'node:readline/promises' + +export function createCLIInterface(username, commandHandler) { + const rl = createInterface({ + input: process.stdin, + output: process.stdout, + prompt: '> ', + }) + + rl.on('line', async input => { + const command = input.trim() + + if (command === '.exit') { + rl.close() + return + } + + if (command) { + await commandHandler(command) + } else { + console.log('Operation failed') + } + + console.log(`You are currently in ${process.cwd()}`) + + rl.prompt() + }) + + rl.on('close', () => { + console.log(`\nThank you for using File Manager, ${username}, goodbye!`) + process.exit(0) + }) + + rl.on('SIGINT', () => { + rl.close() + }) + + return rl +} diff --git a/src/index.js b/src/index.js index d2bf3c0cee..013497e5b3 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,28 @@ +import { createCLIInterface } from './cli/interface.js' import { parseCliArgs } from './cli/parser.js' const { username } = parseCliArgs() +async function handleCommand(command) { + const [cmd, ...args] = command.split(' ') + + switch (cmd) { + case 'up': + console.log('Command up') + break + case 'cd': + console.log(`Command cd ${args[0]}`) + break + case 'ls': + console.log('Command ls') + break + default: + console.log('Invalid input') + } +} + console.log(`Welcome to the File Manager, ${username}!`) console.log(`You are currently in ${process.cwd()}`) + +const rl = createCLIInterface(username, handleCommand) +rl.prompt() From 6a635a5ace206bbf63c9ae7299bce3cc81c77410 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 2 Nov 2025 21:37:30 +0300 Subject: [PATCH 04/14] feat: implement navigation commands --- src/commands/commands.js | 62 ++++++++++++++++++++++++++++++++++++++++ src/index.js | 7 +++-- 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/commands/commands.js diff --git a/src/commands/commands.js b/src/commands/commands.js new file mode 100644 index 0000000000..555bb897bb --- /dev/null +++ b/src/commands/commands.js @@ -0,0 +1,62 @@ +import { readdir } from 'node:fs/promises' +import path from 'node:path' +import process from 'node:process' + +export async function up() { + try { + const currentDir = process.cwd() + process.chdir('..') + + if (currentDir === process.cwd()) { + console.warn('Already at root directory') + return + } + } catch (error) { + throw new Error('Failed to navigate up') + } +} + +export async function cd(targetPath) { + if (!targetPath) { + throw new Error('Path is required') + } + + try { + const destinationPath = path.resolve(targetPath) + process.chdir(destinationPath) + } catch (error) { + throw new Error(`Failed to navigate to ${targetPath}`) + } +} + +export async function ls() { + try { + const entries = await readdir(process.cwd(), { withFileTypes: true }) + + const directories = entries + .filter(entry => entry.isDirectory()) + .map(entry => entry.name) + .sort() + + const files = entries + .filter(entry => entry.isFile) + .map(entry => entry.name) + .sort() + + if (directories.length > 0) { + console.log('\nDirectories:') + directories.forEach(dir => console.log(` 📁 ${dir}`)) + } + + if (files.length > 0) { + console.log('\nFiles:') + files.forEach(file => console.log(` 📄 ${file}`)) + } + + if (directories.length === 0 && files.length === 0) { + console.log('Directory is empty') + } + } catch (error) { + throw new Error('Failed to list directory contents') + } +} diff --git a/src/index.js b/src/index.js index 013497e5b3..81381c49a4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import { createCLIInterface } from './cli/interface.js' import { parseCliArgs } from './cli/parser.js' +import { cd, ls, up } from './commands/commands.js' const { username } = parseCliArgs() @@ -8,13 +9,13 @@ async function handleCommand(command) { switch (cmd) { case 'up': - console.log('Command up') + up() break case 'cd': - console.log(`Command cd ${args[0]}`) + cd(args[0]) break case 'ls': - console.log('Command ls') + ls() break default: console.log('Invalid input') From 225d68da028135f6c2ddfc2c4fbbb85a6c29fba0 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 3 Nov 2025 11:51:03 +0300 Subject: [PATCH 05/14] feat: implement files operations functions --- src/commands/files.js | 112 ++++++++++++++++++++++++++++++++++++++++++ src/index.js | 26 ++++++++-- t1/t1.txt | 1 + t2/3.txt | 1 + 4 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 src/commands/files.js create mode 100644 t1/t1.txt create mode 100644 t2/3.txt diff --git a/src/commands/files.js b/src/commands/files.js new file mode 100644 index 0000000000..35e8ae9afd --- /dev/null +++ b/src/commands/files.js @@ -0,0 +1,112 @@ +import { readFile, rename, writeFile, copyFile, unlink } from 'node:fs/promises' +import path from 'node:path' + +export async function cat(filePath) { + if (!filePath) { + throw new Error('File path is required') + } + + try { + const absFilePath = path.resolve(filePath) + const content = await readFile(absFilePath, 'utf-8') + + process.stdout.write(content + '\n') + } catch (error) { + throw new Error('File to read file') + } +} + +export async function add(fileName) { + console.log('add') + + if (!fileName) { + throw new Error('File name is required') + } + + try { + const filePath = path.join(process.cwd(), fileName) + + await writeFile(filePath, '', { flag: 'wx' }) + console.log(`File ${fileName} successfully created`) + } catch (error) { + if (error.code === 'EEXIST') { + throw new Error('File with this name already exist') + } + throw new Error('Failed to create file') + } +} + +export async function rn(oldFilePath, nameNewFile) { + if (!oldFilePath || !nameNewFile) { + throw new Error('Both arguments are required') + } + + try { + const absOldFilePath = path.resolve(oldFilePath) + const nameDirectory = path.dirname(absOldFilePath) + const absNewFilePath = path.join(nameDirectory, nameNewFile) + + await rename(absOldFilePath, absNewFilePath) + console.log(`File successfully renamed`) + } catch (error) { + throw new Error('Failed to rename file') + } +} + +export async function cp(srcPath, destDir) { + if (!srcPath || !destDir) { + throw new Error('Both arguments are required') + } + + try { + const absSrcPath = path.resolve(srcPath) + const srcFileName = path.basename(absSrcPath) + const absDestPath = path.resolve(destDir) + + const destPath = path.join(absDestPath, srcFileName) + + await copyFile(absSrcPath, destPath) + console.log(`File copied successfully`) + } catch (error) { + console.log(error) + throw new Error('Failed to copy file') + } +} + +export async function mv(srcPath, destDir) { + if (!srcPath || !destDir) { + throw new Error('Both arguments are required') + } + + try { + const absSrcPath = path.resolve(srcPath) + const srcFileName = path.basename(srcPath) + + const absDestPath = path.resolve(destDir) + const destPath = path.join(absDestPath, srcFileName) + + try { + await rename(absSrcPath, destPath) + console.log(`File moved successfully`) + } catch (error) { + throw new Error("Rename doesn't work") + } + } catch (error) { + throw new Error('Failed to move file') + } +} + +export async function rm(filePath) { + if (!filePath) { + throw new Error('File path argument is required') + } + + try { + const absFilePath = path.resolve(filePath) + + await unlink(absFilePath) + console.log(`File deleted successfully`) + } catch (error) { + throw new Error('Failed to delete file') + } +} diff --git a/src/index.js b/src/index.js index 81381c49a4..96000d8ecc 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ import { createCLIInterface } from './cli/interface.js' import { parseCliArgs } from './cli/parser.js' import { cd, ls, up } from './commands/commands.js' +import { add, cat, cp, mv, rm, rn } from './commands/files.js' const { username } = parseCliArgs() @@ -9,14 +10,33 @@ async function handleCommand(command) { switch (cmd) { case 'up': - up() + await up() break case 'cd': - cd(args[0]) + await cd(args[0]) break case 'ls': - ls() + await ls() break + + // File operations + case 'cat': + await cat(args[0]) + break + case 'add': + await add(args[0]) + break + case 'rn': + await rn(args[0], args[1]) + break + case 'cp': + await cp(args[0], args[1]) + break + case 'mv': + await mv(args[0], args[1]) + break + case 'rm': + await rm(args[0], args[1]) default: console.log('Invalid input') } diff --git a/t1/t1.txt b/t1/t1.txt new file mode 100644 index 0000000000..5ab2f8a432 --- /dev/null +++ b/t1/t1.txt @@ -0,0 +1 @@ +Hello \ No newline at end of file diff --git a/t2/3.txt b/t2/3.txt new file mode 100644 index 0000000000..525ea3def4 --- /dev/null +++ b/t2/3.txt @@ -0,0 +1 @@ +sadsad \ No newline at end of file From 1d36f587c754659001a14bc85e7a0c5294633caa Mon Sep 17 00:00:00 2001 From: user Date: Mon, 3 Nov 2025 12:21:41 +0300 Subject: [PATCH 06/14] feat: implement os info functions --- src/commands/os.js | 39 +++++++++++++++++++++++++++++++++++++++ src/index.js | 6 ++++++ 2 files changed, 45 insertions(+) create mode 100644 src/commands/os.js diff --git a/src/commands/os.js b/src/commands/os.js new file mode 100644 index 0000000000..a6723eef31 --- /dev/null +++ b/src/commands/os.js @@ -0,0 +1,39 @@ +import os from 'node:os' + +export async function osInfo(flag) { + if (!flag) { + throw new Error('Flag is required') + } + + try { + switch (flag) { + case '--EOL': + const EOL = JSON.stringify(os.EOL) + console.log(`default system End-Of-Line is - ${EOL}`) + break + case '--cpus': + const cpus = os.cpus() + console.log('Total count CPU - ', cpus.length) + cpus.forEach((cpu, index) => { + console.log( + `${index + 1}. ${cpu.model} - ${(cpu.speed / 1000).toFixed(2)}Ghz` + ) + }) + break + case '--homedir': + console.log(`Home directory is - ${os.homedir}`) + break + case '--username': + const userInfo = os.userInfo() + console.log(`Username is ${userInfo.username}`) + break + case '--architecture': + console.log(`CPU arch is ${os.arch}`) + break + default: + throw new Error('Invalid OS flag') + } + } catch (error) { + throw new Error('Failed to get OS information') + } +} diff --git a/src/index.js b/src/index.js index 96000d8ecc..2dd5e306b3 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import { createCLIInterface } from './cli/interface.js' import { parseCliArgs } from './cli/parser.js' import { cd, ls, up } from './commands/commands.js' import { add, cat, cp, mv, rm, rn } from './commands/files.js' +import { osInfo } from './commands/os.js' const { username } = parseCliArgs() @@ -37,6 +38,11 @@ async function handleCommand(command) { break case 'rm': await rm(args[0], args[1]) + + //OS + case 'os': + await osInfo(args[0]) + break default: console.log('Invalid input') } From fd50242bf477f4e248277674aa638b2ee7120bba Mon Sep 17 00:00:00 2001 From: user Date: Mon, 3 Nov 2025 15:59:43 +0300 Subject: [PATCH 07/14] feat: implement hash function --- src/commands/hash.js | 27 +++++++++++++++++++++++++++ src/index.js | 7 +++++++ t1/t1.txt | 1 - t2/3.txt | 1 - 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/commands/hash.js delete mode 100644 t1/t1.txt delete mode 100644 t2/3.txt diff --git a/src/commands/hash.js b/src/commands/hash.js new file mode 100644 index 0000000000..01e4e95a18 --- /dev/null +++ b/src/commands/hash.js @@ -0,0 +1,27 @@ +import { createHash } from 'node:crypto' +import { createReadStream } from 'node:fs' +import path from 'node:path' + +export async function hash(filePath) { + if (!filePath) { + throw new Error('File path is required') + } + + try { + const absFilePath = path.resolve(filePath) + const rdStream = createReadStream(absFilePath) + + const hash = createHash('sha256') + + for await (const chunk of rdStream) { + hash.update(chunk) + } + + const hex = hash.digest('hex') + console.log(`SHA-256 hash: ${hex}`) + return hex + } catch (error) { + console.log('error-new', error) + throw new Error('Failed to calculate hash') + } +} diff --git a/src/index.js b/src/index.js index 2dd5e306b3..3f35050f06 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import { createCLIInterface } from './cli/interface.js' import { parseCliArgs } from './cli/parser.js' import { cd, ls, up } from './commands/commands.js' import { add, cat, cp, mv, rm, rn } from './commands/files.js' +import { hash } from './commands/hash.js' import { osInfo } from './commands/os.js' const { username } = parseCliArgs() @@ -43,6 +44,12 @@ async function handleCommand(command) { case 'os': await osInfo(args[0]) break + + // HASH + case 'hash': + await hash(args[0]) + break + default: console.log('Invalid input') } diff --git a/t1/t1.txt b/t1/t1.txt deleted file mode 100644 index 5ab2f8a432..0000000000 --- a/t1/t1.txt +++ /dev/null @@ -1 +0,0 @@ -Hello \ No newline at end of file diff --git a/t2/3.txt b/t2/3.txt deleted file mode 100644 index 525ea3def4..0000000000 --- a/t2/3.txt +++ /dev/null @@ -1 +0,0 @@ -sadsad \ No newline at end of file From 7d6c0112d5248d56b854eb39fa49b485ae3d61d1 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 3 Nov 2025 17:33:02 +0300 Subject: [PATCH 08/14] feat: implement compress/decompress function --- Readme.md | 3 -- src/cli/newParser | 26 +++++++++++ src/commands/compress.js | 45 ++++++++++++++++++++ src/commands/{commands.js => navigation.js} | 2 +- src/commands/parser | Bin 0 -> 252 bytes src/index.js | 15 ++++++- 6 files changed, 85 insertions(+), 6 deletions(-) delete mode 100644 Readme.md create mode 100644 src/cli/newParser create mode 100644 src/commands/compress.js rename src/commands/{commands.js => navigation.js} (97%) create mode 100644 src/commands/parser diff --git a/Readme.md b/Readme.md deleted file mode 100644 index 611a505f49..0000000000 --- a/Readme.md +++ /dev/null @@ -1,3 +0,0 @@ -# Node.js basics - -## !!! Please don't submit Pull Requests to this repository !!! diff --git a/src/cli/newParser b/src/cli/newParser new file mode 100644 index 0000000000..e55df3f52b --- /dev/null +++ b/src/cli/newParser @@ -0,0 +1,26 @@ +import { parseArgs } from 'node:util' + +export function parseCliArgs() { + try { + const args = process.argv.slice(2) + + const options = { + username: { + type: 'string', + }, + } + + const { values } = parseArgs({ args, options }) + + if (!values.username) { + return 'user' + } else { + return { + username: values.username, + } + } + } catch (error) { + console.error('Error', error.message) + process.exit(1) + } +} diff --git a/src/commands/compress.js b/src/commands/compress.js new file mode 100644 index 0000000000..1e1a285f15 --- /dev/null +++ b/src/commands/compress.js @@ -0,0 +1,45 @@ +import { createReadStream, createWriteStream } from 'node:fs' +import path from 'node:path' +import { pipeline } from 'node:stream/promises' +import { createGunzip, createGzip } from 'node:zlib' + +export async function compress(srcPath, dstPath) { + if (!srcPath && !dstPath) { + throw new Error('Both arguments is required') + } + + try { + const absSrcPath = path.resolve(srcPath) + const absDstPath = path.resolve(dstPath) + + const source = createReadStream(absSrcPath) + const destination = createWriteStream(absDstPath) + const gzip = createGzip() + + await pipeline(source, gzip, destination) + console.log(`Compressed successfully to ${absDstPath}`) + } catch (error) { + throw new Error('Failed to compress file') + } +} + +export async function decompress(srcPath, dstPath) { + if (!srcPath || !dstPath) { + throw new Error('Both arguments are required') + } + + try { + const absSrcPath = path.resolve(srcPath) + const absDstPath = path.resolve(dstPath) + + const source = createReadStream(absSrcPath) + const destination = createWriteStream(absDstPath) + const gunZlib = createGunzip() + + await pipeline(source, gunZlib, destination) + + console.log(`Decompressed successfully to ${absDstPath}`) + } catch (error) { + throw new Error('Failed to decompress file') + } +} diff --git a/src/commands/commands.js b/src/commands/navigation.js similarity index 97% rename from src/commands/commands.js rename to src/commands/navigation.js index 555bb897bb..75c7ea57fa 100644 --- a/src/commands/commands.js +++ b/src/commands/navigation.js @@ -39,7 +39,7 @@ export async function ls() { .sort() const files = entries - .filter(entry => entry.isFile) + .filter(entry => entry.isFile()) .map(entry => entry.name) .sort() diff --git a/src/commands/parser b/src/commands/parser new file mode 100644 index 0000000000000000000000000000000000000000..e5f368034d15b34c3707fddb0ee0098674fdadc4 GIT binary patch literal 252 zcmVtxUGUOdQJyPaK;=R{&T3$+ z@>@@Z9qu-4$f2+23VLq=hQpvXQ$K-z>-_QMWx5w(*(icySg{3Q$oS{4XF4 Date: Mon, 3 Nov 2025 18:03:42 +0300 Subject: [PATCH 09/14] fix: change display style in direrctory && add goHome fn on start FM --- src/cli/newParser | 26 -------------------------- src/commands/navigation.js | 30 +++++++++++++++++------------- src/commands/parser | Bin 252 -> 0 bytes src/index.js | 5 ++--- 4 files changed, 19 insertions(+), 42 deletions(-) delete mode 100644 src/cli/newParser delete mode 100644 src/commands/parser diff --git a/src/cli/newParser b/src/cli/newParser deleted file mode 100644 index e55df3f52b..0000000000 --- a/src/cli/newParser +++ /dev/null @@ -1,26 +0,0 @@ -import { parseArgs } from 'node:util' - -export function parseCliArgs() { - try { - const args = process.argv.slice(2) - - const options = { - username: { - type: 'string', - }, - } - - const { values } = parseArgs({ args, options }) - - if (!values.username) { - return 'user' - } else { - return { - username: values.username, - } - } - } catch (error) { - console.error('Error', error.message) - process.exit(1) - } -} diff --git a/src/commands/navigation.js b/src/commands/navigation.js index 75c7ea57fa..6f2e78fe48 100644 --- a/src/commands/navigation.js +++ b/src/commands/navigation.js @@ -1,6 +1,6 @@ import { readdir } from 'node:fs/promises' +import os from 'node:os' import path from 'node:path' -import process from 'node:process' export async function up() { try { @@ -35,28 +35,32 @@ export async function ls() { const directories = entries .filter(entry => entry.isDirectory()) - .map(entry => entry.name) - .sort() + .map(entry => ({ + Name: entry.name, + Type: 'directory', + })) const files = entries .filter(entry => entry.isFile()) - .map(entry => entry.name) - .sort() + .map(entry => ({ + Name: entry.name, + Type: 'file', + })) - if (directories.length > 0) { - console.log('\nDirectories:') - directories.forEach(dir => console.log(` 📁 ${dir}`)) - } + const contentDir = [...directories, ...files] - if (files.length > 0) { - console.log('\nFiles:') - files.forEach(file => console.log(` 📄 ${file}`)) - } + console.table(contentDir) if (directories.length === 0 && files.length === 0) { console.log('Directory is empty') } } catch (error) { + console.log(error) throw new Error('Failed to list directory contents') } } + +export async function goHomeDir() { + const homePath = os.homedir() + process.chdir(homePath) +} diff --git a/src/commands/parser b/src/commands/parser deleted file mode 100644 index e5f368034d15b34c3707fddb0ee0098674fdadc4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 252 zcmVtxUGUOdQJyPaK;=R{&T3$+ z@>@@Z9qu-4$f2+23VLq=hQpvXQ$K-z>-_QMWx5w(*(icySg{3Q$oS{4XF4 Date: Mon, 3 Nov 2025 18:13:58 +0300 Subject: [PATCH 10/14] feat: implement mkdir function --- src/commands/files.js | 30 +++++++++++++++++++++++------- src/index.js | 5 ++++- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/commands/files.js b/src/commands/files.js index 35e8ae9afd..3fdd0b2f61 100644 --- a/src/commands/files.js +++ b/src/commands/files.js @@ -1,4 +1,11 @@ -import { readFile, rename, writeFile, copyFile, unlink } from 'node:fs/promises' +import { + readFile, + rename, + writeFile, + copyFile, + unlink, + mkdir as creteDirectory, +} from 'node:fs/promises' import path from 'node:path' export async function cat(filePath) { @@ -85,12 +92,8 @@ export async function mv(srcPath, destDir) { const absDestPath = path.resolve(destDir) const destPath = path.join(absDestPath, srcFileName) - try { - await rename(absSrcPath, destPath) - console.log(`File moved successfully`) - } catch (error) { - throw new Error("Rename doesn't work") - } + await rename(absSrcPath, destPath) + console.log(`File moved successfully`) } catch (error) { throw new Error('Failed to move file') } @@ -110,3 +113,16 @@ export async function rm(filePath) { throw new Error('Failed to delete file') } } + +export async function mkdir(nameDir) { + if (!nameDir) { + throw new Error('Name directory is required') + } + + try { + await creteDirectory(nameDir) + console.log(`Directory successfully created`) + } catch (error) { + throw new Error('Failed to create directory') + } +} diff --git a/src/index.js b/src/index.js index b4e505cfba..4cd2b1bdda 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ import { createCLIInterface } from './cli/interface.js' import { parseCliArgs } from './cli/parser.js' import { cd, goHomeDir, ls, up } from './commands/navigation.js' -import { add, cat, cp, mv, rm, rn } from './commands/files.js' +import { add, cat, cp, mkdir, mv, rm, rn } from './commands/files.js' import { hash } from './commands/hash.js' import { osInfo } from './commands/os.js' import { compress, decompress } from './commands/compress.js' @@ -41,6 +41,9 @@ async function handleCommand(command) { break case 'rm': await rm(args[0], args[1]) + case 'mkdir': + await mkdir(args[0]) + break //OS case 'os': From a5ebd06cac5546331c49038a29e46a682d2461ef Mon Sep 17 00:00:00 2001 From: user Date: Mon, 3 Nov 2025 18:44:58 +0300 Subject: [PATCH 11/14] refactor: navigation function --- src/cli/interface.js | 8 +++-- src/commands/navigation.js | 60 +++++++++++++++----------------------- 2 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/cli/interface.js b/src/cli/interface.js index 54cf69f728..a6b3c359ae 100644 --- a/src/cli/interface.js +++ b/src/cli/interface.js @@ -16,9 +16,11 @@ export function createCLIInterface(username, commandHandler) { } if (command) { - await commandHandler(command) - } else { - console.log('Operation failed') + try { + await commandHandler(command) + } catch (error) { + console.log('Operation failed') + } } console.log(`You are currently in ${process.cwd()}`) diff --git a/src/commands/navigation.js b/src/commands/navigation.js index 6f2e78fe48..b465710f06 100644 --- a/src/commands/navigation.js +++ b/src/commands/navigation.js @@ -3,16 +3,11 @@ import os from 'node:os' import path from 'node:path' export async function up() { - try { - const currentDir = process.cwd() - process.chdir('..') + const currentDirPath = process.cwd() + process.chdir('..') - if (currentDir === process.cwd()) { - console.warn('Already at root directory') - return - } - } catch (error) { - throw new Error('Failed to navigate up') + if (currentDirPath === process.cwd()) { + console.warn('Already at root directory') } } @@ -21,42 +16,33 @@ export async function cd(targetPath) { throw new Error('Path is required') } - try { - const destinationPath = path.resolve(targetPath) - process.chdir(destinationPath) - } catch (error) { - throw new Error(`Failed to navigate to ${targetPath}`) - } + const absDstPath = path.resolve(targetPath) + process.chdir(absDstPath) } export async function ls() { - try { - const entries = await readdir(process.cwd(), { withFileTypes: true }) + const entries = await readdir(process.cwd(), { withFileTypes: true }) - const directories = entries - .filter(entry => entry.isDirectory()) - .map(entry => ({ - Name: entry.name, - Type: 'directory', - })) + const directories = entries + .filter(entry => entry.isDirectory()) + .map(entry => ({ + Name: entry.name, + Type: 'directory', + })) - const files = entries - .filter(entry => entry.isFile()) - .map(entry => ({ - Name: entry.name, - Type: 'file', - })) + const files = entries + .filter(entry => entry.isFile()) + .map(entry => ({ + Name: entry.name, + Type: 'file', + })) - const contentDir = [...directories, ...files] + const contentDir = [...directories, ...files] - console.table(contentDir) + console.table(contentDir) - if (directories.length === 0 && files.length === 0) { - console.log('Directory is empty') - } - } catch (error) { - console.log(error) - throw new Error('Failed to list directory contents') + if (directories.length === 0 && files.length === 0) { + console.log('Directory is empty') } } From 5c3d91c740589a5c3a75f52fb9678f8426eb8398 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 3 Nov 2025 18:56:20 +0300 Subject: [PATCH 12/14] refactor: files operations functions --- src/commands/files.js | 93 ++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 64 deletions(-) diff --git a/src/commands/files.js b/src/commands/files.js index 3fdd0b2f61..f4b4010de7 100644 --- a/src/commands/files.js +++ b/src/commands/files.js @@ -13,34 +13,22 @@ export async function cat(filePath) { throw new Error('File path is required') } - try { - const absFilePath = path.resolve(filePath) - const content = await readFile(absFilePath, 'utf-8') + const absFilePath = path.resolve(filePath) + const content = await readFile(absFilePath, 'utf-8') - process.stdout.write(content + '\n') - } catch (error) { - throw new Error('File to read file') - } + process.stdout.write(content + '\n') } export async function add(fileName) { - console.log('add') - if (!fileName) { throw new Error('File name is required') } - try { - const filePath = path.join(process.cwd(), fileName) + const filePath = path.join(process.cwd(), fileName) - await writeFile(filePath, '', { flag: 'wx' }) - console.log(`File ${fileName} successfully created`) - } catch (error) { - if (error.code === 'EEXIST') { - throw new Error('File with this name already exist') - } - throw new Error('Failed to create file') - } + await writeFile(filePath, '', { flag: 'wx' }) + + console.log(`File ${fileName} successfully created`) } export async function rn(oldFilePath, nameNewFile) { @@ -48,16 +36,12 @@ export async function rn(oldFilePath, nameNewFile) { throw new Error('Both arguments are required') } - try { - const absOldFilePath = path.resolve(oldFilePath) - const nameDirectory = path.dirname(absOldFilePath) - const absNewFilePath = path.join(nameDirectory, nameNewFile) + const absOldFilePath = path.resolve(oldFilePath) + const nameDirectory = path.dirname(absOldFilePath) + const absNewFilePath = path.join(nameDirectory, nameNewFile) - await rename(absOldFilePath, absNewFilePath) - console.log(`File successfully renamed`) - } catch (error) { - throw new Error('Failed to rename file') - } + await rename(absOldFilePath, absNewFilePath) + console.log(`File successfully renamed`) } export async function cp(srcPath, destDir) { @@ -65,19 +49,14 @@ export async function cp(srcPath, destDir) { throw new Error('Both arguments are required') } - try { - const absSrcPath = path.resolve(srcPath) - const srcFileName = path.basename(absSrcPath) - const absDestPath = path.resolve(destDir) + const absSrcPath = path.resolve(srcPath) + const srcFileName = path.basename(absSrcPath) + const absDestPath = path.resolve(destDir) - const destPath = path.join(absDestPath, srcFileName) + const destPath = path.join(absDestPath, srcFileName) - await copyFile(absSrcPath, destPath) - console.log(`File copied successfully`) - } catch (error) { - console.log(error) - throw new Error('Failed to copy file') - } + await copyFile(absSrcPath, destPath) + console.log(`File copied successfully`) } export async function mv(srcPath, destDir) { @@ -85,18 +64,13 @@ export async function mv(srcPath, destDir) { throw new Error('Both arguments are required') } - try { - const absSrcPath = path.resolve(srcPath) - const srcFileName = path.basename(srcPath) + const absSrcPath = path.resolve(srcPath) + const absDestPath = path.resolve(destDir) + const srcFileName = path.basename(srcPath) + const targetPath = path.join(absDestPath, srcFileName) - const absDestPath = path.resolve(destDir) - const destPath = path.join(absDestPath, srcFileName) - - await rename(absSrcPath, destPath) - console.log(`File moved successfully`) - } catch (error) { - throw new Error('Failed to move file') - } + await rename(absSrcPath, targetPath) + console.log(`File moved successfully`) } export async function rm(filePath) { @@ -104,14 +78,9 @@ export async function rm(filePath) { throw new Error('File path argument is required') } - try { - const absFilePath = path.resolve(filePath) - - await unlink(absFilePath) - console.log(`File deleted successfully`) - } catch (error) { - throw new Error('Failed to delete file') - } + const absFilePath = path.resolve(filePath) + await unlink(absFilePath) + console.log(`File deleted successfully`) } export async function mkdir(nameDir) { @@ -119,10 +88,6 @@ export async function mkdir(nameDir) { throw new Error('Name directory is required') } - try { - await creteDirectory(nameDir) - console.log(`Directory successfully created`) - } catch (error) { - throw new Error('Failed to create directory') - } + await creteDirectory(nameDir) + console.log(`Directory successfully created`) } From e58f772e73a8cd52c94847d44a2ce4d2482a43da Mon Sep 17 00:00:00 2001 From: user Date: Mon, 3 Nov 2025 19:01:46 +0300 Subject: [PATCH 13/14] refactor: hash and compress functions --- src/commands/compress.js | 40 ++++++++++++++++------------------------ src/commands/hash.js | 23 +++++++++-------------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/commands/compress.js b/src/commands/compress.js index 1e1a285f15..bda6898a92 100644 --- a/src/commands/compress.js +++ b/src/commands/compress.js @@ -8,19 +8,15 @@ export async function compress(srcPath, dstPath) { throw new Error('Both arguments is required') } - try { - const absSrcPath = path.resolve(srcPath) - const absDstPath = path.resolve(dstPath) - - const source = createReadStream(absSrcPath) - const destination = createWriteStream(absDstPath) - const gzip = createGzip() - - await pipeline(source, gzip, destination) - console.log(`Compressed successfully to ${absDstPath}`) - } catch (error) { - throw new Error('Failed to compress file') - } + const absSrcPath = path.resolve(srcPath) + const absDstPath = path.resolve(dstPath) + + const source = createReadStream(absSrcPath) + const destination = createWriteStream(absDstPath) + const gzip = createGzip() + + await pipeline(source, gzip, destination) + console.log(`Compressed successfully to ${absDstPath}`) } export async function decompress(srcPath, dstPath) { @@ -28,18 +24,14 @@ export async function decompress(srcPath, dstPath) { throw new Error('Both arguments are required') } - try { - const absSrcPath = path.resolve(srcPath) - const absDstPath = path.resolve(dstPath) + const absSrcPath = path.resolve(srcPath) + const absDstPath = path.resolve(dstPath) - const source = createReadStream(absSrcPath) - const destination = createWriteStream(absDstPath) - const gunZlib = createGunzip() + const source = createReadStream(absSrcPath) + const destination = createWriteStream(absDstPath) + const gunZlib = createGunzip() - await pipeline(source, gunZlib, destination) + await pipeline(source, gunZlib, destination) - console.log(`Decompressed successfully to ${absDstPath}`) - } catch (error) { - throw new Error('Failed to decompress file') - } + console.log(`Decompressed successfully to ${absDstPath}`) } diff --git a/src/commands/hash.js b/src/commands/hash.js index 01e4e95a18..c56f5a04be 100644 --- a/src/commands/hash.js +++ b/src/commands/hash.js @@ -7,21 +7,16 @@ export async function hash(filePath) { throw new Error('File path is required') } - try { - const absFilePath = path.resolve(filePath) - const rdStream = createReadStream(absFilePath) + const absFilePath = path.resolve(filePath) + const rdStream = createReadStream(absFilePath) - const hash = createHash('sha256') + const hash = createHash('sha256') - for await (const chunk of rdStream) { - hash.update(chunk) - } - - const hex = hash.digest('hex') - console.log(`SHA-256 hash: ${hex}`) - return hex - } catch (error) { - console.log('error-new', error) - throw new Error('Failed to calculate hash') + for await (const chunk of rdStream) { + hash.update(chunk) } + + const hex = hash.digest('hex') + console.log(`SHA-256 hash: ${hex}`) + return hex } From db7e93ea3031b9530d4d9efef80712d97a97182e Mon Sep 17 00:00:00 2001 From: user Date: Mon, 3 Nov 2025 19:23:45 +0300 Subject: [PATCH 14/14] fix: rm function && error handler interface --- src/cli/interface.js | 6 ++++++ src/index.js | 1 + 2 files changed, 7 insertions(+) diff --git a/src/cli/interface.js b/src/cli/interface.js index a6b3c359ae..a24793ce37 100644 --- a/src/cli/interface.js +++ b/src/cli/interface.js @@ -20,6 +20,12 @@ export function createCLIInterface(username, commandHandler) { await commandHandler(command) } catch (error) { console.log('Operation failed') + + if (error.code === 'ENOENT') { + console.log('ENOENT: no such file or directory') + } else { + console.log(error.message) + } } } diff --git a/src/index.js b/src/index.js index 4cd2b1bdda..5235d9035c 100644 --- a/src/index.js +++ b/src/index.js @@ -41,6 +41,7 @@ async function handleCommand(command) { break case 'rm': await rm(args[0], args[1]) + break case 'mkdir': await mkdir(args[0]) break