diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..709a5590ca 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,18 @@ const parseArgs = () => { - // Write your code here + const resArray = []; + const args = process.argv.slice(2); + + for (let i = 0; i < args.length; i++) { + if (args[i]?.startsWith('--')) { + if (args[i + 1]?.startsWith('--')) { + resArray.push(`${args[i]} undefined`); + continue; + } + resArray.push(`${args[i]} ${args[i + 1]}`); + } + } + + return resArray.join(' '); }; parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..31ec552486 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,12 @@ const parseEnv = () => { - // Write your code here + const resArray = []; + + for (const key in process.env) { + if (key.startsWith('RSS_')) { + resArray.push(`${key}=${process.env[key]}`); + } + } + return resArray.join('; '); }; parseEnv(); diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..a5394ecb68 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,34 @@ +import { readdir, copyFile, mkdir } from 'node:fs/promises'; +import { join } from 'path'; + +const CONFIG = { + dirName: 'files', + copyDirName: 'files_copy', + errorMessage: 'FS operation failed', +}; + +const __dirname = import.meta.dirname; +const targetDirPath = join(__dirname, CONFIG.dirName); +const copyDirPath = join(__dirname, CONFIG.copyDirName); + const copy = async () => { - // Write your code here + try { + await mkdir(copyDirPath); + } catch (error) { + throw new Error(CONFIG.errorMessage); + } + + try { + const files = await readdir(targetDirPath); + + for (const file of files) { + const targetFilePath = join(targetDirPath, file); + const copyFilePath = join(copyDirPath, file); + await copyFile(targetFilePath, copyFilePath); + } + } catch (err) { + throw new Error(CONFIG.errorMessage); + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..db4a46efeb 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,26 @@ +import { writeFile } from 'node:fs/promises'; +import { join } from 'path'; + +const CONFIG = { + file: 'fresh.txt', + dirName: 'files', + content: 'I am fresh and young', + errorMessage: 'FS operation failed', +}; + +const __dirname = import.meta.dirname; +const targetDirPath = join(__dirname, CONFIG.dirName); +const targetFilePath = join(targetDirPath, CONFIG.file); + const create = async () => { - // Write your code here + try { + await writeFile(targetFilePath, CONFIG.content, { + encoding: 'utf8', + flag: 'wx', + }); + } catch (err) { + throw new Error(CONFIG.errorMessage); + } }; await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..31b2cb56fa 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,23 @@ +import { unlink } from 'node:fs/promises'; +import { join } from 'path'; + +const CONFIG = { + file: 'fileToRemove.txt', + dirName: 'files', + errorMessage: 'FS operation failed', +}; + +const __dirname = import.meta.dirname; +const targetDirPath = join(__dirname, CONFIG.dirName); +const targetFilePath = join(targetDirPath, CONFIG.file); + const remove = async () => { - // Write your code here + try { + await unlink(targetFilePath); + console.log(`successfully deleted ${CONFIG.file}`); + } catch (error) { + throw new Error(CONFIG.errorMessage); + } }; await remove(); diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..5c1293ab26 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,21 @@ +import { readdir } from 'node:fs/promises'; +import { join } from 'path'; + +const CONFIG = { + dirName: 'files', + errorMessage: 'FS operation failed', +}; + +const __dirname = import.meta.dirname; +const targetDirPath = join(__dirname, CONFIG.dirName); + const list = async () => { - // Write your code here + try { + const files = await readdir(targetDirPath); + console.log(files); + } catch (err) { + throw new Error(CONFIG.errorMessage); + } }; await list(); diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..754c5e514c 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,23 @@ +import { readFile } from 'node:fs/promises'; +import { join } from 'path'; + +const CONFIG = { + file: 'fileToRead.txt', + dirName: 'files', + errorMessage: 'FS operation failed', +}; + +const __dirname = import.meta.dirname; +const targetDirPath = join(__dirname, CONFIG.dirName); +const targetFilePath = join(targetDirPath, CONFIG.file); + const read = async () => { - // Write your code here + try { + const content = await readFile(targetFilePath, { encoding: 'utf8' }); + console.log(content); + } catch (error) { + throw new Error(CONFIG.errorMessage); + } }; await read(); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..9bf1df8030 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,27 @@ +import { rename as renameFile } from 'node:fs/promises'; +import { join } from 'path'; + +const CONFIG = { + file: 'wrongFilename.txt', + newFile: 'properFilename.txt', + dirName: 'files', + errorMessage: 'FS operation failed', +}; + +const __dirname = import.meta.dirname; +const targetDirPath = join(__dirname, CONFIG.dirName); +const targetOldFilePath = join(targetDirPath, CONFIG.file); +const targetNewFilePath = join(targetDirPath, CONFIG.newFile); + const rename = async () => { - // Write your code here + try { + await renameFile(targetOldFilePath, targetNewFilePath); + console.log( + `successfully renamed from ${CONFIG.file} to ${CONFIG.newFile}` + ); + } catch (error) { + throw new Error(CONFIG.errorMessage); + } }; await rename(); diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..85aaf102ce 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,25 @@ +import { createHash } from 'node:crypto'; +import { createReadStream } from 'node:fs'; +import { stdout } from 'node:process'; +import { join } from 'node:path'; + +const CONFIG = { + file: 'fileToCalculateHashFor.txt', + dirName: 'files', +}; + +const __dirname = import.meta.dirname; +const targetFilePath = join(__dirname, CONFIG.dirName, CONFIG.file); + const calculateHash = async () => { - // Write your code here + const hash = createHash('sha256'); + + const input = createReadStream(targetFilePath); + input.pipe(hash); + input.on('end', () => { + const finalHash = hash.digest('hex'); + console.log(finalHash); + }); }; await calculateHash(); diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs new file mode 100644 index 0000000000..9a953b0b27 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,47 @@ +import path from 'node:path'; +import { release, version } from 'node:os'; +import { createServer as createServerHttp } from 'node:http'; +import { join } from 'path'; +import { readFileSync } from 'node:fs'; + +import './files/c.cjs'; + +const __dirname = import.meta.dirname; +const __filename = import.meta.filename; + +const random = Math.random(); + +const getFileContent = (filePath) => { + try { + const fullPath = join(__dirname, 'files', filePath); + const fileContent = readFileSync(fullPath); + return JSON.parse(fileContent); + } catch (err) { + console.log(err); + } +}; + +const unknownObject = + random > 0.5 ? getFileContent('a.json') : getFileContent('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'); +}); + +export { unknownObject, myServer }; diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..614e7c051c 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,26 @@ +import { createReadStream } from 'node:fs'; +import { stdout } from 'node:process'; +import { join } from 'node:path'; + +const CONFIG = { + file: 'fileToRead.txt', + dirName: 'files', +}; + +const __dirname = import.meta.dirname; +const targetFilePath = join(__dirname, CONFIG.dirName, CONFIG.file); + const read = async () => { - // Write your code here + const input = createReadStream(targetFilePath); + + input.on('error', (err) => { + console.error(err); + }); + input.pipe(stdout, { end: false }); + input.on('end', () => { + stdout.write('\n'); + stdout.end(); + }); }; await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..8f2f81a682 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,15 @@ +import { Transform } from 'node:stream'; +import { stdout, stdin } from 'node:process'; + const transform = async () => { - // Write your code here + const transformStream = new Transform({ + transform(chunk, encoding, callback) { + const upperCaseData = chunk.toString().split('').reverse().join(''); + callback(null, upperCaseData); + }, + }); + + stdin.pipe(transformStream).pipe(stdout); }; await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..63103fe33b 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,23 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import { stdin } from 'node:process'; +import { join } from 'node:path'; + +const CONFIG = { + file: 'fileToWrite.txt', + dirName: 'files', +}; + +const __dirname = import.meta.dirname; +const targetFilePath = join(__dirname, CONFIG.dirName, CONFIG.file); + const write = async () => { - // Write your code here + const input = createWriteStream(targetFilePath); + + input.on('error', (err) => { + console.error(err); + }); + + stdin.pipe(input); }; await write(); diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..cae02c40a7 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,29 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import { join } from 'node:path'; +import { createGzip } from 'node:zlib'; +import { pipeline } from 'node:stream/promises'; + +const CONFIG = { + fileFrom: 'fileToCompress.txt', + fileTo: 'archive.gz', + dirName: 'files', +}; + +const __dirname = import.meta.dirname; +const targetFileFromPath = join(__dirname, CONFIG.dirName, CONFIG.fileFrom); +const targetFileToPath = join(__dirname, CONFIG.dirName, CONFIG.fileTo); + const compress = async () => { - // Write your code here + const gzip = createGzip(); + const sourceReadStream = createReadStream(targetFileFromPath); + const destinationWriteStream = createWriteStream(targetFileToPath); + + try { + await pipeline(sourceReadStream, gzip, destinationWriteStream); + } catch (err) { + console.error(err); + process.exitCode = 1; + } }; await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..07e5a5a908 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,29 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import { join } from 'node:path'; +import { createGunzip } from 'node:zlib'; +import { pipeline } from 'node:stream/promises'; + +const CONFIG = { + fileFrom: 'archive.gz', + fileTo: 'ttt.txt', + dirName: 'files', +}; + +const __dirname = import.meta.dirname; +const targetFileFromPath = join(__dirname, CONFIG.dirName, CONFIG.fileFrom); +const targetFileToPath = join(__dirname, CONFIG.dirName, CONFIG.fileTo); + const decompress = async () => { - // Write your code here + const gzip = createGunzip(); + const sourceReadStream = createReadStream(targetFileFromPath); + const destinationWriteStream = createWriteStream(targetFileToPath); + + try { + await pipeline(sourceReadStream, gzip, destinationWriteStream); + } catch (err) { + console.error(err); + process.exitCode = 1; + } }; await decompress();