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

if (args.length % 2 !== 0) {
throw new Error('Each key must have a corresponding value.');
}

const formattedArgs = args.reduce((acc, el, i, arr) => {
if (el.startsWith('--') && i % 2 === 0) {
acc.push(`${el.slice(2)} is ${arr[i + 1]}`);
} else if (!el.startsWith('--') && i % 2 === 0) {
throw new Error(`Invalid argument format: '${el}' (key must start with '--')`);
}
return acc;
}, []);

console.log(formattedArgs.join(', '));
};

parseArgs();
try {
parseArgs();
} catch (error) {
console.error('\x1b[31m%s\x1b[0m', `Error: ${error.message}`);
}
9 changes: 7 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const parseEnv = () => {
// Write your code here
const env = Object.entries(process.env)
.filter(([key]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`)
.join('; ');

console.log(env || 'No RSS_ environment variables found');
};

parseEnv();
parseEnv();
30 changes: 26 additions & 4 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
const spawnChildProcess = async (args) => {
// Write your code here
import path from 'path';
import { fork } from 'child_process';

import { getPathUrl } from '../util/get-url-path.js';

const filesDir = 'files';

export const spawnChildProcess = async (args) => {
const sourcePath = path.resolve(getPathUrl(import.meta.url), filesDir, 'script.js');

try {
const childProcess = fork(sourcePath, args);

childProcess.on('error', (error) => {
console.error(`Error spawning child process: ${error.message}`);
});

childProcess.on('exit', (code) => {
console.log(`Child process exited with code: ${code}`);
});

console.log(`Child process started with arguments: ${JSON.stringify(args)}`);
} catch (error) {
console.error(`Failed to spawn child process: ${error.message}`);
}
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['Argument1', 'Argument2', 'Argument3']);
67 changes: 66 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
import path from 'path';
import fs from 'fs';
import { getPathUrl } from '../util/get-url-path.js';
import { createDirectory } from '../util/create-dir.js';
import { PropertyRequiredError } from '../util/validation-error.js';

const source = 'files';
const target = 'files_copy';

const sourcePath = path.resolve(getPathUrl(import.meta.url), source);
const targetPath = path.resolve(getPathUrl(import.meta.url), target);

const copyFileStream = async (src, dest) => {
return new Promise((resolve, reject) => {
const readStream = fs.createReadStream(src);
const writeStream = fs.createWriteStream(dest);

readStream.on('error', reject);
writeStream.on('error', reject);
writeStream.on('finish', resolve);

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

const copyDirWithStreams = async (src, dest) => {
try {
await createDirectory(dest);
const entries = await fs.promises.readdir(src, { withFileTypes: true });

for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);

if (entry.isDirectory()) {
await copyDirWithStreams(srcPath, destPath);
} else {
await copyFileStream(srcPath, destPath);
console.log(`File copied: ${srcPath}`);
}
}
} catch (error) {
throw new PropertyRequiredError('FS operation failed');
}
};

const copy = async () => {
// Write your code here
try {
const sourceStat = await fs.promises.stat(sourcePath).catch(() => {
throw new PropertyRequiredError('FS operation failed');
});

if (!sourceStat.isDirectory()) {
throw new PropertyRequiredError('FS operation failed');
}

const targetExists = await fs.promises.access(targetPath).then(() => true).catch(() => false);
if (targetExists) {
throw new PropertyRequiredError('FS operation failed');
}

await copyDirWithStreams(sourcePath, targetPath);
console.log('Copy operation completed successfully!');
} catch (error) {
console.error('Error:', error.message);
throw new PropertyRequiredError('FS operation failed');
}
};

await copy();
28 changes: 25 additions & 3 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
const create = async () => {
// Write your code here
import { access, writeFile, mkdir } from 'fs/promises';
import path from 'path';
import { getPathUrl } from '../util/get-url-path.js';
import { PropertyRequiredError } from '../util/validation-error.js';

const filesDir = 'files';
const newFileName = 'fresh.txt';
const absolutePath = path.resolve(getPathUrl(import.meta.url), filesDir, newFileName);

export const create = async () => {
try {
await access(absolutePath);
throw new PropertyRequiredError('FS operation failed');
} catch (error) {
if (error.code === 'ENOENT') {
const dirPath = path.dirname(absolutePath);
await mkdir(dirPath, { recursive: true });

await writeFile(absolutePath, 'I am fresh and young', 'utf8');
console.log(`File created successfully at ${absolutePath}`);
} else {
throw error;
}
}
};

await create();
await create();
25 changes: 23 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import path from 'path';
import { access, rm } from 'fs/promises';
import { getPathUrl } from '../util/get-url-path.js';
import { PropertyRequiredError } from '../util/validation-error.js';

const filesDir = 'files';

const remove = async () => {
// Write your code here
const sourceFile = path.resolve(getPathUrl(import.meta.url), filesDir, 'fileToRemove.txt');

try {
try {
await access(sourceFile);
} catch {
throw new PropertyRequiredError(`FS operation failed`);
}

await rm(sourceFile);
console.log(`File '${sourceFile}' removed successfully.`);
} catch (error) {
console.error('FS operation failed', error.message);
throw error;
}
};

await remove();
await remove();
24 changes: 22 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import path from 'path';
import { access, readdir } from 'fs/promises';
import { getPathUrl } from '../util/get-url-path.js';
import { PropertyRequiredError } from '../util/validation-error.js';

const filesDir = 'files';
const sourcePath = path.resolve(getPathUrl(import.meta.url), filesDir);

const list = async () => {
// Write your code here
try {
try {
await access(sourcePath);
} catch {
throw new PropertyRequiredError(`FS operation failed`);
}
const files = await readdir(sourcePath);
const arr = files.join(', ')
console.log(`[${arr}]`)
} catch (error) {
console.error('FS operation failed', error.message);
throw error;
}
};

await list();
await list();
29 changes: 27 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import path from 'path';
import { access } from 'fs/promises';
import { createReadStream } from 'fs';

import { getPathUrl } from '../util/get-url-path.js';
import { PropertyRequiredError } from '../util/validation-error.js';

const filesDir = 'files';
const sourceFile = path.resolve(getPathUrl(import.meta.url), filesDir, 'fileToRead.txt');

const read = async () => {
// Write your code here
try {
try {
await access(sourceFile);
} catch {
throw new PropertyRequiredError('FS operation failed');
}

const streamRead = createReadStream(sourceFile, 'utf-8');

streamRead.on('data', (chunk) => process.stdout.write(chunk));
streamRead.on('end', () => console.log('\nRead operation completed.'));
streamRead.on('error', (err) => console.error('Stream error:', err.message));
} catch (error) {
console.error('FS operation failed', error.message);
throw error;
}
};

await read();
await read();
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 path from 'path';
import { access, rename as renamePromise } from 'fs/promises';
import { getPathUrl } from '../util/get-url-path.js';
import { PropertyRequiredError } from '../util/validation-error.js';

const filesDir = 'files';

const rename = async () => {
// Write your code here
const sourceFile = path.resolve(getPathUrl(import.meta.url), filesDir, 'wrongFilename.txt');
const targetRenameFile = path.resolve(getPathUrl(import.meta.url), filesDir, 'properFilename.md');

try {
try {
await access(sourceFile);
} catch {
throw new PropertyRequiredError('FS operation failed: source file does not exist');
}
try {
await access(targetRenameFile);
throw new PropertyRequiredError('FS operation failed: target file already exists');
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}

await renamePromise(sourceFile, targetRenameFile);
console.log(`File '${sourceFile}' successfully renamed to '${targetRenameFile}'`);
} catch (error) {
console.error(error.message);
throw new PropertyRequiredError('FS operation failed');
}
};

await rename();
await rename();
24 changes: 22 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import path from 'path';
import fs from 'fs';
const { createHash } = await import('crypto');
import { getPathUrl } from '../util/get-url-path.js';

const filesDir = 'files';
const calculateHash = async () => {
// Write your code here
const fileName = process.argv[2] || 'fileToCalculateHashFor.txt';
const filePath = path.resolve(getPathUrl(import.meta.url), filesDir, fileName);

try {
if (!fs.existsSync(filePath)) {
console.error(`File not found: ${filePath}`);
process.exit(1);
}

const buffer = await fs.promises.readFile(filePath);
const hash = createHash('sha256').update(buffer).digest('hex');
console.log(`SHA256 hash: ${hash}`);
} catch (error) {
console.error('Error calculating hash:', error.message);
}
};

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

This file was deleted.

Loading