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 result = [];

for (let i = 0; i < args.length; i += 2) {
if (args[i].startsWith('--')) {
const propName = args[i].substring(2);
const value = args[i + 1];
result.push(`${propName} is ${value}`);
}
}

console.log(result.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_'))
.map(([key, value]) => `${key}=${value}`)
.join('; ');

console.log(rssVars);
};

parseEnv();
28 changes: 26 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import { spawn } from 'child_process';
import path from 'path';
import { fileURLToPath } from '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 childProcess = spawn('node', [scriptPath, ...args], {
stdio: ['pipe', 'pipe', 'inherit']
});

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

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

childProcess.on('exit', (code) => {
if (code !== 0) {
console.error(`Child process exited with code ${code}`);
}
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['arg1', 'arg2', 'arg3']);
25 changes: 24 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const copy = async () => {
// Write your code here
const srcFilePath = path.join(__dirname, 'files');
const destFilePath = path.join(__dirname, 'files_copy');

try {
await fs.promises.stat(srcFilePath);
await fs.promises.stat(destFilePath);
throw new Error('FS operation failed');
} catch (err) {
if (err.code !== 'ENOENT') {
throw new Error('FS operation failed');
}
}

try {
await fs.promises.cp(srcFilePath, destFilePath, { recursive: true });
} catch (err) {
throw new Error('FS operation failed');
}
};

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

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

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

try {
await fs.promises.writeFile(filePath, 'I am fresh and young', { flag: 'wx' });
} catch (err) {
throw new Error('FS operation failed');
}
};

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';
import path from 'path';
import { fileURLToPath } from '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.promises.unlink(filePath);
} catch (err) {
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 fs from 'fs';
import path from 'path';
import { fileURLToPath } from '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 {
const files = await fs.promises.readdir(dirPath);
console.log(files);
} catch (err) {
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 fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const read = async () => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');

try {
const data = await fs.readFile(filePath, 'utf8');
console.log(data);
} catch (err) {
throw new Error('FS operation failed');
}
};

await read();
31 changes: 30 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const rename = async () => {
// Write your code here
const oldPath = path.join(__dirname, 'files', 'wrongFilename.txt');
const newPath = path.join(__dirname, 'files', 'properFilename.md');

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

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

try {
await fs.promises.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 fs from 'fs';
import crypto from 'crypto';
import path from 'path';
import { fileURLToPath } from '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');
const hash = crypto.createHash('sha256');
const readStream = fs.createReadStream(filePath);

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

readStream.on('end', () => {
console.log(hash.digest('hex'));
});

readStream.on('error', (err) => {
throw new Error('Hash calculation failed');
});
};

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

This file was deleted.

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

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

await import('./files/c.cjs');

const random = Math.random();

let unknownObject;
if (random > 0.5) {
const data = await readFile(new URL('./files/a.json', import.meta.url));
unknownObject = JSON.parse(data);
} else {
const data = await readFile(new URL('./files/b.json', import.meta.url));
unknownObject = JSON.parse(data);
}

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

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 { unknownObject, myServer };
16 changes: 15 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from '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');
const readStream = fs.createReadStream(filePath, 'utf-8');

readStream.pipe(process.stdout);

readStream.on('error', (err) => {
throw new Error('Read operation failed');
});
};

await read();
13 changes: 9 additions & 4 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const transform = async () => {
// Write your code here
};
import { Transform } from 'stream';

await transform();
const reverseTransform = new Transform({
transform(chunk, encoding, callback) {
const reversed = chunk.toString().split('').reverse().join('');
callback(null, reversed);
}
});

process.stdin.pipe(reverseTransform).pipe(process.stdout);
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 fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const write = async () => {
// Write your code here
const filePath = path.join(__dirname, 'files', 'fileToWrite.txt');
const writeStream = fs.createWriteStream(filePath);

process.stdin.pipe(writeStream);

writeStream.on('error', (err) => {
throw new Error('Write operation failed');
});
};

await write();
Loading