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
14 changes: 12 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const result = args
.map((arg) => {
if (arg.includes("--")) {
return `${arg.replace("--", "")} is`;
}
return `${arg},`;
})
.join(" ")
.slice(0, -1);
console.log(result);
};

parseArgs();
parseArgs();
12 changes: 10 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { env } from 'node:process';

const parseEnv = () => {
// Write your code here
let outputString = '';
Object.entries(env).forEach(([key, value]) => {
if (key.startsWith('RSS_')) {
outputString+=`${key}=${value}; `;
}
});
console.log(outputString);
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const childProcess = fork(__dirname + "/files/script.js", args.split(" "));
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess("--test1 --test2 --test3");
20 changes: 19 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { cp } from "node:fs/promises";
import { existsSync } from "node:fs";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const copy = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const srcPath = __dirname+"/files";
const copyPath = __dirname+"/files_copy";
const errMessage = "FS operation failed";

try {
if (!existsSync(srcPath) || existsSync(copyPath)) {
throw new Error(errMessage);
}
cp(srcPath, copyPath, { recursive: true });
} catch (error) {
console.error(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 { access } from "node:fs";
import { writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const create = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const fileText = "I am fresh and young";
const srcPath = __dirname + "/files/fresh.txt";
const errMessage = "FS operation failed";

access(srcPath, (error) => {
try {
if (error) {
writeFile(srcPath, fileText);
} else {
throw new Error(errMessage);
}
} catch (error) {
console.error(error);
}
});
};

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

const remove = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const removeFilePath = __dirname + "/files/fileToRemove.txt";
const errMessage = "FS operation failed";

try {
if (!existsSync(removeFilePath)) {
throw new Error(errMessage);
}
unlink(removeFilePath);
} catch (error) {
console.error(error);
}
};

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 { existsSync, readdirSync } from "node:fs";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const list = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const folderPath = __dirname + "/files";
const errMessage = "FS operation failed";

try {
if (!existsSync(folderPath)) {
throw new Error(errMessage);
}
console.log(readdirSync(folderPath));
} catch (error) {
console.error(error);
}
};

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

const read = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const filePath = __dirname + "/files/fileToRead.txt";
const errMessage = "FS operation failed";

try {
if (!existsSync(filePath)) {
throw new Error(errMessage);
}
readFile(filePath, "utf8", (err, data) => {
if (err) {
throw new Error(err);
}
console.log(data);
});
} catch (error) {
console.error(error);
}
};

await read();
await read();
21 changes: 19 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { existsSync, renameSync } from "node:fs";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rename = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const wrongFilePath = __dirname + "/files/wrongFilename.txt";
const correctFilePath = __dirname + "/files/properFilename.md";
const errMessage = "FS operation failed";

try {
if (existsSync(correctFilePath) || !existsSync(wrongFilePath)) {
throw new Error(errMessage);
}
renameSync(wrongFilePath, correctFilePath);
} catch (error) {
console.error(error);
}
};

await rename();
await rename();
15 changes: 13 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';

const calculateHash = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const filePath = __dirname + "/files/fileToCalculateHashFor.txt";
const file = await readFile(filePath);
const hash = createHash('sha256');
const hex = hash.update(file).digest('hex');
console.log(hex);
};

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

This file was deleted.

38 changes: 38 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { dirname, sep } from 'node:path';
import { release, version } from "node:os";
import { createServer as createServerHttp } from "node:http";
import { fileURLToPath } from "node:url";
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 "${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 };
14 changes: 12 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { createReadStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const read = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const filePath = __dirname +'/files/fileToRead.txt';
const readableStream = createReadStream(filePath);

await pipeline(readableStream, process.stdout);
};

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

const transform = async () => {
// Write your code here
const reverseData = new Transform({
transform(chunk, _, cb) {
cb(null, chunk.toString().split('').reverse().join(''));
},
});

pipeline(process.stdin, reverseData, process.stdout);
};

await transform();
await transform();
14 changes: 12 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { createWriteStream } from 'node:fs';
import { pipeline } from 'stream/promises';
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const write = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const filePath = __dirname +'/files/fileToWrite.txt';
const writeStream = createWriteStream(filePath);

await pipeline(process.stdin, writeStream);
};

await write();
await write();
Loading