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
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Node.js basics

## !!! Please don't submit Pull Requests to this repository !!!
## This is brunch with completed tasks
16 changes: 16 additions & 0 deletions package-lock.json

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

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

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

console.log(
Object.entries(argsObj)
.map(([key, val]) => `${key} is ${val}`)
.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 variables = Object.entries(process.env)
.filter(([key]) => key.startsWith('RSS_'))
.map(([key, val]) => `${key}=${val}`)
.join('; ');
console.log(variables);
};

parseEnv();
26 changes: 25 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const copy = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filesFolder = path.join(__dirname, 'files');
const copyFolder = path.join(__dirname, 'files_copy');

if (fs.existsSync(filesFolder)) {
if (fs.existsSync(copyFolder)) {
throw new Error ('FS operation failed: files_copy is already exist');
} else {
fs.cp(
filesFolder,
copyFolder,
{ recursive: true },
(error) => {
if (error) throw error;
}
)
}
} else {
throw new Error ('FS operation failed: files folder does not exist');
}
};

await copy();
22 changes: 20 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const create = async () => {
// Write your code here
};
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const fileDir = path.join(__dirname, 'files', 'fresh.txt');

if (fs.existsSync(fileDir)) {
throw new Error ('FS operation failed: fresh.txt is already exist');
} else {
fs.writeFile(
fileDir,
'I am fresh and young',
(error) => {
if (error) throw error;
}
);
}
}

await create();
19 changes: 18 additions & 1 deletion src/fs/delete.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';

const remove = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const file = path.join(__dirname, 'files', 'fileToRemove.txt');

if (fs.existsSync(file)) {
fs.unlink(
file,
(error) => {
if (error) throw error;
}
);
} else {
throw new Error ('FS operation failed: fileToRemove.txt does not exist');
}
};

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

const list = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filesFolder = path.join(__dirname, 'files');

if (fs.existsSync(filesFolder)) {
fs.readdir(filesFolder, (error, files) => {
if (error) throw error;
console.log(files.join('\n'));
});
} else {
throw new Error ('FS operation failed: files folder does not exist');
}
};

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

const read = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const file = path.join(__dirname, 'files', 'fileToRead.txt');

if (fs.existsSync(file)) {
fs.readFile(
file,
'utf-8',
(error, data) => {
if (error) throw error;
console.log(data);
}
);
} else {
throw new Error ('FS operation failed: fileToRead.txt does not exist');
}
};

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 fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const rename = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const wrongFile = path.join(__dirname, 'files', 'wrongFilename.txt');
const properFile = path.join(__dirname, 'files', 'properFilename.md');

if (fs.existsSync(wrongFile)) {
fs.rename(
wrongFile,
properFile,
(error) => {
if (error) throw error;
}
);
} else if (fs.existsSync(properFile)) {
throw new Error ('FS operation failed: properFilename.md is already exist');
} else {
throw new Error ('FS operation failed: wrongFilename.txt does not exist');
}
};

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

const calculateHash = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const file = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');

const readStream = fs.createReadStream(file);
const hash = createHash('sha256');

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

await calculateHash();
29 changes: 15 additions & 14 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');
import path from 'path';
import { fileURLToPath } from 'url';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import { readFile } from 'fs/promises';
import './files/c.js';

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

const a = JSON.parse(await readFile(path.join(__dirname, './files/a.json'), { encoding: 'utf8' }));
const b = JSON.parse(await readFile(path.join(__dirname, './files/b.json'), { encoding: 'utf8' }));

let unknownObject;
const random = Math.random();

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

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

module.exports = {
export {
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 fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const read = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const file = path.join(__dirname, 'files', 'fileToRead.txt');

const readStream = fs.createReadStream(file);

readStream.on('data', (chunk) => process.stdout.write(chunk + '\n'));
};

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

const reverseStream = new Transform({
transform(chunk, encoding, callback) {
callback(null, chunk.toString().split('').reverse().join(''));
}
});

const transform = async () => {
// Write your code here
process.stdin.pipe(reverseStream).pipe(process.stdout);
};

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

const write = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const file = path.join(__dirname, 'files', 'fileToWrite.txt');

const writeStream = fs.createWriteStream(file);

process.stdin.on('data', (chunk) => writeStream.write(chunk));
};

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

const compress = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const file = path.join(__dirname, 'files', 'fileToCompress.txt');
const archive = path.join(__dirname, 'files', 'archive.gz');

const readStream = fs.createReadStream(file, 'utf-8');
const writeStream = fs.createWriteStream(archive);

const gzip = createGzip();

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

await compress();
19 changes: 17 additions & 2 deletions src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { createUnzip } from 'zlib';

const decompress = async () => {
// Write your code here
};
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const file = path.join(__dirname, 'files', 'fileToCompress.txt');
const archive = path.join(__dirname, 'files', 'archive.gz');

const readStream = fs.createReadStream(archive);
const writeStream = fs.createWriteStream(file);

const unzip = createUnzip();

readStream.pipe(unzip).pipe(writeStream);
}

await decompress();