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

const parseArgs = () => {
// Write your code here
let result = '';
const perfix = '--';

argv.forEach((value, index, arr) => {
if (value.includes(perfix))
result += `${value.replace(`${perfix}`, '')} is ${arr[index + 1]}, `
});

console.log(result.trimEnd().slice(0, result.length-2));
};

parseArgs();
10 changes: 9 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { env } from 'node:process';

const parseEnv = () => {
// Write your code here
let result = '';
const perfix = 'RSS_';
for (const entire in env) {
if (entire.includes(perfix))
result += `${entire}=${env[entire]}; `;
}
console.log(result.trimEnd().slice(0, result.length-2));
};

parseEnv();
24 changes: 23 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { opendir, copyFile, access, mkdir } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const copy = async () => {
// Write your code here
const dirName = fileURLToPath(new URL('.', import.meta.url)),
folderName = 'files',
folderToCopy = 'files_copy',
fullPath = path.join(dirName, folderName),
fullPathToCopy = path.join(dirName, folderToCopy);

try {
await access(fullPath);
await mkdir(fullPathToCopy);

const dir = await opendir(fullPath);
for await (const dirent of dir) {
await copyFile(path.join(fullPath, dirent.name), path.join(fullPathToCopy, dirent.name));
}

} catch (error) {
if (error.code === 'ENOENT' || error.code === 'EEXIST') throw new Error('FS operation failed');
else throw error;
}
};

await copy();
17 changes: 16 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { appendFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const create = async () => {
// Write your code here
const dirName = fileURLToPath(new URL('.', import.meta.url)),
folderName = 'files',
fileName = 'fresh.txt',
content = 'I am fresh and young',
fullPath = path.join(dirName, folderName, fileName);

try {
await appendFile(fullPath, content, {flag: 'ax'})
} catch (error) {
if (error.code === 'EEXIST') throw new Error('FS operation failed');
else throw error;
}
};

await create();
17 changes: 16 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { rm, access } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const remove = async () => {
// Write your code here
const dirName = fileURLToPath(new URL('.', import.meta.url)),
folderName = 'files',
fileToDelName = 'fileToRemove.txt',
filePath = path.join(dirName, folderName, fileToDelName);

try {
await access(filePath);
} catch {
throw new Error('FS operation failed');
}

await rm(filePath);
};

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 @@
import { opendir, access } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const list = async () => {
// Write your code here
const dirName = fileURLToPath(new URL('.', import.meta.url)),
folderName = 'files',
fullPath = path.join(dirName, folderName),
filesList = [];

try {
await access(fullPath);
} catch {
throw new Error('FS operation failed');
}

const dir = await opendir(fullPath);
for await (const dirent of dir) {
filesList.push(dirent.name);
}
console.log(filesList);

};

await list();
18 changes: 17 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { access, readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const read = async () => {
// Write your code here
const dirName = fileURLToPath(new URL('.', import.meta.url)),
folderName = 'files',
fileToRead = 'fileToRead.txt',
filePath = path.join(dirName, folderName, fileToRead);

try {
await access(filePath);
} catch {
throw new Error('FS operation failed');
}

const contents = await readFile(filePath, { encoding: 'utf-8'});
console.log(contents);
};

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 { rename as renameItem, access } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const rename = async () => {
// Write your code here
const dirName = fileURLToPath(new URL('.', import.meta.url)),
folderName = 'files',
fileName = 'wrongFilename.txt',
fileRename = 'properFilename.md',
oldPath = path.join(dirName, folderName, fileName),
newPath = path.join(dirName, folderName, fileRename);

try {
await access(oldPath);
} catch {
throw new Error('FS operation failed');
}

try {
await access(newPath);
console.error(new Error('FS operation failed'));
}
catch {
await renameItem(oldPath, newPath);
}
};

await rename();
19 changes: 18 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { createReadStream } from 'node:fs';


const calculateHash = async () => {
// Write your code here
const dirName = fileURLToPath(new URL('.', import.meta.url)),
folderName = 'files',
fileToHash = 'fileToCalculateHashFor.txt',
filePath = path.join(dirName, folderName, fileToHash);
const rstream = createReadStream(filePath);
const hash = createHash('sha256');
hash.setEncoding('hex');
rstream.on('end', () => {
hash.end();
console.log(hash.read());
});
rstream.pipe(hash);
};

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

This file was deleted.

44 changes: 44 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { release, version } from 'node:os';
import { createServer as createServerHttp } from 'node:http';
import a from './files/a.json' with { type: 'json' };
import b from './files/b.json' with { type: 'json' };

import './files/c.js';

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = a;
} else {
unknownObject = b;
}

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

console.log(`Path to current file is ${fileURLToPath(import.meta.url)}`);
console.log(`Path to current directory is ${fileURLToPath(new URL('.', 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 default {
unknownObject,
myServer,
};

18 changes: 17 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { createReadStream } from 'node:fs';
import { fileURLToPath } from 'node:url';
import path from 'node:path'
import { stdout } from 'node:process';

const read = async () => {
// Write your code here
const dirName = fileURLToPath(new URL('.', import.meta.url)),
folderName = 'files',
fileToRead = 'fileToRead.txt',
filePath = path.join(dirName, folderName, fileToRead);

const fileStream = createReadStream(filePath);

for await (const chunk of fileStream) {
stdout.write(chunk);
}

fileStream.close();
};

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

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

const reverse = new Transform({ transform(chunk, encoding, callback) {
callback(null, chunk.toString().split('').reverse().join('')+'\n')
}})
stdin.resume();
stdin.setEncoding('utf8');
stdin.pipe(reverse).pipe(stdout);
};

await transform();
16 changes: 15 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { createWriteStream } from 'node:fs';
import { fileURLToPath } from 'node:url';
import path from 'node:path'
import { stdin } from 'node:process';

const write = async () => {
// Write your code here
const dirName = fileURLToPath(new URL('.', import.meta.url)),
folderName = 'files',
fileToWrite = 'fileToWrite.txt',
filePath = path.join(dirName, folderName, fileToWrite);
const writeStream = createWriteStream(filePath);
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', (data) => {
writeStream.write(data);
});
};

await write();