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
Binary file added .DS_Store
Binary file not shown.
17 changes: 17 additions & 0 deletions package-lock.json

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

Binary file added src/.DS_Store
Binary file not shown.
7 changes: 6 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2, process.argv.length);

args.forEach((arg, index) => {
if (arg.startsWith("--"))
console.log(`${args[index]} is ${args[index + 1]}`);
});
};

parseArgs();
4 changes: 3 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const parseEnv = () => {
// Write your code here
for (const key in process.env) {
if (key.startsWith("RSS_")) console.log(`${key}=${process.env[key]}`);
}
};

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 "node:child_process";
import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const spawnChildProcess = async (args) => {
// Write your code here
const child = spawn(
"node",
[path.join(__dirname, "files/script.js"), ...args],
{
stdio: ["pipe", "pipe", "inherit"],
}
);

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

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

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

const copy = async () => {
// Write your code here
try {
await fs.mkdir(path.join(__dirname, "files_copy"));
await cp(
path.resolve(__dirname, "files"),
path.resolve(__dirname, "files_copy"),
{ recursive: true }
);
} catch {
throw new Error("fs operation failed");
}
};

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

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

const create = async () => {
// Write your code here
try {
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();
13 changes: 12 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";

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

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

await remove();
1 change: 0 additions & 1 deletion src/fs/files/fileToRemove.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
How dare you!
1 change: 1 addition & 0 deletions src/fs/files/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
1 change: 1 addition & 0 deletions src/fs/files_copy/dontLookAtMe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
What are you looking at?!
7 changes: 7 additions & 0 deletions src/fs/files_copy/fileToRead.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
My content
should
be
printed
into
console
!
1 change: 1 addition & 0 deletions src/fs/files_copy/fileToRemove.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
How dare you are!!!
1 change: 1 addition & 0 deletions src/fs/files_copy/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
1 change: 1 addition & 0 deletions src/fs/files_copy/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Node.js
3 changes: 3 additions & 0 deletions src/fs/files_copy/wrongFilename.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is a file with a wrong filename

Hello from **markdown**!
14 changes: 13 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import path from "path";
import { fileURLToPath } from "url";
import fs from "fs/promises";

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

const list = async () => {
// Write your code here
try {
const files = await fs.readdir(path.resolve(__dirname, "files"));
console.log(files);
} catch {
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 path from "path";
import { fileURLToPath } from "url";
import fs from "fs/promises";

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

const read = async () => {
// Write your code here
try {
const fileContent = await fs.readFile(
path.resolve(__dirname, "files/fileToRead.txt"),
{ encoding: "utf-8" }
);
console.log(fileContent);
} catch {
throw new Error("FS operation failed");
}
};

await read();
29 changes: 28 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import { access } from "fs/promises";
import { constants } from "fs/promises";
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";

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

const rename = async () => {
// Write your code here
try {
const isTargetExist = await access(
path.resolve(__dirname, "files/properFilename.md"),
constants.F_OK
)
.then(() => true)
.catch(() => false);

if (isTargetExist) {
throw new Error();
}

await fs.rename(
path.resolve(__dirname, "files/wrongFilename.txt"),
path.resolve(__dirname, "files/properFilename.md")
);
} catch {
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 crypto from "node:crypto";

import path from "path";
import { cp } from "fs/promises";
import { fileURLToPath } from "url";
import { createReadStream } from "node:fs";

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

const calculateHash = async () => {
// Write your code here
const hash = crypto.createHash("sha256");
const stream = createReadStream(
path.join(__dirname, "files/fileToCalculateHashFor.txt")
);

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

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

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

This file was deleted.

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

import a from "./files/a.json" with { type: "json" };
import b from "./files/b.json" with { type: "json" };

const require = createRequire(import.meta.url);
require("./files/c.js");

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

const random = Math.random();

const unknownObject = random > 0.5 ? a : b

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 default {
unknownObject,
myServer,
};
File renamed without changes.
2 changes: 1 addition & 1 deletion src/streams/files/fileToRead.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This file should be read using Streams API
вфывфывфы
3 changes: 3 additions & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dfgdfg
fdgdfg
dfgdf
17 changes: 16 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { createReadStream } from "node:fs";
import path from "path";
import { fileURLToPath } from "url";

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

const read = async () => {
// Write your code here
const readStream = createReadStream(
path.resolve(__dirname, "files/fileToRead.txt")
);

readStream.pipe(process.stdout, { end: false });

readStream.on("end", () => {
console.log();
});
};

await read();
19 changes: 18 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { createReadStream, createWriteStream } from "node:fs";
import path from "path";
import { fileURLToPath } from "url";
import { Transform } from "node:stream";

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

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

this.push(reversedString);
callback();
},
});

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

await transform();
Loading