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 process from 'node:process';

const parseArgs = () => {
// Write your code here
// console.log('process.argv = ', process.argv);
const argsArray = process.argv.slice(2);
argsArray.forEach((arg, index) => {
if (index % 2 === 0) {
const propName = arg.slice(2);
const value = argsArray[index + 1];
console.log(`${propName} is ${value}`);
}
});
};

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 @@
import process from 'node:process';

const parseEnv = () => {
// Write your code here
// console.log('process.env = ', process.env);
Object.keys(process.env).forEach(key => {
if (key.startsWith('RSS_')) {
console.log(`${key}=${process.env[key]};`);
}
});
};

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

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

const pathToFile = join(__dirname, 'files', 'script.js');

const spawnChildProcess = async (args) => {
// Write your code here
const allArgs = [pathToFile].concat(args);
//console.log('allArgs = ', allArgs);
const childProcess = spawn('node', [pathToFile, ...args], {stdio: ["pipe", "pipe", "pipe", "ipc"]});

process.stdin.pipe(childProcess.stdin);

childProcess.stdout.pipe(process.stdout);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['--some-arg value1', '--other 1337', '--arg2 42']);
32 changes: 30 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import { promises as fs, existsSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

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

const pathToFile = join(__dirname, 'files');
const pathToCopy = join(__dirname, 'files_copy');

const copy = async () => {
// Write your code here
if (!existsSync(pathToFile)) {git
throw new Error('Source folder `files` does not exist');
}

if (existsSync(pathToCopy)) {
throw new Error('Destination folder `files_copy` already exists');
}

try {
await fs.mkdir(pathToCopy);
const files = await fs.readdir(pathToFile);
for (const file of files) {
const contents = await fs.readFile(join(pathToFile, file));
await fs.writeFile(join(pathToCopy, file), contents);
}
console.log('All files copied successfully');
} catch (error) {
throw new Error('FS operation failed: ' + error.message);
}
};

await copy();
await copy();
24 changes: 23 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { promises as fs } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

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

// console.log('fs = ', fs);

const pathToFile = join(__dirname, 'files', 'fresh.txt');

// console.log('pathToFile = ', pathToFile);

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

await create();
16 changes: 15 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { promises as fs, existsSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

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

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

const remove = async () => {
// Write your code here
if (existsSync(pathToFile)) {
await fs.unlink(pathToFile);
console.log('File fileToRemove.txt has been deleted');
} else {
throw new Error('FS operation failed: fileToRemove.txt does not exist');
}
};

await remove();
File renamed without changes.
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
!
File renamed without changes.
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/wrongFilename.txt
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**!
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 { promises as fs, existsSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

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

const pathToFile = join(__dirname, 'files');

const list = async () => {
// Write your code here
if (!existsSync(pathToFile)) {
throw new Error('FS operation failed');
}

try {
const files = await fs.readdir(pathToFile);
files.forEach(file => {
console.log(file);
});
} catch (error) {
console.error('Error reading files');
}
};

await list();
20 changes: 19 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { promises as fs, existsSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

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

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

const read = async () => {
// Write your code here
try {
if (!existsSync(pathToFile)) {
throw new Error('FS operation failed: fileToRead.txt does not exist');
}

const content = await fs.readFile(pathToFile, 'utf-8');
console.log(content);
} catch (err) {
throw new Error(err.message);
}
};

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 { promises as fs, existsSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

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

const pathToFile = join(__dirname, 'files');

const rename = async () => {
// Write your code here
const wrongFilename = 'wrongFilename.txt';
const properFilename = 'properFilename.md';

const sourcePath = join(pathToFile, wrongFilename);
const destinationPath = join(pathToFile, properFilename);

if (!existsSync(sourcePath)) {
throw new Error('FS operation failed: wrongFilename.txt does not exist');
}

if (existsSync(destinationPath)) {
throw new Error('FS operation failed: properFilename.md already exists');
}

await fs.rename(sourcePath, destinationPath);
};

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

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

const pathToFile = join(__dirname, 'files', 'fileToCalculateHashFor.txt');

const calculateHash = async () => {
// Write your code here
const hash = createHash('sha256');
// console.log('hash = ', hash);
const readStream = createReadStream(pathToFile);

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

readStream.on('end', () => {
const hexHash = hash.digest('hex');
console.log('SHA256 Hash:', hexHash);
});
};

await calculateHash();
22 changes: 9 additions & 13 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
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 './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}"`);

console.log(`Path to current file is ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);
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');
Expand All @@ -33,8 +33,4 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
unknownObject,
myServer,
};

export { unknownObject, myServer };
2 changes: 1 addition & 1 deletion src/streams/files/fileToRead.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This file should be read using Streams API
This file should be read using Streams API!
11 changes: 11 additions & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sdfgsfg
sdfg
dfg
dsfg

dfg
d
fg

dfg

24 changes: 23 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { createReadStream } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

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

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

const read = async () => {
// Write your code here
const stream = createReadStream(pathToFile);

stream.on('data', (block) => {
// console.log('block = ', block)
process.stdout.write(block);
});

stream.on('end', () => {
// All data has been read!
});

stream.on('error', (err) => {
console.error('Error reading file:', err);
});
};

await read();
Loading