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
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 key = args[i].replace('--', '');
const value = args[i + 1];

console.log(`${key} is ${value}`);
}
};

parseArgs();
8 changes: 7 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const parseEnv = () => {
// Write your code here
let envArray = Object.entries(process.env);
let envVariables = envArray
.filter(([key]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`)
.join('; ');

console.log(envVariables);
};

parseEnv();
34 changes: 33 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import { access, constants, mkdir, readdir, copyFile } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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

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

try {
await access(sourceDir, constants.F_OK);

try {
await access(copiedDir, constants.F_OK);
throw new Error('FS operation failed');
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}

await mkdir(copiedDir, { recursive: true });

const files = await readdir(sourceDir);

for (const file of files) {
const srcPath = join(sourceDir, file);
const newPath = join(copiedDir, file);
await copyFile(srcPath, newPath);
}

} catch {
throw new Error('FS operation failed');
}
};

await copy();
20 changes: 19 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { join, dirname } from 'node:path';
import { access, constants, writeFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';

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

const create = async () => {
// Write your code here
const data = 'I am fresh and young';
const filePath = join(__dirname, 'files', 'fresh.txt');
try {
await access(filePath, constants.F_OK);
throw new Error('FS operation failed');
} catch (error) {
if (error.code === 'ENOENT') {
await writeFile(filePath, data);
} else {
throw new Error('FS operation failed');
}
}
};

await create();
17 changes: 16 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { unlink, access, constants } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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

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

try {
await access(filePath, constants.F_OK);

await unlink(filePath);
} catch (error) {
throw new Error('FS operation failed');
}
};

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 { readdir, access, constants } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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

const list = async () => {
// Write your code here
const folderPath = join(__dirname, 'files');

try {
await access(folderPath, constants.F_OK);
const files = await readdir(folderPath);
console.log(files);
} catch {
throw new Error('FS operation failed');
}
};

await list();
16 changes: 15 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { readFile } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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

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

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

await read();
27 changes: 26 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { rename, access, constants } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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

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

try {
await access(oldPath, constants.F_OK);

try {
await access(newPath, constants.F_OK);
throw new Error('FS operation failed');
} catch (error) {
if (error.code !== 'ENOENT') {
throw new Error('FS operation failed');
}
}

await rename(oldPath, newPath);
} catch (error) {
throw new Error('FS operation failed');
}
};

await rename();
20 changes: 19 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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

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

try {
const data = await readFile(filePath, 'utf-8');
const hash = createHash('sha256');
hash.update(data);
const digest = hash.digest('hex');
console.log(digest);
} catch {
throw new Error('FS operation failed');
}
};

await calculateHash();
18 changes: 17 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { createReadStream } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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 stream = createReadStream(filePath, { encoding: 'utf-8' });

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

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

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

const transform = async () => {
// Write your code here
const reversedStream = new Transform({
transform(chunk, _encoding, callback) {
const reversed = chunk.toString().split('').reverse().join('');
callback(null, reversed);
}
});

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

await transform();
20 changes: 19 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { createWriteStream } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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

const write = async () => {
// Write your code here
const filePath = join(__dirname, 'files', 'fileToWrite.txt');
const writableStream = createWriteStream(filePath);

process.stdin.pipe(writableStream);

writableStream.on('finish', () => {
console.log('Writing finished');
});

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

await write();