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
7 changes: 6 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { argv, stdout } from 'process';

const parseArgs = () => {
// Write your code here
const args = argv.slice(2);
const toString = args.reduce((acc, arg, i) => i % 2 === 0 ? acc.concat(arg.slice(2), ' is ') : acc.concat(arg, '\n'), '');

stdout.write(toString);
};

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 @@
import { env, stdout } from 'process';

const parseEnv = () => {
// Write your code here
for (const key in env) {
if (key.includes('RSS_')) stdout.write(`${key}=${env[key]}`);
}
};

parseEnv();
25 changes: 22 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { spawn } from 'child_process';
import path from 'path';

const __dirname = path.resolve();
const pathToScriptFile = path.join(__dirname, 'files', 'script.js');

const spawnChildProcess = async (args) => {
// Write your code here
const childProcess = spawn('node ' + pathToScriptFile, args, {shell: true});

process.stdin.on('data', data => {
childProcess.stdin.write(data);
});

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

childProcess.on('close', () => {
process.exit();
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
const mockArgs = ['Foo_0', 'Foo_1', 'Foo_2'];
spawnChildProcess(mockArgs);

28 changes: 27 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import { promises, existsSync } from 'fs';
import path from 'path';
import m from './messages.js';

const __dirname = path.resolve();
const currentFolderPath = path.join(__dirname, 'files');
const copiedFolderPath = path.join(__dirname, 'files_copy');

const copy = async () => {
// Write your code here
if (!existsSync(currentFolderPath) || existsSync(copiedFolderPath)) throw new Error(m.ERR);

try {
const dir = await promises.readdir(currentFolderPath, { withFileTypes: true });
await promises.mkdir(copiedFolderPath, { recursive: true });

for (const file of dir) {
if (file.isFile()) {
const copeeFilePath = path.join(currentFolderPath, file.name);
const newFilePath = path.join(copiedFolderPath, file.name);

const data = await promises.readFile(copeeFilePath);
await promises.writeFile(newFilePath, data)
}
}

} catch (err) {
console.log(`${err}`);
}
};

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 { promises, existsSync } from 'fs';
import path from 'path';
import m from './messages.js';

const __dirname = path.resolve();
const newFilePath = path.join(__dirname, 'files', 'fresh.txt');

const create = async () => {
// Write your code here
if (existsSync(newFilePath)) throw new Error(m.ERR);

try {
await promises.writeFile(newFilePath, m.NEW_FILE_CONTENT);
} catch (err) {
console.log(`${err}`);
}
};

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 { promises, existsSync } from 'fs';
import path from 'path';
import m from './messages.js';

const __dirname = path.resolve();
const removeePath = path.join(__dirname, 'files', 'fileToRemove.txt');

const remove = async () => {
// Write your code here
if (!existsSync(removeePath)) throw new Error(m.ERR);

try {
await promises.rm(removeePath);
} catch (err) {
console.log(`${err}`);
}
};

await remove();
1 change: 0 additions & 1 deletion src/fs/files/fileToRemove.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
How dare you!
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, existsSync } from 'fs';
import path from 'path';
import m from './messages.js';

const __dirname = path.resolve();
const readeePath = path.join(__dirname, 'files');

const list = async () => {
// Write your code here
if (!existsSync(readeePath)) throw new Error(m.ERR);

try {
const dir = await promises.readdir(readeePath, { withFileTypes: true });

for (const file of dir) {
if (file.isFile()) {
console.log(file.name);
}
}

} catch (err) {
console.log(`${err}`);
}
};

await list();
4 changes: 4 additions & 0 deletions src/fs/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
ERR: 'FS operation failed',
NEW_FILE_CONTENT: 'I am fresh and young',
}
24 changes: 23 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { promises, existsSync } from 'fs';
import path from 'path';
import m from './messages.js';

const __dirname = path.resolve();
const readeePath = path.join(__dirname, 'files');

const read = async () => {
// Write your code here
if (!existsSync(readeePath)) throw new Error(m.ERR);

try {
const dir = await promises.readdir(readeePath, { withFileTypes: true });

for (const file of dir) {
if (file.isFile()) {
const filePath = path.join(readeePath, file.name);
const data = await promises.readFile(filePath, 'utf-8');
console.log(data);
}
}

} catch (err) {
console.log(`${err}`);
}
};

await read();
16 changes: 15 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { promises, existsSync } from 'fs';
import path from 'path';
import m from './messages.js';

const __dirname = path.resolve();
const renameePath = path.join(__dirname, 'files', 'wrongFilename.txt');
const renamePath = path.join(__dirname, 'files', 'properFilename.md');

const rename = async () => {
// Write your code here
if (!existsSync(renameePath) || existsSync(renamePath)) throw new Error(m.ERR);

try {
await promises.rename(renameePath, renamePath);
} catch (err) {
console.log(`${err}`);
}
};

await rename();
14 changes: 13 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { createHash } from 'crypto';
import { readFile } from 'fs';
import { stdout } from 'process';
import path from 'path';

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

const calculateHash = async () => {
// Write your code here
await readFile(filePath, (err, file) => {
if (err) throw err;
const hexed = createHash('sha256').update(file).digest('hex');
stdout.write(`the hex of sha256 of fileToCalculateHashFor.txt is ${hexed}`)
});
};

await calculateHash();
10 changes: 5 additions & 5 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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: createServerHttp } from 'http';
import './files/c';

