Skip to content
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.

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

const parsedArgs = {};

for (let i = 0; i < args.length; i += 2) {
const propName = args[i].replace(/^--/, '');
const value = args[i + 1];
parsedArgs[propName] = value;
}

for (const [key, value] of Object.entries(parsedArgs)) {
console.log(`${key} is ${value}`);
}
};

parseArgs();
parseArgs();
17 changes: 15 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
const parseEnv = () => {
// Write your code here
const envVars = process.env;

const rssVars = Object.entries(envVars)
.filter(([key]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`);

if (rssVars.length === 0) {
console.log('No found');
return;
}

const output = rssVars.join('; ');

console.log(output);
};

parseEnv();
parseEnv();
43 changes: 41 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const copy = async () => {
// Write your code here
const sourceDir = path.join(__dirname, 'files');
const targetDir = path.join(__dirname, 'files_copy');

try {
await fs.access(sourceDir);
} catch {
throw new Error('operation failed');
}

try {
await fs.access(targetDir);
throw new Error('operation failed');
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

await fs.mkdir(targetDir);

const files = await fs.readdir(sourceDir);

await Promise.all(
files.map(async (file) => {
const sourceFilePath = path.join(sourceDir, file);
const targetFilePath = path.join(targetDir, file);
await fs.copyFile(sourceFilePath, targetFilePath);
})
);
};

await copy();
try {
await copy();
} catch (error) {
console.error(error.message);
}
34 changes: 32 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const fileExists = async (filePath) => {
try {
await fs.access(filePath);
return true;
} catch (error) {
if (error.code === 'ENOENT') {
return false;
}
throw error;
}
};

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

if (await fileExists(filePath)) {
throw new Error('file already exists');
}

await fs.writeFile(filePath, 'I am fresh and young', 'utf8');
console.log('File created');
};

await create();
try {
await create();
} catch (error) {
console.error(error.message);
}
23 changes: 21 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

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

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

await fs.unlink(filePath);
};

await remove();
try {
await remove();
} catch (error) {
console.error(error.message);
}
29 changes: 25 additions & 4 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
const list = async () => {
// Write your code here
};
import { promises as fs } from 'fs';
import path from 'path';

await list();
async function listFiles() {
const dirPath = path.join(process.cwd(), 'src', 'fs', 'files');

try {
await fs.access(dirPath);
} catch {
throw new Error('failed');
}

try {
const files = await fs.readdir(dirPath);

files.forEach(file => {
console.log(file);
});
} catch (error) {
throw new Error('failed');
}
}

listFiles().catch(error => {
console.error(error.message);
});
26 changes: 22 additions & 4 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
const read = async () => {
// Write your code here
};
import { promises as fs } from 'fs';
import path from 'path';

await read();
async function readFileContent() {
const filePath = path.join(process.cwd(), 'src', 'fs', 'files', 'fileToRead.txt');

try {
await fs.access(filePath);
} catch (error) {
throw new Error('failed');
}

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

readFileContent().catch(error => {
console.error(error.message);
});
34 changes: 32 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

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

try {
await fs.access(oldFilePath);
} catch {
throw new Error('wrongFilename.txt does not exist');
}

try {
await fs.access(newFilePath);
throw new Error('properFilename.md already exists');
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}


await fs.rename(oldFilePath, newFilePath);
};

await rename();
try {
await rename();
} catch (error) {
console.error(error.message);
}
33 changes: 31 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import fs from 'fs';
import { createHash } from 'crypto';
import path from 'path';
import { fileURLToPath } from 'url';

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

const calculateHash = async () => {
// Write your code here
const fileToCalculateHashFor = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');
const outputFile = path.join(__dirname, 'files', 'hashOutput.txt');

const hash = createHash('sha256');

const fileStream = fs.createReadStream(fileToCalculateHashFor);

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

fileStream.on('end', () => {
const hashHex = hash.digest('hex');
console.log(`SHA256 Hash: ${hashHex}`);

fs.writeFileSync(outputFile, hashHex);
console.log(`Hash has been written to ${outputFile}`);
});

fileStream.on('error', (err) => {
console.error(`Error reading the file: ${err.message}`);
});
};

await calculateHash();
await calculateHash();
19 changes: 9 additions & 10 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
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';

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = require('./files/a.json');
unknownObject = await import('./files/a.json', { assert: { type: 'json' } });
} else {
unknownObject = require('./files/b.json');
unknownObject = await import('./files/b.json', { assert: { type: '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(`Path to current file is ${import.meta.url}`);
console.log(`Path to current directory is ${path.dirname(import.meta.url)}`);

const myServer = createServerHttp((_, res) => {
res.end('Request accepted');
Expand All @@ -33,8 +33,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
export {
unknownObject,
myServer,
};

25 changes: 23 additions & 2 deletions src/streams/read.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 __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

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

const readStream = fs.createReadStream(fileToRead, { encoding: 'utf8' });

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

readStream.on('end', () => {
console.log('\nFile reading completed.');
});

readStream.on('error', (err) => {
console.error(`Error reading the file: ${err.message}`);
});
};

await read();
await read();
Loading