Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v24.10.0
17 changes: 17 additions & 0 deletions package-lock.json

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

9 changes: 9 additions & 0 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);

const result = [];
for (let i = 0; i < args.length; i++) {
if (args[i].startsWith("--")) {
result.push(`${args[i].replace("--", "")} is ${args[i + 1]}`);
}
}
console.log(result.join(', '));
};

parseArgs();
5 changes: 4 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const parseEnv = () => {
// Write your code here
const prefix = "RSS_";
Object.entries(process.env)
.filter(([key]) => key.startsWith(prefix))
.forEach(([key, value]) => console.log(`${key}=${value};`));
};

parseEnv();
25 changes: 24 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import fs from "node:fs/promises";
import path from "node:path";
import { FS_ERROR_MESSAGE } from "./fs-helper.js";

const copy = async () => {
// Write your code here
const sourceDirName = "files";
const destinationDirName = `${sourceDirName}_copy`;

const currentDirectory = import.meta.dirname;
const sourceDirPath = path.join(currentDirectory, sourceDirName);
const destinationDirPath = path.join(currentDirectory, destinationDirName);

try {
await fs.cp(sourceDirPath, destinationDirPath, {
recursive: true,
force: false,
errorOnExist: true,
});
} catch (error) {
if (error.code === "ERR_FS_CP_EEXIST") {
throw new Error(FS_ERROR_MESSAGE);
} else if (error.code === "ENOENT") {
throw new Error(FS_ERROR_MESSAGE);
}
}
};

await copy();
17 changes: 16 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from "node:fs/promises";
import { FS_ERROR_MESSAGE, getFilePathInFilesDir } from "./fs-helper.js";

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

try {
await fs.writeFile(filePath, content, { flag: "wx+" });
} catch (error) {
if (error.code === "EEXIST") {
throw new Error(FS_ERROR_MESSAGE);
}

throw error;
}
};

await create();
10 changes: 9 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import fs from "node:fs/promises";
import { FS_ERROR_MESSAGE, getFilePathInFilesDir } from "./fs-helper.js";

const remove = async () => {
// Write your code here
try {
const fileToDeletePath = getFilePathInFilesDir("fileToRemove.txt");
await fs.unlink(fileToDeletePath);
} catch {
throw new Error(FS_ERROR_MESSAGE);
}
};

await remove();
11 changes: 11 additions & 0 deletions src/fs/fs-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import path from "node:path";

export const FS_ERROR_MESSAGE = "FS operation failed";

export const getFilesDirPath = () => path.join(import.meta.dirname, "files")

export const getFilePathInFilesDir = (fileName) => {
const filesDir = getFilesDirPath()

return path.join(filesDir, fileName);
};
14 changes: 13 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import fs from "node:fs/promises";
import { FS_ERROR_MESSAGE, getFilesDirPath } from "./fs-helper.js";

const list = async () => {
// Write your code here
const filesDirPath = getFilesDirPath();

try {
const dirContent = await fs.readdir(filesDirPath, {
recursive: true,
});
console.log(dirContent);
} catch {
throw new Error(FS_ERROR_MESSAGE);
}
};

await list();
11 changes: 10 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fs from "node:fs/promises";
import { FS_ERROR_MESSAGE, getFilePathInFilesDir } from "./fs-helper.js";

const read = async () => {
// Write your code here
try {
const filePath = getFilePathInFilesDir("fileToRead.txt");
const content = await fs.readFile(filePath, 'utf-8');
console.log(content);
} catch {
throw new Error(FS_ERROR_MESSAGE);
}
};

await read();
25 changes: 24 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import path from "node:path";
import fs from "node:fs/promises";
import { FS_ERROR_MESSAGE, getFilesDirPath } from "./fs-helper.js";

const rename = async () => {
// Write your code here
const filesDirPath = getFilesDirPath();
const wrongFilenamePath = path.join(filesDirPath, "wrongFilename.txt");
const properFilenamePath = path.join(filesDirPath, "properFilename.md");

// If the destination file (`properFilename.md`) exists throw an error
try {
await fs.access(properFilenamePath);
throw new Error(FS_ERROR_MESSAGE);
} catch (e) {
if (e.message === FS_ERROR_MESSAGE) {
throw e;
}
}

// Will throw an error if the source file (`wrongFilename.txt`) does not exist
try {
await fs.rename(wrongFilenamePath, properFilenamePath);
} catch {
throw new Error(FS_ERROR_MESSAGE);
}
};

await rename();
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 { sep } from "node:path";
import { release, version } from "node:os";
import { createServer as createServerHttp } from "node:http";

import "./files/c.cjs";

const random = Math.random();

const unknownObject =
random > 0.5
? import("./files/a.json", { with: { type: "json" } })
: import("./files/b.json", { with: { type: "json" } });

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

console.log(`Path to current file is ${import.meta.filename}`);
console.log(`Path to current directory is ${import.meta.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 default {
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 { pipeline } from "node:stream/promises";
import { createReadStream } from "node:fs";
import path from "node:path";

const read = async () => {
// Write your code here
try {
const filePath = path.join(import.meta.dirname, "files", "fileToRead.txt");
const readStream = createReadStream(filePath, { encoding: "utf-8" });
await pipeline(readStream, process.stdout);
} catch (error) {
console.log(error);
}
};

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

const transform = async () => {
// Write your code here
const reverseInput = new Transform({
transform(chunk, encoding, callback) {
const reversed = chunk.toString().split("").reverse().join("");
callback(null, reversed);
},
});

await pipeline(process.stdin, reverseInput, process.stdout);
};

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

const write = async () => {
// Write your code here
const filePath = path.join(import.meta.dirname, "files", "fileToWrite.txt");
const writeStream = createWriteStream(filePath, { encoding: "utf-8" });
await pipeline(process.stdin, writeStream);
};

await write();