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
10 changes: 8 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const argValues = process.argv.slice(2);

const parseArgs = () => {
// Write your code here
const data = [];
for (let i = 0; i < argValues.length; i += 2) {
data.push(`${argValues[i].slice(2)} is ${argValues[i + 1]}`);
}
console.log(data.join(', '));
};

parseArgs();
parseArgs();
15 changes: 13 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const envVariables = process.env;

const parseEnv = () => {
// Write your code here
const data = Object.entries(envVariables)
.filter(([key, value]) => {
if (key.startsWith('RSS_')) {
return [key, value];
}
})
.map(([key, value]) => `${key}=${value}`)
.join('; ');

console.log(data);
};

parseEnv();
parseEnv();
40 changes: 39 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
import fs from 'fs/promises';
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);

const isDirExist = async (path) => {
try {
await fs.access(path);
return true;
} catch (error) {
return false;
}
};

const inputDir = path.join(__dirname, 'files');
const outputDir = path.join(__dirname, 'files_copy');

const condition =
(await !isDirExist(inputDir)) || (await isDirExist(outputDir));

const copy = async () => {
// Write your code here
if (condition) {
throw new Error('FS operation failed');
} else {
try {
const inputDirContent = await fs.readdir(inputDir);
await fs.mkdir(outputDir);
inputDirContent.forEach(async (file) => {
try {
const sourcePath = path.join(inputDir, file);
const destPath = path.join(outputDir, file);
await fs.copyFile(sourcePath, destPath);
} catch (error) {
throw new Error('FS copy operation failed');
}
});
console.log('copy operation finished');
} catch (error) {
throw new Error('FS operation failed');
}
}
};

await copy();
27 changes: 25 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import fs from 'fs/promises';
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);

const filePath = path.join(__dirname, 'files', 'fresh.txt');
const fileContent = 'I am fresh and young';
const isFileExist = async (path) => {
try {
await fs.stat(path);
return true;
} catch (error) {
return false;
}
};

const create = async () => {
// Write your code here
const fileStats = await isFileExist(filePath);
if (fileStats) {
throw new Error('FS operation failed');
} else {
try {
await fs.writeFile(filePath, fileContent);
console.log('file created');
} catch (error) {}
}
};

await create();
await create();
26 changes: 24 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import fs from 'fs/promises';
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);

const isFileExist = async (path) => {
try {
await fs.stat(path);
return true;
} catch (error) {
return false;
}
};

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

const remove = async () => {
// Write your code here
if (!(await isFileExist(fileToDelete))) {
throw new Error('FS operation failed');
} else {
try {
await fs.unlink(fileToDelete);
console.log('file was removed successfuly');
} catch (error) {}
}
};

await remove();
await remove();
26 changes: 24 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import fs from 'fs/promises';
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);

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

const isFolderExist = async (path) => {
try {
await fs.access(path);
return true;
} catch (error) {
return false;
}
};

const list = async () => {
// Write your code here
if (!(await isFolderExist(folderPath))) {
throw new Error('FS operation failed');
}
try {
const files = await fs.readdir(folderPath);
const filesNamesArray = files.map((file) => file);
console.log(filesNamesArray);
} catch (error) {}
};

await list();
await list();
26 changes: 24 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import fs from 'fs/promises';
import { dirname, join } from 'path';

const __dirname = dirname(new URL(import.meta.url).pathname);

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

const isFileExist = async (path) => {
try {
await fs.stat(path);
return true;
} catch (error) {
return false;
}
};
const read = async () => {
// Write your code here
if (!(await isFileExist(filePath))) {
throw new Error('FS operation failed');
}

try {
const fileContent = await fs.readFile(filePath, 'utf-8');
console.log(fileContent);
} catch (error) {}
};

await read();
await read();
29 changes: 27 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import fs from 'fs/promises';
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);

const isFileExist = async (path) => {
try {
await fs.stat(path);
return true;
} catch (error) {
return false;
}
};

const wrongFileName = path.join(__dirname, 'files', 'wrongFilename.txt');
const rightFileName = path.join(__dirname, 'files', 'properFilename.md');

const isWrongApsentAndRightNot =
!(await isFileExist(wrongFileName)) && (await isFileExist(rightFileName));

const rename = async () => {
// Write your code here
if (isWrongApsentAndRightNot) {
throw new Error('FS operation failed');
} else {
try {
await fs.rename(wrongFileName, rightFileName);
} catch (error) {}
}
};

await rename();
await rename();
24 changes: 22 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
const __dirname = path.dirname(new URL(import.meta.url).pathname);

const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');
const calculateHash = async () => {
// Write your code here
const hash = crypto.createHash('sha256');
const hashStream = fs.createReadStream(filePath);

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

hashStream.on('error', () => {
throw new Error('smth went wrong with hashing');
});

hashStream.on('end', () => {
const hashHEX = hash.digest('hex');
console.log(hashHEX);
});
};

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

This file was deleted.

36 changes: 36 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import './files/c.js';

const random = Math.random();

let unknownObject;

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

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

console.log(`Path to current file is ${import.meta.url}`);
console.log(`Path to current directory is ${path.dirname(import.meta.url)}`);

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

const PORT = 3000;

console.log(unknownObject);

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

export { unknownObject, myServer };
17 changes: 15 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from 'fs';
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);

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

const read = async () => {
// Write your code here
const readStream = fs.createReadStream(filePath);
readStream.pipe(process.stdout);
readStream.on('error', (err) => {
console.log('error', err);
});
readStream.on('end', () => {
console.log('\n');
});
};

await read();
await read();
13 changes: 11 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Transform } from 'stream';
const transform = async () => {
// Write your code here
const reversedStream = new Transform({
transform: function (chunk, encoding, cb) {
const reversedArray = chunk.toString().split('');
const lastChar = reversedArray.pop();
const reversedChunk = reversedArray.reverse().concat(lastChar).join('');
cb(null, reversedChunk);
},
});
process.stdin.pipe(reversedStream).pipe(process.stdout);
};

await transform();
await transform();
Loading