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/cli/args.js b/src/cli/args.js index 9e3622f791..1b549d4a90 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 propName = args[i].slice(2); + const value = args[i + 1] + console.log(`${propName} is ${value}`) + } }; parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..78d564cc9d 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,9 @@ const parseEnv = () => { - // Write your code here + const variables = process.env; + const filterRSS = Object.keys(variables).filter((key) => key.startsWith('RSS_')); + const result = filterRSS.map((el) => `${el} = ${variables[el]}`); + + console.log(result); }; parseEnv(); diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..4db291172c 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,32 @@ +import fsPromises from 'fs/promises'; + +const filePath = './src/fs/files'; +const filesTo = './src/fs/files_copy' + const copy = async () => { - // Write your code here + try { + await fsPromises.access(filePath); + + try { + await fsPromises.access(filesTo); + throw new Error('FS operation failed') + } catch (error) { + if (error.code && error.code !== 'ENOENT') { + throw new Error('FS operation failed') + } + } + const filesInsideFilesFolder = await fsPromises.readdir(filePath); + await fsPromises.mkdir(filesTo); + + for (let file of filesInsideFilesFolder) { + const from = `${filePath}/${file}`; + const to = `${filesTo}/${file}` + + await fsPromises.copyFile(from, to); + } + } catch { + throw new Error('FS operation failed') + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..473810787d 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,20 @@ +import fsPromises from 'fs/promises'; + +const filePath = './src/fs/files/fresh.txt'; +const message = 'I am fresh and young'; + const create = async () => { - // Write your code here + try { + await fsPromises.access(filePath); + throw new Error('FS operation failed') + } + catch (error) { + if (error.code === 'ENOENT') { + await fsPromises.writeFile(filePath, message); + } else { + throw error; + } + } }; await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..1ffd13a969 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,16 @@ + +import fsPromises from 'fs/promises'; +const pathFile = './src/fs/files/fileToRemove.txt'; + const remove = async () => { - // Write your code here -}; + try { + await fsPromises.access(pathFile); + await fsPromises.unlink(pathFile); + } catch (error) { + if (error.code === 'ENOENT') { + throw new Error('FS operation failed'); + } + } + }; await remove(); diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..3a5f5b46db 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,17 @@ +import fsPromises from 'fs/promises'; +const path = './src/fs/files'; + const list = async () => { - // Write your code here + try { + await fsPromises.access(path); + + const filesInsideFilesFolder = await fsPromises.readdir(path); + console.log(filesInsideFilesFolder); + } catch (error) { + if (error.code === 'ENOENT') { + throw new Error('FS operation failed'); + } + } }; await list(); diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..9c828c5f11 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,15 @@ +import fsPromises from 'fs/promises'; +const path = './src/fs/files/fileToRead.txt'; + const read = async () => { - // Write your code here + try { + const fileContent = await fsPromises.readFile(path, { encoding: 'utf8' }); + console.log(fileContent); + } catch (error) { + if (error.code === 'ENOENT') { + throw new Error('FS operation failed'); + } + } }; await read(); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..0a3436f0c5 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,26 @@ +import fsPromises from 'fs/promises'; +const wrongFilePath = './src/fs/files/wrongFilename.txt'; +const properFilePath = './src/fs/files/properFilename.md'; + const rename = async () => { - // Write your code here + try { + await fsPromises.access(wrongFilePath); + } + catch (error) { + if (error.code === 'ENOENT') { + throw new Error('FS operation failed'); + } + } + + try { + await fsPromises.access(properFilePath); + throw new Error('FS operation failed'); + } catch (error) { + if (error.code !== 'ENOENT') { + throw new Error('FS operation failed'); + } + } + await fsPromises.rename(wrongFilePath, properFilePath); }; await rename(); diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..8631c53b0b 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,19 @@ +import fs from 'fs'; +import crypto from 'crypto'; + +const filePath = './src/hash/files/fileToCalculateHashFor.txt'; + const calculateHash = async () => { - // Write your code here + const hash = crypto.createHash('sha256'); + const readStream = fs.createReadStream(filePath) + + readStream.on('data', (chunk) => { + hash.update(chunk); + }) + readStream.on('end', () => { + const result = hash.digest('hex'); + console.log(result); + }) }; await calculateHash(); diff --git a/src/modules/cjsToEsm.cjs b/src/modules/esm.mjs similarity index 53% rename from src/modules/cjsToEsm.cjs rename to src/modules/esm.mjs index 089bd2db13..50df53f827 100644 --- a/src/modules/cjsToEsm.cjs +++ b/src/modules/esm.mjs @@ -1,12 +1,18 @@ -const path = require('node:path'); -const { release, version } = require('node:os'); -const { createServer: createServerHttp } = require('node:http'); +import path from 'path'; +import { release, version } from 'node:os'; +import { createServer as createServerHttp }from 'node:http'; +import { fileURLToPath } from 'url'; -require('./files/c.cjs'); +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +import a from './files/a.json' with { type: "json" }; +import b from './files/b.json' with { type: "json" }; +const c = await import('./files/c.cjs'); const random = Math.random(); -const unknownObject = random > 0.5 ? require('./files/a.json') : require('./files/b.json'); +const unknownObject = random > 0.5 ? a : b; console.log(`Release ${release()}`); console.log(`Version ${version()}`); @@ -28,7 +34,4 @@ myServer.listen(PORT, () => { console.log('To terminate it, use Ctrl+C combination'); }); -module.exports = { - unknownObject, - myServer, -}; +export {unknownObject, myServer} diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..e87cf42805 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,23 @@ +import fs from 'fs'; +import { once } from 'events'; + +const filePath = './src/streams/files/fileToRead.txt'; + const read = async () => { - // Write your code here + try { + const readStream = fs.createReadStream(filePath, { encoding: 'utf-8' }); + + readStream.on('error', (error) => { + throw error; +}); + + readStream.pipe(process.stdout); + await once(readStream, 'end'); + + console.log('success'); + } catch (error) { + throw error; + } }; await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..a08dbccfb0 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,17 @@ +import { Transform } from 'stream'; + const transform = async () => { - // Write your code here + const streamTransform = new Transform({transform(chunk, encoding, callback) { + const reversedText = chunk.toString().split('').reverse().join(''); + + this.push(reversedText); + callback(); +} +}) +console.log('Type something. Press Ctrl+D or Ctrl+C to exit.'); + +process.stdin.pipe(streamTransform); +streamTransform.pipe(process.stdout); }; await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..7eb67be8c6 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,19 @@ +import fs from 'fs'; +import { once } from 'events'; + +const filePath = './src/streams/files/fileToWrite.txt'; + const write = async () => { - // Write your code here + try { + console.log('Type something. Press Ctrl+D or Ctrl+C to exit.'); + + const writeStream = fs.createWriteStream(filePath); + + process.stdin.pipe(writeStream); + await once(writeStream, 'finish') + } catch (error) { + console.error(error); + } }; await write(); diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..2a8c6d09fb 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,28 @@ +import fs from 'fs'; +import zlib from 'zlib'; +import { once } from 'events'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const fileName = fileURLToPath(import.meta.url); +const dirName = path.dirname(fileName); + +const filePath = path.join(dirName, 'files', 'fileToCompress.txt'); +const archivePath = path.join(dirName, 'files', 'archive.gz'); + const compress = async () => { - // Write your code here + try { + const readStream = fs.createReadStream(filePath); + const writeStream = fs.createWriteStream(archivePath); + const gzip = zlib.createGzip(); + + readStream.pipe(gzip); + gzip.pipe(writeStream); + + await once(writeStream, 'finish'); + } catch (error) { + console.error(error); + } }; await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..f961330af2 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,28 @@ +import fs from 'fs'; +import zlib from 'zlib'; +import { once } from 'events'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const fileName = fileURLToPath(import.meta.url); +const dirName = path.dirname(fileName); + +const filePath = path.join(dirName, 'files', 'fileToCompress.txt'); +const archivePath = path.join(dirName, 'files', 'archive.gz'); + const decompress = async () => { - // Write your code here + try { + const readStream = fs.createReadStream(archivePath); + const writeStream = fs.createWriteStream(filePath); + const gunzip = zlib.createGunzip(); + + readStream.pipe(gunzip); + gunzip.pipe(writeStream); + + await once(writeStream, 'finish'); + } catch (error) { + console.error(error); + } }; await decompress();