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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
9 changes: 8 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseArgs = () => {
// Write your code here
const argsArr = process.argv.slice(2)
const parsedArgs = argsArr.reduce((acc, item, index) => {
if(item.startsWith('--')) {
return [...acc, `${item.slice(2)} is ${argsArr[index + 1]}`]
}
return acc;
}, []).join(', ')
console.log(parsedArgs)
};

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 parsedArr = Object.keys(process.env).reduce((acc, item) => {
if(item.startsWith('RSS_')) {
return [...acc, `${item}=${process.env[item]}`]
}
return acc;
},[]).join('; ')

console.log(parsedArr)
};

parseEnv();
26 changes: 22 additions & 4 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
const spawnChildProcess = async (args) => {
// Write your code here
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 = (args = []) => {
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('error', (err) => console.error('Child error:', err.message));
child.on('exit', (code) => console.log(`Child exited with code ${code}`));
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['test1', 'test2', 'test3', '444']);


30 changes: 29 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import { getDirInfo } from '../utils/getDirInfo.js';

const { __dirname } = getDirInfo(import.meta.url);

const copy = async () => {
// Write your code here
const sourcePath = path.join(__dirname, 'files');
const destinationPath = path.join(__dirname, 'files_copy');

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

try {
await fs.access(destinationPath);
throw new Error('FS operation failed');
} catch (err) {
if (err.code === 'ENOENT') {
await fs.cp(sourcePath, destinationPath, { recursive: true });
} else {
throw err;
}
}
};

await copy();
22 changes: 20 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import { getDirInfo } from '../utils/getDirInfo.js';

const { __dirname } = getDirInfo(import.meta.url);

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

try {
await fs.access(filePath);
throw new Error('FS operation failed');
} catch (err) {
if (err.code === 'ENOENT') {
await fs.writeFile(filePath, content);
} else {
throw err;
}
}
};

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 path from 'node:path';
import fs from 'node:fs/promises';
import { getDirInfo } from '../utils/getDirInfo.js';

const { __dirname } = getDirInfo(import.meta.url);

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

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

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

const { __dirname } = getDirInfo(import.meta.url);

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

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

try {
const files = await fs.readdir(folderPath);
for (const file of files) {
console.log(file);
}
} catch (err) {
throw err;
}
};

await list();
21 changes: 20 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import { getDirInfo } from '../utils/getDirInfo.js';

const { __dirname } = getDirInfo(import.meta.url);

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

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

try {
const content = await fs.readFile(fileToReadPath, 'utf8');
console.log(content);
} catch (err) {
throw err;
}
};

await read();
27 changes: 26 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import { getDirInfo } from '../utils/getDirInfo.js';

const { __dirname } = getDirInfo(import.meta.url);

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

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

try {
await fs.access(destinationFile);
throw new Error('FS operation failed');
} catch (err) {
if (err.code === 'ENOENT') {
await fs.rename(sourceFile, destinationFile);
} else {
throw err;
}
}

};

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 crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import { getDirInfo } from '../utils/getDirInfo.js';

const { __dirname } = getDirInfo(import.meta.url);

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

return new Promise((resolve, reject) => {
const hash = crypto.createHash('sha256');
const stream = fs.createReadStream(filePath);

stream.on('data', (chunk) => hash.update(chunk));
stream.on('end', () => {
const finalHash = hash.digest('hex');
console.log(finalHash);
resolve(finalHash);
});
stream.on('error', (err) => reject(err));
});
};

await calculateHash();
21 changes: 12 additions & 9 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const path = require('node:path');
const { release, version } = require('node:os');
const { createServer: createServerHttp } = require('node:http');
import path from 'node:path'
import { fileURLToPath } from 'node:url';
import { release, version } from 'node:os'
import { createServer as createServerHttp } from 'node:http'
import a from './files/a.json' with { type: 'json' };
import b from './files/b.json' with { type: 'json' };

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

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

const random = Math.random();

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

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
Expand All @@ -28,7 +34,4 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
unknownObject,
myServer,
};
export default { 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 'node:fs';
import path from 'node:path';
import { getDirInfo } from '../utils/getDirInfo.js';

const { __dirname } = getDirInfo(import.meta.url);

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

const stream = fs.createReadStream(filePath);

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

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

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

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

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

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

const { __dirname } = getDirInfo(import.meta.url);

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

const stream = fs.createWriteStream(filePath);

process.stdin.pipe(stream);
};

await write();
9 changes: 9 additions & 0 deletions src/utils/getDirInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import path from 'node:path';
import { fileURLToPath } from 'node:url';

export const getDirInfo = (metaUrl) => {
const __filename = fileURLToPath(metaUrl);
const __dirname = path.dirname(__filename);
return { __filename, __dirname };
};
37 changes: 36 additions & 1 deletion src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
import os from 'node:os';
import { Worker } from 'node:worker_threads';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const workerPath = path.join(__dirname, 'worker.js');

const performCalculations = async () => {
// Write your code here
const cores = os.cpus().length;
const start = 10;

const tasks = Array.from({ length: cores }, (_, i) => {
const n = start + i;

return new Promise((resolve) => {
const w = new Worker(workerPath, { workerData: n });
let settled = false;

w.once('message', (value) => {
settled = true;
resolve({ status: 'resolved', data: value });
});

w.once('error', () => {
if (!settled) resolve({ status: 'error', data: null });
});

w.once('exit', (code) => {
if (code !== 0 && !settled) resolve({ status: 'error', data: null });
});
});
});

const results = await Promise.all(tasks);
console.log(results);
};

await performCalculations();
Loading