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
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.

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

for (let i = 0; i < args.length; i+= 2) {
const propName = args[i].slice(2);
const value = args[i + 1]
console.log(`${propName} is ${value}`)
}
};

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 variables = process.env;
const filterRSS = Object.keys(variables).filter((key) => key.startsWith('RSS_'));
const result = filterRSS.map((el) => `${el} = ${variables[el]}`);

console.log(result);
};

parseEnv();
29 changes: 28 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import fsPromises from 'fs/promises';

const filePath = './src/fs/files';
const filesTo = './src/fs/files_copy'

const copy = async () => {
// Write your code here
try {
await fsPromises.access(filePath);

try {
await fsPromises.access(filesTo);
throw new Error('FS operation failed')
} catch (error) {
if (error.code && error.code !== 'ENOENT') {
throw new Error('FS operation failed')
}
}
const filesInsideFilesFolder = await fsPromises.readdir(filePath);
await fsPromises.mkdir(filesTo);

for (let file of filesInsideFilesFolder) {
const from = `${filePath}/${file}`;
const to = `${filesTo}/${file}`

await fsPromises.copyFile(from, to);
}
} catch {
throw new Error('FS operation failed')
}
};

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 fsPromises from 'fs/promises';

const filePath = './src/fs/files/fresh.txt';
const message = 'I am fresh and young';

const create = async () => {
// Write your code here
try {
await fsPromises.access(filePath);
throw new Error('FS operation failed')
}
catch (error) {
if (error.code === 'ENOENT') {
await fsPromises.writeFile(filePath, message);
} else {
throw error;
}
}
};

await create();
15 changes: 13 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@

import fsPromises from 'fs/promises';
const pathFile = './src/fs/files/fileToRemove.txt';

const remove = async () => {
// Write your code here
};
try {
await fsPromises.access(pathFile);
await fsPromises.unlink(pathFile);
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error('FS operation failed');
}
}
};

await remove();
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 fsPromises from 'fs/promises';
const path = './src/fs/files';

const list = async () => {
// Write your code here
try {
await fsPromises.access(path);

const filesInsideFilesFolder = await fsPromises.readdir(path);
console.log(filesInsideFilesFolder);
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error('FS operation failed');
}
}
};

await list();
12 changes: 11 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fsPromises from 'fs/promises';
const path = './src/fs/files/fileToRead.txt';

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

await read();
23 changes: 22 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import fsPromises from 'fs/promises';
const wrongFilePath = './src/fs/files/wrongFilename.txt';
const properFilePath = './src/fs/files/properFilename.md';

const rename = async () => {
// Write your code here
try {
await fsPromises.access(wrongFilePath);
}
catch (error) {
if (error.code === 'ENOENT') {
throw new Error('FS operation failed');
}
}

try {
await fsPromises.access(properFilePath);
throw new Error('FS operation failed');
} catch (error) {
if (error.code !== 'ENOENT') {
throw new Error('FS operation failed');
}
}
await fsPromises.rename(wrongFilePath, properFilePath);
};

await rename();
16 changes: 15 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from 'fs';
import crypto from 'crypto';

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

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

readStream.on('data', (chunk) => {
hash.update(chunk);
})
readStream.on('end', () => {
const result = hash.digest('hex');
console.log(result);
})
};

await calculateHash();
21 changes: 12 additions & 9 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const path = require('node:path');
const { release, version } = require('node:os');
const { createServer: createServerHttp } = require('node:http');
import path from 'path';
import { release, version } from 'node:os';
import { createServer as createServerHttp }from 'node:http';
import { fileURLToPath } from 'url';

require('./files/c.cjs');
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

import a from './files/a.json' with { type: "json" };
import b from './files/b.json' with { type: "json" };
const c = await import('./files/c.cjs');

const random = Math.random();

const unknownObject = random > 0.5 ? require('./files/a.json') : require('./files/b.json');
const unknownObject = random > 0.5 ? a : b;

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
Expand All @@ -28,7 +34,4 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
unknownObject,
myServer,
};
export {unknownObject, myServer}
20 changes: 19 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import fs from 'fs';
import { once } from 'events';

const filePath = './src/streams/files/fileToRead.txt';

const read = async () => {
// Write your code here
try {
const readStream = fs.createReadStream(filePath, { encoding: 'utf-8' });

readStream.on('error', (error) => {
throw error;
});

readStream.pipe(process.stdout);
await once(readStream, 'end');

console.log('success');
} catch (error) {
throw error;
}
};

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

const transform = async () => {
// Write your code here
const streamTransform = new Transform({transform(chunk, encoding, callback) {
const reversedText = chunk.toString().split('').reverse().join('');

this.push(reversedText);
callback();
}
})
console.log('Type something. Press Ctrl+D or Ctrl+C to exit.');

process.stdin.pipe(streamTransform);
streamTransform.pipe(process.stdout);
};

await transform();
16 changes: 15 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from 'fs';
import { once } from 'events';

const filePath = './src/streams/files/fileToWrite.txt';

const write = async () => {
// Write your code here
try {
console.log('Type something. Press Ctrl+D or Ctrl+C to exit.');

const writeStream = fs.createWriteStream(filePath);

process.stdin.pipe(writeStream);
await once(writeStream, 'finish')
} catch (error) {
console.error(error);
}
};

await write();
25 changes: 24 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import fs from 'fs';
import zlib from 'zlib';
import { once } from 'events';
import path from 'path';
import { fileURLToPath } from 'url';

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

const filePath = path.join(dirName, 'files', 'fileToCompress.txt');
const archivePath = path.join(dirName, 'files', 'archive.gz');

const compress = async () => {
// Write your code here
try {
const readStream = fs.createReadStream(filePath);
const writeStream = fs.createWriteStream(archivePath);
const gzip = zlib.createGzip();

readStream.pipe(gzip);
gzip.pipe(writeStream);

await once(writeStream, 'finish');
} catch (error) {
console.error(error);
}
};

await compress();
25 changes: 24 additions & 1 deletion src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import fs from 'fs';
import zlib from 'zlib';
import { once } from 'events';
import path from 'path';
import { fileURLToPath } from 'url';

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

const filePath = path.join(dirName, 'files', 'fileToCompress.txt');
const archivePath = path.join(dirName, 'files', 'archive.gz');

const decompress = async () => {
// Write your code here
try {
const readStream = fs.createReadStream(archivePath);
const writeStream = fs.createWriteStream(filePath);
const gunzip = zlib.createGunzip();

readStream.pipe(gunzip);
gunzip.pipe(writeStream);

await once(writeStream, 'finish');
} catch (error) {
console.error(error);
}
};

await decompress();