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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
node_modules/
Empty file added output.txt
Empty file.
14 changes: 12 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const parseArgs = () => {
// Write your code here
const outputArray = [];

for (let index = 2; index < process.argv.length; index += 2) {
const key = process.argv[index].slice(2);
const value = process.argv[index + 1];

outputArray.push(`${key} is ${value}`);
}

const outputSeparator = ', ';
console.log(outputArray.join(outputSeparator))
};

parseArgs();
parseArgs();
18 changes: 15 additions & 3 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const parseEnv = () => {
// Write your code here
};
const prefix = 'RSS_'

parseEnv();
const allEnvVariables = process.env
const suitableVariables = []

for (const [key, value] of Object.entries(allEnvVariables)) {
if (key.startsWith(prefix)) {
suitableVariables.push(`${key}=${value}`)
}
}

const outputSeparator = '; '
console.log(suitableVariables.join(outputSeparator));
}

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

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


const spawnChildProcess = async (args) => {
// Write your code here
const childProcessCodePath = path.resolve(__dirname, './files/script.js');
const childProcess = spawn('node', [childProcessCodePath, ...args]);

process.stdin.pipe(childProcess.stdin);
childProcess.stdout.pipe(process.stdout);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess( ['arg1', 'arg2']);
24 changes: 22 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const copy = async () => {
// Write your code here
};
const sourcePath = path.resolve(__dirname, './files');
const destinationPath = path.resolve(__dirname, './files_copy');

if (!fs.existsSync(sourcePath) || fs.existsSync(destinationPath)) {
throw new Error('FS operation failed');
}

await new Promise(resolve => {
fs.mkdir(destinationPath, {}, resolve);
});

await new Promise(resolve => {
fs.cp(sourcePath, destinationPath, { recursive: true}, resolve);
});
}

await copy();
18 changes: 16 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const create = async () => {
// Write your code here
const fileContent = 'I am fresh and young';
const filePath = path.resolve(__dirname, './files/fresh.txt');

if (fs.existsSync(filePath)) {
throw new Error('FS operation failed');
}

await fs.promises.writeFile(filePath, fileContent, { encoding: 'utf-8' });
};

await create();
await create();
17 changes: 15 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const remove = async () => {
// Write your code here
const filePathToDelete = path.resolve(__dirname, './files/fileToRemove.txt');

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

fs.rmSync(filePathToDelete);
};

await remove();
await remove();
2 changes: 1 addition & 1 deletion src/fs/files/fileToRemove.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
How dare you!
How dare you
1 change: 1 addition & 0 deletions src/fs/files/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
25 changes: 22 additions & 3 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import fs from 'fs'
import path from 'path'
import {fileURLToPath} from 'url'

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

const list = async () => {
// Write your code here
};
const folderPath = path.resolve(__dirname, './files/')

if (!fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) {
throw new Error('FS operation failed');
}

const files = await new Promise(resolve => {
fs.readdir(folderPath, (error, files) => {
resolve(files);
});
});

console.log(files);
}

await list();
await list();
18 changes: 16 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const read = async () => {
// Write your code here
const filePath = path.resolve(__dirname, './files/fileToRead.txt');

if (!fs.existsSync(filePath) || !fs.lstatSync(filePath).isFile()) {
throw new Error('FS operation failed');
}

const content = await fs.promises.readFile(filePath, { encoding: 'utf-8' });
console.log(content);
};

await read();
await read();
18 changes: 16 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const rename = async () => {
// Write your code here
const sourcePath = path.resolve(__dirname, './files/wrongFilename.txt');
const resultPath = path.resolve(__dirname, './files/properFilename.md');

if (!fs.existsSync(sourcePath) || fs.existsSync(resultPath)) {
throw new Error('FS operation failed');
}

fs.renameSync(sourcePath, resultPath);
};

await rename();
await rename();
32 changes: 30 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import { createHash } from 'crypto';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const calculateHash = async () => {
// Write your code here
const filePath = path.resolve(__dirname, './files/fileToCalculateHashFor.txt');

const fileContent = await new Promise((resolve) => {
let data = '';

const stream = fs.createReadStream(filePath, {encoding: 'utf8'});

stream.on('data', part => {
data += part.toString();
});

stream.on('close', () => {
resolve(data);
});
});

const hash = createHash('sha256');
hash.update(fileContent);
const hashedValue = hash.digest('hex');

console.log(hashedValue);
};

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

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

const random = Math.random();

let unknownObject;

const require = createRequire(import.meta.url);
if (random > 0.5) {
unknownObject = require('./files/a.json');
} else {
unknownObject = require('./files/b.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
};

30 changes: 28 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import {createHash} from 'crypto';
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';

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

const read = async () => {
// Write your code here
const filePath = path.resolve(__dirname, './files/fileToRead.txt');

const data = await new Promise(resolve => {
const stream = fs.createReadStream(filePath);
let fileData = '';

stream.on('readable', () => {
let chunk;

while (null !== (chunk = stream.read(1))) {
fileData += chunk;
}
});

stream.on('end', () => resolve(fileData));
});


process.stdout.write(data);
};

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


const transform = async () => {
// Write your code here
const reverseTransform = new Transform({
transform(chunk, _, callback) {
this.push([...chunk.toString()].reverse().join(''));
callback();
},
encoding: 'utf-8'
});

process.stdin.pipe(reverseTransform).pipe(process.stdout);
};

await transform();
await transform();
15 changes: 13 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
import {pipeline} from 'stream';

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

const write = async () => {
// Write your code here
const filePath = path.resolve(__dirname, './files/fileToWrite.txt');
const fileWriteStream = fs.createWriteStream(filePath, {encoding: 'utf-8'});

process.stdin.pipe(fileWriteStream);
};

await write();
await write();
Loading