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
10 changes: 10 additions & 0 deletions node_modules/.yarn-integrity

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

16 changes: 14 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const parseArgs = () => {
// Write your code here
// Skip the first two elements (node and script path)
const args = process.argv.slice(2);

const parsedArgs = [];

// why i += 2 -> [ '--some-arg', 'value1', '--other', '1337', '--arg2', '42' ]
for (let i = 0; i < args.length; i += 2) {
const propName = args[i].replace("--", "");
const value = args[i + 1];
parsedArgs.push(`${propName} is ${value}`);
}

console.log(parsedArgs.join(", "));
};

parseArgs();
parseArgs();
11 changes: 9 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseEnv = () => {
// Write your code here
const envVars = process.env;
console.log("Object.keys(envVars)", Object.keys(envVars));
const rssVars = Object.keys(envVars)
.filter((key) => key.startsWith("RSS_"))
.map((key) => `${key}=${envVars[key]}`)
.join("; ");

console.log(rssVars);
};

parseEnv();
parseEnv();
19 changes: 17 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { spawn } from "child_process";

const spawnChildProcess = async (args) => {
// Write your code here
const child = spawn("node", ["./src/cp/files/script.js", ...args]);

process.stdin.pipe(child.stdin);

child.stdout.pipe(process.stdout);

child.on("error", (error) => {
console.error(`Error: ${error.message}`);
});

child.on("exit", (code) => {
console.log(`Child process exited with code ${code}`);
});
};

const args = process.argv.slice(2);
// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(args);
8 changes: 4 additions & 4 deletions src/cp/files/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ console.log(`Total number of arguments is ${args.length}`);
console.log(`Arguments: ${JSON.stringify(args)}`);

const echoInput = (chunk) => {
const chunkStringified = chunk.toString();
if (chunkStringified.includes('CLOSE')) process.exit(0);
process.stdout.write(`Received from master process: ${chunk.toString()}\n`)
const chunkStringified = chunk.toString();
if (chunkStringified.includes("CLOSE")) process.exit(0);
process.stdout.write(`Received from master process: ${chunk.toString()}\n`);
};

process.stdin.on('data', echoInput);
process.stdin.on("data", echoInput);
27 changes: 26 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { promises as fs } from "fs";
import path from "path";

const copy = async () => {
// Write your code here
const folder = "src/fs/";
const sourceDir = path.join(folder, "files");
const targetDir = path.join(folder, "files_copy");

try {
await fs.access(sourceDir);

try {
await fs.access(targetDir);
throw new Error("FS operation failed: Target directory already exists");
} catch {}

await fs.mkdir(targetDir);

const files = await fs.readdir(sourceDir);
for (const file of files) {
const srcFile = path.join(sourceDir, file);
const destFile = path.join(targetDir, file);
await fs.copyFile(srcFile, destFile);
}
} catch (error) {
throw new Error(`FS operation failed: ${error}`);
}
};

await copy();
34 changes: 32 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import { promises as fs } from "fs";
import path from "path";

const create = async () => {
// Write your code here
const folder = "src/fs/files";
const pathToFolder = path.join(folder, "fresh.txt");
const text = "I am fresh and young";

let folderExists = true;
try {
await fs.access(folder, fs.constants.R_OK);
} catch {
folderExists = false;
}

if (!folderExists) {
await fs.mkdir(folder);
}

let fileExists = true;
try {
await fs.access(pathToFolder);
} catch {
fileExists = false;
}

if (!fileExists) {
await fs.writeFile(pathToFolder, text, "utf-8");
console.log("File created successfully.");
} else {
console.error("FS operation failed: File already exists");
}
};

await create();
await create();
18 changes: 15 additions & 3 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const remove = async () => {
// Write your code here
import { promises as fs } from "fs";
import path from "path";

const removeFile = async () => {
const folder = "src/fs/files/";
const fileToRemove = path.join(folder, "fileToRemove.txt");

try {
await fs.access(fileToRemove);

await fs.unlink(fileToRemove);
} catch (error) {
throw new Error(`FS operation failed: ${error.message}`);
}
};

await remove();
await removeFile();
20 changes: 17 additions & 3 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
const list = async () => {
// Write your code here
import { promises as fs } from "fs";
import path from "path";

const listFiles = async () => {
const folder = "src/fs/";

const filesFolder = path.join(folder, "files");

try {
await fs.access(filesFolder);

const filenames = await fs.readdir(filesFolder);
console.log(filenames);
} catch (error) {
throw new Error(`FS operation failed: ${error.message}`);
}
};

await list();
await listFiles();
19 changes: 16 additions & 3 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
const read = async () => {
// Write your code here
import { promises as fs } from "fs";
import path from "path";

const readFile = async () => {
const folder = "src/fs/files";
const fileToRead = path.join(folder, "fileToRead.txt");

try {
await fs.access(fileToRead);

const content = await fs.readFile(fileToRead, "utf8");
console.log(content);
} catch (error) {
throw new Error(`FS operation failed: ${error.message}`);
}
};

await read();
await readFile();
24 changes: 21 additions & 3 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
const rename = async () => {
// Write your code here
import { promises as fs } from "fs";
import path from "path";

const renameFile = async () => {
const folder = "src/fs/files/";
const sourceFile = path.join(folder, "wrongFilename.txt");
const targetFile = path.join(folder, "properFilename.md");

try {
await fs.access(sourceFile);

try {
await fs.access(targetFile);
throw new Error("FS operation failed: properFilename.md already exists");
} catch {}

await fs.rename(sourceFile, targetFile);
} catch (error) {
throw new Error(`FS operation failed: ${error.message}`);
}
};

await rename();
await renameFile();
21 changes: 19 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import crypto from "crypto";
import fs from "fs";

const calculateHash = async () => {
// Write your code here
const hash = crypto.createHash("sha256");

try {
const hashDigest = hash.digest("hex");

await fs.promises.writeFile(
"src/hash/files/fileToCalculateHashFor.txt",
hashDigest,
"utf-8"
);

console.log(`Hash calculated successfully: ${hashDigest}`);
} catch (error) {
console.error("Error calculating hash:", error);
}
};

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

This file was deleted.

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

import "./files/c.js";

const random = Math.random();

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 ${fileURLToPath(import.meta.url)}`);
console.log(`Path to current directory is ${fileURLToPath(import.meta.url)}`);

const myServer = createServerHttp((_, res) => {
res.end("Request accepted");
});

const PORT = 3000;

console.log(unknownObject);

unknownObject.then((unknownObjectModule) => {
console.log(unknownObjectModule.default);

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log("To terminate it, use Ctrl+C combination");
});
});

export { unknownObject, myServer };
11 changes: 9 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fs from "fs";

const read = async () => {
// Write your code here
const readableStream = fs.createReadStream(
"src/streams/files/fileToRead.txt",
"utf8"
);

readableStream.pipe(process.stdout);
};

await read();
await read();
Loading