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.

9 changes: 8 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseArgs = () => {
// Write your code here
const arg = process.argv.slice(2)
let result = ''
arg.forEach((item, i) => {
if(i % 2 === 0){
result = result + `${item.slice(2)} is ${arg[i + 1]}, `
}
})
console.log(result)
};

parseArgs();
12 changes: 11 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const parseEnv = () => {
// Write your code here
const env = process.env;
const result = {};

for (const key in env) {
if (key.startsWith('RSS_')) {
result[key] = env[key];
}
}

const output = Object.entries(result).map(([key, value]) => `${key}=${value}`).join('; ');
console.log(output);
};

parseEnv();
16 changes: 15 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url';
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const tagetSourceDirName = path.join(dirname, 'files');
const tagetDistDirName = path.join(dirname, 'files_copy');

const copy = async () => {
// Write your code here
try {
await fs.access(tagetSourceDirName)
await fs.cp(tagetSourceDirName, tagetDistDirName, {recursive: true , errorOnExist: true, force: false})

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

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 fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url';
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const freshFile = path.join(dirname, 'files','fresh.txt');



const create = async () => {
// Write your code here
try {
await fs.access(freshFile)
throw new Error('FS operation failed');
} catch (error) {
if (error.code === 'ENOENT') {
await fs.writeFile(freshFile , 'I am fresh and young');
} else {
console.error(error.message);
}
}
};

await create();
13 changes: 12 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url';
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const targetFile = path.join(dirname, 'files','fileToRemove.txt');

const remove = async () => {
// Write your code here
try {
await fs.unlink(targetFile)
} catch(error){
throw new Error('FS operation failed')
}
};

await remove();
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 {readdir} from 'node:fs/promises'
import {dirname, join} from 'node:path'
import { fileURLToPath } from 'node:url';
const fileName = fileURLToPath(import.meta.url)
const dirName = dirname(fileName)
const tagetDirName = join(dirName, 'files')



const list = async () => {
// Write your code here
try {
const files = await readdir(tagetDirName)
console.log("files",files)
} catch (error) {
throw new Error('FS operation failed')
}
};

await list();
14 changes: 13 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import {readFile} from 'node:fs/promises'
import {dirname, join} from 'node:path'
import { fileURLToPath } from 'node:url';
const fileName = fileURLToPath(import.meta.url)
const dirName = dirname(fileName)
const tagetFileName = join(dirName, 'files', 'fileToRead.txt')

const read = async () => {
// Write your code here
try {
const content = await readFile(tagetFileName, 'utf-8')
console.log(content)
} catch (error) {
throw new Error('FS operation failed')
}
};

await read();
24 changes: 23 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url';
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const oldFileName = path.join(dirname, 'files', 'wrongFilename.txt')
const newFileName = path.join(dirname, 'files', 'properFilename.md')
const rename = async () => {
// Write your code here
try {
await fs.access(newFileName)
throw new Error('FS operation failed')
} catch (error) {
if(error.code === 'ENOENT'){
try {
await fs.access(oldFileName)
await fs.rename(oldFileName, newFileName)
} catch (error) {
throw new Error('FS operation failed')
}

} else{
console.error(error.message);
}
}
};

await rename();
9 changes: 8 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import crypto from 'crypto'
import fs from 'fs'

const calculateHash = async () => {
// Write your code here
const hash = crypto.createHash('sha256');
const stream = fs.createReadStream(new URL('./files/fileToCalculateHashFor.txt', import.meta.url));

stream.on('data', (chunk) => hash.update(chunk));
stream.on('end', () => console.log(hash.digest('hex')));
};

await calculateHash();
21 changes: 12 additions & 9 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c.cjs');
import path from 'path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import { fileURLToPath } from 'url';
import { readFileSync } from 'fs';
import './files/c.cjs';

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = require('./files/a.json');
unknownObject = JSON.parse(readFileSync(new URL('./files/a.json', import.meta.url)));
} else {
unknownObject = require('./files/b.json');
unknownObject = JSON.parse(readFileSync(new URL('./files/b.json', import.meta.url)));
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

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

module.exports = {
export {
unknownObject,
myServer,
};

};
8 changes: 7 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createReadStream } from 'node:fs';

const read = async () => {
// Write your code here
const readStream = createReadStream(new URL('./files/fileToRead.txt', import.meta.url), 'utf8');
readStream.pipe(process.stdout)
readStream.on('end', () => {
console.log('\n');
});
};

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

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

const transform = new Transform({
transform(chunk, _, cb) {
const result = chunk.toString().split('').reverse().join('');
cb(null, result + '\n');
}
});

console.log('Введите текст (для выхода Ctrl+С)');

pipeline(
process.stdin,
transform,
process.stdout,
(err)=>{}
);
};

await transform();
13 changes: 12 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { createWriteStream } from 'node:fs';
import { pipeline } from 'stream';
const write = async () => {
// Write your code here
const writableStream = createWriteStream(new URL('./files/fileToWrite.txt', import.meta.url), 'utf8');

console.log('Введите текст (для выхода Ctrl+С)');

pipeline(
process.stdin,
writableStream,
(err)=>{}
);

};

await write();
7 changes: 6 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import fs from 'fs'
import zlib from 'zlib'
const compress = async () => {
// Write your code here
const readableStream = fs.createReadStream(new URL('./files/fileToCompress.txt', import.meta.url))
const writeableStream = fs.createWriteStream(new URL('./files/archive.gz', import.meta.url))
const gzip = zlib.createGzip()
readableStream.pipe(gzip).pipe(writeableStream)
};

await compress();
7 changes: 6 additions & 1 deletion src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import fs from 'fs';
import zlib from 'zlib';
const decompress = async () => {
// Write your code here
const readableStream = fs.createReadStream( new URL('./files/archive.gz', import.meta.url));
const writeableStream = fs.createWriteStream(new URL('./files/fileToCompress.txt', import.meta.url));
const gunzip = zlib.createGunzip();
readableStream.pipe(gunzip).pipe(writeableStream);
};

await decompress();