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

for (let i = 0; i < args.length; i++) {
if (args[i].startsWith("--")) {
resultsPairs.push(
`\x1b[32m${args[i].slice(2)}\x1b[0m is \x1b[33m${args[i + 1]}\x1b[0m`
);
i++;
}
}
if (resultsPairs.length) {
console.log(
`\x1b[36m Command cli arg${
resultsPairs.length > 1 ? "s are" : " is"
}: \x1b[0m \n`
);
console.log(resultsPairs.join(", "));
}
};

parseArgs();
12 changes: 11 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const parseEnv = () => {
// Write your code here
const prefix = "RSS_";

const props = Object.entries(process.env).filter(x =>
x[0].startsWith(prefix)
);

console.log("\x1b[36m env variables start with \x1b[0m", `"${prefix}":\n`);
const result = props
.map(prop => `\x1b[32m${prop[0]}\x1b[0m=\x1b[33m${prop[1]}\x1b[0m`)
.join("; ");
console.log(result);
};

parseEnv();
3 changes: 3 additions & 0 deletions src/common/consts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const createFileText = 'I am fresh and young';

export const fsErrorTest = 'FS operation failed';
10 changes: 10 additions & 0 deletions src/common/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import path from "path";
import { fileURLToPath } from "url";

export const getPath = (metaUrl, relativePath) => {
return path.join(path.dirname(fileURLToPath(metaUrl)), relativePath);
};

export const getFilePath = metaUrl => fileURLToPath(metaUrl);

export const getDirPath = metaUrl => path.dirname(fileURLToPath(metaUrl));
14 changes: 11 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
const spawnChildProcess = async (args) => {
// Write your code here
import { fork } from "child_process";
import { getPath } from "../common/utils.js";

const scriptFilePath = getPath(import.meta.url, "./files/script.js");

const spawnChildProcess = async args => {
const child = fork(scriptFilePath, args, { silent: true });

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["someArgument1", "someArgument2", "...", 42, 77, 84]);
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 { mkdir, access, constants, cp } from "node:fs/promises";
import { getPath } from "../common/utils.js";
import { fsErrorTest } from "../common/consts.js";

const copy = async () => {
// Write your code here
try {
const inDirPath = getPath(import.meta.url, "./files");
const outDirPath = getPath(import.meta.url, "./files_copy");

await access(inDirPath, constants.R_OK);
await mkdir(outDirPath, { recursive: false });
await cp(inDirPath, outDirPath, {
recursive: true,
force: false,
errorOnExist: true,
});
} catch (error) {
throw new Error(fsErrorTest);
}
};

await copy();
12 changes: 11 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { writeFile } from "node:fs/promises";
import { getPath } from "../common/utils.js";
import { createFileText, fsErrorTest } from "../common/consts.js";

const create = async () => {
// Write your code here
const filename = getPath(import.meta.url, "./files/fresh.txt");

try {
await writeFile(filename, createFileText, { flag: "wx" });
} catch (err) {
throw new Error(fsErrorTest);
}
};

await create();
17 changes: 16 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { unlink, stat } from "node:fs/promises";
import { getPath } from "../common/utils.js";
import { fsErrorTest } from "../common/consts.js";

const remove = async () => {
// Write your code here
try {
const deleteFilePath = getPath(import.meta.url, "./files/fileToRemove.txt");
const isInputFile = await stat(deleteFilePath).then(x => x.isFile());

if (!isInputFile) {
throw new Error(fsErrorTest);
}

await unlink(deleteFilePath);
} catch (error) {
throw new Error(fsErrorTest);
}
};

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 } from "node:fs/promises";
import { getPath } from "../common/utils.js";
import { fsErrorTest } from "../common/consts.js";

const list = async () => {
// Write your code here
try {
const sourceFolderPath = getPath(import.meta.url, "./files");
const dirents = await readdir(sourceFolderPath, { withFileTypes: true });
const fileNames = dirents.filter(x => x.isFile()).map(x => x.name);

console.log("\x1b[36m%s\x1b[0m", "Files:\n");
for (const fileName of fileNames) {
console.log("\x1b[33m%s\x1b[0m", fileName);
}
console.log("\x1b[0m");
} catch (error) {
throw new Error(fsErrorTest);
}
};

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 { readFile, stat } from "node:fs/promises";
import { getPath } from "../common/utils.js";
import { fsErrorTest } from "../common/consts.js";