const random = Math.random();

Expand Down Expand Up @@ -33,7 +33,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
export {
unknownObject,
myServer,
};
Expand Down
1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zxcv
10 changes: 9 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { createReadStream } from 'fs';
import { stdout } from 'process';
import path from 'path';

const __dirname = path.resolve();
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');

const read = async () => {
// Write your code here
const readStream = createReadStream(filePath);
readStream.on('data', chunk => stdout.write(chunk));
};

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

const transform = async () => {
// Write your code here
const transformer = new Transform({
transform(chunk, _encoding, callback) {
const data = String(chunk);
const reversed = data.split('').reverse().join('').concat('\n');
callback(null, reversed);
}
});

const input = stdin;
const output = stdout;

pipeline(input, transformer, output, err => stdout.write(String(err)));
};

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

const __dirname = path.resolve();
const filePath = path.join(__dirname, 'files', 'fileToWrite.txt');

const write = async () => {
// Write your code here
const writeStream = createWriteStream(filePath);

stdin.on('data', data => {
writeStream.write(data.toString());
});
};

await write();
39 changes: 36 additions & 3 deletions src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
const performCalculations = async () => {
// Write your code here
import { cpus } from 'os';
import { Worker } from 'worker_threads';
import path from 'path';
const __dirname = path.resolve();
const pathToFile = path.join(__dirname, 'worker.js');

export const performCalculations = async () => {
const workersResults = [];

const createWokers = (num) => new Promise((resolve, reject) => {
const worker = new Worker(pathToFile, { workerData: 10 + num });

worker.on('message', value => {
resolve(workersResults.push({status: 'resolved', data: value}));
});

worker.on('error', _err => {
reject(workersResults.push({status: 'error', data: null}));
});

worker.on('exit', (exitCode) => {
if (exitCode === 0) {
return null;
}

return reject(new Error(`Worker has stopped with code ${exitCode}`));
});
})

const cores = cpus();
for (let i = 0; i < cores.length; i++) {
await createWokers(i);
}

console.log(workersResults);
};

await performCalculations();
await performCalculations();
6 changes: 4 additions & 2 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// n should be received from main thread
import { workerData, parentPort } from 'worker_threads';

const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2);

const sendResult = () => {
// This function sends result of nthFibonacci computations to main thread
const data = nthFibonacci(workerData);
parentPort.postMessage(data);
};

sendResult();
15 changes: 14 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { createReadStream, createWriteStream } from 'fs';
import { createGzip } from 'zlib';
import { pipeline } from 'stream/promises';
import path from 'path';

const __dirname = path.resolve();
const source = path.join(__dirname, 'files', 'fileToCompress.txt');
const destination = path.join(__dirname, 'files', 'archive.gz');

const compress = async () => {
// Write your code here
const gzip = createGzip();
const input = createReadStream(source);
const output = createWriteStream(destination);

await pipeline(input, gzip, output).catch(err => process.stdout.write(String(err)));
};

await compress();
Loading