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

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

10 changes: 10 additions & 0 deletions node_modules/.yarn-integrity

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

13 changes: 13 additions & 0 deletions package-lock.json

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

11 changes: 10 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const result = [];

for (let index = 0; index < args.length; index += 2) {
const argumentName = args[index].replace('--', '');
const argumentValue = args[index + 1];
result.push(`${argumentName} is ${argumentValue}`);
}

console.log(result.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
const prefix = "RSS_";
const allEnvVariables = process.env;
const arrEnvVarialblesWhichStartWithPrefix = [];
for (const envVariable in allEnvVariables) {
if (envVariable.startsWith(prefix)) {
arrEnvVarialblesWhichStartWithPrefix.push(`${envVariable}=${allEnvVariables[envVariable]}`);
}
}

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

parseEnv();
8 changes: 8 additions & 0 deletions src/constants/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const ERROR_MESSAGES = {
fsFailed: 'FS operation failed',
}

export const ERROR_CODES = {
cpExist: 'ERR_FS_CP_EEXIST',
eoent: 'ENOENT'
}
25 changes: 23 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import { join } from 'path';
import { fork } from 'child_process';
import { getDirname } from '../utils/get-dirname.js';

const __dirname = getDirname(import.meta.url);
const scriptPath = join(__dirname, 'files', 'script.js');

const spawnChildProcess = async (args) => {
// Write your code here
const childProcess = fork(scriptPath, args, { silent: true });

const handleError = () => {
console.log('Error');
readStream.destroy();
writeStream.end('Finished with error...');
}

process.stdin
.on('error', handleError)
.pipe(childProcess.stdin);

childProcess.stdout
.on('error', handleError)
.pipe(process.stdout);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess( ['someArgument1', 'someArgument2'] );
32 changes: 31 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import { access, cp } from 'fs/promises';
import { join} from 'path';
import { ERROR_CODES, ERROR_MESSAGES } from '../constants/errors.js';
import { getDirname } from '../utils/get-dirname.js';

const __dirname = getDirname(import.meta.url);
const initFolderPath = join(__dirname, 'files');
const copyFolderPath = join(__dirname, 'files_copy');

const copy = async () => {
// Write your code here
try {
const isInitFolderExist = await access(initFolderPath)
.then(() => true)
.catch(() => false);
if (!isInitFolderExist) {
throw new Error(ERROR_MESSAGES.fsFailed);
}

await cp(
initFolderPath,
copyFolderPath,
{ recursive: true, force: false, errorOnExist: true }
);
} catch (error) {
if (error.code === ERROR_CODES.cpExist) {
throw new Error(ERROR_MESSAGES.fsFailed);
}

if (error.code !== ERROR_CODES.eoent) {
throw error;
}
}
};

await copy();
20 changes: 19 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { access, writeFile } from 'fs/promises';
import { join } from 'path';
import { ERROR_CODES, ERROR_MESSAGES } from '../constants/errors.js';
import { getDirname } from '../utils/get-dirname.js';

const __dirname = getDirname(import.meta.url);
const filePath = join(__dirname, 'files', 'fresh.txt');
const fileContent = 'I am fresh and young';

const create = async () => {
// Write your code here
try {
await access(filePath);
throw new Error(ERROR_MESSAGES.fsFailed);
} catch (error) {
if (error.code !== ERROR_CODES.eoent) {
throw error;
}
}

await writeFile(filePath, fileContent);
};

await create();
24 changes: 23 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { unlink, access } from 'fs/promises';
import { join } from 'path';
import { getDirname } from '../utils/get-dirname.js';
import { ERROR_CODES, ERROR_MESSAGES } from '../constants/errors.js';

const __dirname = getDirname(import.meta.url);
const fileToRemove = join(__dirname, 'files', 'fileToRemove.txt');

const remove = async () => {
// Write your code here
try {
const isFileToRemoveExist = await access(fileToRemove)
.then(() => true)
.catch(() => false);

if (!isFileToRemoveExist) {
throw new Error(ERROR_MESSAGES.fsFailed);
}

await unlink(fileToRemove);
} catch (error) {
if (error.code !== ERROR_CODES.eoent) {
throw error;
}
}
};

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
27 changes: 26 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { access, readdir } from 'fs/promises';
import { join} from 'path';
import { ERROR_CODES, ERROR_MESSAGES } from '../constants/errors.js';
import { getDirname } from '../utils/get-dirname.js';

const __dirname = getDirname(import.meta.url);
const initFolderPath = join(__dirname, 'files');

const list = async () => {
// Write your code here
try {

const isInitFolderExist = await access(initFolderPath)
.then(() => true)
.catch(() => false);
if (!isInitFolderExist) {
throw new Error(ERROR_MESSAGES.fsFailed);
}
const files = await readdir(initFolderPath);

for (const file of files) {
console.log(file);
}
} catch (error) {
if (error.code !== ERROR_CODES.eoent) {
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 @@
import {createReadStream} from 'fs';
import readline from "readline";
import events from "events";
import { join} from 'path';
import { ERROR_CODES, ERROR_MESSAGES } from '../constants/errors.js';
import { getDirname } from '../utils/get-dirname.js';

const __dirname = getDirname(import.meta.url);
const fileToReadPath = join(__dirname, 'files', 'fileToRead.txt');

const read = async () => {
// Write your code here
try {
const readLine = readline.createInterface({
input: createReadStream(fileToReadPath)
});

readLine.on('line', line => console.log(line));

await events.once(readLine, 'close');
} catch (error) {
if (error.code === ERROR_CODES.eoent) {
throw new Error('FS operation failed');
}
}
};

await read();
28 changes: 27 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import { rename as fileRename, access } from 'fs/promises';
import { join } from 'path';
import { getDirname } from '../utils/get-dirname.js';
import { ERROR_CODES, ERROR_MESSAGES } from '../constants/errors.js';

const __dirname = getDirname(import.meta.url);
const wrongFilenamePath = join(__dirname, 'files', 'wrongFilename.txt');
const properFilenamePath = join(__dirname, 'files', 'properFilename.md');

const rename = async () => {
// Write your code here
try {
const isWrongFileExist = await access(wrongFilenamePath)
.then(() => true)
.catch(() => false);

if (!isWrongFileExist) {
throw new Error(ERROR_MESSAGES.fsFailed);
}

await access(properFilenamePath);
throw new Error(ERROR_MESSAGES.fsFailed);
} catch (error) {
if (error.code !== ERROR_CODES.eoent) {
throw error;
}
}

await fileRename( wrongFilenamePath, properFilenamePath );
};

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

const __dirname = getDirname(import.meta.url);
const filePath = join(__dirname, 'files', 'fileToCalculateHashFor.txt');

const calculateHash = async () => {
// Write your code here
const fileContent = await readFile(filePath);

const hash = createHash('sha256');
hash.update(fileContent);
const hashHex = hash.digest('hex');

console.log(hashHex);
};

await calculateHash();
24 changes: 17 additions & 7 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
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 {fileURLToPath} from "url";
import { getDirname } from '../utils/get-dirname.js';
import './files/c.js';

const random = Math.random();

let unknownObject;

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

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.sep}"`);


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

console.log(`Path to current file is ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

Expand All @@ -33,7 +43,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
export {
unknownObject,
myServer,
};
Expand Down
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 {createReadStream} from 'fs';
import { join } from 'path';
import { getDirname } from '../utils/get-dirname.js';

const __dirname = getDirname(import.meta.url);
const fileToReadPath = join(__dirname, 'files', 'fileToRead.txt');

import fs from 'fs';

const read = async () => {
// Write your code here
const readStream = createReadStream(fileToReadPath);

const handleError = () => {
console.log('Error');
readStream.destroy();
}

readStream
.on('error', handleError)
.pipe(process.stdout);

};

await read();
Loading