const read = async () => {
// Write your code here
try {
const sourceFilePath = getPath(import.meta.url, "./files/fileToRead.txt");
const isInputFile = await stat(sourceFilePath).then(x => x.isFile());

if (!isInputFile) {
throw new Error(fsErrorTest);
}

const fileContent = await readFile(sourceFilePath);

console.log("\x1b[36m%s\x1b[0m", "File content:\n");
console.log("\x1b[33m%s\x1b[0m", fileContent);
console.log("\x1b[0m");
} catch (error) {
throw new Error(fsErrorTest);
}
};

await read();
24 changes: 23 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { rename as fsRename, access, constants, stat } from "node:fs/promises";
import { getPath } from "../common/utils.js";
import { fsErrorTest } from "../common/consts.js";

const rename = async () => {
// Write your code here
try {
let isOutputFileExists = false;
const wrongFilePath = getPath(import.meta.url, "./files/wrongFilename.txt");
const rightFilePath = getPath(import.meta.url, "./files/properFilename.md");
const isInputFile = await stat(wrongFilePath).then(x => x.isFile());

try {
await access(rightFilePath, constants.F_OK);
isOutputFileExists = true;
} catch (error) {}

if (isOutputFileExists || !isInputFile) {
throw new Error(fsErrorTest);
}

await fsRename(wrongFilePath, rightFilePath);
} catch (error) {
throw new Error(fsErrorTest);
}
};

await rename();
34 changes: 33 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import { createReadStream } from "node:fs";
import { stat } from "node:fs/promises";
import { createHash } from "node:crypto";
import { getPath } from "../common/utils.js";
import { fsErrorTest } from "../common/consts.js";

const calculateHash = async () => {
// Write your code here
const filename = getPath(
import.meta.url,
"./files/fileToCalculateHashFor.txt"
);
try {
const isInputFile = await stat(filename).then(x => x.isFile());

if (!isInputFile) {
throw new Error(fsErrorTest);
}

const hash = createHash("sha256");
createReadStream(filename)
.pipe(hash)
.on("finish", () => {
console.log(
`Hash of file \x1b[36m ${filename} \x1b[0m is \n \x1b[33m${hash.digest(
"hex"
)}\x1b[0m`
);
})
.on("error", () => {
throw new Error(fsErrorTest);
});
} catch (error) {
throw new Error(fsErrorTest);
}
};

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

This file was deleted.

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

const random = Math.random();
const filepath = random > 0.5 ? "./files/a.json" : "./files/b.json";

const newRequire = createRequire(import.meta.url);

let unknownObject = newRequire(filepath);

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

console.log(`Path to current file is ${getFilePath(import.meta.url)}`);
console.log(`Path to current directory is ${getDirPath(import.meta.url)}`);

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 { myServer, unknownObject };
29 changes: 28 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import { createReadStream } from "node:fs";
import { stat } from "node:fs/promises";
import { getPath } from "../common/utils.js";
import { fsErrorTest } from "../common/consts.js";

const read = async () => {
// Write your code here
const filename = getPath(import.meta.url, "./files/fileToRead.txt");
try {
const isInputFile = await stat(filename).then(x => x.isFile());

if (!isInputFile) {
throw new Error(fsErrorTest);
}

createReadStream(filename)
.on("end", () => {
console.log("");
})
.on("error", () => {
throw new Error(fsErrorTest);
})
.pipe(process.stdout)
.on("error", () => {
throw new Error(fsErrorTest);
});

} catch (error) {
throw new Error(fsErrorTest);
}
};

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

class MyTransform extends Transform {
_transform(chunk, encoding, callback) {
try {
const resultString =
chunk.toString("utf-8").split("").reverse().join("") + "\n";
callback(null, resultString);
} catch (err) {
callback(err);
}
}
}

const transform = async () => {
// Write your code here
const myTransformStream = new MyTransform();
pipeline(process.stdin, myTransformStream, process.stdout, err => {
if (err) {
throw new Error(fsErrorTest);
}
});
};

await transform();
Loading