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
16 changes: 15 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// parses command line arguments (format --propName value --prop2Name value2) and prints them to the console
// format propName is value, prop2Name is value2
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
if (args.length === 0) {
return console.log('No arguments provided');
}

const result = [];

for (let i = 0; i < args.length; i += 2) {
if (args[i].startsWith('--') && args[i + 1] !== undefined) {
result.push(`${args[i].slice(2)} is ${args[i + 1]}`);
}
}
console.log(result.length ? result.join(', ') : 'No arguments parsed');
};

parseArgs();
13 changes: 12 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// parses environment variables with prefix RSS_ and prints them to the console
// format RSS_name1=value1; RSS_name2=value2
const parseEnv = () => {
// Write your code here
const prefix = 'RSS_';
const result = [];

for (const [key, value] of Object.entries(process.env)) {
if (key.startsWith(prefix)) {
result.push(`${key}=${value}`);
}
}

console.log(result.length ? result.join('; ') : 'No environment variables with prefix RSS_ parsed');
};

parseEnv();
43 changes: 42 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
// copies folder files files with all its content into folder files_copy at the same level
// if files folder doesn't exist or files_copy has already been created Error with message FS operation failed must be thrown
import { promises as fs } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const copy = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));

const srcDir = join(__dirname, 'files');
const destDir = join(__dirname, 'files_copy');

const exists = async (path) => {
try {
await fs.access(path);
return true;
} catch {
return false;
}
};

if (!(await exists(srcDir))) {
throw new Error('Source folder does not exist');
}

if (await exists(destDir)) {
throw new Error('Destination folder already exists');
}

try {
await fs.mkdir(destDir, { recursive: true });
const files = await fs.readdir(srcDir);
await Promise.all(
files.map(async (file) => {
const srcFile = join(srcDir, file);
const destFile = join(destDir, file);
await fs.copyFile(srcFile, destFile);
})
);
} catch (error) {
throw new Error('FS operation failed');
}
console.log('Files copied successfully');
};

await copy();
25 changes: 24 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
// creates new file fresh.txt with content I am fresh and young inside of the files folder
// if file already exists Error with message FS operation failed must be thrown
import { promises as fs } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

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

const create = async () => {
// Write your code here

const filePath = join(__dirname, 'files', 'fresh.txt');;
const content = "I am fresh and young";

try {
await fs.access(filePath);
throw new Error('FS operation failed');
}
catch (error) {
if (error.code === 'ENOENT') {
await fs.writeFile(filePath, content);
console.log('File created successfully');
} else {
throw new Error('FS operation failed');
}
}
};

await create();
20 changes: 18 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// deletes file fileToRemove.txt
// if there's no file fileToRemove.txt Error with message FS operation failed must be thrown

import { promises as fs } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

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

const remove = async () => {
// Write your code here
const filePath = join(__dirname, 'files', 'fileToRemove.txt');

try {
await fs.access(filePath);
await fs.unlink(filePath);
} catch (err) {
throw new Error('FS operation failed');
}
};

await remove();
await remove();
15 changes: 14 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// prints array of all filenames from files folder into console
// if files folder doesn't exists Error with message FS operation failed must be thrown
import { promises as fs } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const list = async () => {
// Write your code here
const folderPath = join(__dirname, 'files');
try {
const files = await fs.readdir(folderPath);
console.log(files);
} catch (err) {
throw new Error('FS operation failed');
}
};

await list();
18 changes: 17 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// prints content of the fileToRead.txt into console
// if there's no file fileToRead.txt Error with message FS operation failed must be thrown

import { promises as fs } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

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

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

try {
const content = await fs.readFile(filePath, 'utf-8');
console.log(content);
} catch (err) {
throw new Error('FS operation failed');
}
};

await read();
30 changes: 28 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// renames file wrongFilename.txt to properFilename with extension .md
// if there's no file wrongFilename.txt or properFilename.md already exists Error with message FS operation failed must be thrown

import { promises as fs } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

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

const rename = async () => {
// Write your code here
};
const srcPath = join(__dirname, 'files', 'wrongFilename.txt');
const destPath = join(__dirname, 'files', 'properFilename.md');

try {
await fs.access(srcPath);
try {
await fs.access(destPath);
throw new Error('FS operation failed');
} catch (err) {
if (err.code !== 'ENOENT') {
throw new Error('FS operation failed');
}
}

await fs.rename(srcPath, destPath);
} catch (err) {
throw new Error('FS operation failed');
}
};

await rename();
29 changes: 28 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
// calculates SHA256 hash for file fileToCalculateHashFor.txt
// logs it into console as hex using Streams API

import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { createHash } from 'crypto';
import { createReadStream } from 'fs';

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

const calculateHash = async () => {
// Write your code here
const filePath = join(__dirname, 'files', 'fileToCalculateHashFor.txt');
const hash = createHash('sha256');
const readStream = createReadStream(filePath, { encoding: 'utf8' });

readStream.on('error', () => {
console.error('FS operation failed');
});

readStream.on('data', (chunk) => {
hash.update(chunk);
});

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

};

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

This file was deleted.

53 changes: 53 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import path from 'path';
import { dirname } from 'path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import { fileURLToPath } from 'url';
import { createRequire } from 'module';

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

const startServer = async () => {
try {
await import('./files/c.cjs');

const random = Math.random();

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

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.sep}"`);

console.log(`Path to current file is ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

console.log(unknownObject);

const myServer = createServerHttp((_, res) => {
res.end('Request accepted');
});

const PORT = 3000;

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log('To terminate it, use Ctrl+C combination');
});

return { unknownObject, myServer };
} catch (err) {
console.error('Failed to start server:', err);
}
};

const { unknownObject, myServer } = await startServer();

export { unknownObject, myServer };
Empty file.
1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fdsfdtdytfgfg
21 changes: 20 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
// reads file fileToRead.txt content using Readable Stream and prints it's content into process.stdout

import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { createReadStream } from 'fs';

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

const read = async () => {
// Write your code here

const filePath = join(__dirname, 'files', 'fileToRead.txt');

const readableStream = createReadStream(filePath);
readableStream.pipe(process.stdout);

readableStream.on('error', () => {
console.error('FS operation failed');
});
readableStream.on('end', () => process.stdout.write('\n'));

};

await read();
Loading