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
9 changes: 8 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const argsObject = {};

for (let i = 0; i < args.length; i += 2) {
argsObject[args[i].slice(2)] = args[i + 1];
}

console.log(Object.keys(argsObject).map((argName) => `${argName} is ${argsObject[argName]}`).join(', '));
};

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 rssEnvVariablesNames = Object.keys(process.env).filter(envVariable => envVariable.startsWith('RSS_'));

const rssEnvVariablesValues = rssEnvVariablesNames.map((rssEnvVarName) => `${rssEnvVarName}=${process.env[rssEnvVarName]}`).join('; ');

console.log(rssEnvVariablesValues);
};

parseEnv();
12 changes: 11 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fsPromises from "fs/promises";
import path from 'path';
import { fileURLToPath } from 'url';

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

const copy = async () => {
// Write your code here
try {
await fsPromises.cp(path.join(__dirname, '/files/'), path.join(__dirname, '/files_copy/'), { force: false, errorOnExist: true, recursive: true });
} catch (error) {
throw new Error('FS operation failed');
}
};

await copy();
12 changes: 11 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fsPromises from "fs/promises";
import path from 'path';
import { fileURLToPath } from 'url';

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

const create = async () => {
// Write your code here
try {
await fsPromises.writeFile(path.join(__dirname, '/files/fresh.txt'), 'I am fresh and young', { flag: 'wx' });
} catch (error) {
throw new Error('FS operation failed');
}
};

await create();
12 changes: 11 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fsPromises from "fs/promises";
import path from 'path';
import { fileURLToPath } from 'url';

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

const remove = async () => {
// Write your code here
try {
await fsPromises.rm(path.join(__dirname, '/files/fileToRemove.txt'));
} catch (error) {
throw new Error('FS operation failed');
}
};

await remove();
13 changes: 12 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fsPromises from "fs/promises";
import path from 'path';
import { fileURLToPath } from 'url';

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

const list = async () => {
// Write your code here
try {
const fileNames = await fsPromises.readdir(path.join(__dirname, '/files'));
console.log(fileNames);
} catch (error) {
throw new Error('FS operation failed');
}
};

await list();
13 changes: 12 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fsPromises from "fs/promises";
import path from 'path';
import { fileURLToPath } from 'url';

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

const read = async () => {
// Write your code here
try {
const fileContent = await fsPromises.readFile(path.join(__dirname, '/files/fileToRead.txt'), { encoding: 'utf8' });
console.log(fileContent);
} catch (error) {
throw new Error('FS operation failed');
}
};

await read();
28 changes: 27 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import fsPromises from "fs/promises";
import path from 'path';
import { fileURLToPath } from 'url';

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

const filePath = path.join(__dirname, 'files', 'wrongFilename.txt');
const newFilePath = path.join(__dirname, 'files', 'properFilename.md');

const rename = async () => {
// Write your code here
try {
if (await checkFileExists(newFilePath)) {
throw new Error;
} else {
await fsPromises.rename(filePath, newFilePath);
}
} catch (error) {
throw new Error('FS operation failed');
}
};

const checkFileExists = async (filePath) => {
try {
await fsPromises.access(filePath);
return true;
} catch {
return false;
}
}

await rename();
18 changes: 17 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import fs from "fs";
import path from 'path';
import { fileURLToPath } from 'url';
import { createHash } from 'crypto';

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

const calculateHash = async () => {
// Write your code here
const readStream = fs.createReadStream(path.join(__dirname, 'files', 'fileToCalculateHashFor.txt'));
let fileContent = '';

readStream.on('data', (data) => {
fileContent += data.toString();
});

readStream.on('end', () => {
console.log(createHash('sha256').update(fileContent).digest('hex'));
});
};

await calculateHash();
18 changes: 13 additions & 5 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');
import path from "path";
import { release, version } from "os";
import { createServer as createServerHttp } from "http";
import "./files/c.js"

import { fileURLToPath } from 'url';

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

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);

const random = Math.random();

Expand Down Expand Up @@ -33,7 +41,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
export {
unknownObject,
myServer,
};
Expand Down
10 changes: 9 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import fs from "fs";
import path from 'path';
import { fileURLToPath } from 'url';

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

const read = async () => {
// Write your code here
const readableStream = fs.createReadStream(path.join(__dirname, 'files', 'fileToRead.txt'));

readableStream.pipe(process.stdout);
};

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

const reverse = new Transform({
transform(chunk, encoding, callback) {
const inputString = chunk.toString().trim();
callback(null, inputString.split("").reverse().join("") + "\n");
},
});

const transform = async () => {
// Write your code here
pipeline(
process.stdin,
reverse,
process.stdout,
(error) => console.error(error)
)
};

await transform();
10 changes: 9 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import fs from "fs";
import path from 'path';
import { fileURLToPath } from 'url';

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

const write = async () => {
// Write your code here
const writeStream = fs.createWriteStream(path.join(__dirname, 'files', 'fileToWrite.txt'))

process.stdin.pipe(writeStream);
};

await write();
19 changes: 18 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import fs from "fs";
import path from 'path';
import { fileURLToPath } from 'url';
import zlib from "zlib";
import { pipeline } from "stream";

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

const compress = async () => {
// Write your code here
const readStream = fs.createReadStream(path.join(__dirname, "files", "fileToCompress.txt"));
const gzipStream = zlib.createGzip();
const gzipWriteStream = fs.createWriteStream(path.join(__dirname, "files", "archive.gz"));

pipeline(
readStream,
gzipStream,
gzipWriteStream,
(error) => { if (error) console.error(error) }
)
};

await compress();
19 changes: 18 additions & 1 deletion src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import fs from "fs";
import path from 'path';
import { fileURLToPath } from 'url';
import zlib from "zlib";
import { pipeline } from "stream";

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

const decompress = async () => {
// Write your code here
const gzipReadStream = fs.createReadStream(path.join(__dirname, "files", "archive.gz"));
const unzipStream = zlib.createUnzip();
const writeStream = fs.createWriteStream(path.join(__dirname, "files", "fileToCompress.txt"));

pipeline(
gzipReadStream,
unzipStream,
writeStream,
(error) => { if (error) console.error(error) }
)
};

await decompress();