From dd5e21af40418c633795619fef9923d66e6984dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=B8=D1=8F=20=D0=9C=D0=B0=D1=80=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 5 Jun 2022 21:53:02 +0300 Subject: [PATCH 1/3] implement some basic nodejs functions --- src/cli/args.js | 5 ++++- src/cli/env.js | 7 +++++-- src/cp/cp.js | 12 +++++++++++- src/fs/constants.js | 1 + src/fs/copy.js | 30 ++++++++++++++++++++++++++++-- src/fs/create.js | 17 +++++++++++++++-- src/fs/files/dontLookAtMe.txt | 1 - src/fs/files/fileToRead.txt | 7 ------- src/fs/files/fileToRemove.txt | 1 - src/fs/files/fresh.txt | 1 + src/fs/files/hello.txt | 1 - src/hash/calcHash.js | 17 +++++++++++++++-- src/modules/cjsToEsm.cjs | 31 ------------------------------- src/modules/cjsToEsm.mjs | 34 ++++++++++++++++++++++++++++++++++ src/streams/read.js | 12 ++++++++++-- src/streams/transform.js | 22 ++++++++++++++++++++-- src/streams/write.js | 14 ++++++++++++-- src/wt/main.js | 23 +++++++++++++++++++++-- src/wt/worker.js | 11 +++++++---- src/zip/compress.js | 21 +++++++++++++++++++-- src/zip/decompress.js | 21 +++++++++++++++++++-- src/zip/files/archive.gz | Bin 0 -> 20 bytes 22 files changed, 222 insertions(+), 67 deletions(-) create mode 100644 src/fs/constants.js create mode 100644 src/fs/files/fresh.txt delete mode 100644 src/modules/cjsToEsm.cjs create mode 100644 src/modules/cjsToEsm.mjs create mode 100644 src/zip/files/archive.gz diff --git a/src/cli/args.js b/src/cli/args.js index d5d723807d..68c8ee17d9 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,3 +1,6 @@ export const parseArgs = () => { - // Write your code here + const args = process.argv; + for (let argKey of Object.keys(args)) { + console.log(`${argKey} is ${args[argKey]}`) + } }; \ No newline at end of file diff --git a/src/cli/env.js b/src/cli/env.js index 30fbb5034c..72546ac291 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,3 +1,6 @@ export const parseEnv = () => { - // Write your code here -}; \ No newline at end of file + const keys = Object.keys(process.env).filter(key => key.slice(4)); + for (const key of keys) { + process.stdout.write(`${key}=${process.env[key]}\n`) + } +}; diff --git a/src/cp/cp.js b/src/cp/cp.js index b7f4e2c9d8..c02ff693ef 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,3 +1,13 @@ +import child_process from 'child_process'; +import path, {dirname} from "path"; +import {fileURLToPath} from "url"; + export const spawnChildProcess = async (args) => { - // Write your code here + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filePath = path.join(__dirname, 'files', 'script.js'); + + const child = child_process.fork(filePath, args) + + process.stdin.pipe(child.stdin); + child.stdout.pipe(process.stdin); }; \ No newline at end of file diff --git a/src/fs/constants.js b/src/fs/constants.js new file mode 100644 index 0000000000..9889a71e51 --- /dev/null +++ b/src/fs/constants.js @@ -0,0 +1 @@ +export const DEFAULT_ERROR_MESSAGE = 'FS operation failed.'; \ No newline at end of file diff --git a/src/fs/copy.js b/src/fs/copy.js index f134d98ac4..ffbdd9ab55 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,3 +1,29 @@ +import fs from 'fs'; +import path, {dirname} from 'path'; +import {fileURLToPath} from 'url'; +import {DEFAULT_ERROR_MESSAGE} from "./constants.js"; + + export const copy = async () => { - // Write your code here -}; \ No newline at end of file + const __dirname = dirname(fileURLToPath(import.meta.url)); + const srcPath = path.join(__dirname, 'files'); + const destPath = path.join(__dirname, 'files_copy'); + + if (fs.existsSync(destPath)) { + throw new Error(DEFAULT_ERROR_MESSAGE); + } else { + fs.mkdirSync(destPath); + } + + fs.readdir(srcPath, (err, files) => { + try { + Promise.all(files.map(file => { + return fs.promises.copyFile(path.join(srcPath, file), path.join(destPath, file)) + })) + } catch { + throw new Error(DEFAULT_ERROR_MESSAGE) + } + }) +}; + +copy(); \ No newline at end of file diff --git a/src/fs/create.js b/src/fs/create.js index 16a74c9a23..877a9468f9 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,3 +1,16 @@ +import fs from 'fs/promises'; +import path, { dirname } from 'path'; +import { fileURLToPath } from 'url'; +import { DEFAULT_ERROR_MESSAGE } from "./constants.js"; + + export const create = async () => { - // Write your code here -}; \ No newline at end of file + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filePath = path.join(__dirname, 'files', 'fresh.txt'); + const content = 'I am fresh and young'; + try { + await fs.writeFile(filePath, content, {flag: 'wx'}) + } catch { + throw new Error(DEFAULT_ERROR_MESSAGE) + } +}; diff --git a/src/fs/files/dontLookAtMe.txt b/src/fs/files/dontLookAtMe.txt index 8979bab743..e69de29bb2 100644 --- a/src/fs/files/dontLookAtMe.txt +++ b/src/fs/files/dontLookAtMe.txt @@ -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 index 5d66c332d6..e69de29bb2 100644 --- a/src/fs/files/fileToRead.txt +++ b/src/fs/files/fileToRead.txt @@ -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 index 43e64cd45c..e69de29bb2 100644 --- a/src/fs/files/fileToRemove.txt +++ b/src/fs/files/fileToRemove.txt @@ -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/hello.txt b/src/fs/files/hello.txt index 4e65f7775f..e69de29bb2 100644 --- a/src/fs/files/hello.txt +++ b/src/fs/files/hello.txt @@ -1 +0,0 @@ -Hello Node.js \ No newline at end of file diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index c98ad7c74e..62c84edf67 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,3 +1,16 @@ +import crypto from 'crypto'; +import fs from 'fs'; +import path, {dirname} from "path"; +import {fileURLToPath} from "url"; + export const calculateHash = async () => { - // Write your code here -}; \ No newline at end of file + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt'); + const readable = fs.createReadStream(filePath); + const hash = crypto.createHash('sha256'); + + return new Promise(resolve => { + readable.on('data', data => hash.update(data)).on('end', () => resolve(hash.digest('hex'))) + }) +}; + diff --git a/src/modules/cjsToEsm.cjs b/src/modules/cjsToEsm.cjs deleted file mode 100644 index ec022f2334..0000000000 --- a/src/modules/cjsToEsm.cjs +++ /dev/null @@ -1,31 +0,0 @@ -const path = require('path'); -const { release, version } = require('os'); -const { createServer: createServerHttp } = require('http'); -require('./files/c'); - -const random = Math.random(); - -let unknownObject; - -if (random > 0.5) { - unknownObject = require('./files/a.json'); -} else { - unknownObject = 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 createMyServer = createServerHttp((_, res) => { - res.end('Request accepted'); -}); - -module.exports = { - unknownObject, - createMyServer, -}; - diff --git a/src/modules/cjsToEsm.mjs b/src/modules/cjsToEsm.mjs new file mode 100644 index 0000000000..6260be3e89 --- /dev/null +++ b/src/modules/cjsToEsm.mjs @@ -0,0 +1,34 @@ +import { release, version } from 'os'; +import { createServer as createServerHttp } from 'http'; +import path, { dirname } from "path"; +import { fileURLToPath } from "url"; +import './files/c.js'; + +const random = Math.random(); +const __dirname = dirname(fileURLToPath(import.meta.url)); +const __filename = fileURLToPath(import.meta.url); + +let unknownObject; + +if (random > 0.5) { + unknownObject = import('./files/a.json', { assert: { type: "json" } }); +} else { + unknownObject = import('./files/b.json', { assert: { type: "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 createMyServer = createServerHttp((_, res) => { + res.end('Request accepted'); +}); + +export { + unknownObject, + createMyServer, +}; + diff --git a/src/streams/read.js b/src/streams/read.js index 97de16448b..f9e185d6ad 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,3 +1,11 @@ +import { dirname } from "path"; +import { fileURLToPath } from "url"; +import path from "path"; +import fs from 'fs'; + export const read = async () => { - // Write your code here -}; \ No newline at end of file + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filePath = path.join(__dirname, 'files', 'fileToRead.txt'); + const readableStream = fs.createReadStream(filePath); + readableStream.pipe(process.stdout); +}; diff --git a/src/streams/transform.js b/src/streams/transform.js index 8647b19cb7..d611872abd 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,3 +1,21 @@ +import { pipeline, Transform } from 'stream' + export const transform = async () => { - // Write your code here -}; \ No newline at end of file + const readableFromTerminal = process.stdin; + const writableToTerminal = process.stdout; + const transform = new Transform({ + transform(chunk, _, callback) { + const chunkStringified = chunk.toString().trim(); + const reversedChunk = chunkStringified.split('').reverse().join(''); + + callback(null, reversedChunk + '\n') + } + }) + + pipeline( + readableFromTerminal, + transform, + writableToTerminal, + err => console.log(err) + ); +}; diff --git a/src/streams/write.js b/src/streams/write.js index 5de058e9d0..a9295cb1e3 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,3 +1,13 @@ +import { dirname } from "path"; +import { fileURLToPath } from "url"; +import fs from 'fs'; +import path from 'path'; + export const write = async () => { - // Write your code here -}; \ No newline at end of file + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filePath = path.join(__dirname, 'files', 'fileToWrite.txt'); + const readableFromTerminal = process.stdin; + const writableToFile = fs.createWriteStream(filePath); + readableFromTerminal.pipe(writableToFile); +}; + diff --git a/src/wt/main.js b/src/wt/main.js index bf1a722c5d..a91a7f9a47 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,3 +1,22 @@ +import os from 'os'; +import { Worker } from 'worker_threads'; +import path, {dirname} from "path"; +import {fileURLToPath} from "url"; + export const performCalculations = async () => { - // Write your code here -}; \ No newline at end of file + const numOfCpus = os.cpus().length; + const workers = []; + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filePath = path.join(__dirname, 'worker.js'); + + for (let i = 0; i <= numOfCpus; i++) { + const workerPromise = new Promise((resolve) => { + const worker = new Worker(filePath, {workerData: 10 + i}); + worker.on('message', message => resolve({ status: 'resolved', data: message })) + worker.on('error', () => resolve({ status: 'error', data: null })) + }) + workers.push(workerPromise) + } + + return await Promise.all(workers); +}; diff --git a/src/wt/worker.js b/src/wt/worker.js index cd70ff6e3d..71ed67ebcf 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,6 +1,9 @@ -// n should be received from main thread +import { workerData, parentPort } from 'worker_threads'; + export const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); -export const sendResult = () => { - // This function sends result of nthFibonacci computations to main thread -}; \ No newline at end of file +export const sendResult = (data) => { + return nthFibonacci(data); +}; + +parentPort.postMessage(sendResult(workerData)); \ No newline at end of file diff --git a/src/zip/compress.js b/src/zip/compress.js index cb3b11ce62..f15817ad22 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,3 +1,20 @@ +import zlib from 'zlib'; +import fs from 'fs'; +import { pipeline } from "stream"; +import path, { dirname } from "path"; +import { fileURLToPath } from "url"; +import { promisify } from 'util'; + + export const compress = async () => { - // Write your code here -}; \ No newline at end of file + const pipe = promisify(pipeline); + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filePath = path.join(__dirname, 'files', 'fileToCompress.txt'); + const archivePath = path.join(__dirname, 'files', 'archive.gz'); + const zip = zlib.createGzip(); + const readable = fs.createReadStream(filePath); + const writable = fs.createWriteStream(archivePath); + await pipe(readable, zip, writable); +}; + +compress(); \ No newline at end of file diff --git a/src/zip/decompress.js b/src/zip/decompress.js index b509804edf..fa450071f1 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,3 +1,20 @@ +import zlib from 'zlib'; +import fs from 'fs'; +import { pipeline } from "stream"; +import path, { dirname } from "path"; +import { fileURLToPath } from "url"; +import { promisify } from 'util'; + + export const decompress = async () => { - // Write your code here -}; \ No newline at end of file + const pipe = promisify(pipeline); + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filePath = path.join(__dirname, 'files', 'fileToCompress.txt'); + const archivePath = path.join(__dirname, 'files', 'archive.gz'); + const unzip = zlib.createUnzip(); + const readable = fs.createReadStream(archivePath); + const writable = fs.createWriteStream(filePath); + await pipe(readable, unzip, writable); +}; + +decompress() \ No newline at end of file diff --git a/src/zip/files/archive.gz b/src/zip/files/archive.gz new file mode 100644 index 0000000000000000000000000000000000000000..229151a5a27ab0cc4661f529cc0eda27e3c03e10 GIT binary patch literal 20 Rcmb2|=3oE=W@ZQtBmoVe0J#7F literal 0 HcmV?d00001 From 644672d947f94b7068687d6ab5813218431eaa54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=B8=D1=8F=20=D0=9C=D0=B0=D1=80=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 5 Jun 2022 22:36:26 +0300 Subject: [PATCH 2/3] implement some basic nodejs functions --- .gitignore | 1 + Readme.md | 1 - src/fs/delete.js | 16 ++++++++++++++-- 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/list.js | 19 +++++++++++++++++-- src/fs/read.js | 17 +++++++++++++++-- src/fs/rename.js | 18 ++++++++++++++++-- 10 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..723ef36f4e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/Readme.md b/Readme.md index 611a505f49..05a64a8fbb 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,2 @@ # Node.js basics -## !!! Please don't submit Pull Requests to this repository !!! diff --git a/src/fs/delete.js b/src/fs/delete.js index 56381a8582..0e36c97521 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,3 +1,15 @@ +import path, {dirname} from "path"; +import fs from 'fs'; +import {fileURLToPath} from "url"; +import {DEFAULT_ERROR_MESSAGE} from "./constants.js"; + export const remove = async () => { - // Write your code here -}; \ No newline at end of file + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filePath = path.join(__dirname, 'files', 'fileToRemove.txt'); + fs.unlink(filePath, (err) => { + if (err) { + throw new Error(DEFAULT_ERROR_MESSAGE); + } + }) +}; + diff --git a/src/fs/files/dontLookAtMe.txt b/src/fs/files/dontLookAtMe.txt index e69de29bb2..8979bab743 100644 --- a/src/fs/files/dontLookAtMe.txt +++ b/src/fs/files/dontLookAtMe.txt @@ -0,0 +1 @@ +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 index e69de29bb2..5d66c332d6 100644 --- a/src/fs/files/fileToRead.txt +++ b/src/fs/files/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/fileToRemove.txt b/src/fs/files/fileToRemove.txt index e69de29bb2..43e64cd45c 100644 --- a/src/fs/files/fileToRemove.txt +++ b/src/fs/files/fileToRemove.txt @@ -0,0 +1 @@ +How dare you! \ No newline at end of file diff --git a/src/fs/files/hello.txt b/src/fs/files/hello.txt index e69de29bb2..ba7d34ee9f 100644 --- a/src/fs/files/hello.txt +++ b/src/fs/files/hello.txt @@ -0,0 +1 @@ +Hello node.js \ No newline at end of file diff --git a/src/fs/list.js b/src/fs/list.js index 4d4d95cb02..3575435efd 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,3 +1,18 @@ +import path, {dirname} from "path"; +import {fileURLToPath} from "url"; +import fs from "fs"; +import {DEFAULT_ERROR_MESSAGE} from "./constants.js"; + export const list = async () => { - // Write your code here -}; \ No newline at end of file + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filesPath = path.join(__dirname, 'files'); + + if (!fs.existsSync(filesPath)) { + throw new Error(DEFAULT_ERROR_MESSAGE); + } + + fs.readdir(filesPath, (_, files) => { + console.log(files) + }) +} + diff --git a/src/fs/read.js b/src/fs/read.js index 97de16448b..1b009fb024 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,3 +1,16 @@ +import path, {dirname} from "path"; +import {fileURLToPath} from "url"; +import fs from "fs"; +import {DEFAULT_ERROR_MESSAGE} from "./constants.js"; + export const read = async () => { - // Write your code here -}; \ No newline at end of file + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filePath = path.join(__dirname, 'files', 'fileToRead.txt'); + + if (!fs.existsSync(filePath)) { + throw new Error(DEFAULT_ERROR_MESSAGE); + } + + fs.readFile(filePath, 'utf8', (err, data) => console.log(data)) +}; + diff --git a/src/fs/rename.js b/src/fs/rename.js index 15ac96f569..6fabebb1b3 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,3 +1,17 @@ +import path, {dirname} from "path"; +import {fileURLToPath} from "url"; +import fs from "fs"; +import {DEFAULT_ERROR_MESSAGE} from "./constants.js"; + export const rename = async () => { - // Write your code here -}; \ No newline at end of file + const __dirname = dirname(fileURLToPath(import.meta.url)); + const wrongNamedFilePath = path.join(__dirname, 'files', 'wrongFilename.txt'); + const newFilePath = path.join(__dirname, 'files', 'wrongFilename.txt') + + if (!fs.existsSync(wrongNamedFilePath) || fs.existsSync(newFilePath)) { + throw new Error(DEFAULT_ERROR_MESSAGE); + } + + await fs.rename(wrongNamedFilePath, newFilePath, () => console.log('The file was renamed!')); +}; + From a0a7607f92929cde8f5cc5ed6c27a76bb31849d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=B8=D1=8F=20=D0=9C=D0=B0=D1=80=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Thu, 9 Jun 2022 20:42:21 +0300 Subject: [PATCH 3/3] add proper implementation to some functions --- package.json | 19 ++++++++++++++++++- src/cli/args.js | 19 ++++++++++++++----- src/cli/env.js | 11 +++++++---- src/common.js | 8 ++++++++ src/cp/cp.js | 10 ++++++---- src/fs/constants.js | 2 +- src/fs/copy.js | 27 ++++++++++----------------- src/fs/create.js | 11 ++++++----- src/fs/delete.js | 12 ++++++++---- src/fs/files/fresh.txt | 1 - src/fs/list.js | 7 ++++--- src/fs/read.js | 7 ++++--- src/fs/rename.js | 20 ++++++++++++++------ src/hash/calcHash.js | 1 + src/modules/cjsToEsm.mjs | 17 +++++++---------- src/streams/read.js | 7 ++++--- src/streams/transform.js | 9 ++++++--- src/streams/write.js | 6 +++--- src/wt/main.js | 8 +++++--- src/wt/worker.js | 6 +++--- src/zip/compress.js | 21 +++++++++++++-------- src/zip/decompress.js | 14 +++++++------- 22 files changed, 149 insertions(+), 94 deletions(-) create mode 100644 src/common.js delete mode 100644 src/fs/files/fresh.txt diff --git a/package.json b/package.json index 867fb44337..79e1053463 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,24 @@ "description": "This repository is the part of nodejs-assignments https://github.com/AlreadyBored/nodejs-assignments", "type": "module", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "read": "node src/fs/read.js", + "copy": "node src/fs/copy.js", + "create": "node src/fs/create.js", + "list-files": "node src/fs/list.js", + "delete": "node src/fs/delete.js", + "rename": "node src/fs/rename.js", + "cp": "node src/cp/cp.js", + "args": "node src/cli/args.js --first-name Masha --last-name Marchenko", + "env": "npx cross-env RSS_first_name=Masha RSS_last_name=Marchenko birthyear=2001 node src/cli/env.js", + "calc-hash": "node src/hash/calcHash.js", + "modules": "node src/modules/cjsToEsm.mjs", + "read-stream": "node src/streams/read.js", + "transform-stream": "node src/streams/transform.js", + "write-stream": "node src/streams/write.js", + "wt": "node src/wt/main.js", + "compress": "node src/zip/compress.js", + "decompress": "node src/zip/decompress.js" }, "repository": { "type": "git", diff --git a/src/cli/args.js b/src/cli/args.js index 68c8ee17d9..644262cf50 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,6 +1,15 @@ export const parseArgs = () => { - const args = process.argv; - for (let argKey of Object.keys(args)) { - console.log(`${argKey} is ${args[argKey]}`) - } -}; \ No newline at end of file + const userInputArgs = process.argv.slice(2); + const cliArguments = userInputArgs.reduce((acc, arg, index, arr) => { + const value = arr[index + 1]; + if (value && arg.startsWith('--')) { + const transformedArg = arg.slice(2); + const cliArgumentsTransformed = `${transformedArg} is ${value}`; + acc.push(cliArgumentsTransformed); + } return acc; + }, []) + + console.log(cliArguments.join(', ')); +}; + +parseArgs(); \ No newline at end of file diff --git a/src/cli/env.js b/src/cli/env.js index 72546ac291..cd8a32faad 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,6 +1,9 @@ export const parseEnv = () => { - const keys = Object.keys(process.env).filter(key => key.slice(4)); - for (const key of keys) { - process.stdout.write(`${key}=${process.env[key]}\n`) - } + const rssVariables = Object.entries(process.env) + .filter(([key, _]) => key.startsWith('RSS_')) + .map(([key, value]) => `${key}=${value}`) + + console.log(rssVariables.join('; ')) }; + +parseEnv(); \ No newline at end of file diff --git a/src/common.js b/src/common.js new file mode 100644 index 0000000000..52bac9fd8f --- /dev/null +++ b/src/common.js @@ -0,0 +1,8 @@ +import { dirname } from "path"; +import { fileURLToPath } from "url"; + +export function getDirname(url) { + const __filename = fileURLToPath(url); + + return dirname(__filename); +} \ No newline at end of file diff --git a/src/cp/cp.js b/src/cp/cp.js index c02ff693ef..77ab27a334 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,13 +1,15 @@ import child_process from 'child_process'; -import path, {dirname} from "path"; -import {fileURLToPath} from "url"; +import path from "path"; +import {getDirname} from "../common.js"; export const spawnChildProcess = async (args) => { - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const filePath = path.join(__dirname, 'files', 'script.js'); const child = child_process.fork(filePath, args) process.stdin.pipe(child.stdin); child.stdout.pipe(process.stdin); -}; \ No newline at end of file +}; + +spawnChildProcess('--silent --all') \ No newline at end of file diff --git a/src/fs/constants.js b/src/fs/constants.js index 9889a71e51..6ecf0f4867 100644 --- a/src/fs/constants.js +++ b/src/fs/constants.js @@ -1 +1 @@ -export const DEFAULT_ERROR_MESSAGE = 'FS operation failed.'; \ No newline at end of file +export const DEFAULT_ERROR_MESSAGE = 'FS operation failed.'; diff --git a/src/fs/copy.js b/src/fs/copy.js index ffbdd9ab55..144c9e7666 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,29 +1,22 @@ -import fs from 'fs'; -import path, {dirname} from 'path'; -import {fileURLToPath} from 'url'; +import fs from 'fs/promises'; +import path from 'path'; import {DEFAULT_ERROR_MESSAGE} from "./constants.js"; +import { getDirname } from '../common.js' export const copy = async () => { - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const srcPath = path.join(__dirname, 'files'); const destPath = path.join(__dirname, 'files_copy'); - if (fs.existsSync(destPath)) { + try { + await fs.mkdir(destPath); + const srcFiles = await fs.readdir(srcPath); + await Promise.all(srcFiles.map(file => fs.copyFile(path.join(srcPath, file), path.join(destPath, file)))) + .then(() => 'The files were copied to a new folder') + } catch { throw new Error(DEFAULT_ERROR_MESSAGE); - } else { - fs.mkdirSync(destPath); } - - fs.readdir(srcPath, (err, files) => { - try { - Promise.all(files.map(file => { - return fs.promises.copyFile(path.join(srcPath, file), path.join(destPath, file)) - })) - } catch { - throw new Error(DEFAULT_ERROR_MESSAGE) - } - }) }; copy(); \ No newline at end of file diff --git a/src/fs/create.js b/src/fs/create.js index 877a9468f9..ed17c0e6c7 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,16 +1,17 @@ import fs from 'fs/promises'; -import path, { dirname } from 'path'; -import { fileURLToPath } from 'url'; +import path from 'path'; import { DEFAULT_ERROR_MESSAGE } from "./constants.js"; +import { getDirname } from "../common.js"; export const create = async () => { - const __dirname = dirname(fileURLToPath(import.meta.url)); - const filePath = path.join(__dirname, 'files', 'fresh.txt'); + const filePath = path.join(getDirname(import.meta.url), 'files', 'fresh.txt'); const content = 'I am fresh and young'; try { - await fs.writeFile(filePath, content, {flag: 'wx'}) + await fs.writeFile(filePath, content, {flag: 'wx'}).then(() => console.log('"fresh.txt" was created')) } catch { throw new Error(DEFAULT_ERROR_MESSAGE) } }; + +create(); \ No newline at end of file diff --git a/src/fs/delete.js b/src/fs/delete.js index 0e36c97521..a371fe4067 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,15 +1,19 @@ -import path, {dirname} from "path"; +import path from "path"; import fs from 'fs'; -import {fileURLToPath} from "url"; import {DEFAULT_ERROR_MESSAGE} from "./constants.js"; +import {getDirname} from "../common.js"; export const remove = async () => { - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const filePath = path.join(__dirname, 'files', 'fileToRemove.txt'); - fs.unlink(filePath, (err) => { + + fs.rm(filePath, (err) => { if (err) { throw new Error(DEFAULT_ERROR_MESSAGE); + } else { + console.log('The file was deleted'); } }) }; +remove(); \ 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/list.js b/src/fs/list.js index 3575435efd..e681b7035a 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,10 +1,10 @@ -import path, {dirname} from "path"; -import {fileURLToPath} from "url"; +import path from "path"; import fs from "fs"; import {DEFAULT_ERROR_MESSAGE} from "./constants.js"; +import {getDirname} from "../common.js"; export const list = async () => { - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const filesPath = path.join(__dirname, 'files'); if (!fs.existsSync(filesPath)) { @@ -16,3 +16,4 @@ export const list = async () => { }) } +list(); \ No newline at end of file diff --git a/src/fs/read.js b/src/fs/read.js index 1b009fb024..36d9976783 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,10 +1,10 @@ -import path, {dirname} from "path"; -import {fileURLToPath} from "url"; +import path from "path"; import fs from "fs"; import {DEFAULT_ERROR_MESSAGE} from "./constants.js"; +import {getDirname} from "../common.js"; export const read = async () => { - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const filePath = path.join(__dirname, 'files', 'fileToRead.txt'); if (!fs.existsSync(filePath)) { @@ -14,3 +14,4 @@ export const read = async () => { fs.readFile(filePath, 'utf8', (err, data) => console.log(data)) }; +read(); \ No newline at end of file diff --git a/src/fs/rename.js b/src/fs/rename.js index 6fabebb1b3..b6ac917f56 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,17 +1,25 @@ -import path, {dirname} from "path"; -import {fileURLToPath} from "url"; +import path from "path"; import fs from "fs"; import {DEFAULT_ERROR_MESSAGE} from "./constants.js"; +import {getDirname} from "../common.js"; export const rename = async () => { - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const wrongNamedFilePath = path.join(__dirname, 'files', 'wrongFilename.txt'); - const newFilePath = path.join(__dirname, 'files', 'wrongFilename.txt') + const newFilePath = path.join(__dirname, 'files', 'properFilename.md') if (!fs.existsSync(wrongNamedFilePath) || fs.existsSync(newFilePath)) { - throw new Error(DEFAULT_ERROR_MESSAGE); + throw new Error(DEFAULT_ERROR_MESSAGE); } - await fs.rename(wrongNamedFilePath, newFilePath, () => console.log('The file was renamed!')); + fs.rename(wrongNamedFilePath, newFilePath, (err) => { + if (err) { + throw new Error(DEFAULT_ERROR_MESSAGE); + } else { + console.log('The file was renamed') + } + }); }; +rename(); + diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index 62c84edf67..35532f787a 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -14,3 +14,4 @@ export const calculateHash = async () => { }) }; +console.log(await calculateHash()); \ No newline at end of file diff --git a/src/modules/cjsToEsm.mjs b/src/modules/cjsToEsm.mjs index 6260be3e89..0f4234a87c 100644 --- a/src/modules/cjsToEsm.mjs +++ b/src/modules/cjsToEsm.mjs @@ -1,20 +1,17 @@ import { release, version } from 'os'; import { createServer as createServerHttp } from 'http'; -import path, { dirname } from "path"; -import { fileURLToPath } from "url"; +import path from 'path'; +import { fileURLToPath } from 'url'; import './files/c.js'; +import { getDirname } from "../common.js"; const random = Math.random(); -const __dirname = dirname(fileURLToPath(import.meta.url)); +const __dirname = getDirname(import.meta.url); const __filename = fileURLToPath(import.meta.url); -let unknownObject; - -if (random > 0.5) { - unknownObject = import('./files/a.json', { assert: { type: "json" } }); -} else { - unknownObject = import('./files/b.json', { assert: { type: "json" } }); -} +const { default: unknownObject } = random > 0.5 ? + await import('./files/a.json', { assert: { type: "json" } }) : + import('./files/b.json', { assert: { type: "json" } }); console.log(`Release ${release()}`); console.log(`Version ${version()}`); diff --git a/src/streams/read.js b/src/streams/read.js index f9e185d6ad..28a014f9ab 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,11 +1,12 @@ -import { dirname } from "path"; -import { fileURLToPath } from "url"; import path from "path"; import fs from 'fs'; +import {getDirname} from "../common.js"; export const read = async () => { - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const filePath = path.join(__dirname, 'files', 'fileToRead.txt'); const readableStream = fs.createReadStream(filePath); readableStream.pipe(process.stdout); }; + +read(); \ No newline at end of file diff --git a/src/streams/transform.js b/src/streams/transform.js index d611872abd..4f869e4f54 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,4 +1,4 @@ -import { pipeline, Transform } from 'stream' +import {pipeline, Transform} from 'stream' export const transform = async () => { const readableFromTerminal = process.stdin; @@ -16,6 +16,9 @@ export const transform = async () => { readableFromTerminal, transform, writableToTerminal, - err => console.log(err) - ); + err => { + if (err) console.log(err); + }); }; + +transform(); \ No newline at end of file diff --git a/src/streams/write.js b/src/streams/write.js index a9295cb1e3..ef198ac3ea 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,13 +1,13 @@ -import { dirname } from "path"; -import { fileURLToPath } from "url"; import fs from 'fs'; import path from 'path'; +import { getDirname } from "../common.js"; export const write = async () => { - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const filePath = path.join(__dirname, 'files', 'fileToWrite.txt'); const readableFromTerminal = process.stdin; const writableToFile = fs.createWriteStream(filePath); readableFromTerminal.pipe(writableToFile); }; +write(); \ No newline at end of file diff --git a/src/wt/main.js b/src/wt/main.js index a91a7f9a47..c576673f8d 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,12 +1,12 @@ import os from 'os'; import { Worker } from 'worker_threads'; -import path, {dirname} from "path"; -import {fileURLToPath} from "url"; +import path from "path"; +import { getDirname } from "../common.js"; export const performCalculations = async () => { const numOfCpus = os.cpus().length; const workers = []; - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const filePath = path.join(__dirname, 'worker.js'); for (let i = 0; i <= numOfCpus; i++) { @@ -20,3 +20,5 @@ export const performCalculations = async () => { return await Promise.all(workers); }; + +console.log(await performCalculations()); \ No newline at end of file diff --git a/src/wt/worker.js b/src/wt/worker.js index 71ed67ebcf..ccf01708ac 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -2,8 +2,8 @@ import { workerData, parentPort } from 'worker_threads'; export const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); -export const sendResult = (data) => { - return nthFibonacci(data); +export const sendResult = () => { + parentPort.postMessage(nthFibonacci(workerData)); }; -parentPort.postMessage(sendResult(workerData)); \ No newline at end of file +sendResult(); diff --git a/src/zip/compress.js b/src/zip/compress.js index f15817ad22..e9d6d0669c 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,20 +1,25 @@ import zlib from 'zlib'; import fs from 'fs'; -import { pipeline } from "stream"; -import path, { dirname } from "path"; -import { fileURLToPath } from "url"; -import { promisify } from 'util'; +import {pipeline} from "stream"; +import path from "path"; +import {getDirname} from "../common.js"; export const compress = async () => { - const pipe = promisify(pipeline); - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const filePath = path.join(__dirname, 'files', 'fileToCompress.txt'); const archivePath = path.join(__dirname, 'files', 'archive.gz'); const zip = zlib.createGzip(); const readable = fs.createReadStream(filePath); const writable = fs.createWriteStream(archivePath); - await pipe(readable, zip, writable); + pipeline( + readable, + zip, + writable, + (err) => { + if (err) console.log(err); + } + ); }; -compress(); \ No newline at end of file +compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index fa450071f1..9352b9e36e 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,20 +1,20 @@ import zlib from 'zlib'; import fs from 'fs'; -import { pipeline } from "stream"; -import path, { dirname } from "path"; -import { fileURLToPath } from "url"; -import { promisify } from 'util'; +import {pipeline} from "stream"; +import path from "path"; +import {getDirname} from "../common.js"; export const decompress = async () => { - const pipe = promisify(pipeline); - const __dirname = dirname(fileURLToPath(import.meta.url)); + const __dirname = getDirname(import.meta.url); const filePath = path.join(__dirname, 'files', 'fileToCompress.txt'); const archivePath = path.join(__dirname, 'files', 'archive.gz'); const unzip = zlib.createUnzip(); const readable = fs.createReadStream(archivePath); const writable = fs.createWriteStream(filePath); - await pipe(readable, unzip, writable); + pipeline(readable, unzip, writable, (err) => { + if (err) console.log(err) + }); }; decompress() \ No newline at end of file