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

const resArr = [];

for (let i = 0; i < props.length; i += 2) {
resArr.push(`${props[i].slice(2)} is ${props[i + 1]}`);
}

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

parseArgs();
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 envs = process.env;
const entries = Object.entries(envs);

console.log(
entries
.filter((env) => env[0].startsWith("RSS_"))
.map((env) => `${env[0]}=${env[1]};`)
.join(" ")
);
};

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

const filePath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files",
"script.js"
);

const spawnChildProcess = async (args) => {
// Write your code here
const child = fork(filePath, args, { stdio: "pipe" });

process.stdin.on("data", (chunk) => {
const chunkStr = chunk.toString();
child.stdin.write(chunkStr);
});

child.stdout.on("data", (chunk) => {
const chunkStr = chunk.toString();
process.stdout.write(chunkStr);
});

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess([1, 2, 3]);
42 changes: 41 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
import * as fsPromises from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

const folderPathFrom = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files"
);

const folderPathTo = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files_copy"
);

const copy = async () => {
// Write your code here
try {
await fsPromises.access(folderPathFrom);
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
}

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

try {
await fsPromises.access(folderPathTo);
throw new Error("FS operation failed");
} catch (e) {
if (e.code !== "ENOENT") throw e;

await fsPromises.mkdir(folderPathTo);

const files = await fsPromises.readdir(folderPathFrom);

files.forEach(async (file) => {
await fsPromises.copyFile(
path.join(folderPathFrom, file),
path.join(folderPathTo, file)
);
});
}
};

await copy();
23 changes: 21 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import * as fsPromises from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

const filePath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files",
"fresh.txt"
);
const textContent = "I am fresh and young";

const create = async () => {
// Write your code here
try {
await fsPromises.stat(filePath);
throw new Error("FS operation failed");
} catch (e) {
if (e.code !== "ENOENT") {
throw new Error(e);
}
await fsPromises.writeFile(filePath, textContent);
}
};

await create();
await create();
19 changes: 17 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import * as fsPromises from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

const fileToRemovePath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files",
"fileToRemove.txt"
);

const remove = async () => {
// Write your code here
try {
await fsPromises.rm(fileToRemovePath);
} catch (e) {
if (e.code !== "ENOENT") throw e;
throw new Error("FS operation failed");
}
};

await remove();
await remove();
22 changes: 20 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import * as fsPromises from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

const folderPath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files"
);

const list = async () => {
// Write your code here
try {
const files = await fsPromises.readdir(folderPath);
console.log(files);
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
}

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

await list();
await list();
22 changes: 20 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import * as fsPromises from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

const filePath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files",
"fileToRead.txt"
);

const read = async () => {
// Write your code here
try {
const fileContent = await fsPromises.readFile(filePath, {
encoding: "utf-8",
});
console.log(fileContent);
} catch (e) {
if (e.code !== "ENOENT") throw e;
throw new Error("FS operation failed");
}
};

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

const oldPath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files",
"wrongFilename.txt"
);

const newPath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files",
"properFilename.md"
);

const rename = async () => {
// Write your code here
try {
await fsPromises.stat(oldPath);
} catch (e) {
if (e.code !== "ENOENT") throw e;
throw new Error("FS operation failed");
}

try {
await fsPromises.stat(newPath);
throw new Error("FS operation failed");
} catch (e) {
if (e.code !== "ENOENT") throw e;

await fsPromises.rename(oldPath, newPath);
}
};

await rename();
await rename();
25 changes: 23 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { createHash } from "node:crypto";
import * as fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const filePath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files",
"fileToCalculateHashFor.txt"
);

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

const readStream = fs.createReadStream(filePath, { highWaterMark: 2 });

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

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

await calculateHash();
await calculateHash();
39 changes: 39 additions & 0 deletions src/modules/cjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 = await import("./files/a.json", { with: { type: "json" } });
} else {
unknownObject = await import("./files/b.json", { with: { 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 ${path.dirname(fileURLToPath(import.meta.url))}`
);

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

const PORT = 3000;

console.log(unknownObject.default);

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

export { unknownObject, myServer };
15 changes: 13 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import * as fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const filePath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files",
"fileToRead.txt"
);

const read = async () => {
// Write your code here
const readStream = fs.createReadStream(filePath);
readStream.pipe(process.stdout);
};

await read();
await read();
13 changes: 11 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import Stream from "node:stream";

const transform = async () => {
// Write your code here
const transformStream = new Stream.Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().split("").reverse().join(""));
callback();
},
});

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

await transform();
await transform();
15 changes: 13 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import * as fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const filePath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"files",
"fileToWrite.txt"
);

const write = async () => {
// Write your code here
const writeStream = fs.createWriteStream(filePath);
process.stdin.pipe(writeStream);
};

await write();
await write();
Loading