Skip to content
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

Nodejs basics #605

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

155 changes: 155 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import process from "node:process";

const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
args.forEach((arg, idx) => {
if (arg.startsWith('--')) {
console.log(`${arg} is ${args[idx + 1]}`);
}
})
};

parseArgs();
parseArgs();
9 changes: 7 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import process from "node:process";

const parseEnv = () => {
// Write your code here
const keys = Object.keys(process.env).filter((key) => key.startsWith("RSS_"));
keys.forEach((key) => {
console.log(`${key}=${process.env[key]}`);
})
};

parseEnv();
parseEnv();
13 changes: 12 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import {useFSVariables} from "./utils.js";

const copy = async () => {
// Write your code here
try {
const { __dirname} = useFSVariables();
const sourcePath = path.resolve(__dirname, 'files');
const destPath = path.resolve(__dirname, 'files_copy');
await fs.cp(sourcePath, destPath, { recursive: true, errorOnExist: true, force: false });
} catch {
throw new Error('FS operation failed');
}
};

await copy();
14 changes: 12 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import {useFSVariables} from "./utils.js";

const create = async () => {
// Write your code here
try {
const { __dirname} = useFSVariables();
await fs.writeFile(path.resolve(__dirname, 'files', 'fresh.txt'), 'I am fresh and young', { flag: 'wx' });
} catch {
throw new Error('FS operation failed');
}
};

await create();
await create();
14 changes: 12 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import {useFSVariables} from "./utils.js";
import fs from "node:fs/promises";
import path from "node:path";

const remove = async () => {
// Write your code here
try {
const { __dirname } = useFSVariables();
const filePath = path.resolve(__dirname, 'files', 'fileToRemove.txt');
await fs.unlink(filePath);
} catch {
throw new Error('FS operation failed');
}
};

await remove();
await remove();
16 changes: 14 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import {useFSVariables} from "./utils.js";
import fs from "node:fs/promises";
import path from "node:path";

const list = async () => {
// Write your code here
try {
const { __dirname } = useFSVariables();
const dirPath = path.resolve(__dirname, 'files');
const files = await fs.readdir(dirPath, { withFileTypes: true, recursive: true });
console.log(files.map(({ name }) => name))
} catch {
throw new Error('FS operation failed');
}

};

await list();
await list();
15 changes: 13 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import {useFSVariables} from "./utils.js";
import fs from "node:fs/promises";
import path from "node:path";

const read = async () => {
// Write your code here
try {
const { __dirname } = useFSVariables();
const filePath = path.resolve(__dirname, 'files', 'fileToRead.txt');
const fileData = await fs.readFile(filePath, { encoding: 'utf8' });
console.log(fileData);
} catch {
throw new Error('FS operation failed');
}
};

await read();
await read();
15 changes: 13 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import {useFSVariables} from "./utils.js";

const rename = async () => {
// Write your code here
try {
const { __dirname } = useFSVariables();
const oldPath = path.resolve(__dirname, 'files', 'wrongFilename.txt');
const newPath = path.resolve(__dirname, 'files', 'properFilename.md');
await fs.rename(oldPath, newPath);
} catch {
throw new Error('FS operation failed');
}
};

await rename();
await rename();
8 changes: 8 additions & 0 deletions src/fs/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {fileURLToPath} from "node:url";
import path from "node:path";

export const useFSVariables = () => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
return { __filename, __dirname };
}
17 changes: 15 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { createHash } from 'node:crypto';
import {fileURLToPath} from "node:url";
import path from "node:path";
import {createReadStream} from "node:fs";

const calculateHash = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filePath = path.resolve(__dirname, 'files', 'fileToCalculateHashFor.txt');
const readStream = createReadStream(filePath);
const hash = createHash('sha256');

readStream.pipe(hash).on('finish', () => {
console.log(hash.digest("hex"));
})
};

await calculateHash();
await calculateHash();
Loading