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
11 changes: 9 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//--propName value --prop2Name value2
export const parseArgs = () => {
// Write your code here
};
// Write your code here
process.argv.forEach((arg,index)=>{
if (index%2===0 && index !==0){
console.log(`${arg.substring(2)} is ${process.argv[index+1]}`)
}
})
};
parseArgs();
12 changes: 10 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// set env variable on Windows powershell $env:USER_ID_TEST = ';MyTestResourceGroup2'
export const parseEnv = () => {
// Write your code here
};
// Write your code here
// console.log(process.env)
// console.log(process.env.USER_ID_TEST)
const envVarArray = Object.entries(process.env);
const filteredEnvVar = envVarArray.filter(([key, value]) => key.slice(0,4) === 'RSS_');
filteredEnvVar.forEach(([envvarkey, envvarvalue])=>console.log(`${envvarkey}=${envvarvalue}`))

};
parseEnv();
34 changes: 32 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
import fs from 'fs';
import {fileURLToPath} from "url";
import {dirname} from "path";
import * as path from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export const copy = async () => {
// Write your code here
};
// Write your code here

// Check if folder needs to be created or integrated
let sourceFolder = path.join(__dirname, 'files');
let targetFolder = path.join(__dirname, 'files_copy');

if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
} else throw new Error('FS operation failed');

if (!fs.existsSync(sourceFolder)) {
throw new Error('FS operation failed')
}


// Copy

let files = fs.readdirSync(sourceFolder);
files.forEach(function (file) {
let sourceFilePath = path.join(sourceFolder, file);
let targetFilePath = path.join(targetFolder, file);
fs.writeFileSync(targetFilePath , fs.readFileSync(sourceFilePath));
});
};
copy();
25 changes: 23 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import fs from 'fs';
import {fileURLToPath} from 'url';
import {dirname} from 'path';
import * as path from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export const create = async () => {
// Write your code here
};


fs.access(path.join(__dirname, 'files',"fresh.txt"), fs.constants.F_OK, (err) => {
if (err) {
fs.writeFile (
path.join(__dirname, 'files',"fresh.txt"),
"I am fresh and young" ,
(err) => {
if (err)
throw new Error('FS operation failed')
})
} else {
throw new Error('FS operation failed')
}
});
};
create();
21 changes: 19 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import fs from 'fs';
import {fileURLToPath} from "url";
import {dirname} from "path";
import * as path from "path";

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

export const remove = async () => {
// Write your code here
};
// Write your code here
const filePath = path.join(__dirname, 'files',"fileToRemove.txt");
if (!fs.existsSync(filePath)) {
throw new Error('FS operation failed')
}
fs.unlink(filePath, (err) => {
if (err) throw err;
console.log('path/file.txt was deleted');
});
};
remove();
20 changes: 18 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
import fs from 'fs';
import {fileURLToPath} from "url";
import {dirname} from "path";
import * as path from "path";

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

export const list = async () => {
// Write your code here
};
let sourceFolder = path.join(__dirname, 'files');
// Write your code here
if (!fs.existsSync(sourceFolder)) {
throw new Error('FS operation failed')
}

let files = fs.readdirSync(sourceFolder);
console.log(files)
};
list();
24 changes: 22 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
import fs from 'fs';
import {fileURLToPath} from "url";
import {dirname} from "path";
import * as path from "path";

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

export const read = async () => {
// Write your code here
};
// Write your code here
let sourceFolder = path.join(__dirname,'files',"fileToRead.txt");
if (!fs.existsSync(sourceFolder)) {
throw new Error('FS operation failed')
}

fs.readFile(sourceFolder, 'utf8', (err, data) => {
if (err) throw err;
console.log (data)
} )


};
read();
22 changes: 20 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
import fs from 'fs';
import {fileURLToPath} from "url";
import {dirname} from "path";
import * as path from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export const rename = async () => {
// Write your code here
};
const oldPath = path.join(__dirname, 'files',"wrongFilename.txt");
const newPath = path.join(__dirname, 'files',"wrongFilename.txt");
// Write your code here
if (!fs.existsSync(oldPath) || fs.existsSync(newPath) ) {
throw new Error('FS operation failed')
}

fs.rename(oldPath, newPath, function(err) {
if ( err ) console.log('ERROR: ' + err);
});

};
rename();
24 changes: 22 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
import fs from 'fs/promises';
import {fileURLToPath} from "url";
import path, {dirname} from "path";

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

export const calculateHash = async () => {
// Write your code here
};
const {
createHash
} = await import('crypto');
let sourceFolder = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');
const calchash = async () => {
const hash = createHash('sha256');
const data = await fs.readFile(sourceFolder, "utf8");
hash.update(Buffer.from(data));
return hash.copy().digest('hex')
}
console.log(await calchash());
return await calchash();

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

This file was deleted.

38 changes: 38 additions & 0 deletions src/modules/cjsToEsm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import path, {dirname} from 'path';
import {release, version} from 'os'
import { createServer as createServerHttp } from 'http'
import './files/c.js';
import {fileURLToPath} from "url";
const random = Math.random();


export let unknownObject;

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

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.sep}"`);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
console.log(`Path to current file is ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

export const createMyServer = createServerHttp((_, res) => {
res.end('Request accepted');
});


13 changes: 11 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import fs from 'fs';

import {fileURLToPath} from "url";
import path, {dirname} from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export const read = async () => {
// Write your code here
};
// Write your code here
const stream = fs.createReadStream(path.join(__dirname, 'files', 'fileToRead.txt'));
stream.pipe(process.stdout)
};
read();
17 changes: 15 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import {Transform} from 'stream'

export const transform = async () => {
// Write your code here
};
// Write your code here
let fileStream = process.stdin;
const transformedData= process.stdout;

const reverse = new Transform({
transform(chunk, encoding, callback) {
callback(null, chunk.reverse());
},
});

fileStream.pipe(reverse).pipe(transformedData);
};
transform();
16 changes: 14 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// Enter any texts in console( User input)
import fs from 'fs';

import {fileURLToPath} from "url";
import path, {dirname} from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export const write = async () => {
// Write your code here
};
// Write your code here
let writebleProcess = process.stdin;
let writeStream = fs.createWriteStream(path.join(__dirname, 'files', 'fileToWrite.txt'))
writebleProcess.pipe(writeStream)
};
write();
33 changes: 32 additions & 1 deletion src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
import os from 'os';
import {fileURLToPath} from "url";
import {Worker, isMainThread, workerData, BroadcastChannel} from 'worker_threads';
import {nthFibonacci} from "./worker.js";

const __filename = fileURLToPath(import.meta.url);
const bc = new BroadcastChannel('channelFibonacci')

export const performCalculations = async () => {
// Write your code here
};
const systemCpuCores = os.cpus().length;
let resultArray = [];
if (isMainThread) {

for (let n = 10; n < 10 + systemCpuCores; n++) {
console.log(n)
const n = 10;
new Worker(__filename, {workerData: {n}});
}

bc.onmessage = (msg) => {
resultArray.push({status: 'resolved?', data: msg})
}

} else {
let {n} = workerData

let calcRes = nthFibonacci(n);
bc.postMessage(calcRes)
}

console.log(resultArray)
};
performCalculations();
Loading