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
19 changes: 17 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
const parseArgs = () => {
// Write your code here
const arr = [];
const args = process.argv.slice(2);
args.forEach((el, idx) => {
if (!(idx % 2)) {
arr.push(el.slice(2));
} else {
arr.push(el);
}
});
const newArr = arr.reduce((result, value, index, array) => {
if (index % 2 === 0) result.push(array.slice(index, index + 2));
return result;
}, []);
newArr.forEach(([key, value]) => {
console.log(`${key} is ${value}`);
});
};

parseArgs();
parseArgs();
11 changes: 8 additions & 3 deletions 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 object = process.env;
for (const key in object) {
if (Object.hasOwnProperty.call(object, key)) {
const element = object[key];
if (key.includes("RSS_")) console.log(key + "=" + element);
}
}
};

parseEnv();
parseEnv();
15 changes: 11 additions & 4 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const spawnChildProcess = async (args) => {
// Write your code here
import { fork } from 'child_process';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const forkChildProcess = async (args) => {
const child = fork(join(dirname(fileURLToPath(import.meta.url)), 'files', 'script.js'));
child.send(args);
child.on('message', (data) => {
process.stdout.write(data);
})
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
forkChildProcess(['someArgument1', 'someArgument2']);
5 changes: 4 additions & 1 deletion src/cp/files/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ console.log(`Arguments: ${JSON.stringify(args)}`);
const echoInput = (chunk) => {
const chunkStringified = chunk.toString();
if (chunkStringified.includes('CLOSE')) process.exit(0);
process.stdout.write(`Received from master process: ${chunk.toString()}\n`)
process.send(`Received from child process: ${chunk.toString()}\n`)
};

process.stdin.on('data', echoInput);
process.on('message' , (data) => {
process.stdout.write(`reseved from master process : ${data}`)
})
11 changes: 7 additions & 4 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const copy = async () => {
// Write your code here
};
import path from 'path';
import { copy, getDirname } from './modules.js';

await copy();
const _dirname = getDirname(import.meta.url);
const srcDir = path.join(_dirname, 'files');
const destDir = path.join(_dirname, 'files_copy');

copy(srcDir, destDir);
9 changes: 5 additions & 4 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const create = async () => {
// Write your code here
};
import {create, getDirname} from './modules.js';

await create();
const _dirname = getDirname(import.meta.url);
const path = `${_dirname}/fresh.txt`;

create(path);
10 changes: 6 additions & 4 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const remove = async () => {
// Write your code here
};
import { getDirname, remove } from './modules.js'
import path from 'path';

await remove();
const _dirname = getDirname(import.meta.url);
const pathToFolder = path.join(_dirname, '/files');

await remove(pathToFolder,'fileToRemove.txt');
9 changes: 5 additions & 4 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const list = async () => {
// Write your code here
};
import path from 'path';
import { getDirname, list } from './modules.js'
const _dirname = getDirname(import.meta.url);
const pathToDir = path.join(_dirname, '/files');

await list();
await list(pathToDir);
86 changes: 86 additions & 0 deletions src/fs/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import path, { join, dirname, parse } from "path";
import { fileURLToPath } from "url";
import {
existsSync,
appendFile,
readdirSync,
copyFile,
mkdir,
renameSync,
unlinkSync,
readFileSync,
} from "fs";
const getDirname = (importMetaUrl) => {
const _filename = fileURLToPath(importMetaUrl);
const _dirname = dirname(_filename);
return _dirname;
};
const create = (path) => {
if (existsSync(path)) {
throw new Error("FS operation failed");
} else {
appendFile(path, "I am fresh and young", (err) => {
if (err) throw err;
});
}
};
const copy = async (src, dest) => {
if (existsSync(src)) {
const files = readdirSync(src, (err, data) => {
if (err) throw err;
return data;
});
mkdir(dest, (err) => {
if (err) throw new Error("FS operation failed");
});
files.map((file) => {
copyFile(path.join(src, file), path.join(dest, file), (err) => {
if (err) throw err;
});
});
} else {
throw new Error("FS operation failed");
}
};
const rename = async (pathToFolder, oldFile, newFile) => {
const pathOldFile = path.join(pathToFolder, oldFile);
if (
existsSync(pathOldFile) &&
!existsSync(path.join(pathToFolder, newFile))
) {
renameSync(pathOldFile, join(pathToFolder, newFile));
} else {
throw new Error("FS operation failed");
}
};
const remove = async (pathToFolder, fileToDelete) => {
const pathWithFile = join(pathToFolder, fileToDelete);
if (existsSync(pathWithFile)) {
unlinkSync(pathWithFile, (err) => {
if (err) throw err;
});
} else {
throw new Error("FS operation failed");
}
};
const list = async (pathToDir) => {
const files = readdirSync(pathToDir);
if (files.length !== 0) {
files.forEach((file) => {
console.log(parse(file).name);
});
} else {
throw new Error("FS operation failed");
}
};
const read = async (pathToFolder, file) => {
const pathToFile = path.join(pathToFolder, file);
if (existsSync(pathToFile)) {
const data = readFileSync(pathToFile, "utf8");
console.log(data);
} else {
throw new Error("FS operation failed");
}
};

export { getDirname, create, copy, rename, remove, list, read };
9 changes: 5 additions & 4 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const read = async () => {
// Write your code here
};
import path from 'path';
import { getDirname, read } from './modules.js'
const _dirname = getDirname(import.meta.url);
const pathToFolder = path.join(_dirname, '/files')

await read();
await read(pathToFolder, 'fileToRead.txt');
9 changes: 5 additions & 4 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const rename = async () => {
// Write your code here
};
import path from "path";
import { rename, getDirname } from "./modules.js";

await rename();
const _dirname = getDirname(import.meta.url);
const pathToFileFolder = path.join(_dirname, "./files");
rename(pathToFileFolder, 'wrongFilename.txt', 'properFilename.md');
14 changes: 10 additions & 4 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const calculateHash = async () => {
// Write your code here
};
import crypto from 'crypto';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { readFileSync } from 'fs';
import { calculateHash } from './modules.js';

await calculateHash();
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
const pathToFile = join(_dirname, './files', 'fileToCalculateHashFor.txt');
const textFromFile = readFileSync(pathToFile, 'utf-8')
await calculateHash(textFromFile);
6 changes: 6 additions & 0 deletions src/hash/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import crypto from 'crypto';

const calculateHash = async (textFromFile) => {
console.log(crypto.createHash('sha256').update(textFromFile).digest('hex'));
};
export { calculateHash };
40 changes: 0 additions & 40 deletions src/modules/cjsToEsm.cjs

This file was deleted.

42 changes: 42 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import path, { dirname } from 'path';
import { release, version } from 'os';
import { createServer } from 'http';
import { createRequire } from 'module';
import * as c from './files/c.js';
import { fileURLToPath } from 'url';

const require = createRequire(import.meta.url);
const a = require('./files/a.json');
const b = require('./files/b.json');
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const random = Math.random();

let unknownObject;
c
if (random > 0.5) {
unknownObject = a
} else {
unknownObject = b
}

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 = createServer((_, 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 };
11 changes: 9 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { createReadStream } from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const pathTofile = join(__dirname, "./files", "fileToRead.txt");

const read = async () => {
// Write your code here
createReadStream(pathTofile).pipe(process.stdout);
};

await read();
await read();
15 changes: 11 additions & 4 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const transform = async () => {
// Write your code here
};
import { Transform } from 'stream'

await transform();
const transformToReversesText = () => {
return new Transform({
transform(chunk, enc, cb) {
const tranformChunck = chunk.toString().split('').reverse().join('');
cb(null, tranformChunck);
}
})
};
const tranformText = transformToReversesText();
process.stdin.pipe(tranformText).pipe(process.stdout);
10 changes: 9 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { createWriteStream } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const pathToFile = join(__dirname, './files', 'fileToWrite.txt');

const write = async () => {
// Write your code here
process.stdin.pipe(createWriteStream(pathToFile));
process.stdin.resume();
};

await write();
Loading