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 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);

const result = [];

for (let i = 0; i < args.length; i += 2) {
const key = args[i].replace(/^--/, '');
const value = args[i + 1];
result.push(`${key} is ${value}`);
}

console.log(result.join(', '));
};

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

const env = process.env;

const rssVars = Object.entries(env)
.filter(([key]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`);

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

parseEnv();
32 changes: 29 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
import { spawn } from 'node:child_process';
import { stdin, stdout } from 'node:process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

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

const spawnChildProcess = async (args) => {
// Write your code here
return new Promise((resolve, reject) => {
const scriptPath = path.join(__dirname, 'files', 'script.js');

const child = spawn('node', [scriptPath, ...args], {
stdio: ['pipe', 'pipe', 'inherit'],
});

stdin.pipe(child.stdin);

child.stdout.pipe(stdout);

child.on('close', (code) => {
resolve(`Child process exited with code ${code}`);
});

child.on('error', (err) => {
reject(err);
});
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
// Пример вызова для теста
spawnChildProcess(['arg1', 'arg2']);
35 changes: 34 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import fs from 'fs/promises';
import path from 'path';

const copy = async () => {
// Write your code here
const srcFolder = path.join('src', 'fs', 'files');
const destFolder = path.join('src', 'fs', 'files_copy');

try {
await fs.access(srcFolder);

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

}

await fs.mkdir(destFolder);

// Читаем список файлов в исходной папке
const items = await fs.readdir(srcFolder, { withFileTypes: true });

for (const item of items) {
if (item.isFile()) { // копируем только файлы
const srcPath = path.join(srcFolder, item.name);
const destPath = path.join(destFolder, item.name);
await fs.copyFile(srcPath, destPath);
}
}

console.log('Папка скопирована');

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

await copy();
15 changes: 13 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'fs/promises';
import path from 'path';

const create = async () => {
// Write your code here
const filePath = path.join('src', 'fs', 'files', 'fresh.txt');
const content = 'I am fresh and young';

try {
await fs.writeFile(filePath, content, { flag: 'wx' });
console.log('fresh.txt создан');
} catch {
throw new Error('FS operation failed');
}
};

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

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

try {
await fs.access(filePath);

await fs.unlink(filePath);

console.log('fileToRemove.txt удалён');
} catch {
throw new Error('FS operation failed');
}
};

await remove();
1 change: 0 additions & 1 deletion src/fs/files/fileToRemove.txt

This file was deleted.

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/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/properFilename.md
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**!
15 changes: 14 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from 'fs/promises';
import path from 'path';

const list = async () => {
// Write your code here
const folderPath = path.join('src', 'fs', 'files');

try {
await fs.access(folderPath);

const files = await fs.readdir(folderPath);

console.log(files);
} catch {
throw new Error('FS operation failed');
}
};

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

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

try {
await fs.access(filePath);

const content = await fs.readFile(filePath, 'utf8');

console.log(content);
} catch {
throw new Error('FS operation failed');
}
};

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

const rename = async () => {
// Write your code here
const folder = path.join('src', 'fs', 'files');
const oldName = 'wrongFilename.txt';
const newName = 'properFilename.md';

const oldPath = path.join(folder, oldName);
const newPath = path.join(folder, newName);

try {
await fs.access(oldPath);

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

}

await fs.rename(oldPath, newPath);
console.log(`${oldName} переименован в ${newName}`);
} catch {
throw new Error('FS operation failed');
}
};

await rename();
22 changes: 21 additions & 1 deletion 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';
import { pipeline } from 'stream/promises';

const calculateHash = async () => {
// Write your code here
try {
const filePath = path.join('src', 'hash', 'files', 'fileToCalculateHashFor.txt');

await fs.promises.access(filePath);

const hash = crypto.createHash('sha256');
const fileStream = fs.createReadStream(filePath);

await pipeline(fileStream, hash);

const hexHash = hash.digest('hex');
console.log(hexHash);

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

await calculateHash();
26 changes: 15 additions & 11 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const path = require('node:path');
const { release, version } = require('node:os');
const { createServer: createServerHttp } = require('node:http');

import path from 'node:path';
import { release, version } from 'node:os';
import { createServer as createServerHttp } from 'node:http';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
require('./files/c.cjs');
const aJson = require('./files/a.json');
const bJson = require('./files/b.json');

const random = Math.random();

const unknownObject = random > 0.5 ? require('./files/a.json') : require('./files/b.json');
const unknownObject = random > 0.5 ? aJson : bJson;

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

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

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

Expand All @@ -20,15 +28,11 @@ const myServer = createServerHttp((_, res) => {
});

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');
});

module.exports = {
unknownObject,
myServer,
};
export { unknownObject, myServer };
17 changes: 16 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from 'fs';
import path from 'path';
import { pipeline } from 'stream/promises';

const read = async () => {
// Write your code here
try {
const filePath = path.join('src', 'streams', 'files', 'fileToRead.txt');

await fs.promises.access(filePath);

const fileStream = fs.createReadStream(filePath, { encoding: 'utf-8' });

await pipeline(fileStream, process.stdout);

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

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

const transform = async () => {
// Write your code here
const reverseStream = new Transform({
transform(chunk, encoding, callback) {
const reversed = chunk.toString().split('').reverse().join('');
callback(null, reversed);
}
});

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

await new Promise((resolve, reject) => {
reverseStream.on('end', resolve);
reverseStream.on('error', () => reject(new Error('FS operation failed')));
process.stdin.on('error', () => reject(new Error('FS operation failed')));
});
};

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

const write = async () => {
// Write your code here
const filePath = path.join('src', 'streams', 'files', 'fileToWrite.txt');

try {
const writeStream = fs.createWriteStream(filePath, { encoding: 'utf8' });

process.stdin.pipe(writeStream);

await new Promise((resolve, reject) => {
writeStream.on('finish', resolve);
writeStream.on('error', () => reject(new Error('FS operation failed')));
process.stdin.on('error', () => reject(new Error('FS operation failed')));
});
} catch {
throw new Error('FS operation failed');
}
};

await write();
Loading