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;

const result = [];

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

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

parseArgs();
6 changes: 5 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const parseEnv = () => {
// Write your code here
console.log(
Object.entries(process.env)
.filter(([key]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`).join('; ')
)
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const scriptPath = join('files', 'script.js');

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

child.on('close', (code) => {
console.log(`Child process exited with code ${code}`);
});
child.on('error', (err) => {
console.error('Failed to start child process:', err);
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['arg1', 'arg2']);
23 changes: 22 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { join } from 'node:path';
import { stat, cp } from 'node:fs/promises';

const copy = async () => {
// Write your code here
const folderExists = async (path) => {
return stat(path)
.then(stats => stats.isDirectory())
.catch(() => false);
};

const src = join('files');
const dest = join('files_copy');

const srcExists = await folderExists(src);
const destExists = await folderExists(dest);

if (!srcExists || destExists) {
throw new Error('FS operation failed');
}

await cp(src, dest, { recursive: true }).catch(() => {
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 { join } from 'node:path';
import { stat, writeFile } from 'node:fs/promises';

const create = async () => {
// Write your code here
const fileExists = async (path) => {
return stat(path)
.then(() => true)
.catch(() => false);
};

const filePath = join('files', 'fresh.txt');
const text = 'I am fresh and young';

const exists = await fileExists(filePath);

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

await writeFile(filePath, text, 'utf8');
};

await create();
22 changes: 21 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import { join } from 'node:path';
import { stat, rm } from 'node:fs/promises';

const remove = async () => {
// Write your code here
const fileExists = async (path) => {
return stat(path)
.then(stats => stats.isFile())
.catch(() => false);
};

const folder = 'files';
const path = join(folder, 'fileToRemove.txt');

const isFileExists = await fileExists(path);

if (!isFileExists) {
throw new Error('FS operation failed');
}

await rm(path).catch(() => {
throw new Error('FS operation failed');
});
};

await remove();
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 { join } from 'node:path';
import { stat, readdir } from 'node:fs/promises';

const list = async () => {
// Write your code here
const folderExists = async (path) => {
return stat(path)
.then(stats => stats.isDirectory())
.catch(() => false);
};

const folder = join('files');

const isFolderExists = await folderExists(folder);

if (!isFolderExists) {
throw new Error('FS operation failed');
}

const files = await readdir(folder).catch(() => {
throw new Error('FS operation failed');
});
console.log(files);
};

await list();
23 changes: 22 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { join } from 'node:path';
import { stat, readFile } from 'node:fs/promises';

const read = async () => {
// Write your code here
const fileExists = async (path) => {
return stat(path)
.then(stats => stats.isFile())
.catch(() => false);
};

const folder = 'files';
const path = join(folder, 'fileToRead.txt');

const isFileExists = await fileExists(path);

if (!isFileExists) {
throw new Error('FS operation failed');
}

const file = await readFile(path, 'utf-8').catch(() => {
throw new Error('FS operation failed');
});
console.log(file);
};

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 { join } from 'node:path';
import { stat, rename as fsRename } from 'node:fs/promises';

const rename = async () => {
// Write your code here
const fileExists = async (path) => {
return stat(path)
.then(stats => stats.isFile())
.catch(() => false);
};

const folder = 'files';
const oldPath = join(folder, 'wrongFilename.txt');
const newPath = join(folder, 'properFilename.md');

const isOldFileExists = await fileExists(oldPath);
const isNewFileExists = await fileExists(newPath);

if (!isOldFileExists || isNewFileExists) {
throw new Error('FS operation failed');
}

await fsRename(oldPath, newPath).catch(() => {
throw new Error('FS operation failed');
});
};

await rename();
31 changes: 30 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import { createHash } from 'node:crypto';
import { join } from 'node:path';
import { createReadStream } from 'node:fs';
import { stat } from 'node:fs/promises';


const calculateHash = async () => {
// Write your code here
const fileExists = async (path) => {
return stat(path)
.then(() => true)
.catch(() => false);
};

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

const exists = await fileExists(filePath);

if (!exists) {
throw new Error('FS operation failed');
}

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

await new Promise((resolve, reject) => {
stream.on('data', chunk => hash.update(chunk));
stream.on('end', resolve);
stream.on('error', reject);
});

console.log(hash.digest('hex'));
};

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

This file was deleted.

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

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

const random = Math.random();

const dataA = await import('./files/a.json', { with: { type: 'json' } });
const dataB = await import('./files/b.json', { with: { type: 'json' } });
const unknownObject = random > 0.5 ? dataA : dataB;

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${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,
};
9 changes: 8 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { createReadStream } from 'node:fs';
import { join } from 'node:path';

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

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

readableStream.pipe(process.stdout);
};

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

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

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

await transform();
9 changes: 8 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { createWriteStream } from 'node:fs';
import { join } from 'node:path';

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

const writableStream = createWriteStream(filePath, { encoding: 'utf-8' });

process.stdin.pipe(writableStream);
};

await write();
Loading