From e4985e68aadbd6d497368dfd8aa6d09733b229b6 Mon Sep 17 00:00:00 2001 From: Alla Tsaiukova Date: Sun, 26 Oct 2025 13:55:08 +0100 Subject: [PATCH 1/4] feat: add solutions for cli-tasks --- src/cli/args.js | 8 +++++++- src/cli/env.js | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..fa60645c0a 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,11 @@ const parseArgs = () => { - // Write your code here + const args = process.argv.slice(2); + for (let i = 0; i < args.length; i += 2) { + const key = args[i].replace('--', ''); + const value = args[i + 1]; + + console.log(`${key} is ${value}`); + } }; parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..ea6a500a04 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,11 @@ const parseEnv = () => { - // Write your code here + let envArray = Object.entries(process.env); + let envVariables = envArray + .filter(([key]) => key.startsWith('RSS_')) + .map(([key, value]) => `${key}=${value}`) + .join('; '); + + console.log(envVariables); }; parseEnv(); From c000d15e12dce2b2fddb216ef6bdcf5e8eb2e32c Mon Sep 17 00:00:00 2001 From: Alla Tsaiukova Date: Sun, 26 Oct 2025 23:21:32 +0100 Subject: [PATCH 2/4] feat: add solutions for fs-tasks --- src/fs/copy.js | 34 +++++++++++++++++++++++++++++++++- src/fs/create.js | 20 +++++++++++++++++++- src/fs/delete.js | 17 ++++++++++++++++- src/fs/list.js | 17 ++++++++++++++++- src/fs/read.js | 16 +++++++++++++++- src/fs/rename.js | 27 ++++++++++++++++++++++++++- 6 files changed, 125 insertions(+), 6 deletions(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..307e873e78 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,37 @@ +import { access, constants, mkdir, readdir, copyFile } from 'node:fs/promises'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const copy = async () => { - // Write your code here + const sourceDir = join(__dirname, 'files'); + const copiedDir = join(__dirname, 'files_copy'); + + try { + await access(sourceDir, constants.F_OK); + + try { + await access(copiedDir, constants.F_OK); + throw new Error('FS operation failed'); + } catch (error) { + if (error.code !== 'ENOENT') throw error; + } + + await mkdir(copiedDir, { recursive: true }); + + const files = await readdir(sourceDir); + + for (const file of files) { + const srcPath = join(sourceDir, file); + const newPath = join(copiedDir, file); + await copyFile(srcPath, newPath); + } + + } catch { + throw new Error('FS operation failed'); + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..8f015cd2b5 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,23 @@ +import { join, dirname } from 'node:path'; +import { access, constants, writeFile } from 'node:fs/promises'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const create = async () => { - // Write your code here + const data = 'I am fresh and young'; + const filePath = join(__dirname, 'files', 'fresh.txt'); + try { + await access(filePath, constants.F_OK); + throw new Error('FS operation failed'); + } catch (error) { + if (error.code === 'ENOENT') { + await writeFile(filePath, data); + } else { + throw new Error('FS operation failed'); + } + } }; await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..f869342613 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,20 @@ +import { unlink, access, constants } from 'node:fs/promises'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const remove = async () => { - // Write your code here + const filePath = join(__dirname, 'files', 'fileToRemove.txt'); + + try { + await access(filePath, constants.F_OK); + + await unlink(filePath); + } catch (error) { + throw new Error('FS operation failed'); + } }; await remove(); diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..b1f43a3928 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,20 @@ +import { readdir, access, constants } from 'node:fs/promises'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const list = async () => { - // Write your code here + const folderPath = join(__dirname, 'files'); + + try { + await access(folderPath, constants.F_OK); + const files = await readdir(folderPath); + console.log(files); + } catch { + throw new Error('FS operation failed'); + } }; await list(); diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..b41d5b8081 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,19 @@ +import { readFile } from 'node:fs/promises'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const read = async () => { - // Write your code here + const filePath = join(__dirname, 'files', 'fileToRead.txt'); + + try { + const content = await readFile(filePath, 'utf-8'); + console.log(content); + } catch { + throw new Error('FS operation failed'); + } }; await read(); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..7da11eef66 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,30 @@ +import { rename, access, constants } from 'node:fs/promises'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const rename = async () => { - // Write your code here + const oldPath = join(__dirname, 'files', 'wrongFilename.txt'); + const newPath = join(__dirname, 'files', 'properFilename.md'); + + try { + await access(oldPath, constants.F_OK); + + try { + await access(newPath, constants.F_OK); + throw new Error('FS operation failed'); + } catch (error) { + if (error.code !== 'ENOENT') { + throw new Error('FS operation failed'); + } + } + + await rename(oldPath, newPath); + } catch (error) { + throw new Error('FS operation failed'); + } }; await rename(); From 0238ab84d9dd5bb9e395afafe982808fc8cb2fe0 Mon Sep 17 00:00:00 2001 From: Alla Tsaiukova Date: Mon, 27 Oct 2025 00:11:26 +0100 Subject: [PATCH 3/4] feat: add solution for hash-task --- src/hash/calcHash.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..29c860c0bc 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,23 @@ +import { createHash } from 'node:crypto'; +import { readFile } from 'node:fs/promises'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const calculateHash = async () => { - // Write your code here + const filePath = join(__dirname, 'files', 'fileToCalculateHashFor.txt'); + + try { + const data = await readFile(filePath, 'utf-8'); + const hash = createHash('sha256'); + hash.update(data); + const digest = hash.digest('hex'); + console.log(digest); + } catch { + throw new Error('FS operation failed'); + } }; await calculateHash(); From a246ea09b65f3b77e9346f5bf5b70970c8ef0f50 Mon Sep 17 00:00:00 2001 From: Alla Tsaiukova Date: Mon, 27 Oct 2025 00:56:54 +0100 Subject: [PATCH 4/4] feat: add solutions for streams-tasks --- src/streams/read.js | 18 +++++++++++++++++- src/streams/transform.js | 11 ++++++++++- src/streams/write.js | 20 +++++++++++++++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..25316ed345 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,21 @@ +import { createReadStream } from 'node:fs'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const read = async () => { - // Write your code here + const filePath = join(__dirname, 'files', 'fileToRead.txt'); + const stream = createReadStream(filePath, { encoding: 'utf-8' }); + + stream.on('data', (chunk) => { + process.stdout.write(chunk); + }); + + stream.on('error', () => { + console.error('FS operation failed'); + }); }; await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..934120560e 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,14 @@ +import { Transform } from 'node:stream'; + const transform = async () => { - // Write your code here + const reversedStream = new Transform({ + transform(chunk, _encoding, callback) { + const reversed = chunk.toString().split('').reverse().join(''); + callback(null, reversed); + } + }); + + process.stdin.pipe(reversedStream).pipe(process.stdout); }; await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..3ee6c964a2 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,23 @@ +import { createWriteStream } from 'node:fs'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const write = async () => { - // Write your code here + const filePath = join(__dirname, 'files', 'fileToWrite.txt'); + const writableStream = createWriteStream(filePath); + + process.stdin.pipe(writableStream); + + writableStream.on('finish', () => { + console.log('Writing finished'); + }); + + writableStream.on('error', () => { + console.error('FS operation failed'); + }); }; await write();