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: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
const parseArgs = () => {
let lines = process.argv.slice(2).reduce((lines, arg, index, array)=>{
const nextValue = array[index+1]
if(arg.split("--").length>1&&nextValue){
lines.push(`${arg.slice(2)} is ${nextValue}`);
}
return lines;
}, [])
console.log(lines.join(", "))
// Write your code here
};

Expand Down
9 changes: 9 additions & 0 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
const parseEnv = () => {
let variables_array=[];
let index=1;
for(let variable in process.env){
if(variable.split("RSS_").length>1){
variables_array.push(`${variable}=${process.env[variable]}`);
index++;
}
}
console.log(variables_array.reverse().join("; "))
// Write your code here
};

Expand Down
6 changes: 6 additions & 0 deletions src/cli/pathGenerator/pathGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url)).split("pathGenerator")[0];

export default __dirname;
9 changes: 6 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const spawnChildProcess = async (args) => {
// Write your code here
import { fork } from "child_process"
import __dirname from './pathGenerator/pathGenerator.js';

export const spawnChildProcess = async (args) => {
fork(`${__dirname}files/script.js`, [...args.split(' ')]);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess("--some-arg value1 --other 1337 --arg2 42");
6 changes: 6 additions & 0 deletions src/cp/pathGenerator/pathGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url)).split("pathGenerator")[0];

export default __dirname;
23 changes: 21 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { mkdir, readdir, copyFile, rmdir } from 'fs/promises';
import __dirname from './pathGenerator/pathGenerator.js';

const copy = async () => {
// Write your code here
await mkdir(`${__dirname}files_copy`, async () => {
//console.log("success")
})
.then(async ()=>{
const allFiles = await readdir(`${__dirname}files`);
await Promise.all(allFiles.map( file => copyFile(`${__dirname}files/${file}`, `${__dirname}files_copy/${file}`)))
})
.catch(async (err) => {
if(err.code === 'ENOENT'){
await rmdir(`${__dirname}files_copy`);
throw new Error('FS operation failed');
}
else if (err.code === 'EEXIST'){
throw new Error('FS operation failed');
}
throw err;
})
};

await copy();
await copy();
12 changes: 11 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { writeFile } from 'fs/promises';
import __dirname from './pathGenerator/pathGenerator.js';

const create = async () => {
// Write your code here
await writeFile(`${__dirname}files/fresh.txt`, 'I am fresh and young', { flag: "wx" }, () => {
//console.log("success")
}).catch(err => {
if (err.code === 'EEXIST') {
throw new Error('FS operation failed');
}
throw err;
})
};

await create();
12 changes: 11 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { unlink } from 'fs/promises';
import __dirname from './pathGenerator/pathGenerator.js';

const remove = async () => {
// Write your code here
await unlink(`${__dirname}files/fileToRemove.txt`, () => {
//console.log("success")
}).catch(err => {
if (err.code === 'ENOENT') {
throw new Error('FS operation failed');
}
throw err;
})
};

await remove();
14 changes: 13 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { readdir } from 'fs/promises';
import __dirname from './pathGenerator/pathGenerator.js';

const list = async () => {
// Write your code here
const allFiles = await readdir(`${__dirname}files`, async () => {})
.catch(err => {
if (err.code === 'ENOENT') {
throw new Error('FS operation failed');
}
throw err;
})
for(let file of allFiles){
console.log(file);
}
};

await list();
6 changes: 6 additions & 0 deletions src/fs/pathGenerator/pathGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url)).split("pathGenerator")[0];

export default __dirname;
12 changes: 11 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { readFile } from 'fs/promises';
import __dirname from './pathGenerator/pathGenerator.js';

const read = async () => {
// Write your code here
await readFile(`${__dirname}files/fileToRead.txt`, "utf-8")
.then(data=>{
console.log(data)
}).catch(err=>{
if (err.code === 'ENOENT') {
throw new Error('FS operation failed');
}
})
};

