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
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 incomingArgs = process.argv.slice(2);
const incomingArgsPairs = incomingArgs.reduce((array, currentArg, index) => {
if (index % 2 === 0) {
array.push(`${currentArg.slice(2)} is ${incomingArgs[index + 1]}`);
}

return array;
}, []);
console.log(incomingArgsPairs.join(", "));
};

parseArgs();
parseArgs();
6 changes: 5 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const parseEnv = () => {
// Write your code here
const envVars = []
for (const key in process.env) {
envVars.push(`RSS_${key}=${process.env[key]}`);
}
console.log(envVars.join('; '))
};

parseEnv();
1 change: 1 addition & 0 deletions src/fs/consts/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FS_ERROR = 'FS operation failed'
11 changes: 11 additions & 0 deletions src/fs/consts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export { FS_ERROR } from "./error.js";
export { FILES_PATH } from "./paths.js";
export {
NEW_FILE_NAME,
FILES_FOLDER_NAME,
COPY_FOLDER_NAME,
WRONG_FILE_NAME,
PROPPER_FILE_NAME,
DELETE_FILE_NAME,
READ_FILE_NAME,
} from "./names.js";
7 changes: 7 additions & 0 deletions src/fs/consts/names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const NEW_FILE_NAME = "fresh.txt";
export const COPY_FOLDER_NAME = "files_copy";
export const FILES_FOLDER_NAME = "files";
export const WRONG_FILE_NAME = 'wrongFilename.txt'
export const PROPPER_FILE_NAME = 'properFilename.md'
export const DELETE_FILE_NAME = 'fileToRemove.txt'
export const READ_FILE_NAME = 'fileToRead.txt'
1 change: 1 addition & 0 deletions src/fs/consts/paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FILES_PATH = 'src/fs/files'
32 changes: 31 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";
import { checkIfExists, throwFSError } from "./utils/index.js";
import { COPY_FOLDER_NAME, FILES_FOLDER_NAME } from "./consts/index.js";

const dirname = path.dirname(fileURLToPath(import.meta.url));
const sourceFolderPath = path.join(dirname, FILES_FOLDER_NAME);
const copyFolderPath = path.join(dirname, COPY_FOLDER_NAME);

const copy = async () => {
// Write your code here
try {
const isSourceFolderExists = await checkIfExists(sourceFolderPath);
const isCopyFolderAlreadyExists = await checkIfExists(copyFolderPath);

if (!isSourceFolderExists || isCopyFolderAlreadyExists) {
throwFSError();
}

await fs.cp(
sourceFolderPath,
copyFolderPath,
{ recursive: true },
(err) => {
if (error) {
throw error;
}
}
);
} catch (error) {
console.error(error);
}
};

await copy();
27 changes: 25 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import fs from "fs/promises";
import path from "path";
import { checkIfExists, throwFSError } from "./utils/index.js";
import { FILES_PATH, NEW_FILE_NAME } from "./consts/index.js";

const filePath = path.join(FILES_PATH, NEW_FILE_NAME);
const fileContent = "I am fresh and young";

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

if (isFileExist) {
throwFSError();
}

await fs.writeFile(filePath, fileContent, (err) => {
if (err) {
throwFSError(`Cannot create a file: ${err}`);
return;
}
});
} catch (err) {
console.error(err);
}
};

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 path from "path";
import { FILES_PATH, DELETE_FILE_NAME } from "./consts/index.js";
import { checkIfExists } from "./utils/index.js";

const fileToDeletePath = path.join(FILES_PATH, DELETE_FILE_NAME);

const remove = async () => {
// Write your code here
try {
const isFileToDeleteExists = await checkIfExists(fileToDeletePath);

if (!isFileToDeleteExists) {
throwFSError();
}

await fs.unlink(fileToDeletePath, function (err) {
if (err) throw err;
});
} catch (err) {
console.error(err);
}
};

await remove();
await remove();
20 changes: 18 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import fs from "fs/promises";
import path from "path";
import { FILES_PATH } from "./consts/index.js";
import { checkIfExists, throwFSError } from "./utils/index.js";

const list = async () => {
// Write your code here
try {
const isFolderExists = await checkIfExists(FILES_PATH);

if (!isFolderExists) {
throwFSError();
}

const filesNames = await fs.readdir(FILES_PATH);
console.log(filesNames);
} catch (err) {
console.error(err);
}
};

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 fs from "fs/promises";
import path from "path";
import { FILES_PATH, READ_FILE_NAME } from "./consts/index.js";
import { checkIfExists, throwFSError } from "./utils/index.js";

const fileToReadPath = path.join(FILES_PATH, READ_FILE_NAME);

const read = async () => {
// Write your code here
try {
const isFileToReadExists = await checkIfExists(fileToReadPath);

if (!isFileToReadExists) {
throwFSError();
}

const fileContent = await fs.readFile(fileToReadPath, "utf8");
console.log(fileContent);
} catch (err) {
console.error(err);
}
};

await read();
await read();
29 changes: 27 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import fs from "fs/promises";
import path from "path";
import {
FILES_PATH,
PROPPER_FILE_NAME,
WRONG_FILE_NAME,
} from "./consts/index.js";
import { checkIfExists, throwFSError } from "./utils/index.js";

const wrongFilePath = path.join(FILES_PATH, WRONG_FILE_NAME);
const propperFilePath = path.join(FILES_PATH, PROPPER_FILE_NAME);

const rename = async () => {
// Write your code here
try {
const isWrongFileExists = await checkIfExists(wrongFilePath);
const isPropperFileExists = await checkIfExists(propperFilePath);

if (!isWrongFileExists || isPropperFileExists) {
throwFSError();
}

await fs.rename(wrongFilePath, propperFilePath, function (err) {
if (err) throw err;
});
} catch (err) {
console.error(err);
}
};

await rename();
await rename();
12 changes: 12 additions & 0 deletions src/fs/utils/checkIfExists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FS_ERROR } from "../consts/index.js";
import fs from "fs/promises";
import { throwFSError } from "./throwFSError.js";

export async function checkIfExists(filePath) {
try {
await fs.access(filePath, fs.constants.F_OK);
return true;
} catch {
return false;
}
}
2 changes: 2 additions & 0 deletions src/fs/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {checkIfExists} from './checkIfExists.js'
export {throwFSError} from './throwFSError.js'
5 changes: 5 additions & 0 deletions src/fs/utils/throwFSError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FS_ERROR } from "../consts/error.js";

export function throwFSError(error = FS_ERROR) {
throw new Error(error)
}
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 crypto, { createHmac } from "crypto";
import fs from "fs";

const filePath = "src/hash/files/fileToCalculateHashFor.txt";

const calculateHash = async () => {
// Write your code here
const hash = await crypto.createHash("sha256");
const stream = await fs.createReadStream(filePath);

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

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

stream.on("error", (error) => {
console.error(error);
});
};

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

This file was deleted.

44 changes: 44 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 unknownObjectPath;

if (random > 0.5) {
unknownObjectPath = "./files/a.json";
} else {
unknownObjectPath = "./files/b.json";
}

const unknownObject = await import(unknownObjectPath, {
assert: { type: "json" },
}).then((m) => m.default);

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

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