-
Notifications
You must be signed in to change notification settings - Fork 0
Nodejs basics #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ac79609
c872710
a3bc484
118f259
97ccdfc
47c44a2
43c603d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| const parseArgs = () => { | ||
| // Write your code here | ||
| const args = process.argv.slice(2); | ||
| let output = []; | ||
|
|
||
| for (let i = 0; i < args.length; i += 2) { | ||
| const key = args[i].startsWith("--") ? args[i].slice(2) : args[i]; | ||
| const value = args[i + 1]; | ||
|
|
||
| output.push(`${key} is ${value}`); | ||
| } | ||
|
|
||
| console.log(output.join(", ")); | ||
| }; | ||
|
|
||
| parseArgs(); | ||
| parseArgs(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| const ENV_NAME="RSS_"; | ||
| process.env.RSS_name1 = 'value1'; | ||
| process.env.RSS_name2 = 'value2'; | ||
|
|
||
| const parseEnv = () => { | ||
| // Write your code here | ||
| const rssVariables = Object.entries(process.env) | ||
| .filter(([key, _]) => key.startsWith(ENV_NAME)) | ||
| .map(([key, value]) => `${key}=${value}`) | ||
| .join('; '); | ||
|
|
||
| console.log(rssVariables); | ||
| }; | ||
|
|
||
| parseEnv(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,29 @@ | ||
| import { spawn } from 'child_process'; | ||
| import { fileURLToPath } from 'url'; | ||
| import path from 'path'; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
| const DIR = "files"; | ||
| const FILE_NAME = "script.js"; | ||
|
|
||
| const spawnChildProcess = async (args) => { | ||
| // Write your code here | ||
| const scriptPath = path.join(__dirname, DIR, FILE_NAME); | ||
| const child = spawn("node", [scriptPath, ...args], { | ||
| stdio: ["pipe", "pipe", "inherit", "ipc"], | ||
| }); | ||
|
|
||
| process.stdin.pipe(child.stdin); | ||
|
|
||
| child.stdout.pipe(process.stdout); | ||
|
|
||
| child.on("error", (error) => { | ||
| console.error(`Error in child process: ${error.message}`); | ||
| }); | ||
|
|
||
| child.on("exit", (code) => { | ||
| console.log(`Child process exited with code ${code}`); | ||
| }); | ||
| }; | ||
|
|
||
| // Put your arguments in function call to test this functionality | ||
| spawnChildProcess( /* [someArgument1, someArgument2, ...] */); | ||
| spawnChildProcess([1, 2, 3, 4, 5]); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,37 @@ | ||
| import { promises as fs } from "fs"; | ||
| import path from "path"; | ||
| import { fileURLToPath } from "url"; | ||
| import { dirname } from "path"; | ||
| import { exists } from "./vendors.js"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
| const DIR = "files"; | ||
|
|
||
| const copy = async () => { | ||
| // Write your code here | ||
| const sourceDir = path.join(__dirname, DIR); | ||
| const destDir = path.join(__dirname, "files_copy"); | ||
|
|
||
| if (!(await exists(sourceDir)) || (await exists(destDir))) { | ||
| throw new Error("FS operation failed"); | ||
| } | ||
|
|
||
| copyFiles(sourceDir, destDir); | ||
| }; | ||
|
|
||
| const copyFiles = async (src, dest) => { | ||
| await fs.mkdir(dest, { recursive: true }); | ||
|
|
||
| const entries = await fs.readdir(src, { withFileTypes: true }); | ||
|
|
||
| for (let entry of entries) { | ||
| let srcPath = path.join(src, entry.name); | ||
| let destPath = path.join(dest, entry.name); | ||
|
|
||
| entry.isDirectory() | ||
| ? copyDirectory(srcPath, destPath) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is copyDirectory??? Where is it? |
||
| : await fs.copyFile(srcPath, destPath); | ||
| } | ||
| }; | ||
|
|
||
| await copy(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,22 @@ | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import path, { dirname } from "path"; You have two imports from one module |
||
| import { fileURLToPath } from "url"; | ||
| import { dirname } from "path"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
| const MESSAGE = "I am fresh and young"; | ||
| const FILE_NAME = "fresh.txt"; | ||
| const DIR = "files"; | ||
|
|
||
| const create = async () => { | ||
| // Write your code here | ||
| const filePath = path.join(__dirname, DIR, FILE_NAME); | ||
|
|
||
| if (fs.existsSync(filePath)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Мне кажется, здесь Вы делаете лишнюю работу, но она недостаточна... |
||
| throw new Error("FS operation failed"); | ||
| } | ||
|
|
||
| fs.writeFileSync(filePath, MESSAGE); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В задании рекомендовано использовать асинхронные методы. Сейчас это не важно, но в данное задание - подготовка к следующим и там синхронные уже не прокатят. |
||
| }; | ||
|
|
||
| await create(); | ||
| await create(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,21 @@ | ||
| import { promises as fs } from "fs"; | ||
| import path from "path"; | ||
| import { fileURLToPath } from "url"; | ||
| import { dirname } from "path"; | ||
| import { exists } from "./vendors.js"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
|
|
||
| const remove = async () => { | ||
| // Write your code here | ||
| const dirPath = path.join(__dirname, "files"); | ||
| const filePath = path.join(dirPath, "fileToRemove.txt"); | ||
|
|
||
| if (!(await exists(filePath))) { | ||
| throw new Error("FS operation failed"); | ||
| } | ||
|
|
||
| await fs.unlink(filePath); | ||
| }; | ||
|
|
||
| await remove(); | ||
| await remove(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,23 @@ | ||
| import { promises as fs } from "fs"; | ||
| import path from "path"; | ||
| import { fileURLToPath } from "url"; | ||
| import { dirname } from "path"; | ||
| import { exists } from "./vendors.js"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
|
|
||
| const list = async () => { | ||
| // Write your code here | ||
| const dirPath = path.join(__dirname, "files"); | ||
|
|
||
| if (!(await exists(dirPath))) { | ||
| throw new Error("FS operation failed"); | ||
| } | ||
|
|
||
| const files = await fs.readdir(dirPath); | ||
| files.forEach((file) => { | ||
| console.log(file); | ||
| }); | ||
| }; | ||
|
|
||
| await list(); | ||
| await list(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,27 @@ | ||
| import { promises as fs } from "fs"; | ||
| import path from "path"; | ||
| import { fileURLToPath } from "url"; | ||
| import { dirname } from "path"; | ||
| import { exists } from "./vendors.js"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
|
|
||
| const read = async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another version of list.js? |
||
| // Write your code here | ||
| const dirPath = path.join(__dirname, "files"); | ||
|
|
||
| if (!(await exists(dirPath))) { | ||
| throw new Error("FS operation failed: files folder does not exist"); | ||
| } | ||
|
|
||
| try { | ||
| const files = await fs.readdir(dirPath); | ||
| files.forEach((file) => { | ||
| console.log(file); | ||
| }); | ||
| } catch (err) { | ||
| throw new Error("Error reading files from the directory"); | ||
| } | ||
| }; | ||
|
|
||
| await read(); | ||
| await read(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,24 @@ | ||
| import { promises as fs } from "fs"; | ||
| import path from "path"; | ||
| import { fileURLToPath } from "url"; | ||
| import { dirname } from "path"; | ||
| import { exists } from "./vendors.js"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
| const DIR = "files"; | ||
|
|
||
| const rename = async () => { | ||
| // Write your code here | ||
| const fileDir = path.join(__dirname, DIR); | ||
|
|
||
| const oldFilePath = path.join(fileDir, "wrongFilename.txt"); | ||
| const newFilePath = path.join(fileDir, "properFilename.md"); | ||
|
|
||
| if (!(await exists(oldFilePath)) || (await exists(newFilePath))) { | ||
| throw new Error("FS operation failed"); | ||
| } | ||
|
|
||
| await fs.rename(oldFilePath, newFilePath); | ||
| }; | ||
|
|
||
| await rename(); | ||
| rename(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { promises as fs } from "fs"; | ||
|
|
||
| export const exists = async (path) => { | ||
| try { | ||
| await fs.stat(path); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,31 @@ | ||
| import fs from 'fs'; | ||
| import crypto from 'crypto'; | ||
| import path from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
| import { dirname } from 'path'; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
| const FILE_NAME = "fileToCalculateHashFor.txt"; | ||
| const DIR = "files"; | ||
|
|
||
| const calculateHash = async () => { | ||
| // Write your code here | ||
| const filePath = path.join(__dirname, DIR, FILE_NAME); | ||
| const hash = crypto.createHash('sha256'); | ||
| const fileStream = fs.createReadStream(filePath); | ||
|
|
||
| fileStream.on('data', (data) => { | ||
| hash.update(data); | ||
| }); | ||
|
|
||
| fileStream.on('end', () => { | ||
| const hexHash = hash.digest('hex'); | ||
| console.log(`SHA256 Hash: ${hexHash}`); | ||
| }); | ||
|
|
||
| fileStream.on('error', (err) => { | ||
| console.error(`Error reading file: ${err.message}`); | ||
| }); | ||
| }; | ||
|
|
||
| await calculateHash(); |
This file was deleted.
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should rename this file. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import path from "path"; | ||
| import { fileURLToPath } from "url"; | ||
| import os from "os"; | ||
| import { createServer as createServerHttp } from "http"; | ||
| import "./files/c.js"; | ||
|
|
||
| const random = Math.random(); | ||
| let unknownObject; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| if (random > 0.5) { | ||
| unknownObject = await import("./files/a.json", { assert: { type: "json" } }); | ||
| } else { | ||
| unknownObject = await import("./files/b.json", { assert: { type: "json" } }); | ||
| } | ||
|
|
||
| console.log(`Release ${os.release()}`); | ||
| console.log(`Version ${os.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 }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,18 @@ | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
| import { fileURLToPath } from "url"; | ||
| import { dirname } from "path"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
| const FILE_NAME = "fileToRead.txt"; | ||
| const DIR = "files"; | ||
|
|
||
| const read = async () => { | ||
| // Write your code here | ||
| const filePath = path.join(__dirname, DIR, FILE_NAME); | ||
| const readStream = fs.createReadStream(filePath); | ||
|
|
||
| readStream.pipe(process.stdout); | ||
| }; | ||
|
|
||
| await read(); | ||
| await read(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this is here???
This side effect gives the wrong result of function.