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
15 changes: 14 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import pr from 'node:process';

const cliArgs = pr.argv.slice(2);

const parseArgs = () => {
// Write your code here
let arrToDisplay = [];

for (let i = 0; i < cliArgs.length; i += 2) {
let prop = `${cliArgs[i].substring(2)} is ${cliArgs[i + 1]}`;
arrToDisplay.push(prop);
}

arrToDisplay.join(', ');

console.log(arrToDisplay);
};

parseArgs();
16 changes: 15 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import pr from 'node:process';

const envVariables = pr.env;
const prefixToCheck = "RSS_";

const parseEnv = () => {
// Write your code here
let varsToPrint = [];

for (let key in envVariables) {
if (key.startsWith(prefixToCheck)) {
varsToPrint.push(`${key}=${envVariables[key]}`)
}
}

varsToPrint = varsToPrint.join('; ');
console.log(varsToPrint);
};

parseEnv();
16 changes: 14 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { spawn } from 'node:child_process';
import url from 'node:url';
import path from 'node:path';

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

const filePath = path.join(__dirname, 'files', 'script.js');

const spawnChildProcess = async (args) => {
// Write your code here
const childProcess = spawn('node', [filePath, ...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(/* [someArgument1, someArgument2] */);
70 changes: 69 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@
import fs from 'node:fs';
import url from 'node:url';
import path from 'node:path';

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

const filesPath = path.join(__dirname, 'files');
const filesPathCopy = path.join(__dirname, 'files-copy');

const copy = async () => {
// Write your code here
if (!fs.existsSync(filesPath) || fs.existsSync(filesPathCopy)) {
throw new Error('FS operation failed');
}

fs.mkdirSync(filesPathCopy);

fs.readdir(filesPath, { withFileTypes: false }, (err, files) => {
if (err) {
console.log('*** An error occurred while reading files ***');
console.error(err);

return;
}

if (!files.length) {
console.log('*** No files were found while reading the target directory. Copy operation cannot be performed ***');
}

files.forEach((file) => {
let initialFilePath = path.join(filesPath, file);
let destinationFilePath = path.join(filesPathCopy, file);

/**
* 1st option of implementation of the method (preferred by async requirement)
*/

fs.copyFileSync(initialFilePath, destinationFilePath, fs.constants.COPYFILE_EXCL, function (err) {
if (err) throw err;

console.log('*** Files copied successfully! ***'); // BUG not working
});

/**
* 2nd option of implementation of the method
*/

/* try {
fs.copyFileSync(initialFilePath, destinationFilePath, fs.constants.COPYFILE_EXCL);

console.log(`*** Copy operation completed successfully for ${file} ***`);
} catch (error) {
console.log('*** An error occurred while copying files ***');
console.error(error);
} */

/**
* 3rd option of implementation of the method
* Known issues:
* @param {object} errorOnExist - not working
* @param {object} mode - not working
*/

/* fs.cp(initialFilePath, destinationFilePath, { errorOnExist: true, mode: fs.constants.COPYFILE_EXCL }, function (err) {
if (err) throw new Error(err);

console.log('*** Copy operation completed successfully ***');
}) */
})
})
};

await copy();
18 changes: 17 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import fs from 'node:fs';
import url from 'node:url';
import path from 'node:path';

const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const fullFilePath = path.join(__dirname, 'files', 'fresh.txt');

const create = async () => {
// Write your code here
if (fs.existsSync(fullFilePath)) {
throw new Error('FS operation failed');
}

fs.appendFile(fullFilePath, 'I am fresh and young', err => {
if (err) throw err;

console.log('*** File created successfully! ***');
})
};

await create();
20 changes: 19 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import fs from 'node:fs';
import url from 'node:url';
import path from 'node:path';

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

const fileToDelete = path.join(__dirname, 'files', 'fileToRemove.txt');

const remove = async () => {
// Write your code here
if (!fs.existsSync(fileToDelete)) {
throw new Error('FS operation failed');
}

fs.unlink(fileToDelete, err => {
if (err) throw err;

console.log('*** File deleted successfully! ***');
})

};

await remove();
41 changes: 40 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
import fs from 'node:fs';
import url from 'node:url';
import path from 'node:path';

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

const filesDirPath = path.join(__dirname, 'files');

const list = async () => {
// Write your code here
if (!fs.existsSync(filesDirPath)) {
throw new Error('FS operation failed');
}

let filesArr = [];

try {
let files = await fs.promises.readdir(filesDirPath, { withFileTypes: true, recursive: true });

files.forEach(file => {
let tempFile = {};

let fileType;

if (file.isDirectory()) {
fileType = 'Directory'
} else if (file.isFile()) {
fileType = 'File'
}

tempFile.name = file.name;
tempFile.path = file.path;
tempFile.type = fileType;

filesArr.push(tempFile);
})

console.log('*** Files array ***', filesArr);
} catch (error) {
console.error(error);
}
};

await list();
21 changes: 20 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import fs from 'node:fs';
import url from 'node:url';
import path from 'node:path';

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

const fileToRead = path.join(__dirname, 'files', 'fileToRead.txt');

const read = async () => {
// Write your code here
if (!fs.existsSync(fileToRead)) {
throw new Error('FS operation failed');
}

let fileContent;

fs.readFile(fileToRead, { encoding: 'utf8' }, (err, data) => {
if (err) throw err;

console.log(data);
})
};

await read();
20 changes: 19 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import fs from 'node:fs';
import url from 'node:url';
import path from 'node:path';

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

const oldFilePath = path.join(__dirname, 'files', 'wrongFilename.txt');
const newFilePath = path.join(__dirname, 'files', 'properFileName.md');

const rename = async () => {
// Write your code here
if (!fs.existsSync(oldFilePath) || fs.existsSync(newFilePath)) {
throw new Error('FS operation failed');
}

fs.rename(oldFilePath, newFilePath, err => {
if (err) throw err;

console.log('*** File renamed successfully! ***');
})
};

await rename();
28 changes: 27 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import cr from 'node:crypto';
import fs from 'node:fs';
import url from 'node:url';
import path from 'node:path';

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

const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');

const calculateHash = async () => {
// Write your code here
let readStream = fs.createReadStream(filePath);
let hash = cr.createHash('sha256');
hash.setEncoding('hex');

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

readStream.on('error', (err) => {
throw err;
})

readStream.on('end', () => {
let finalHash = hash.digest('hex');

console.log(finalHash);
})
};

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

This file was deleted.

Loading