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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 20 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { argv } from "process";

const parseArgs = () => {
// Write your code here
const args = argv.slice(2);
const argsObj = {};

for (let i = 0; i < args.length; i += 2) {
argsObj[args[i].slice(2)] = args[i + 1];
}

let msg = "";
const argsKeys = Object.keys(argsObj);
for (let i = 0; i < argsKeys.length; i++) {
if (i + 1 === argsKeys.length) {
msg += `${argsKeys[i]} is ${argsObj[argsKeys[i]]}`;
break;
}
msg += `${argsKeys[i]} is ${argsObj[argsKeys[i]]}, `;
}

console.log(msg);
};

parseArgs();
17 changes: 16 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { env } from "process";

const parseEnv = () => {
// Write your code here
const variables = Object.keys(env).filter((value) =>
value.startsWith("RSS_")
);

let msg = "";
for (let i = 0; i < variables.length; i++) {
if (i + 1 === variables.length) {
msg += `${variables[i]}=${env[variables[i]]}`;
break;
}
msg += `${variables[i]}=${env[variables[i]]}; `;
}

console.log(msg);
};

parseEnv();
14 changes: 11 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { fork } from "node:child_process";

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

const spawnChildProcess = async (args) => {
// Write your code here
const filesDir = join(__dirname, "files");
const scriptFilePath = join(filesDir, "script.js");

fork(scriptFilePath, args);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["mem, wow, kek"]);
20 changes: 19 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { cp } from "fs/promises";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

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

const copy = async () => {
// Write your code here
const srcDir = join(__dirname, "files");
const destDir = join(__dirname, "files_copy");
try {
await cp(srcDir, destDir, {
recursive: true,
errorOnExist: true,
force: false,
});
} catch (error) {
if (error.code === "ENOENT" || error.code === "ERR_FS_CP_EEXIST") {
throw new Error("FS operation failed");
}
}
};

await copy();
18 changes: 17 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { writeFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { dirname, join } from "path";

const create = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const filesDir = join(__dirname, "files");
const targetPath = join(filesDir, "fresh.txt");

try {
await writeFile(targetPath, "I am fresh and young", {
flag: "wx",
});
} catch (error) {
if (error.code === "EEXIST") {
throw new Error("FS operation failed");
}
}
};

await create();
17 changes: 16 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { rm } from "fs/promises";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

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

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

try {
await rm(targetFilePath);
} catch (error) {
if (error.code === "ENOENT") {
throw new Error("FS operation failed");
}
}
};

await remove();
17 changes: 16 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { readdir } from "fs/promises";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

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

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

try {
const files = await readdir(filesDir);
console.log(files);
} catch (error) {
if (error.code === "ENOENT") {
throw new Error("FS operation failed");
}
}
};

await list();
18 changes: 17 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { readFile } from "fs/promises";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

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

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

try {
const fileContent = await readFile(targetFilePath, { encoding: "utf-8" });
console.log(fileContent);
} catch (error) {
if (error.code === "ENOENT") {
throw new Error("FS operation failed");
}
}
};

await read();
27 changes: 26 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { access, rename as renameFn } from "fs/promises";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

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

const rename = async () => {
// Write your code here
const filesDir = join(__dirname, "files");
const srcFilePath = join(filesDir, "wrongFilename.txt");
const targetFilePath = join(filesDir, "properFilename.md");

try {
await access(targetFilePath);
throw new Error("FS operation faile");
} catch (error) {
if (error.code !== "ENOENT") {
throw error;
}
}

try {
await renameFn(srcFilePath, targetFilePath);
} catch (error) {
if (error.code === "ENOENT") {
throw new Error("FS operation failed");
}
}
};

await rename();
21 changes: 20 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { createHash } from "crypto";
import { createReadStream } from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

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

const calculateHash = async () => {
// Write your code here
const filesDir = join(__dirname, "files");
const srcFilePath = join(filesDir, "fileToCalculateHashFor.txt");

const stream = createReadStream(srcFilePath);

return new Promise((resolve) => {
const hash = createHash("sha256");
stream.on("data", (data) => hash.update(data, "utf-8"));
stream.on("end", () => {
console.log(hash.digest("hex"));
resolve();
});
});
};

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

This file was deleted.

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

import "./files/c.cjs";

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

const random = Math.random();

const jsonPath = random > 0.5 ? "./files/a.json" : "./files/b.json";
const unknownObject = (
await import(path.join(__dirname, jsonPath), { with: { type: "json" } })
).default;

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 };
20 changes: 19 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { createReadStream } from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { stdout } from "process";

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

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

const readStream = createReadStream(srcFilePath);

return new Promise((resolve) => {
readStream.on("data", (data) => stdout.write(data));
readStream.on("end", () => {
stdout.write("\n");
resolve();
});
});
};

await read();
14 changes: 13 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { stdin, stdout } from "process";
import { Transform } from "stream";
import { pipeline } from "stream/promises";

const transform = async () => {
// Write your code here
const transformStream = new Transform({
transform(data, _, cb) {
const result = data.toString().split("").reverse().join("");

cb(null, result + "\n");
},
});

await pipeline(stdin, transformStream, stdout);
};

await transform();
13 changes: 12 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { createWriteStream } from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { stdin } from "process";
import { pipeline } from "stream/promises";

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

const write = async () => {
// Write your code here
const filesDir = join(__dirname, "files");
const targetFilePath = join(filesDir, "fileToWrite.txt");

await pipeline(stdin, createWriteStream(targetFilePath));
};

await write();
Loading