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
13 changes: 12 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);

const output = args.reduce((acc, curr, index, array) => {
if (curr.startsWith('--')) {
const key = curr.slice(2);
const value = array[index + 1];
acc.push(`${key} is ${value}`);
}
return acc;
}, []);

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

parseArgs();
7 changes: 6 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const parseEnv = () => {
// Write your code here
const rssVars = Object.entries(process.env)
.filter(([key]) => key.startsWith('RSS_'));

const output = rssVars.map(([key, value]) => `${key}=${value}`).join('; ');

console.log(output);
};

parseEnv();
27 changes: 25 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import { spawn } from 'node:child_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
const scriptPath = path.join(__dirname, 'files', 'script.js');

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

process.stdin.pipe(child.stdin);

child.stdout.pipe(process.stdout);

child.on('exit', (code) => console.log(`Child process exited with code ${code}`));

child.on('error', (err) => console.error('Child process error:', err));
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['hello', 'world']);
33 changes: 32 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
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 copy = async () => {
// Write your code here
const srcDir = path.join(__dirname, 'files');
const destDir = path.join(__dirname, 'files_copy');

try {

await fs.access(srcDir);

try {

await fs.access(destDir);

throw new Error('FS operation failed');

} catch (err) {

if (err.code !== 'ENOENT') {
throw new Error('FS operation failed');
}
}

await fs.cp(srcDir, destDir, { recursive: true });

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

await copy();
33 changes: 32 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
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 create = async () => {
// Write your code here

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

try {

await fs.access(dirPath);

try {

await fs.access(filePath);

throw new Error('FS operation failed');

} catch (err) {

if (err.code === 'ENOENT') {
await fs.writeFile(filePath, 'I am fresh and young', 'utf8');
} else {
throw new Error('FS operation failed');
}
}
} catch (err) {
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 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 remove = async () => {
// Write your code here
const filePath = path.join(__dirname, 'files', 'fileToRemove.txt');

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

await remove();
19 changes: 18 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
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 list = async () => {
// Write your code here
const dirPath = path.join(__dirname, 'files');

try {
await fs.access(dirPath);

const files = await fs.readdir(dirPath);

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

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 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 read = async () => {
// Write your code here
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');

try {

await fs.access(filePath);

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

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

await read();
28 changes: 27 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
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 rename = async () => {
// Write your code here
const dirPath = path.join(__dirname, 'files');
const oldPath = path.join(dirPath, 'wrongFilename.txt');
const newPath = path.join(dirPath, 'properFilename.md');

try {
await fs.access(oldPath);

try {
await fs.access(newPath);
throw new Error('FS operation failed');
} catch (err) {
if (err.code !== 'ENOENT') {
throw new Error('FS operation failed');
}
}

await fs.rename(oldPath, newPath);
} catch (err) {
throw new Error('FS operation failed');
}
};

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

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

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

try {
const hash = createHash('sha256');
const stream = createReadStream(filePath);

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

stream.on('error', () => {
throw new Error('FS operation failed');
});
} catch (err) {
throw new Error('FS operation failed');
}
};

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

This file was deleted.

38 changes: 38 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import path from 'node:path';
import { release, version } from 'node:os';
import { createServer as createServerHttp } from 'node:http';
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';

import './files/c.cjs';

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

const random = Math.random();

const jsonFilePath = random > 0.5
? path.join(__dirname, 'files', 'a.json')
: path.join(__dirname, 'files', 'b.json');

const unknownData = JSON.parse(await readFile(jsonFilePath, 'utf8'));

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(unknownData);

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

const PORT = 3000;

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

export { unknownData as unknownObject, myServer };
32 changes: 31 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import { createReadStream } from 'node:fs';
import { access } 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 read = async () => {
// Write your code here
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');

try {
await access(filePath);

const stream = createReadStream(filePath, 'utf8');

stream.on('error', () => {
throw new Error('FS operation failed');
});

stream.on('data', chunk => {
process.stdout.write(chunk);
});

stream.on('end', () => {
process.stdout.write('\n');
});

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


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

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

console.log('Type something. Press Ctrl+Enter (on Windows) to finish.');

process.stdin.pipe(transform).pipe(process.stdout);
};

await transform();
Loading