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
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
const parseArgs = () => {
// Write your code here
const envArguments = process.argv,
resultArray = [];
if(envArguments) {
envArguments.forEach((argValue, index, envArguments) => {
if (argValue.startsWith('--')){
const resultString = `${argValue.split('--')[1]} is ${envArguments[index + 1]}`;
resultArray.push(resultString);
}
});

console.log(resultArray.join(', '));
} else {
throw new Error('No environment arguments were found');
}

};

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
const envProps = Object.entries(process.env);
if (envProps) {
const resultString = envProps
.filter(([propName, _]) => propName.startsWith('RSS_'))
.map(([propName, propValue]) => `${propName}=${propValue}`)
.join(';');
console.log(resultString);
} else {
throw new Error('No environment props were found');
}
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const __filename = fileURLToPath(import.meta.url),
__dirname = dirname(__filename),
pathToScript = './files/script.js';

const childProcess = spawn('node', [join(__dirname, pathToScript), ...args], {
stdio: ['pipe', 'pipe', 'inherit', 'ipc']
});

childProcess.stdout.pipe(process.stdout);
process.stdin.pipe(childProcess.stdin);

childProcess.on('error', (childprocessError) => {
throw childprocessError;
});

return childProcess;
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['balerina capuchina', 'some other meme from tik-tok']);
27 changes: 26 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import fs from 'fs';

const copy = async () => {
// Write your code here
const sourceDirPath = './src/fs/files/',
targetDirPath = './src/fs/files_copy/';

fs.access(sourceDirPath, fs.constants.F_OK, (accessError) => {
if (accessError) {
throw new Error('FS operation failed');
}
});

fs.access(targetDirPath, fs.constants.F_OK, (accessError) => {
if (accessError) {
fs.cp(sourceDirPath, targetDirPath, {force: false, errorOnExist: true, recursive: true}, (copyFolderError) => {
if (copyFolderError) {
throw copyFolderError;
} else {
console.log('Folder "Files" is successfully copied to "Files_copy"');
}

});
} else {
throw new Error('FS operation failed');
}
});

};

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

const create = async () => {
// Write your code here
const filePath = './src/fs/files/fresh.txt';

fs.access(filePath, fs.constants.F_OK, (accessError) => {
if (accessError) {
fs.writeFile(filePath, 'I am fresh and young', (creationError) => {
if (creationError) {
throw creationError;
} else {
console.log('File is created successfully');
}

});
} else {
throw new Error('FS operation failed');
}
})
};

await create();
11 changes: 10 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fs from 'fs/promises';

const remove = async () => {
// Write your code here
const targetFilePath = './src/fs/files/fileToRemove.txt';

try {
await fs.rm(targetFilePath);
console.log('File "fileToRemove" is removed');
} catch (removeFileError) {
throw new Error('FS operation failed');
}
};

await remove();
1 change: 1 addition & 0 deletions src/fs/files/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
File renamed without changes.
1 change: 1 addition & 0 deletions src/fs/files_copy/dontLookAtMe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
What are you looking at?!
7 changes: 7 additions & 0 deletions src/fs/files_copy/fileToRead.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
My content
should
be
printed
into
console
!
1 change: 1 addition & 0 deletions src/fs/files_copy/fileToRemove.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
How dare you!
1 change: 1 addition & 0 deletions src/fs/files_copy/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
1 change: 1 addition & 0 deletions src/fs/files_copy/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Node.js
3 changes: 3 additions & 0 deletions src/fs/files_copy/wrongFilename.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is a file with a wrong filename

Hello from **markdown**!
16 changes: 15 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from 'fs/promises';

const list = async () => {
// Write your code here
const targerDirPath = './src/fs/files';
try {
await fs.access(targerDirPath, fs.constants.F_OK);
} catch (accessError) {
throw new Error('FS operation failed');
}

try {
const listOfFiles = await fs.readdir(targerDirPath, {recursive: true});
console.log('Files from folder "fiiles":', listOfFiles);
} catch (readDirError) {
throw readDirError;
}
};

await list();
11 changes: 10 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fs from 'fs/promises';

const read = async () => {
// Write your code here
const targetFilePath = './src/fs/files/fileToRead.txt';

try {
const filesContent = await fs.readFile(targetFilePath, { encoding: 'utf8' });
console.log(filesContent);
} catch (readFileError) {
throw new Error('FS operation failed');
}
};

await read();
25 changes: 24 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import fs from 'fs';

const rename = async () => {
// Write your code here
const wrongFilenamePath = './src/fs/files/wrongFilename.txt',
correctFilenamePath = './src/fs/files/properFilename.md';

fs.access(wrongFilenamePath, fs.constants.F_OK, (accessError) => {
if (accessError) {
throw new Error('FS operation failed');
}
});

fs.access(correctFilenamePath, fs.constants.F_OK, (accessError) => {
if (accessError) {
fs.rename(wrongFilenamePath, correctFilenamePath, (renameFileError) => {
if (renameFileError) {
throw renameFileError;
} else {
console.log('Filename is changed to correct one');
}
});
} else {
throw new Error('FS operation failed');
}
});
};

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

const calculateHash = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url),
__dirname = dirname(__filename),
fullPathToTargetFile = resolve(__dirname, './files/fileToCalculateHashFor.txt'),
fileContent = await readFile(fullPathToTargetFile, 'utf8');

const resultHash = createHash('sha256').update(fileContent).digest('hex');
console.log(resultHash);
};

await calculateHash();
54 changes: 54 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { dirname, sep, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { release, version } from 'node:os';
import { createServer as createServerHttp } from 'node:http';
import { readFile } from 'fs/promises';
import './files/c.cjs';

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

async function loadFile(pathToFile) {
try {
const fullPathToFile = resolve(__dirname, pathToFile);
const fileContent = await readFile(fullPathToFile, 'utf8');
const json = JSON.parse(fileContent);
return json;
} catch (error) {
console.error('Error loading file:', error);
throw error;
}
}

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${sep}"`);
console.log(`Path to current file is ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

const myServer = createServerHttp((_, res) => {
res.end('Request accepted');
});

const PORT = 3000;

// Выбираем случайно JSON файл
if (random > 0.5) {
unknownObject = await loadFile('./files/a.json');
} else {
unknownObject = await loadFile('./files/b.json');
}

console.log('Loaded object:', unknownObject);

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log('To terminate it, use Ctrl+C combination');
});

export {
unknownObject,
myServer,
};
1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
balerina capuchina
21 changes: 20 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import fs from 'fs';
import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path';

const read = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url),
__dirname = dirname(__filename),
pathToFile = './files/fileToRead.txt',
fullPathToFile = resolve(__dirname, pathToFile);
const readableStream = fs.createReadStream(fullPathToFile, 'utf8');

readableStream.pipe(process.stdout);

readableStream.on('open', () => {
console.log('File is opened successfully');
})

readableStream.on('error', (readError) => {
throw readError;
});

};

await read();
25 changes: 24 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { Transform } from 'node:stream';

const transform = async () => {
// Write your code here
const transformStream = new Transform({
transform(chunk, encoding, callback) {
const reversedData = chunk.toString().split('').reverse().join('');
this.push(reversedData);
callback();
}
});

process.stdin.pipe(transformStream).pipe(process.stdout);

process.stdin.on('error', (writeError) => {
throw writeError;
});

process.stdout.on('error', (readError) => {
throw readError;
});

transformStream.on('error', (transformError) => {
throw transformError;
});

};

await transform();
Loading