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
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 variables = process.argv.slice(2);

let res = [];
for (let i = 0; i < variables.length; i += 2) {
res.push(`${variables[i].slice(2)} is ${variables[i+1]}`)
}

console.log(res.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 rssVariables = Object.entries(process.env)
.filter(([key]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`)
.join('; ');

console.log(rssVariables);
};

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

const filePath = path.join(import.meta.dirname, 'files', 'script.js');

const spawnChildProcess = async (args) => {
// Write your code here
const child = spawn('node', [filePath, ...args]);

process.stdin.pipe(child.stdin);

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

child.stderr.on('data', (error) => {
process.stderr.write(`Error from child process: ${error}`);
});

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

};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["someArgument1", 'someArgument2']);
41 changes: 40 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
import fs from 'fs/promises';
import path from 'path';

const textError = 'FS operation failed';
const dirPathSrc = path.join(process.cwd(), 'src', 'fs', 'files');
const dirPathDest = path.join(process.cwd(), 'src', 'fs', 'files_copy');

const copyFiles = async (src, dest) => {
try {
const files = await fs.readdir(src);

await fs.mkdir(dest);

for (const file of files) {
const srcPath = path.join(src, file);
const destPath = path.join(dest, file);

await fs.copyFile(srcPath, destPath);
}
} catch (err) {
throw new Error();
}
};

const copy = async () => {
// Write your code here
try {
await fs.access(dirPathSrc, fs.constants.F_OK);

try {
await fs.access(dirPathDest, fs.constants.F_OK);
console.error(textError);
return;

} catch (err) {}

await copyFiles(dirPathSrc, dirPathDest);

} catch (err) {
console.error(textError);
}

};

await copy();
19 changes: 18 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import fs from 'fs/promises';
import path from 'path';

const create = async () => {
// Write your code here
const text = 'I am fresh and young';
const textError = 'FS operation failed';

const dirPath = path.join(process.cwd(), 'src', 'fs', 'files');
const filePath = path.join(dirPath, 'fresh.txt');

try {
const stats = await fs.stat(filePath);
if (stats) {
console.error(textError)
}

} catch (e) {
await fs.writeFile(filePath, text);
}
};

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/promises';
import path from 'path';

const textError = 'FS operation failed';
const dirPathSrc = path.join(process.cwd(), 'src', 'fs', 'files', 'fileToRemove.txt');

const remove = async () => {
// Write your code here
try {
await fs.access(dirPathSrc);

await fs.rm(dirPathSrc)

} catch (e) {
console.error(textError);
}
};

await remove();
17 changes: 16 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from 'fs/promises';
import path from 'path';

const textError = 'FS operation failed';
const dirPathSrc = path.join(process.cwd(), 'src', 'fs', 'files');

const list = async () => {
// Write your code here
try {
await fs.access(dirPathSrc);

const files = await fs.readdir(dirPathSrc);

console.log('Array of filenames: ', files)

} catch (e) {
console.error(textError);
}
};

await list();
13 changes: 12 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'fs/promises';
import path from 'path';

const read = async () => {
// Write your code here
const filePath = path.join(process.cwd(), 'src', 'fs', 'files', 'fileToRead.txt');
const textError = 'FS operation failed';

try {
const data = await fs.readFile(filePath, 'utf-8');
console.log(data);
} catch (err) {
console.error(textError);
}
};

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 fs from 'fs/promises';
import path from 'path';

const textError = 'FS operation failed';
const dirPath = path.join(process.cwd(), 'src', 'fs', 'files');
const dirPathSrc = path.join(dirPath, 'wrongFilename.txt');
const dirPathDest = path.join(dirPath, 'properFilename.md');

const rename = async () => {
// Write your code here
try {
await fs.access(dirPathSrc, fs.constants.F_OK);

try {
await fs.access(dirPathDest, fs.constants.F_OK);
console.error(textError);
return;

} catch (err) {}

await fs.rename(dirPathSrc, dirPathDest)

} catch (err) {
console.error(textError);
}
};

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

const calculateHash = async () => {
// Write your code here
const filePath = path.join(import.meta.dirname, 'files', 'fileToCalculateHashFor.txt');
const fileStream = createReadStream(filePath);

const hash = createHash('sha256');

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

fileStream.on('end', () => {
const resultHash = hash.digest('hex');
console.log(`Hash: ${resultHash}`);
});

};

await calculateHash();
22 changes: 12 additions & 10 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
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 as createServerHttp } from 'http';
import './files/c.js';
import a from './files/a.json' with { type: 'json' };
import b from './files/b.json' with { type: 'json' };

const __filename = import.meta.filename;
const __dirname = import.meta.dirname;

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = require('./files/a.json');
unknownObject = a;
} else {
unknownObject = require('./files/b.json');
unknownObject = b;
}

console.log(`Release ${release()}`);
Expand All @@ -33,8 +38,5 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
unknownObject,
myServer,
};
export { unknownObject, myServer };

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 path from 'path'
import { createReadStream } from 'fs'

const read = async () => {
// Write your code here
const filePath = path.join(import.meta.dirname, 'files', 'fileToRead.txt');
const fileStream = createReadStream(filePath, { encoding: 'utf-8' });

fileStream.on('data', (chunk) => {
process.stdout.write(chunk + '\n');
});
};

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

const transform = async () => {
// Write your code here
const streamTransform = new Transform({
transform(chunk, encoding, callback) {

const reversedChunk = chunk.toString().split('').reverse().join('');
callback(null, reversedChunk);
}
});

pipeline(
process.stdin,
streamTransform,
process.stdout,
(err) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Completed successfully.');
}
}
);
};

await transform();
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 path from 'path'
import { createWriteStream } from 'fs'

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

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

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

process.stdin.on('end', () => {
writableStream.end();
});

};

await write();
36 changes: 35 additions & 1 deletion src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
import { Worker } from 'worker_threads';
import os from 'os';
import path from 'path';

const createWorker = async (number, filePath) => {
return new Promise((resolve) => {
const worker = new Worker(filePath)

worker.postMessage(number);
worker.once('message', (msg) => {
resolve({ status: 'resolved', data: msg })
})
worker.once('error', () => {
resolve({ status: 'error', data: null})
})
worker.once('exit', (code) => {
if (code !== 0) {
resolve({ status: 'error', data: null });
}
});
})
}

const performCalculations = async () => {
// Write your code here
const numberCores = os.cpus().length;
const filePath = path.join(import.meta.dirname, 'worker.js');

const workers = [];

for (let i = 0; i < numberCores; i++) {
workers.push(createWorker(10 + i, filePath));
}

const results = await Promise.all(workers)
console.log('results => ', results)

};

await performCalculations();
Loading