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
23 changes: 18 additions & 5 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
const parseArgs = () => {
// Write your code here
};

parseArgs();
const parseArgs = () => {
let arg = process.argv;
const getArg = arg.reduce((acc, current, index) => {
if (current.startsWith("--")) {
const val = current.substring(2);
const next = arg[index + 1];
if (next) {
const formattedPair = `${val} is ${next}`;
return acc + (acc.length ? ", " : "") + formattedPair;
}
}
return acc;
}, "");

console.log(getArg);
};

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

parseEnv();
const parseEnv = () => {
let env = process.env;
let selectRss = Object.entries(env)
.filter(([key]) => key.startsWith("RSS_"))
.map(([key, value], index) => {
return `${key}=${value}`;
});
let result = selectRss.join("; ");

console.log(result);
};

parseEnv();
12 changes: 6 additions & 6 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const spawnChildProcess = async (args) => {
// Write your code here
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
const spawnChildProcess = async (args) => {
// Write your code here
};
// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
38 changes: 19 additions & 19 deletions src/cp/files/script.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { EOL } from 'node:os';
import { argv, stdout, stdin, exit } from 'node:process';

const args = argv.slice(2);

console.log(`Total number of arguments is ${args.length}`);
console.log(`Arguments: ${JSON.stringify(args)}${EOL}`);

const echoInput = (chunk) => {
const chunkStringified = chunk.toString();

if (chunkStringified.includes('CLOSE')) {
exit(0);
}

stdout.write(`Received from master process: ${chunk.toString()}${EOL}`);
};

stdin.on('data', echoInput);
import { EOL } from 'node:os';
import { argv, stdout, stdin, exit } from 'node:process';
const args = argv.slice(2);
console.log(`Total number of arguments is ${args.length}`);
console.log(`Arguments: ${JSON.stringify(args)}${EOL}`);
const echoInput = (chunk) => {
const chunkStringified = chunk.toString();
if (chunkStringified.includes('CLOSE')) {
exit(0);
}
stdout.write(`Received from master process: ${chunk.toString()}${EOL}`);
};
stdin.on('data', echoInput);
39 changes: 34 additions & 5 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
const copy = async () => {
// Write your code here
};

await copy();
import fs from "fs/promises";
import path from "path";

const copy = async () => {
let folderFiles = path.join(import.meta.dirname, "files");
let folderFilesCopy = path.join(import.meta.dirname, "files_copy");
let errorText = "FS operation failed";

try {
//create promise to check if folders exist
let results = await Promise.allSettled([
fs.access(folderFiles, fs.constants.F_OK),
fs.access(folderFilesCopy, fs.constants.F_OK),
]);
//save exist check results
let isFolderFiles = results[0].status === "fulfilled";
let isfolderFilesCopy = results[1].status === "rejected";

//copy to new folder if conditions true
if (isFolderFiles && isfolderFilesCopy) {
try {
await fs.cp(folderFiles, folderFilesCopy, { recursive: true });
} catch (err) {
throw new Error(errorText);
}
} else {
throw new Error(errorText);
}
} catch (error) {
throw new Error(errorText);
}
};

await copy();
21 changes: 16 additions & 5 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const create = async () => {
// Write your code here
};

await create();
import fs from "fs/promises";
import path from "path";

const create = async () => {
let pathFolder = path.join(import.meta.dirname, "files", "fresh.txt");
let fileText = "I am fresh and young";
let errorText = "FS operation failed";

try {
await fs.writeFile(pathFolder, fileText, { flag: "wx" });
} catch (error) {
throw new Error(errorText);
}
};

await create();
20 changes: 15 additions & 5 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const remove = async () => {
// Write your code here
};

await remove();
import fs from "fs/promises";
import path from "path";

const remove = async () => {
let removeFile = path.join(import.meta.dirname, "files", "fileToRemove.txt");
let errorText = "FS operation failed";

try {
await fs.rm(removeFile);
} catch (error) {
throw new Error(errorText);
}
};

await remove();
12 changes: 6 additions & 6 deletions src/fs/files/fileToRead.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
My content
should
be
printed
into
console
My content
should
be
printed
into
console
!
4 changes: 2 additions & 2 deletions src/fs/files/wrongFilename.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# This is a file with a wrong filename

# This is a file with a wrong filename
Hello from **markdown**!
21 changes: 16 additions & 5 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const list = async () => {
// Write your code here
};

await list();
import fs from "fs/promises";
import path from "path";

const list = async () => {
let folderPath = path.join(import.meta.dirname, "files");
let errorText = "FS operation failed";

try {
let data = await fs.readdir(folderPath);
console.log(data);
} catch (error) {
throw new Error(errorText);
}
};

await list();
21 changes: 16 additions & 5 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const read = async () => {
// Write your code here
};

await read();
import fs from "fs/promises";
import path from "path";

const read = async () => {
let filePath = path.join(import.meta.dirname, "files", "fileToRead.txt");
let errorText = "FS operation failed";

try {
let data = await fs.readFile(filePath, "utf8");
console.log(data);
} catch (error) {
throw new Error(errorText);
}
};

await read();
36 changes: 31 additions & 5 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
const rename = async () => {
// Write your code here
};

await rename();
import fs from "fs/promises";
import path from "path";

const rename = async () => {
let oldFile = path.join(import.meta.dirname, "files", "wrongFilename.txt");
let newFile = path.join(import.meta.dirname, "files", "properFilename.md");
let errorText = "FS operation failed";

try {
let results = await Promise.allSettled([
fs.access(oldFile, fs.constants.F_OK),
fs.access(newFile, fs.constants.F_OK),
]);
let isOldFiles = results[0].status === "fulfilled";
let isNewFiles = results[1].status === "rejected";

if (isOldFiles && isNewFiles) {
try {
await fs.rename(oldFile, newFile);
} catch (err) {
throw new Error(errorText);
}
} else {
throw new Error(errorText);
}
} catch (error) {
throw new Error(errorText);
}
};

await rename();
31 changes: 26 additions & 5 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
const calculateHash = async () => {
// Write your code here
};

await calculateHash();
import { createHash } from "crypto";
import { createReadStream } from "fs";
import { pipeline } from "stream/promises";
import path from "path";

const calculateHash = async () => {
const hashFile = path.join(
import.meta.dirname,
"files",
"fileToCalculateHashFor.txt"
);

const hash = createHash("sha256");
const stream = createReadStream(hashFile);

try {
await pipeline(stream, hash);
const hexHash = hash.digest("hex");
console.log(hexHash);
return hexHash;
} catch (error) {
throw new Error(error);
}
};

await calculateHash();
45 changes: 45 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import path from "path";
import { release, version } from "os";
import { createServer as createServerHttp } from "http";
import "./files/c.cjs";

const random = Math.random();
let unknownObject;
if (random > 0.5) {
unknownObject = (
await import("./files/a.json", {
with: { type: "json" },
})
).default;
} else {
unknownObject = (
await import("./files/b.json", {
with: { type: "json" },
})
).default;
}

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.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,
};
10 changes: 5 additions & 5 deletions src/modules/files/a.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"a": 1,
"b": 2,
"c": 3
}
{
"a": 1,
"b": 2,
"c": 3
}
10 changes: 5 additions & 5 deletions src/modules/files/b.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"a": 11,
"b": 22,
"c": 33
}
{
"a": 11,
"b": 22,
"c": 33
}
2 changes: 1 addition & 1 deletion src/modules/files/c.cjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('Hello from c.cjs!');
console.log('Hello from c.cjs!');
1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading