Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
17 changes: 15 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// args.js - implement function that parses command line arguments
// (given in format --propName value --prop2Name value2, you don't need to validate it)
// and prints them to the console in the format propName is value, prop2Name is value2
const parseArgs = () => {
// Write your code here
const re = /^--.+/;
const res = [];
const args = process.argv;
for (let i = 0; i < args.length; i++) {
if (i + 1 < args.length && re.test(args[i]) && !re.test(args[i + 1])) {
const name = args[i].substring(2);
const val = args[i + 1];
res.push(name + ' is ' + val);
}
}
console.log(res.join(', '));
};

parseArgs();
parseArgs();
14 changes: 12 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// env.js - implement function that parses environment variables with prefix RSS_
// and prints them to the console in the format "RSS_name1=value1; RSS_name2=value2"

const parseEnv = () => {
// Write your code here
const re = /^RSS_/;
const rssvars = [];
Object.keys(process.env).forEach((key) => {
if (re.test(key)) {
rssvars.push(`${key}=${process.env[key]}`);
}
});
console.log(rssvars.join('; '));
};

parseEnv();
parseEnv();
20 changes: 18 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
// implement function spawnChildProcess that receives array of arguments args and creates child process from file
import { spawn } from 'child_process';
import path from 'path';

import { fileURLToPath } from 'url';
import { dirname } from 'path';

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

const spawnChildProcess = async (args) => {
// Write your code here
const nodejs = process.argv[0];
const scriptFile = path.join(__dirname, 'files', 'script.js');
args.unshift(scriptFile);

const childProcess = spawn(nodejs, args);
process.stdin.pipe(childProcess.stdin);
childProcess.stdout.pipe(process.stdout);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['--opt1', 'val1', '--opt2', 'val2']);
42 changes: 41 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
// copy.js - implement function that copies folder files files with all its content into folder files_copy
// at the same level (if files folder doesn't exists or files_copy has already been created Error with
// message FS operation failed must be thrown)
import fs from 'fs';
import path from 'path';

import { fileURLToPath } from 'url';
import { dirname } from 'path';


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

const copy = async () => {
// Write your code here
const filesDir = path.join(__dirname, 'files');
const filesCopyDir = path.join(__dirname, 'files_copy');
fs.stat(filesDir, (err, stats) => {
if (err && err.code === 'ENOENT') {
throw new Error("FS operation failed");
}
fs.access(filesCopyDir, fs.constants.F_OK, (err) => {
if (err === null) {
throw new Error("FS operation failed");
}
fs.mkdir(filesCopyDir, { recursive: true }, (err) => {
if (err) throw err;
fs.readdir(filesDir, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
files.forEach((file) => {
const srcPath = path.join(filesDir, file);
const dstPath = path.join(filesCopyDir, file);
fs.copyFile(srcPath, dstPath, (err) => {
if (err) throw err;
});
});
});
});
});
});
};

await copy();
30 changes: 28 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// implement function that 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 fs from 'fs';
import path from 'path';

import { fileURLToPath } from 'url';
import { dirname } from 'path';


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

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

fs.access(freshFile, fs.constants.F_OK, (err) => {
if (err === null) {
throw new Error("FS operation failed");
}
const freshContent = 'I am fresh and young';
fs.writeFile(freshFile, freshContent, (err) => {
if (err) {
throw err;
}
});
});

};

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

import fs from 'fs';
import path from 'path';

import { fileURLToPath } from 'url';
import { dirname } from 'path';


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

const remove = async () => {
// Write your code here
const filesDir = path.join(__dirname, 'files');
const removedFile = path.join(filesDir, 'fileToRemove.txt');
fs.access(removedFile, fs.constants.F_OK, (err) => {
if (err) {
throw new Error("FS operation failed");
}
fs.unlink(removedFile, (err) => {
if (err) {
throw err;
}
});
});
};

await remove();
await remove();
26 changes: 25 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
// list.js - implement function that prints all array of filenames from
// files folder into console (if files folder doesn't exists Error with
// message FS operation failed must be thrown)
import fs from 'fs';
import path from 'path';

import { fileURLToPath } from 'url';
import { dirname } from 'path';


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

const list = async () => {
// Write your code here
const filesDir = path.join(__dirname, 'files');
fs.stat(filesDir, (err, stats) => {
if (err && err.code === 'ENOENT') {
throw new Error("FS operation failed");
}
fs.readdir(filesDir, (err, files) => {
if (err) {
throw new Error("FS operation failed");
}
console.log(files);
});
});
};

await list();
30 changes: 28 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// read.js - implement function that 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 fs from 'fs';
import path from 'path';

import { fileURLToPath } from 'url';
import { dirname } from 'path';


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

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

fs.stat(readFile, (err, stats) => {
if (err && err.code === 'ENOENT') {
throw new Error("FS operation failed");
}
fs.readFile(readFile, 'utf8', (err, data) => {
if (err) {
throw new Error("FS operation failed");
}
console.log(data);
});
});

};

await read();
await read();
35 changes: 33 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
// rename.js - implement function that 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 fs from 'fs';
import path from 'path';

import { fileURLToPath } from 'url';
import { dirname } from 'path';


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

const rename = async () => {
// Write your code here
const filesDir = path.join(__dirname, 'files');
const wrongFile = path.join(filesDir, 'wrongFilename.txt');
const properFile = path.join(filesDir, 'properFilename.md');
fs.access(wrongFile, fs.constants.F_OK, (err) => {
if (err) {
throw new Error("FS operation failed");
}
fs.access(properFile, fs.constants.F_OK, (err) => {
if (err === null) {
throw new Error("FS operation failed");
}
fs.rename(wrongFile, properFile, (err) => {
if (err) {
throw new Error("FS operation failed");
}
});
});
});
};

await rename();
await rename();
28 changes: 26 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
// implement function that calculates SHA256 hash for file fileToCalculateHashFor.txt
// and logs it into console as hex
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';

import { fileURLToPath } from 'url';
import { dirname } from 'path';

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

const calculateHash = async () => {
// Write your code here
const hash = crypto.createHash('sha256');
const fileName = path.join(__dirname, 'files/fileToCalculateHashFor.txt');

const fileStream = fs.createReadStream(fileName);

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

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

};

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

This file was deleted.

Loading