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

for (let i = 0, len = args.length; i < len; i++) {
if(args[i].startsWith("--")) {
console.log(args[i].slice(2) + ' is ' + args[i + 1]);
}
}
};

parseArgs();
parseArgs();

// 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
12 changes: 11 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const env_variables = process.env;

const result = [];

const parseEnv = () => {
// Write your code here
for (const key in env_variables) {
if (key.startsWith("RSS_")) {
result.push(`${key}: ${env_variables[key]}`);
}
}
console.log(result);
return result;
};

parseEnv();
35 changes: 32 additions & 3 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
const copy = async () => {
// Write your code here
import fs from 'fs';

const checkIfDirExists = async (dirPath) => {
try {
await fs.promises.access(dirPath, fs.constants.F_OK);
return true;
} catch (err) {
return false;
}

}


const copy = async (dest_dir, copy_name) => {

const existsDestFolder = await checkIfDirExists(dest_dir);
const existsCopyFolder = await checkIfDirExists(copy_name);

if (!existsDestFolder) {
throw new Error(`Could not find ${dest_dir}`);
}
if (existsCopyFolder) {
throw new Error(` ${copy_name} already exists`);
}
fs.cp(dest_dir, copy_name, {recursive: true}, (err) => {
if (err) throw err.message;
})
};

await copy();

await copy('files', 'files_copy');


// 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 exist or files_copy has already been created Error with message FS operation failed must be thrown)
23 changes: 20 additions & 3 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import fs from 'fs';

const content = 'I am fresh and young :)'

const create = async () => {
// Write your code here
};
fs.access('./files/fresh.tsx', fs.constants.F_OK, (err) => {
if (err) {
return fs.writeFile('./files/fresh.tsx', content, err => {
if (err) return console.log(err);
})
} else {
console.log('File or directory exists');
}
});
}

await create();


//TODO: add throw

await create();
// create.js - 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)
15 changes: 12 additions & 3 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const remove = async () => {
// Write your code here
import fs from 'fs';

const filePath = './files/fileToRemove.txt'

const remove = async (filePath) => {

fs.unlink(filePath, (err) => {
if (err) throw err.message;
})
};

await remove();
await remove(filePath);

// 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)
1 change: 0 additions & 1 deletion src/fs/files/fileToRemove.txt

This file was deleted.

3 changes: 0 additions & 3 deletions src/fs/files/wrongFilename.txt

This file was deleted.

15 changes: 13 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'fs';
const path = './files';

const list = async () => {
// Write your code here
fs.readdir(path, (err, files) => {
if (err) {
throw err.message;
}
console.log(files);
})
};

await list();
await list();


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


const read = async () => {

const filePath = path.join(process.cwd(), './files/fileToRead.txt');

fs.readFile(filePath, 'utf8', (err, data) => {
if(err) {
if(err.code === 'ENOENT') {
throw new Error(err.message);
}
else {
throw err;
}
}
console.log(data);
})
// Write your code here
};

await read();
await read();


// 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)
31 changes: 28 additions & 3 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
const rename = async () => {
// Write your code here
import fs from 'fs';

const checkIfFileExists = async (file_name) => {
try {
await fs.promises.access(file_name, fs.constants.R_OK);
return true;
}
catch (e) {
return false;
}

}


const rename = async (current_name, new_name) => {
const exists = checkIfFileExists(current_name);
if (!exists) {
throw new Error(`File ${current_name} does not exist`);
}

fs.rename(current_name, new_name, (err) => {
if (err) {
throw err.message;
}
})
};

await rename();
await rename('./files/wrongFilename.txt', './files/properFilename.md');

// 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)
4 changes: 3 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ const calculateHash = async () => {
// Write your code here
};

await calculateHash();
await calculateHash();

// calcHash.js - implement function that calculates SHA256 hash for file fileToCalculateHashFor.txt and logs it into console as hex using Streams API
40 changes: 0 additions & 40 deletions src/modules/cjsToEsm.cjs

This file was deleted.

2 changes: 1 addition & 1 deletion src/modules/files/c.cjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('Hello from c.cjs!');
console.log('Hello from c.js!');