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
27 changes: 25 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
const parseArgs = () => {
// Write your code here
};
const [, , ...commandLineArgs] = process.argv;

const parsedArgs = parseCommandLineArgs(commandLineArgs);
printParsedArgs(parsedArgs);
};

const parseCommandLineArgs = (args) => {
const parsedArgs = {};

for (let i = 0; i < args.length; i += 2) {
const argName = cleanArgName(args[i]);
const argValue = args[i + 1];
parsedArgs[argName] = argValue;
}

return parsedArgs;
};

const cleanArgName = (arg) => arg.replace(/^--/, '');

const printParsedArgs = (parsedArgs) => {
for (const [argName, argValue] of Object.entries(parsedArgs)) {
console.log(`${argName} is ${argValue}`);
}
};

parseArgs();
14 changes: 13 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const parseEnv = () => {
// Write your code here
const parsedVars = {};

for (const key in process.env) {
if (key.startsWith('RSS_')) {
const varName = key;
const varValue = process.env[key];
parsedVars[varName] = varValue;
}
}

for (const [varName, varValue] of Object.entries(parsedVars)) {
console.log(`${varName}=${varValue}`);
}
};

parseEnv();
16 changes: 13 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { spawn } from "child_process";
import { fileURLToPath } from "url";
import path from "path";
import { dirname } from "path";

const spawnChildProcess = async (args) => {
// Write your code here
const currentDir = dirname(fileURLToPath(import.meta.url));

const childModule = path.resolve(currentDir, "files", "script.js");
const childProcess = spawn("node", [childModule, ...args]);

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["node", "js"]);
37 changes: 36 additions & 1 deletion src/fs/copy.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't write error message "Error accessing folders:" when source folder doesn't exist
("if files folder doesn't exist Error with message FS operation failed must be thrown")

Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
import fs from "fs/promises";
import { fileURLToPath } from "url";
import path from "path";
import { dirname } from "path";

const copy = async () => {
// Write your code here
const currentModulePath = dirname(fileURLToPath(import.meta.url));

const sourceFolder = path.resolve(currentModulePath, "files");
const targetFolder = path.resolve(currentModulePath, "files_copy");

try {
await fs.access(sourceFolder);
await fs.access(targetFolder);

console.error("Error: Target folder already exists.");
} catch (error) {
if (error.code === "ENOENT") {
try {
await fs.mkdir(targetFolder);
const files = await fs.readdir(sourceFolder);

for (const file of files) {
const sourceFilePath = path.join(sourceFolder, file);
const targetFilePath = path.join(targetFolder, file);

await fs.copyFile(sourceFilePath, targetFilePath);
}

console.log("Copy operation complete.");
} catch (copyError) {
console.error("Copy operation failed:", copyError);
}
} else {
console.error("Error accessing folders:", error);
}
}
};

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

const create = async () => {
// Write your code here
const fileName = "fresh.txt";
const content = "I am fresh and young";

const currentModulePath = dirname(fileURLToPath(import.meta.url));

const filePath = path.resolve(currentModulePath, "files", fileName);

try {
await fs.writeFile(filePath, content, { flag: "wx" });
console.log("Create operation complete.");
} catch (error) {
if (error.code === "EEXIST") {
console.error("Error: File already exists.");
Copy link

@ElenVasileva ElenVasileva Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From task description:

Error with message FS operation failed must be thrown

(console.log instead of throw error, here and everywhere)

} else {
console.error("Create operation failed:", error);
}
}
};

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

const remove = async () => {
// Write your code here
const fileName = "fileToRemove.txt";

const currentModulePath = dirname(fileURLToPath(import.meta.url));
const filePath = path.resolve(currentModulePath, "files", fileName);

try {
await fs.unlink(filePath);
console.log("Delete operation complete.");
} catch (error) {
if (error.code === "ENOENT") {
console.error("Error: File does not exist.");
} else {
console.error("Delete operation failed:", error);
}
}
};

await remove();
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!
24 changes: 22 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import fs from "fs/promises";
import { fileURLToPath } from "url";
import path from "path";
import { dirname } from "path";

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

try {
const files = await fs.readdir(folderPath);
console.log('Files in the "files" folder:');
files.forEach((file) => {
console.log(file);
});
} catch (error) {
if (error.code === "ENOENT") {
console.error('Error: "files" folder does not exist.');
} else {
console.error("List operation failed:", error);
}
}
};

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

const read = async () => {
// Write your code here
const fileName = "fileToRead.txt";

const currentModulePath = dirname(fileURLToPath(import.meta.url));
const filePath = path.resolve(currentModulePath, "files", fileName);

try {
const content = await fs.readFile(filePath, "utf-8");
console.log(`Content of ${fileName}:`);
console.log(content);
} catch (error) {
if (error.code === "ENOENT") {
console.error(`Error: File ${fileName} does not exist.`);
} else {
console.error("Read operation failed:", error);
}
}
};

await read();
await read();
47 changes: 45 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
import fs from "fs/promises";
import { fileURLToPath } from "url";
import path from "path";
import { dirname } from "path";

const fileExists = async (filePath) => {
try {
await fs.access(filePath);
return true;
} catch (error) {
if (error.code === "ENOENT") {
return false;
}
throw error;
}
};

const rename = async () => {
// Write your code here
const fileName = "wrongFilename.txt";
const fileNameChanged = "properFilename.md";

const currentModulePath = dirname(fileURLToPath(import.meta.url));

const sourceFilePath = path.resolve(currentModulePath, "files", fileName);
const targetFilePath = path.resolve(
currentModulePath,
"files",
fileNameChanged
);

try {
if (
(await fileExists(sourceFilePath)) &&
!(await fileExists(targetFilePath))
) {
await fs.rename(sourceFilePath, targetFilePath);
console.log("Rename operation complete.");
} else {
throw new Error(
"Error: Target file already exists or source file does not exist."
);
}
} catch (error) {
console.error(error.message);
}
};

await rename();
await rename();
6 changes: 3 additions & 3 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


const calculateHash = async () => {
// Write your code here

};

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

This file was deleted.

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

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

let unknownObject;

if (random > 0.5) {
unknownObject = await import('./files/a.json', {
assert: {
type: "json",
}
})
} else {
unknownObject = await import('./files/b.json', {
assert: {
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 ${__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 };
Loading