await read();
28 changes: 26 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import { rename as setName, access, constants } from 'fs/promises';
import __dirname from './pathGenerator/pathGenerator.js';

const rename = async () => {
// Write your code here
};
let destUrl = `${__dirname}files/properFilename.md`;
let err_status = 0;
await access(destUrl, constants.F_OK, (err) => {
//console.log("success")
}).then(async ()=>{
err_status = 1;
throw new Error();
}).catch(async (err)=>{
if(err_status){
throw new Error("FS operation failed");
}
else{
await setName(`${__dirname}files/wrongFilename.txt`, destUrl, () => {
//console.log("success")
}).catch(err => {
if (err.code === 'ENOENT') {
throw new Error('FS operation failed');;
}
throw 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 { readFile } from 'fs/promises';
import { createHash } from 'crypto';
import __dirname from './pathGenerator/pathGenerator.js';

const calculateHash = async () => {
// Write your code here
await readFile(`${__dirname}files/fileToCalculateHashFor.txt`, () => {
//console.log(success)
})
.then(fileData => {
console.log(createHash('sha256').update(fileData).digest('hex'));
})
.catch(err => {
throw err;
})
};

await calculateHash();
6 changes: 6 additions & 0 deletions src/hash/pathGenerator/pathGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url)).split("pathGenerator")[0];

export default __dirname;
46 changes: 46 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import path from 'path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import './files/c.js';

import { dirname } from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);


const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = await import("./files/a.json", { assert: { type: "json" } });
} else {
unknownObject = await import("./files/a.json", { assert: { type: "json" } });
}

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,
};

3 changes: 3 additions & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
123
123
444444
6 changes: 6 additions & 0 deletions src/streams/pathGenerator/pathGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url)).split("pathGenerator")[0];

export default __dirname;
6 changes: 5 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { createReadStream } from 'fs';
import __dirname from './pathGenerator/pathGenerator.js';

const read = async () => {
// Write your code here
const newReadStream = createReadStream(`${__dirname}files/fileToRead.txt`)
newReadStream.pipe(process.stdout);
};

await read();
32 changes: 30 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import { pipeline, Transform } from 'stream';
import __dirname from './pathGenerator/pathGenerator.js';

const reverse = (str) => {
if (str === "") return "";
else return reverse(str.substr(1)) + str.charAt(0);
}

const transform = async () => {
// Write your code here
const transformedData = new Transform({
transform(chunk, encoding, callback){
try{
const chunkString = chunk.toString().trim();
const reversedChunk = reverse(chunkString);
callback(null, `${reversedChunk}\n`)
}
catch{
callback(err)
}
}
})
pipeline(
process.stdin,
transformedData,
process.stdout,
err => {
throw err;
}
)
};

await transform();
await transform();

6 changes: 5 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { createWriteStream } from 'fs';
import __dirname from './pathGenerator/pathGenerator.js';

const write = async () => {
// Write your code here
const newWriteStream = createWriteStream(`${__dirname}files/fileToWrite.txt`)
process.stdin.pipe(newWriteStream);
};

await write();
28 changes: 27 additions & 1 deletion src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@

import { Worker } from "worker_threads";
import { cpus } from "os";
import __dirname from './pathGenerator/pathGenerator.js';


const performCalculations = async () => {
// Write your code here
const procs = cpus();
//console.log(procs);

const results = await Promise.allSettled(procs.map((proc, index)=>{
//console.log(index, "index")
return new Promise((res, rej)=> {
const worker = new Worker(`${__dirname}worker.js`, {
workerData: index+10
})
worker.on("message", msg => res(msg))
worker.on("error", err => rej(err))
})
}))

const output = results.map(({status, value}) =>(
{
status : status==="fulfilled" ? "resolved" : "error",
data: status==="fulfilled" ? value : null
}
))
console.log(output)
};

await performCalculations();
Loading