From 1e696045d8d9227a144e38019860d1806f24c19a Mon Sep 17 00:00:00 2001 From: Asta Date: Sun, 26 Oct 2025 16:55:40 +0200 Subject: [PATCH 1/5] feat: add fs folder solutions --- package-lock.json | 17 +++++++++++++++ src/fs/copy.js | 15 ++++++++++++- src/fs/create.js | 21 +++++++++++++++++-- src/fs/delete.js | 11 ++++++++-- src/fs/files/fileToRemove.txt | 1 - src/fs/files/fresh.txt | 1 + .../{wrongFilename.txt => properFilename.md} | 0 src/fs/files_copy/dontLookAtMe.txt | 1 + src/fs/files_copy/fileToRead.txt | 7 +++++++ src/fs/files_copy/hello.txt | 1 + src/fs/files_copy/properFilename.md | 3 +++ src/fs/list.js | 12 ++++++++++- src/fs/read.js | 11 +++++++++- src/fs/rename.js | 9 +++++++- 14 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 package-lock.json delete mode 100644 src/fs/files/fileToRemove.txt create mode 100644 src/fs/files/fresh.txt rename src/fs/files/{wrongFilename.txt => properFilename.md} (100%) create mode 100644 src/fs/files_copy/dontLookAtMe.txt create mode 100644 src/fs/files_copy/fileToRead.txt create mode 100644 src/fs/files_copy/hello.txt create mode 100644 src/fs/files_copy/properFilename.md diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000000..34b7366dcf --- /dev/null +++ b/package-lock.json @@ -0,0 +1,17 @@ +{ + "name": "node-nodejs-basics", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "node-nodejs-basics", + "version": "1.0.0", + "license": "ISC", + "engines": { + "node": ">=24.10.0", + "npm": ">=10.9.2" + } + } + } +} diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..6131076429 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,18 @@ +import { promises as fs } from 'fs'; +import { readdir } from 'node:fs/promises'; + const copy = async () => { - // Write your code here + try { + const copyFolderCheckFiles = await readdir('src/fs/files'); + if (copyFolderCheckFiles){ + await fs.cp('src/fs/files', 'src/fs/files_copy', {errorOnExist: true, recursive: true, force: false}) + } else { + throw new Error('FS operation failed') + } + } + catch(err){ + throw new Error('FS operation failed') + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..2ffcf73404 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,22 @@ +import { promises as fs } from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const fileName = 'fresh.txt' +const text = 'I am fresh and young'; +// it could be simplified by providing manually constructed path, example 'src/fs/files' +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const fileToWrite = path.join(__dirname, 'files', fileName); + const create = async () => { - // Write your code here -}; + try { + await fs.writeFile(fileToWrite, text, {flag: 'wx'}); + } + catch(error) { + throw new Error('FS operation failed'); + } +} await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..bd0ab31b75 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,12 @@ +import { promises as fs } from 'fs'; + const remove = async () => { - // Write your code here + try { + await fs.rm('src/fs/files/fileToRemove.txt'); + } + catch(error) { + throw new Error('FS operation failed') + } }; -await remove(); +await remove() 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/fresh.txt b/src/fs/files/fresh.txt new file mode 100644 index 0000000000..205d704cb7 --- /dev/null +++ b/src/fs/files/fresh.txt @@ -0,0 +1 @@ +I am fresh and young \ No newline at end of file diff --git a/src/fs/files/wrongFilename.txt b/src/fs/files/properFilename.md similarity index 100% rename from src/fs/files/wrongFilename.txt rename to src/fs/files/properFilename.md diff --git a/src/fs/files_copy/dontLookAtMe.txt b/src/fs/files_copy/dontLookAtMe.txt new file mode 100644 index 0000000000..8979bab743 --- /dev/null +++ b/src/fs/files_copy/dontLookAtMe.txt @@ -0,0 +1 @@ +What are you looking at?! \ No newline at end of file diff --git a/src/fs/files_copy/fileToRead.txt b/src/fs/files_copy/fileToRead.txt new file mode 100644 index 0000000000..5d66c332d6 --- /dev/null +++ b/src/fs/files_copy/fileToRead.txt @@ -0,0 +1,7 @@ +My content +should +be +printed +into +console +! \ No newline at end of file diff --git a/src/fs/files_copy/hello.txt b/src/fs/files_copy/hello.txt new file mode 100644 index 0000000000..4e65f7775f --- /dev/null +++ b/src/fs/files_copy/hello.txt @@ -0,0 +1 @@ +Hello Node.js \ No newline at end of file diff --git a/src/fs/files_copy/properFilename.md b/src/fs/files_copy/properFilename.md new file mode 100644 index 0000000000..38cca5db19 --- /dev/null +++ b/src/fs/files_copy/properFilename.md @@ -0,0 +1,3 @@ +# 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 index 0c0fa21f7e..45515cef54 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,15 @@ +import { promises as fs } from 'fs'; + const list = async () => { - // Write your code here + try { + const files = await fs.readdir('src/fs/files') + const arr = []; + files.map(file => arr.push(file)) + console.log(arr) + } + catch { + throw new Error('FS operation failed') + } }; await list(); diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..991d99000a 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,14 @@ +import { promises as fs } from 'fs'; + + const read = async () => { - // Write your code here + try { + const content = await fs.readFile('src/fs/files/fileToRead.txt', 'utf8') + console.log(content) + } + catch (err){ + throw new Error('FS operation failed') + } }; await read(); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..8a4070fe6b 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,12 @@ +import { promises as fs } from 'fs'; + const rename = async () => { - // Write your code here + try { + await fs.rename('src/fs/files/wrongFilename.txt', 'src/fs/files/properFilename.md') + } + catch { + throw new Error('FS operation failed') + } }; await rename(); From e62d41d588ad3fe6be56ccade104e5f21c116dcb Mon Sep 17 00:00:00 2001 From: Asta Date: Sun, 26 Oct 2025 20:40:14 +0200 Subject: [PATCH 2/5] feat: add cli solutions --- src/cli/args.js | 13 +++++++++++-- src/cli/env.js | 9 ++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..969099c276 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,14 @@ const parseArgs = () => { - // Write your code here -}; + const res = process.argv.slice(2); + // considering that all pairs have both keys and values + const length = res.length / 2 + const keys = res.filter((arg, i) => i % 2 === 0 && arg.startsWith('--')) + const cleanKeys = keys.map(key => key.replace(/^--/, '')) + const values = res.filter((arg, i) => i % 2 === 1 && !arg.startsWith('--') ) + for (let i = 0; i < length; i++) + if (cleanKeys !== undefined && values !== undefined) + console.log(`${cleanKeys[i]} is ${values[i]}`) + +} parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..6d9aa69428 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,12 @@ const parseEnv = () => { - // Write your code here + const objEnv = process.env + const arr = [] + for (const [key, value] of Object.entries(objEnv)) { + if (key.startsWith('RSS_')){ + arr.push(`${key}=${value}`) + } + } + console.log(arr.join('; ')) }; parseEnv(); From 7da54022cb65582fc52e53b59b7a0debf1ec1f98 Mon Sep 17 00:00:00 2001 From: Asta Date: Sun, 26 Oct 2025 21:42:55 +0200 Subject: [PATCH 3/5] fix: undo file changes after running commands --- src/fs/files/fileToRemove.txt | 1 + src/fs/files/fresh.txt | 1 - src/fs/files/{properFilename.md => wrongFilename.txt} | 0 src/fs/files_copy/dontLookAtMe.txt | 1 - src/fs/files_copy/fileToRead.txt | 7 ------- src/fs/files_copy/hello.txt | 1 - src/fs/files_copy/properFilename.md | 3 --- 7 files changed, 1 insertion(+), 13 deletions(-) create mode 100644 src/fs/files/fileToRemove.txt delete mode 100644 src/fs/files/fresh.txt rename src/fs/files/{properFilename.md => wrongFilename.txt} (100%) delete mode 100644 src/fs/files_copy/dontLookAtMe.txt delete mode 100644 src/fs/files_copy/fileToRead.txt delete mode 100644 src/fs/files_copy/hello.txt delete mode 100644 src/fs/files_copy/properFilename.md diff --git a/src/fs/files/fileToRemove.txt b/src/fs/files/fileToRemove.txt new file mode 100644 index 0000000000..181e577680 --- /dev/null +++ b/src/fs/files/fileToRemove.txt @@ -0,0 +1 @@ +How dare you!hhhhhhhhhhhhh \ No newline at end of file diff --git a/src/fs/files/fresh.txt b/src/fs/files/fresh.txt deleted file mode 100644 index 205d704cb7..0000000000 --- a/src/fs/files/fresh.txt +++ /dev/null @@ -1 +0,0 @@ -I am fresh and young \ No newline at end of file diff --git a/src/fs/files/properFilename.md b/src/fs/files/wrongFilename.txt similarity index 100% rename from src/fs/files/properFilename.md rename to src/fs/files/wrongFilename.txt diff --git a/src/fs/files_copy/dontLookAtMe.txt b/src/fs/files_copy/dontLookAtMe.txt deleted file mode 100644 index 8979bab743..0000000000 --- a/src/fs/files_copy/dontLookAtMe.txt +++ /dev/null @@ -1 +0,0 @@ -What are you looking at?! \ No newline at end of file diff --git a/src/fs/files_copy/fileToRead.txt b/src/fs/files_copy/fileToRead.txt deleted file mode 100644 index 5d66c332d6..0000000000 --- a/src/fs/files_copy/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_copy/hello.txt b/src/fs/files_copy/hello.txt deleted file mode 100644 index 4e65f7775f..0000000000 --- a/src/fs/files_copy/hello.txt +++ /dev/null @@ -1 +0,0 @@ -Hello Node.js \ No newline at end of file diff --git a/src/fs/files_copy/properFilename.md b/src/fs/files_copy/properFilename.md deleted file mode 100644 index 38cca5db19..0000000000 --- a/src/fs/files_copy/properFilename.md +++ /dev/null @@ -1,3 +0,0 @@ -# This is a file with a wrong filename - -Hello from **markdown**! \ No newline at end of file From 2335a7363832732df2e997238e303b0df0874625 Mon Sep 17 00:00:00 2001 From: Asta Date: Sun, 26 Oct 2025 21:44:20 +0200 Subject: [PATCH 4/5] fix: remove typos --- src/fs/files/fileToRemove.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fs/files/fileToRemove.txt b/src/fs/files/fileToRemove.txt index 181e577680..43e64cd45c 100644 --- a/src/fs/files/fileToRemove.txt +++ b/src/fs/files/fileToRemove.txt @@ -1 +1 @@ -How dare you!hhhhhhhhhhhhh \ No newline at end of file +How dare you! \ No newline at end of file From 31509e639470c67262a7a8959c45a07b435101c4 Mon Sep 17 00:00:00 2001 From: Asta Date: Sun, 26 Oct 2025 22:21:50 +0200 Subject: [PATCH 5/5] feat: add modules solution --- src/modules/cjsToEsm.cjs | 34 ---------------------------- src/modules/esm.mjs | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 34 deletions(-) delete mode 100644 src/modules/cjsToEsm.cjs create mode 100644 src/modules/esm.mjs 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/esm.mjs b/src/modules/esm.mjs new file mode 100644 index 0000000000..c53bbe85e9 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,48 @@ +import path, { dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { readFile } from 'fs/promises'; +import { release, version } from 'node:os'; +import { createServer } from 'node:http'; + +import './files/c.cjs'; +const filesA = JSON.parse( + await readFile( + new URL('./files/a.json', import.meta.url) + ) +); +const filesB = JSON.parse( + await readFile( + new URL('./files/b.json', import.meta.url) + ) +); + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const random = Math.random(); + +const unknownObject = random > 0.5 ? filesA : filesB; + +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 = createServer((_, 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'); +}); + +export { + unknownObject, + myServer, +};