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
15 changes: 14 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
const handleArgs = (acc, arg, index, arr) =>
arg.substring(0, 2) === "--"
? [
...acc,
`${arg.slice(2)} is ${
arr[index + 1].substring(0, 2) !== "__" ? arr[index + 1] : "no"
}`,
]
: acc;

const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const handledArgs = args.reduce(handleArgs, []).join(", ");

console.log(handledArgs);
};

parseArgs();
11 changes: 10 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const parseEnv = () => {
// Write your code here
const envVariables = process.env;
const RSSVariables = Object.keys(envVariables).filter((variable) =>
variable.includes("RSS_")
);

console.log(
RSSVariables.map(
(variable) => `${variable}=${envVariables[variable]}`
).join("; ")
);
};

parseEnv();
15 changes: 13 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import path from "path";
import { stdin } from "process";
import { spawn } from "child_process";
import { getDirName } from "../utils.js";

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

const spawnChildProcess = async (args) => {
// Write your code here
const childProcess = spawn("node", [filePath, ...args]);

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(process.argv.slice(2));
33 changes: 32 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
import path from "path";
import { cp } from "fs/promises";
import { getDirName, isExists } from "../utils.js";

const __dirname = getDirName(import.meta.url);
const oldPath = path.join(__dirname, "files");
const newPath = path.join(__dirname, "files_copy");

const copy = async () => {
// Write your code here
try {
const isOldPathExists = await isExists(oldPath);
const isNewPathExists = await isExists(newPath);

if (!isOldPathExists) {
throw new Error("FS operation failed: no folder to copy!");
}

if (isNewPathExists) {
throw new Error(
"FS operation failed: folder 'files_copy' already exists!"
);
}

await cp(oldPath, newPath, {
recursive: true,
force: false,
errorOnExist: true,
});

console.log("Directory was successfully copied!");
} catch (error) {
console.error(error);
}
};

await copy();
21 changes: 20 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import path from "path";
import { writeFile } from "fs/promises";
import { getDirName, isExists } from "../utils.js";

const __dirname = getDirName(import.meta.url);
const filePath = path.join(__dirname, "files", "fresh.txt");

const create = async () => {
// Write your code here
try {
const isFileExists = await isExists(filePath);

if (isFileExists) {
throw new Error("FS operation failed: file already exists!");
}

await writeFile(filePath, "I am fresh and young", { flag: "wx" });

console.log("File was successfully created!");
} catch (error) {
console.error(error);
}
};

await create();
21 changes: 20 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import path from "path";
import { unlink } from "fs/promises";
import { getDirName, isExists } from "../utils.js";

const __dirname = getDirName(import.meta.url);
const filePath = path.join(__dirname, "files", "fileToRemove.txt");

const remove = async () => {
// Write your code here
try {
const isFileExists = await isExists(filePath);

if (!isFileExists) {
throw new Error("FS operation failed: no file to delete!");
}

await unlink(filePath);

console.log("File was successfully deleted!");
} catch (error) {
console.error(error);
}
};

await remove();
21 changes: 20 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import path from "path";
import { readdir } from "fs/promises";
import { getDirName, isExists } from "../utils.js";

const __dirname = getDirName(import.meta.url);
const dirPath = path.join(__dirname, "files");

const list = async () => {
// Write your code here
try {
const isDirExists = await isExists(dirPath);

if (!isDirExists) {
throw new Error("FS operation failed: no folder to read!");
}

const files = await readdir(dirPath);

console.log(files);
} catch (error) {
console.error(error);
}
};

await list();
21 changes: 20 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import path from "path";
import { readFile } from "fs/promises";
import { getDirName, isExists } from "../utils.js";

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

const read = async () => {
// Write your code here
try {
const isFileExists = await isExists(filePath);

if (!isFileExists) {
throw new Error("FS operation failed: no file to read!");
}

const content = await readFile(filePath, { encoding: "utf-8" });

console.log(content);
} catch (error) {
console.error(error);
}
};

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 path from "path";
import { rename as renameFile } from "fs/promises";
import { getDirName, isExists } from "../utils.js";

const __dirname = getDirName(import.meta.url);
const oldFilePath = path.join(__dirname, "files", "wrongFilename.txt");
const newFilePath = path.join(__dirname, "files", "properFilename.md");

const rename = async () => {
// Write your code here
try {
const isOldFileExists = await isExists(oldFilePath);
const isNewFileExists = await isExists(newFilePath);

if (!isOldFileExists) {
throw new Error("FS operation failed: no file to rename!");
}

if (isNewFileExists) {
throw new Error(
"FS operation failed: file 'properFilename.md' already exists!"
);
}

await renameFile(oldFilePath, newFilePath);

console.log("File was successfully renamed!");
} catch (error) {
console.error(error);
}
};

await rename();
15 changes: 14 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import path from "path";
import { readFile } from "fs/promises";
import { createHash } from "crypto";
import { getDirName } from "../utils.js";

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

const calculateHash = async () => {
// Write your code here
const fileBuffer = await readFile(filePath);

const hash = createHash("sha256");
const hex = hash.update(fileBuffer).digest("hex");

console.log(hex);
};

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

This file was deleted.

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

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

const random = Math.random();

const unknownObject =
random > 0.5 ? require("./files/a.json") : require("./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 };
12 changes: 11 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import path from "path";
import { createReadStream } from "fs";
import { stdout } from "process";

import { getDirName } from "../utils.js";

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

const read = async () => {
// Write your code here
const stream = createReadStream(filePath, { encoding: "utf-8" });
stream.pipe(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 "stream";
import { stdin, stdout } from "process";

const reverseString = new Transform({
transform(chunk, _, callback) {
callback(null, `${String(chunk).split("").reverse().join("")}\n`);
},
});

const transform = async () => {
// Write your code here
stdin.pipe(reverseString).pipe(stdout);
};

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

import { getDirName } from "../utils.js";

const __dirname = getDirName(import.meta.url);

const filePath = path.join(__dirname, "files", "fileToWrite.txt");

const write = async () => {
// Write your code here
const stream = createWriteStream(filePath, { encoding: "utf-8" });
process.stdin.pipe(stream);
};

await write();
15 changes: 15 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import path from "path";
import { stat } from "fs/promises";
import { fileURLToPath } from "url";

export const isExists = async (filePath) => {
try {
await stat(filePath);

return true;
} catch (error) {
return false;
}
};

export const getDirName = (url) => path.dirname(fileURLToPath(url));
Loading