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
13 changes: 12 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const parseArgs = () => {
// Write your code here
// Write your code here
const argumentsFromCL = process.argv.slice(2);
const reducedArguments = argumentsFromCL.reduce((previousValue, currentValue, currentIndex, array) => {
const nextValue = array[currentIndex + 1];
if (nextValue !== undefined && currentValue.startsWith('--')) {
previousValue.push(`${currentValue.slice(2)} is ${nextValue}`);
}

return previousValue;
}, []);

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

parseArgs();
11 changes: 10 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const parseEnv = () => {
// Write your code here
// Write your code here
const RSSVariables = Object.entries(process.env).reduce((accumulator, [key, value]) => {
if (key.startsWith('RSS_')) {
accumulator.push(`${key}=${value}`);
}

return accumulator;
}, []);

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

parseEnv();
23 changes: 22 additions & 1 deletion src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
const spawnChildProcess = async (args) => {
// Write your code here
const childProcess = await import('node:child_process');
const url = await import('node:url');
const path = await import('node:path');

const currentJSFileAbsolutePath = url.fileURLToPath(import.meta.url);
const currentDirPath = path.dirname(currentJSFileAbsolutePath);
const folderName = 'files';
const folderPath = path.join(currentDirPath, folderName);
const childFile = 'script.js';
const filePathWithChildFile = path.join(folderPath, childFile);

const childScriptProcess = childProcess.spawn(`node`,
[filePathWithChildFile, ...args.split(' ')]);

process.stdin.on('data', (message) => {
childScriptProcess.stdin.write(message);
})

childScriptProcess.stdout.on('data', (data) => {
console.log(data.toString());
})
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess('--arg1 d--arg2');
23 changes: 22 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
const copy = async () => {
// Write your code here
// Write your code here
const fs = await import('node:fs/promises');
const path = await import('node:path');
const {fileURLToPath} = await import('node:url')

const currentJSFileAbsolutePath = fileURLToPath(import.meta.url);
const currentDirPath = path.dirname(currentJSFileAbsolutePath);
const sourceFolderName = 'files';
const sourceFolderPath = path.join(currentDirPath, sourceFolderName);
const destinationFolderName = 'files_copy';
const destinationFolderPath = path.join(currentDirPath, destinationFolderName);
const errorMessage = 'FS operation failed.';

try {
await fs.cp(sourceFolderPath, destinationFolderPath, {recursive: true, errorOnExist: true, force: false});
}
catch (error) {
if (error.code === 'ERR_FS_CP_EEXIST') {
error.message = errorMessage;
}
throw error;
}
};

await copy();
24 changes: 23 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
const create = async () => {
// Write your code here
// Write your code here
const fs= await import('node:fs/promises');
const path= await import('node:path');
const {fileURLToPath} = await import('node:url')

const currentJSFileAbsolutePath = fileURLToPath(import.meta.url);
const currentDirPath = path.dirname(currentJSFileAbsolutePath);
const folderName = 'files';
const folderPath = path.join(currentDirPath, folderName);
const fileName = 'fresh.txt';
const filePath = path.join(folderPath, fileName);
const fileContent = 'I am fresh and young';
const errorMessage = 'FS operation failed.'

try {
await fs.writeFile(filePath, fileContent, {flag: 'wx'});
}
catch (error) {
if (error.code === 'EEXIST') {
error.message = errorMessage;
}
throw error;
}
};

await create();
23 changes: 22 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
const remove = async () => {
// Write your code here
// Write your code here
const fs= await import('node:fs/promises');
const path= await import('node:path');
const {fileURLToPath} = await import('node:url')

const currentJSFileAbsolutePath = fileURLToPath(import.meta.url);
const currentDirPath = path.dirname(currentJSFileAbsolutePath);
const folderName = 'files';
const folderPath = path.join(currentDirPath, folderName);
const fileToRemoveName = 'fileToRemove.txt';
const filePathWithFileToDelete = path.join(folderPath, fileToRemoveName);
const errorMessage = 'FS operation failed.';

try {
await fs.rm(filePathWithFileToDelete)
}
catch (error) {
if (error.code === 'ENOENT') {
error.message = errorMessage;
}
throw error;
}
};

await remove();
22 changes: 21 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
const list = async () => {
// Write your code here
// Write your code here
const fs= await import('node:fs/promises');
const path= await import('node:path');
const {fileURLToPath} = await import('node:url')

const currentJSFileAbsolutePath = fileURLToPath(import.meta.url);
const currentDirPath = path.dirname(currentJSFileAbsolutePath);
const folderName = 'files';
const folderPath = path.join(currentDirPath, folderName);
const errorMessage = 'FS operation failed.';

try {
const files = await fs.readdir(folderPath);
console.log(files);
}
catch (error) {
if (error.code === 'ENOENT') {
error.message = errorMessage;
}
throw error;
}
};

await list();
24 changes: 23 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
const read = async () => {
// Write your code here
// Write your code here
const fs= await import('node:fs/promises');
const path= await import('node:path');
const {fileURLToPath} = await import('node:url')

const currentJSFileAbsolutePath = fileURLToPath(import.meta.url);
const currentDirPath = path.dirname(currentJSFileAbsolutePath);
const folderName = 'files';
const folderPath = path.join(currentDirPath, folderName);
const fileToReadName = 'fileToRead.txt';
const filePathWithFileToRead = path.join(folderPath, fileToReadName);
const errorMessage = 'FS operation failed.';

try {
const fileContent = await fs.readFile(filePathWithFileToRead, 'utf8');
console.log(fileContent);
}
catch (error) {
if (error.code === 'ENOENT') {
error.message = errorMessage;
}
throw error;
}
};

await read();
37 changes: 36 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
const rename = async () => {
// Write your code here
// Write your code here
const fs= await import('node:fs/promises');
const path= await import('node:path');
const {fileURLToPath} = await import('node:url')

const currentJSFileAbsolutePath = fileURLToPath(import.meta.url);
const currentDirPath = path.dirname(currentJSFileAbsolutePath);
const folderName = 'files';
const folderPath = path.join(currentDirPath, folderName);
const wrongFilename = 'wrongFilename.txt';
const filePathWithWrongFile = path.join(folderPath, wrongFilename);
const properFilename = 'properFilename.md';
const filePathWithProperFile = path.join(folderPath, properFilename);
const errorMessage = 'FS operation failed.';

try {
await fs.access(filePathWithProperFile, fs.constants.F_OK);
throw new Error(errorMessage)
}
catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

try {

await fs.access(filePathWithWrongFile, fs.constants.F_OK);
await fs.rename(filePathWithWrongFile, filePathWithProperFile);
}
catch (error) {
if (error.code === 'ENOENT') {
error.message = errorMessage;
}
throw error;
}
};

await rename();
23 changes: 22 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
const calculateHash = async () => {
// Write your code here
// Write your code here
const fs= await import('node:fs/promises');
const path= await import('node:path');
const {fileURLToPath} = await import('node:url')
const {createHash} = await import('node:crypto');

const currentJSFileAbsolutePath = fileURLToPath(import.meta.url);
const currentDirPath = path.dirname(currentJSFileAbsolutePath);
const folderName = 'files';
const folderPath = path.join(currentDirPath, folderName);
const filenameToRead = 'fileToCalculateHashFor.txt';
const filePathWithFileToRead = path.join(folderPath, filenameToRead);
const hash = createHash('sha256')

try {
const fileContent = await fs.readFile(filePathWithFileToRead);
hash.update(fileContent);
console.log('Hash = ' + hash.digest('hex'));
}
catch (error) {
throw error;
}
};

await calculateHash();
29 changes: 12 additions & 17 deletions src/modules/cjsToEsm.cjs → src/modules/cjsToEsm.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');

import path from 'path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import './files/c';
import {fileURLToPath} from 'node:url';

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

let unknownObject;

if (random > 0.5) {
unknownObject = require('./files/a.json');
} else {
unknownObject = require('./files/b.json');
}
let unknownObject = random > 0.5 ?
await import('./files/a.json', {assert: {type: 'json'}}) :
await import('./files/b.json', {assert: {type: 'json'}});

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
Expand All @@ -33,8 +32,4 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
unknownObject,
myServer,
};

export { unknownObject, myServer };
20 changes: 19 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
const read = async () => {
// Write your code here
// Write your code here
const fs = await import('node:fs');
const path = await import('node:path');
const {fileURLToPath} = await import('node:url')

const currentJSFileAbsolutePath = fileURLToPath(import.meta.url);
const currentDirPath = path.dirname(currentJSFileAbsolutePath);
const folderName = 'files';
const folderPath = path.join(currentDirPath, folderName);
const filenameToRead = 'fileToRead.txt';
const filePathWithFileToRead = path.join(folderPath, filenameToRead);

const readStream = fs.createReadStream(filePathWithFileToRead, {encoding: 'utf8'});
readStream.on('readable', function() {
let data;
while ((data = readStream.read()) !== null) {
console.log(data);
}
})
};

await read();
18 changes: 17 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
const transform = async () => {
// Write your code here
// Write your code here
const stream = await import('stream');

const input = process.stdin;
const output = process.stdout;

const reverse = new stream.Transform({
transform(chunk, encoding, callback) {
const inputString = chunk.toString().trim();
const reversedString = inputString.split("").reverse().join("");
callback(null, reversedString + "\n");
}
});

await stream.pipeline(input, reverse, output, function(error){
console.error(error);
});
};

await transform();
17 changes: 16 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
const write = async () => {
// Write your code here
// Write your code here
const fs = await import('node:fs');
const path = await import('node:path');
const url = await import('node:url')

const currentJSFileAbsolutePath = url.fileURLToPath(import.meta.url);
const currentDirPath = path.dirname(currentJSFileAbsolutePath);
const folderName = 'files';
const folderPath = path.join(currentDirPath, folderName);
const filenameToWrite = 'fileToWrite.txt';
const filePathWithFileToWrite = path.join(folderPath, filenameToWrite);

const writeStream = fs.createWriteStream(filePathWithFileToWrite, {encoding: 'utf8'});
process.stdin.on('data', function (data) {
writeStream.write(data)
})
};

await write();
Loading