forked from AlreadyBored/node-nodejs-basics
-
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
Open
ISerhiienko
wants to merge
9
commits into
main
Choose a base branch
from
dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e87225b
feat: done cli/args task
ISerhiienko a701b0f
feat: done cli env task
ISerhiienko 5eff339
feat: done module esm task
ISerhiienko 47b5641
feat: done compress task
ISerhiienko 8d036ca
feat: done decompress task
ISerhiienko bea67bd
fix: delete archive file
ISerhiienko b9dd720
feat: done streams tasks
ISerhiienko 1a46128
fear: done fs tasks
ISerhiienko 89c6873
feat: done child process task
ISerhiienko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,28 @@ | ||
| const parseArgs = () => { | ||
| // Write your code here | ||
| }; | ||
| const [, , ...commandLineArgs] = process.argv; | ||
|
|
||
| const parsedArgs = parseCommandLineArgs(commandLineArgs); | ||
| printParsedArgs(parsedArgs); | ||
| }; | ||
|
|
||
| const parseCommandLineArgs = (args) => { | ||
| const parsedArgs = {}; | ||
|
|
||
| for (let i = 0; i < args.length; i += 2) { | ||
| const argName = cleanArgName(args[i]); | ||
| const argValue = args[i + 1]; | ||
| parsedArgs[argName] = argValue; | ||
| } | ||
|
|
||
| return parsedArgs; | ||
| }; | ||
|
|
||
| const cleanArgName = (arg) => arg.replace(/^--/, ''); | ||
|
|
||
| const printParsedArgs = (parsedArgs) => { | ||
| for (const [argName, argValue] of Object.entries(parsedArgs)) { | ||
| console.log(`${argName} is ${argValue}`); | ||
| } | ||
| }; | ||
|
|
||
| parseArgs(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,17 @@ | ||
| const parseEnv = () => { | ||
| // Write your code here | ||
| const parsedVars = {}; | ||
|
|
||
| for (const key in process.env) { | ||
| if (key.startsWith('RSS_')) { | ||
| const varName = key; | ||
| const varValue = process.env[key]; | ||
| parsedVars[varName] = varValue; | ||
| } | ||
| } | ||
|
|
||
| for (const [varName, varValue] of Object.entries(parsedVars)) { | ||
| console.log(`${varName}=${varValue}`); | ||
| } | ||
| }; | ||
|
|
||
| parseEnv(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| import { spawn } from "child_process"; | ||
| import { fileURLToPath } from "url"; | ||
| import path from "path"; | ||
| import { dirname } from "path"; | ||
|
|
||
| const spawnChildProcess = async (args) => { | ||
| // Write your code here | ||
| const currentDir = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const childModule = path.resolve(currentDir, "files", "script.js"); | ||
| const childProcess = spawn("node", [childModule, ...args]); | ||
|
|
||
| process.stdin.pipe(childProcess.stdin); | ||
| childProcess.stdout.pipe(process.stdout); | ||
| }; | ||
|
|
||
| // Put your arguments in function call to test this functionality | ||
| spawnChildProcess( /* [someArgument1, someArgument2, ...] */); | ||
| spawnChildProcess(["node", "js"]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,40 @@ | ||
| import fs from "fs/promises"; | ||
| import { fileURLToPath } from "url"; | ||
| import path from "path"; | ||
| import { dirname } from "path"; | ||
|
|
||
| const copy = async () => { | ||
| // Write your code here | ||
| const currentModulePath = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const sourceFolder = path.resolve(currentModulePath, "files"); | ||
| const targetFolder = path.resolve(currentModulePath, "files_copy"); | ||
|
|
||
| try { | ||
| await fs.access(sourceFolder); | ||
| await fs.access(targetFolder); | ||
|
|
||
| console.error("Error: Target folder already exists."); | ||
| } catch (error) { | ||
| if (error.code === "ENOENT") { | ||
| try { | ||
| await fs.mkdir(targetFolder); | ||
| const files = await fs.readdir(sourceFolder); | ||
|
|
||
| for (const file of files) { | ||
| const sourceFilePath = path.join(sourceFolder, file); | ||
| const targetFilePath = path.join(targetFolder, file); | ||
|
|
||
| await fs.copyFile(sourceFilePath, targetFilePath); | ||
| } | ||
|
|
||
| console.log("Copy operation complete."); | ||
| } catch (copyError) { | ||
| console.error("Copy operation failed:", copyError); | ||
| } | ||
| } else { | ||
| console.error("Error accessing folders:", error); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| await copy(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,26 @@ | ||
| import fs from "fs/promises"; | ||
| import { fileURLToPath } from "url"; | ||
| import path from "path"; | ||
| import { dirname } from "path"; | ||
|
|
||
| const create = async () => { | ||
| // Write your code here | ||
| const fileName = "fresh.txt"; | ||
| const content = "I am fresh and young"; | ||
|
|
||
| const currentModulePath = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const filePath = path.resolve(currentModulePath, "files", fileName); | ||
|
|
||
| try { | ||
| await fs.writeFile(filePath, content, { flag: "wx" }); | ||
| console.log("Create operation complete."); | ||
| } catch (error) { | ||
| if (error.code === "EEXIST") { | ||
| console.error("Error: File already exists."); | ||
|
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. From task description:
(console.log instead of throw error, here and everywhere) |
||
| } else { | ||
| console.error("Create operation failed:", error); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| await create(); | ||
| await create(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,24 @@ | ||
| import fs from "fs/promises"; | ||
| import { fileURLToPath } from "url"; | ||
| import path from "path"; | ||
| import { dirname } from "path"; | ||
|
|
||
| const remove = async () => { | ||
| // Write your code here | ||
| const fileName = "fileToRemove.txt"; | ||
|
|
||
| const currentModulePath = dirname(fileURLToPath(import.meta.url)); | ||
| const filePath = path.resolve(currentModulePath, "files", fileName); | ||
|
|
||
| try { | ||
| await fs.unlink(filePath); | ||
| console.log("Delete operation complete."); | ||
| } catch (error) { | ||
| if (error.code === "ENOENT") { | ||
| console.error("Error: File does not exist."); | ||
| } else { | ||
| console.error("Delete operation failed:", error); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| await remove(); | ||
| await remove(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +0,0 @@ | ||
| How dare you! | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,25 @@ | ||
| import fs from "fs/promises"; | ||
| import { fileURLToPath } from "url"; | ||
| import path from "path"; | ||
| import { dirname } from "path"; | ||
|
|
||
| const list = async () => { | ||
| // Write your code here | ||
| const currentModulePath = dirname(fileURLToPath(import.meta.url)); | ||
| const folderPath = path.resolve(currentModulePath, "files"); | ||
|
|
||
| try { | ||
| const files = await fs.readdir(folderPath); | ||
| console.log('Files in the "files" folder:'); | ||
| files.forEach((file) => { | ||
| console.log(file); | ||
| }); | ||
| } catch (error) { | ||
| if (error.code === "ENOENT") { | ||
| console.error('Error: "files" folder does not exist.'); | ||
| } else { | ||
| console.error("List operation failed:", error); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| await list(); | ||
| await list(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,25 @@ | ||
| import fs from "fs/promises"; | ||
| import { fileURLToPath } from "url"; | ||
| import path from "path"; | ||
| import { dirname } from "path"; | ||
|
|
||
| const read = async () => { | ||
| // Write your code here | ||
| const fileName = "fileToRead.txt"; | ||
|
|
||
| const currentModulePath = dirname(fileURLToPath(import.meta.url)); | ||
| const filePath = path.resolve(currentModulePath, "files", fileName); | ||
|
|
||
| try { | ||
| const content = await fs.readFile(filePath, "utf-8"); | ||
| console.log(`Content of ${fileName}:`); | ||
| console.log(content); | ||
| } catch (error) { | ||
| if (error.code === "ENOENT") { | ||
| console.error(`Error: File ${fileName} does not exist.`); | ||
| } else { | ||
| console.error("Read operation failed:", error); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| await read(); | ||
| await read(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,48 @@ | ||
| import fs from "fs/promises"; | ||
| import { fileURLToPath } from "url"; | ||
| import path from "path"; | ||
| import { dirname } from "path"; | ||
|
|
||
| const fileExists = async (filePath) => { | ||
| try { | ||
| await fs.access(filePath); | ||
| return true; | ||
| } catch (error) { | ||
| if (error.code === "ENOENT") { | ||
| return false; | ||
| } | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| const rename = async () => { | ||
| // Write your code here | ||
| const fileName = "wrongFilename.txt"; | ||
| const fileNameChanged = "properFilename.md"; | ||
|
|
||
| const currentModulePath = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const sourceFilePath = path.resolve(currentModulePath, "files", fileName); | ||
| const targetFilePath = path.resolve( | ||
| currentModulePath, | ||
| "files", | ||
| fileNameChanged | ||
| ); | ||
|
|
||
| try { | ||
| if ( | ||
| (await fileExists(sourceFilePath)) && | ||
| !(await fileExists(targetFilePath)) | ||
| ) { | ||
| await fs.rename(sourceFilePath, targetFilePath); | ||
| console.log("Rename operation complete."); | ||
| } else { | ||
| throw new Error( | ||
| "Error: Target file already exists or source file does not exist." | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| console.error(error.message); | ||
| } | ||
| }; | ||
|
|
||
| await rename(); | ||
| await rename(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
|
|
||
|
|
||
| const calculateHash = async () => { | ||
| // Write your code here | ||
|
|
||
| }; | ||
|
|
||
| await calculateHash(); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import path from 'path'; | ||
| import { release, version } from 'os'; | ||
| import { createServer as createServerHttp } from 'http'; | ||
| import { fileURLToPath } from 'url'; | ||
| import { dirname } from 'path'; | ||
| import './files/c.js'; | ||
|
|
||
| const random = Math.random(); | ||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
|
|
||
| let unknownObject; | ||
|
|
||
| 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 ${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 }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The function doesn't write error message "Error accessing folders:" when source folder doesn't exist
("if
filesfolder doesn't existErrorwith messageFS operation failedmust be thrown")