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
14 changes: 12 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const parseArgs = () => {
// Write your code here
let res = "";

for (let i = 0; i < process.argv.length; i += 1) {
if(process.argv[i].slice(0, 2) === "--") {
res += process.argv[i].slice(2);
if (process.argv[i + 1] && process.argv[i + 1].slice(0, 2) !== "--") {
res += ` is ${process.argv[i + 1]}, `;
}
}
}
console.log(res.slice(0, -2));
};

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
let res = "";

for (let k in process.env) {
if (k.slice(0, 4) === "RSS_") {
res += `${k}=${process.env[k]}; `;
}
}
console.log(res.slice(0, -2));
};

parseEnv();
parseEnv();
10 changes: 8 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as path from "path";
import * as child_process from "child_process";
import { fileURLToPath } from "url";

const spawnChildProcess = async (args) => {
// Write your code here
const fileName = fileURLToPath(import.meta.url);
const dirPath = path.dirname(fileName);
child_process.fork(path.join(dirPath, "files", "script.js"), args);
};

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

const copy = async () => {
// Write your code here
const fileName = fileURLToPath(import.meta.url);
const srcPath = path.join(path.dirname(fileName), "files");
const destPath = path.join(path.dirname(fileName), "files_copy");

try {
await fs.promises.cp(srcPath, destPath, {
errorOnExist: true,
force: false,
recursive: true,
});
} catch (error) {
throw new Error("FS operation failed");
}
};

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

const create = async () => {
// Write your code here
const fileName = fileURLToPath(import.meta.url);
const filePath = path.join(path.dirname(fileName), "files", "fresh.txt");
const fileContent = "I am fresh and young";

try {
await fs.promises.writeFile(filePath, fileContent, { flag: "wx" });
} catch (error) {
throw new Error("FS operation failed");
}
};

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

const remove = async () => {
// Write your code here
const fileName = fileURLToPath(import.meta.url);
const filePath = path.join(
path.dirname(fileName),
"files",
"fileToRemove.txt"
);

try {
await fs.promises.rm(filePath);
} catch (error) {
throw new Error("FS operation failed");
}
};

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

const list = async () => {
// Write your code here
const fileName = fileURLToPath(import.meta.url);
const folderPath = path.join(path.dirname(fileName), "files");

try {
const files = await fs.promises.readdir(folderPath);
files.map((file) => console.log(file));
} catch (error) {
throw new Error("FS operation failed");
}
};

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

const read = async () => {
// Write your code here
const fileName = fileURLToPath(import.meta.url);
const filePath = path.join(
path.dirname(fileName),
"files",
"fileToRead.txt"
);

try {
const content = await fs.promises.readFile(filePath, "utf-8");
console.log(content);
} catch (error) {
throw new Error("FS operation failed");
}
};

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

const rename = async () => {
// Write your code here
const fileName = fileURLToPath(import.meta.url);
const oldFileName = path.join(
path.dirname(fileName),
"files",
"wrongFilename.txt"
);
const newFileName = path.join(
path.dirname(fileName),
"files",
"properFilename.md"
);

try {
await fs.promises.rename(oldFileName, newFileName);
} catch (error) {
throw new Error("FS operation failed");
}
};

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

const calculateHash = async () => {
// Write your code here
const fileName = fileURLToPath(import.meta.url);
const filePath = path.join(
path.dirname(fileName),
"files",
"fileToCalculateHashFor.txt"
);
const hashFn = crypto.createHash("sha256");
const stream = fs.createReadStream(filePath);
stream.on("readable", () => {
const data = stream.read();
if (data) hashFn.update(data);
else {
console.log(`${hashFn.digest("hex")}`);
}
});
};

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

This file was deleted.

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

import "./files/c.js";

import { fileURLToPath } from "url";

import { createRequire } from "module";
const require = createRequire(import.meta.url);

const fileName = fileURLToPath(import.meta.url);
const dirName = path.dirname(fileName);

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = require(path.join(dirName, "files", "a.json"));
} else {
unknownObject = require(path.join(dirName, "files", "b.json"));
}

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 };
16 changes: 14 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "url";

const read = async () => {
// Write your code here
const fileName = fileURLToPath(import.meta.url);
const filePath = path.join(
path.dirname(fileName),
"files",
"fileToRead.txt"
);

const stream = fs.createReadStream(filePath);
stream.on("data", (chunk) => process.stdout.write(chunk));
};

await read();
await read();
19 changes: 17 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import * as stream from "node:stream";

const transform = async () => {
// Write your code here
stream.pipeline(
process.stdin,
new stream.Transform({
transform(chunk, encoding, callback) {
const chunkStringified = chunk.toString().slice(0, -1);
const reversed = chunkStringified.split("").reverse().join("");
callback(null, reversed + "\n");
},
}),
process.stdout,
(err) => {
if (err) throw err;
}
);
};

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

const write = async () => {
// Write your code here
const fileName = fileURLToPath(import.meta.url);
const filePath = path.join(
path.dirname(fileName),
"files",
"fileToWrite.txt"
);
const stream = fs.createWriteStream(filePath);

process.stdin.on("data", (data) => stream.write(data));
process.on("SIGINT", () => process.exit());
process.on("exit", () => stream.end());
};

await write();
await write();
Loading