Skip to content
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
16 changes: 14 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// implement function that parses command line arguments (given in format
// --propName value --prop2Name value2, you don't need to validate it) and
// prints them to the console in the format propName is value, prop2Name is value2

const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const argsArr = [];
for (let i = 0; i < args.length; i += 2) {
const key = args[i].slice(2);
argsArr.push({ key, value: args[i + 1] });
}
argsArr.forEach((arg) => {
console.log(`${arg.key} is ${arg.value}`);
});
};

parseArgs();
parseArgs();
14 changes: 12 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// implement function that parses environment variables with prefix RSS_ and
// prints them to the console in the format RSS_name1=value1; RSS_name2=value2

const prefix = "RSS_";

const parseEnv = () => {
// Write your code here
const keys = Object.keys(process.env);
const filteredKeys = keys.filter((key) => key.startsWith(prefix));

filteredKeys.forEach((key) => {
console.log(`${key}=${process.env[key]}`);
});
};

parseEnv();
parseEnv();
24 changes: 21 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
const spawnChildProcess = async (args) => {
// Write your code here
// implement function spawnChildProcess that receives array of arguments args
// and creates child process from file script.js, passing these args to it.
// This function should create IPC-channel between stdin and stdout of master
// process and child process:
// child process stdin should receive input from master process stdin
// child process stdout should send data to master process stdout

import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const spawnChildProcess = async (...args) => {
const childProcess = spawn('node', [join(__dirname, 'files', 'script.js'), ...args]);

childProcess.stdout.pipe(process.stdout);
process.stdin.pipe(childProcess.stdin);

};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess('Hello from master process', 'how are you?');
31 changes: 30 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
// implement function that copies folder files files with all its content into
// folder files_copy at the same level (if files folder doesn't exists or
// files_copy has already been created Error with message FS operation failed
// must be thrown)

import { stat, cp } from 'fs/promises';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const FS_OPERATION_FAILED = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const isExists = async (path) =>
stat(path)
.then(() => true)
.catch(() => false);

const copy = async () => {
// Write your code here
const source = join(__dirname, 'files');
const destination = join(__dirname, 'files_copy');

const isSourceExists = await isExists(source);
const isDestinationExists = await isExists(destination);

if (!isSourceExists || isDestinationExists) {
throw new Error(FS_OPERATION_FAILED);
}

cp(source, destination, { recursive: true });
};

await copy();
31 changes: 29 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
// implement function that creates new file fresh.txt with content I am fresh
// and young inside of the files folder (if file already exists Error with
// message FS operation failed must be thrown)

import { stat, writeFile } from 'fs/promises';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const FS_OPERATION_FAILED = 'FS operation failed';
const FILE_CONTENT = 'I am fresh and young';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const isExists = async (path) =>
stat(path)
.then(() => true)
.catch(() => false);

const create = async () => {
// Write your code here
const destination = join(__dirname, 'files', 'fresh.txt');

const isDestinationExists = await isExists(destination);

if (isDestinationExists) {
throw new Error(FS_OPERATION_FAILED);
}

writeFile(destination, FILE_CONTENT);
};

await create();
await create();
25 changes: 23 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
// implement function that deletes file fileToRemove.txt (if there's no file
// fileToRemove.txt Error with message FS operation failed must be thrown)

import { rm } from 'fs/promises';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const FS_OPERATION_FAILED = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const remove = async () => {
// Write your code here
const fileToRemove = join(__dirname, 'files', 'fileToRemove.txt');

try {
await rm(fileToRemove);
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(FS_OPERATION_FAILED);
}
throw error;
}
};

await remove();
await remove();
28 changes: 26 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
// implement function that prints all array of filenames from files folder into
// console (if files folder doesn't exists Error with message FS operation
// failed must be thrown)

import { readdir } from 'fs/promises';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const FS_OPERATION_FAILED = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const list = async () => {
// Write your code here
const source = join(__dirname, 'files');

try {
const files = await readdir(source);

files.forEach((item) => console.log(item));
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(FS_OPERATION_FAILED);
}
throw error;
}
};

await list();
await list();
27 changes: 25 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
// implement function that prints content of the fileToRead.txt into console
// (if there's no file fileToRead.txt Error with message FS operation failed
// must be thrown)

import { readFile } from 'fs/promises';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const FS_OPERATION_FAILED = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const read = async () => {
// Write your code here
const fileToRead = join(__dirname, 'files', 'fileToRead.txt');

try {
const content = await readFile(fileToRead, 'utf-8');
console.log(content);
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(FS_OPERATION_FAILED);
}
throw error;
}
};

await read();
await read();
33 changes: 31 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
// implement function that renames file wrongFilename.txt to properFilename
// with extension .md (if there's no file wrongFilename.txt or properFilename.md
// already exists Error with message FS operation failed must be thrown)

import fs from 'fs/promises';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const FS_OPERATION_FAILED = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const isExists = async (path) =>
fs
.stat(path)
.then(() => true)
.catch(() => false);

const rename = async () => {
// Write your code here
const source = join(__dirname, 'files', 'wrongFilename.txt');
const destination = join(__dirname, 'files', 'properFilename.md');

const isSourceExists = await isExists(source);
const isDestinationExists = await isExists(destination);

if (!isSourceExists || isDestinationExists) {
throw new Error(FS_OPERATION_FAILED);
}

fs.rename(source, destination);
};

await rename();
await rename();
27 changes: 25 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
// implement function that calculates SHA256 hash for file
// fileToCalculateHashFor.txt and logs it into console as hex

import { createHash } from "crypto";
import { readFile } from "fs/promises";
import { fileURLToPath } from "url";
import { dirname, join } from "path";

const filepath = "./files/fileToCalculateHashFor.txt";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const calculateHash = async () => {
// Write your code here
const hash = createHash("sha256");
hash.on("readable", () => {
const data = hash.read();
if (data) {
console.log(data.toString("hex"));
}
});

const stream = await readFile(join(__dirname, filepath));
hash.write(stream);
hash.end();
};

await calculateHash();
await calculateHash();
40 changes: 0 additions & 40 deletions src/modules/cjsToEsm.cjs

This file was deleted.

45 changes: 45 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { sep, dirname } from "path";
import { release, version } from "os";
import { createServer as createServerHttp } from "http";
import { fileURLToPath } from "url";

import "./files/c.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const random = Math.random();

let unknownObject;

if (random > 0.5) {
await import("./files/a.json", {
assert: { type: "json" },
}).then(({ default: myDefault }) => (unknownObject = myDefault));
} else {
await import("./files/b.json", {
assert: { type: "json" },
}).then(({ default: myDefault }) => (unknownObject = myDefault));
}

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${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 };
Loading