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

const outputArray = [];
for (let i = 0; i < argv.length; i += 2) {
outputArray.push(`${argv[i].slice(2)} is ${argv[i + 1]}`);
}

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

parseArgs();
9 changes: 8 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseEnv = () => {
// Write your code here
const outputArray = [];
for (let envKey in process.env) {
if (envKey.startsWith('RSS_')) {
outputArray.push(`${envKey}=${process.env[envKey]}`);
}
}

console.log(outputArray.join('; '));
};

parseEnv();
20 changes: 19 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { existsSync } from 'fs';
import { cp } from "fs/promises";

const copy = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const isFilesFolderExist = existsSync(`${__dirname}/files`);
const isFilesCopyFolderExist = existsSync(`${__dirname}/files_copy`);

if (!isFilesFolderExist || isFilesCopyFolderExist) {
throw new Error('FS operation failed');
}

try {
await cp(`${__dirname}/files`, `${__dirname}/files_copy`, { recursive: true })
} catch (error) {
console.log(error);
}
};

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 { fileURLToPath } from 'url';
import { dirname } from 'path';
import { existsSync } from 'fs';
import { writeFile } from "fs/promises";

const create = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const isFileExist = existsSync(`${__dirname}/files/fresh.txt`);

try {
if (!isFileExist) {
const data = 'I am fresh and young';
await writeFile(`${__dirname}/files/fresh.txt`, data, { encoding: 'utf8' });
} else {
throw new Error('FS operation failed');
}
} catch(error) {
throw new Error('FS operation failed');
}
};

await create();
await create();
13 changes: 12 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { rm } from "fs/promises";

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

try {
await rm(`${__dirname}/files/fileToRemove.txt`);
} catch (error) {
throw new Error('FS operation failed');
}
};

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 @@
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { readdir } from "fs/promises";

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

try {
const dirents = await readdir(`${__dirname}/files`, { withFileTypes: true });
const files = dirents.filter(d => d.isFile()).map(d => d.name);
console.log(files);
} catch(error) {
throw new Error('FS operation failed');
}
};

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

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

try {
const contents = await readFile(`${__dirname}/files/fileToRead.txt`, { encoding: 'utf8' });
console.log(contents);
} catch (error) {
throw new Error('FS operation failed')
}
};

await read();
16 changes: 14 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { rename as renameFile } from "fs/promises";
import { fileURLToPath } from 'url';
import { dirname } from 'path';

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

try {
await renameFile(`${__dirname}/files/wrongFilename.txt`, `${__dirname}/files/properFilename.md`);
} catch (error) {
console.log("error", error);
throw new Error("FS operation failed");
}
};

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const content = await readFile(`${__dirname}/files/fileToCalculateHashFor.txt`, { encoding: 'utf8' });
console.log('content', content);

const calculateHash = async () => {
// Write your code here
const hash = createHash('sha256');

hash.on('readable', () => {
const data = hash.read();
if (data) {
console.log(data.toString('hex'));
}
});

hash.write(content);
hash.end();
};

await calculateHash();