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
13 changes: 12 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const parsedArgs = [];

for (let i = 0; i < args.length; i += 2) {
if (i + 1 < args.length) {
const propName = args[i].replace("--", "");
const value = args[i + 1];
parsedArgs.push(`${propName} is ${value}`);
}
}

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

parseArgs();
10 changes: 9 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const parseEnv = () => {
// Write your code here
const prefix = "RSS_";
const entries = Object.entries(process.env)
.filter(([k]) => k.startsWith(prefix))
.map(([k, v]) => `${k}=${v}`);
if (entries.length === 0) {
console.log("");
return;
}
console.log(entries.join("; "));
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const scriptPath = join(process.cwd(), "src", "cp", "files", "script.js");

const child = spawn("node", [scriptPath, ...args], {
stdio: ["inherit", "inherit", "inherit", "ipc"],
});

// Forward stdin to child process
process.stdin.pipe(child.stdin);

// Forward child stdout to parent stdout
child.stdout.pipe(process.stdout);

// Handle child process events
child.on("error", (error) => {
console.error("Child process error:", error);
});

child.on("exit", (code) => {
process.exit(code);
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["someArgument1", "someArgument2"]);
30 changes: 29 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import { cp, access } from "node:fs/promises";
import { join } from "node:path";

const copy = async () => {
// Write your code here
const sourcePath = join(process.cwd(), "src", "fs", "files");
const destPath = join(process.cwd(), "src", "fs", "files_copy");

try {
// Check if files directory exists
await access(sourcePath);

// Check if files_copy directory exists
try {
await access(destPath);
throw new Error("FS operation failed");
} catch (error) {
if (error.code === "ENOENT") {
// do copy
await cp(sourcePath, destPath, { recursive: true });
} else {
throw new Error("FS operation failed");
}
}
} catch (error) {
if (error.message === "FS operation failed") {
throw error;
}

throw new Error("FS operation failed");
}
};

await copy();
23 changes: 22 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { writeFile, access } from "node:fs/promises";
import { join } from "node:path";

const create = async () => {
// Write your code here
const filePath = join(process.cwd(), "src", "fs", "files", "fresh.txt");

try {
// Check if file exists
await access(filePath);
throw new Error("FS operation failed");
} catch (error) {
if (error.code === "ENOENT") {
// File doesn't exist, create
try {
await writeFile(filePath, "I am fresh and young");
} catch (writeError) {
throw new Error("FS operation failed");
}
} else {
// other
throw new Error("FS operation failed");
}
}
};

await create();
22 changes: 21 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import { unlink, access } from "node:fs/promises";
import { join } from "node:path";

const remove = async () => {
// Write your code here
const filePath = join(
process.cwd(),
"src",
"fs",
"files",
"fileToRemove.txt"
);

try {
await access(filePath);
await unlink(filePath);
} catch (error) {
if (error.code === "ENOENT") {
throw new Error("FS operation failed");
} else {
throw new Error("FS operation failed");
}
}
};

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

const list = async () => {
// Write your code here
const dirPath = join(process.cwd(), "src", "fs", "files");

try {
await access(dirPath);
const files = await readdir(dirPath);
console.log(files);
} catch (error) {
if (error.code === "ENOENT") {
// Directory doesn't exist, error
throw new Error("FS operation failed");
} else {
throw new Error("FS operation failed");
}
}
};

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

const read = async () => {
// Write your code here
const filePath = join(process.cwd(), "src", "fs", "files", "fileToRead.txt");

try {
await access(filePath);
const content = await readFile(filePath, "utf8");
console.log(content);
} catch (error) {
if (error.code === "ENOENT") {
throw new Error("FS operation failed");
} else {
throw new Error("FS operation failed");
}
}
};

await read();
40 changes: 39 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
import { rename, access } from "node:fs/promises";
import { join } from "node:path";

const rename = async () => {
// Write your code here
const oldPath = join(
process.cwd(),
"src",
"fs",
"files",
"wrongFilename.txt"
);
const newPath = join(
process.cwd(),
"src",
"fs",
"files",
"properFilename.md"
);

try {
// Check if wrongFilename.txt exists
await access(oldPath);

// Check if properFilename.md file exists
try {
await access(newPath);
throw new Error("FS operation failed");
} catch (error) {
if (error.code === "ENOENT") {
await rename(oldPath, newPath);
} else {
throw new Error("FS operation failed");
}
}
} catch (error) {
if (error.message === "FS operation failed") {
throw error;
}
throw new Error("FS operation failed");
}
};

await rename();
27 changes: 26 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { createReadStream } from "node:fs";
import { createHash } from "node:crypto";
import { join } from "node:path";

const calculateHash = async () => {
// Write your code here
const filePath = join(
process.cwd(),
"src",
"hash",
"files",
"fileToCalculateHashFor.txt"
);

const hash = createHash("sha256");
const stream = createReadStream(filePath);

stream.on("data", (chunk) => {
hash.update(chunk);
});

stream.on("end", () => {
console.log(hash.digest("hex"));
});

stream.on("error", (error) => {
console.error("Error reading file:", error);
});
};

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

import("./files/c.cjs");

const random = Math.random();

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

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.sep}"`);

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

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 };
14 changes: 13 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { createReadStream } from "node:fs";
import { join } from "node:path";

const read = async () => {
// Write your code here
const filePath = join(
process.cwd(),
"src",
"streams",
"files",
"fileToRead.txt"
);

const stream = createReadStream(filePath);
stream.pipe(process.stdout);
};

await read();
11 changes: 10 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Transform } from "node:stream";

const transform = async () => {
// Write your code here
const reverseTransform = new Transform({
transform(chunk, encoding, callback) {
const reversed = chunk.toString().split("").reverse().join("");
callback(null, reversed);
},
});

process.stdin.pipe(reverseTransform).pipe(process.stdout);
};

await transform();
14 changes: 13 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { createWriteStream } from "node:fs";
import { join } from "node:path";

const write = async () => {
// Write your code here
const filePath = join(
process.cwd(),
"src",
"streams",
"files",
"fileToWrite.txt"
);

const stream = createWriteStream(filePath);
process.stdin.pipe(stream);
};

await write();
Loading